Stap 4: De Software voor de Lightning-Detector
Na heel het zoeken en spelen met een aantal pakketten, vonden we een goed pakket door Phil Fenstermacher. Met een beetje gek rond en het opzetten van interrupts, kregen we de software te werken.
De volledige software voor WeatherPi, waarin de AS3935-software is beschikbaar op github.com.
Hier is de setup-software:
################ # ad3935 Set up Lightning Detector # turn I2CBus 2 on tca9545.write_control_register(TCA9545_CONFIG_BUS2) time.sleep(0.003) #tca9545.write_control_register(TCA9545_CONFIG_BUS0) #time.sleep(0.003) as3935 = RPi_AS3935(address=0x03, bus=1) as3935.set_indoors(False) as3935.set_noise_floor(0) #as3935.calibrate(tun_cap=0x0F) as3935.calibrate(tun_cap=0x05) as3935LastInterrupt = 0 as3935LightningCount = 0 as3935LastDistance = 0 as3935LastStatus = "" as3935Interrupt = False # turn I2CBus 0 on tca9545.write_control_register(TCA9545_CONFIG_BUS0) time.sleep(0.003)
Volgende de processing software voor de Interrupts.
def process_as3935_interrupt(): global as3935Interrupt global as3935, as3935LastInterrupt, as3935LastDistance, as3935LastStatus as3935Interrupt = False print "processing Interrupt from as3935" # turn I2CBus 0 on #tca9545.write_control_register(TCA9545_CONFIG_BUS0) # turn I2CBus 2 on tca9545.write_control_register(TCA9545_CONFIG_BUS2) time.sleep(0.003) reason = as3935.get_interrupt() as3935LastInterrupt = reason if reason == 0x00: as3935LastStatus = "Spurious Interrupt" elif reason == 0x01: as3935LastStatus = "Noise Floor too low. Adjusting" as3935.raise_noise_floor() elif reason == 0x04: as3935LastStatus = "Disturber detected - masking" as3935.set_mask_disturber(True) elif reason == 0x08: now = datetime.now().strftime('%H:%M:%S - %Y/%m/%d') distance = as3935.get_distance() as3935LastDistance = distance as3935LastStatus = "Lightning Detected " + str(distance) + "km away. (%s)" % now pclogging.log(pclogging.INFO, __name__, "Lightning Detected " + str(distance) + "km away. (%s)" % now) print "Last Interrupt = 0x%x: %s" % (as3935LastInterrupt, as3935LastStatus) tca9545.write_control_register(TCA9545_CONFIG_BUS0) time.sleep(0.003)
De Interrupt Handler. Kort en zoet, zoals interrupt handlers zou moeten zijn.
def handle_as3935_interrupt(channel): global as3935Interrupt print "as3935 Interrupt" as3935Interrupt = True
De Raspberry Pi GPIOs instellen om te ontvangen van interrupts.
#as3935pin = 18 as3935pin = 22 GPIO.setup(as3935pin, GPIO.IN) #GPIO.setup(as3935pin, GPIO.IN,pull_up_down=GPIO.PUD_UP) GPIO.add_event_detect(as3935pin, GPIO.RISING, callback=handle_as3935_interrupt)
En de rapportage software voor het lezen van de Interrupts.
# process Interrupts from Lightning if (as3935Interrupt == True): try: process_as3935_interrupt() except: print "exception - as3935 I2C did not work"