David-Enst / WeeWX-BCRobotics

A WeeWX driver for the BC Robotics and SparkFun weather station Raspberry Pi based hardware.
1 stars 1 forks source link

Hoping for some help with a customization #5

Open mytechguyri opened 2 years ago

mytechguyri commented 2 years ago

Thank you for putting this driver together btw... works great.... So, in my particular weather station, I've also got a lightning sensor... a Sparkfun AS3935.... and I've got it working on the SPI bus, and driving an interrupt in a stand-alone piece of code as shown here:

#!/bin/python3
import time
import board
import biffobear_as3935
import RPi.GPIO as GPIO
import digitalio

interrupt_pin = board.D7
spi = board.SPI()
cs_pin = board.D8
sensor = biffobear_as3935.AS3935(spi, cs_pin, interrupt_pin=interrupt_pin)
sensor.outdoor = False
GPIO.setmode(GPIO.BCM)
GPIO.setup(7, GPIO.IN)
GPIO.add_event_detect(7, GPIO.RISING)

def as3935_callback(interrupt):
    if sensor.interrupt_set:  # An event has occurred
        # The interrupt_status is cleared after a read, so assign it
        # to a variable in case you need the value later.
        event_type = sensor.interrupt_status
        if event_type == sensor.LIGHTNING:  # It's a lightning event
            print(f"Strike Energy = {sensor.energy}")
            print(f"Distance to storm front = {sensor.distance} km")
        elif event_type == sensor.DISTURBER:
            print("Disturber")
        elif event_type == sensor.NOISE:
            print("Noise")
        else:
            print(event_type)
    else:
        print("No event detected")
    # Minimum time between strike events is 1 second so poll frequently!
    time.sleep(0.5)

GPIO.add_event_callback(7, as3935_callback)

while True:

    time.sleep(0.5)

And it works as it should.... So, I attempted to copy that routine over to the BCRobotics.py code in a block with the wind and rain sensor interrupt driven events.... seems the most logical place, and the expectation is it would function just like the wind and rain interrupt callbacks... but for some reason... it never does the callback.... I have verified, the GPIO pulls HIGH when the interrupt is triggered.... I can read the GPIO from within the main loop and poll and see that it has gone high... but the callback is NEVER triggered for some reason.... and if I try and call the zaptrig callback in the main loop, it complains "not defined" I'm guessing there's something I'm just not seeing here, and that you, since you wrote the original, might have a better idea what it is that I'm missing.


         loginf('Interval is %s' % interval)
         try:
             # Set GPIO pins to use BCM pin numbers
             GPIO.setmode(GPIO.BCM)

             # WIND: Set digital pin 17 to an input and enable the pullup
             GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)

             # RAIN: Set digital pin 23 to an input and enable the pullup
             GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)

             # LIGHTNING: Set digital pin 7 to an input and enable the pulldown
             GPIO.setup(7, GPIO.IN)    #tried both with and without a pulldown

             # Event to detect wind (4 ticks per revolution)
             GPIO.add_event_detect(17, GPIO.BOTH)
             def windtrig(self):
                 global windTick
                 windTick += 1

             GPIO.add_event_callback(17, windtrig)
             loginf('Wind sensor configured')

             # Event to detect rainfall tick
             GPIO.add_event_detect(23, GPIO.FALLING, bouncetime=5)

             def raintrig(self):
                 global rainTick
                 rainTick += 1

             GPIO.add_event_callback(23, raintrig)
             loginf('Rain sensor configured')
             # Event to detect lightning
             GPIO.add_event_detect(7, GPIO.RISING)     #tried rising, falling, and both

             def zaptrig(self):
                 global lightning
                 global lightning_disturber
                 global lightning_noise
                 event_type == lightningsensor.interrupt_status
                 loginf('AS3935 %s' % event_type)
                 if event_type == lightningsensor.NOISE:
                     lightning_noise += 1
                     noise_floor = reduce_noise(noise_floor)
                 elif event_type == lightningsensor.DISTURBER:
                     lightning_disturber += 1
                     watchdog_threshold = increase_threshold(watchdog_threshold)
                 elif event_type == lightningsensor.LIGHTENING:
                     lightning_dist_q.appendleft(lightningsensor.distance)
                     lightning_energy_q.appendleft(lightningsensor.energy)
                     loginf('AS3935 Distance %s, Energy %s' % (lightningsensor.distance,lightningsensor.energy))
                     lightning += 1

             GPIO.add_event_callback(7, zaptrig)
             loginf('Lightning sensor configured')

here are the relevant blocks for the includes that this uses and setup of the SPI interface at the beginning

import board
import biffobear_as3935
import RPi.GPIO as GPIO
import digitalio

interrupt_pin = board.D7
spi = board.SPI()
cs_pin = board.D8
lightningsensor = biffobear_as3935.AS3935(spi, cs_pin, interrupt_pin=interrupt_pin)
lightningsensor.outdoor = False
David-Enst commented 1 year ago

Looks reasonable ... not sure why it doesn't work. Did you figure it out?

PS: Just uploaded a much improved driver.

mytechguyri commented 8 months ago

I had taken a break for a while, but I've got some free time on my hands again, so going to give it another go with the improved driver and see what kind of progress I can make.

David-Enst commented 8 months ago

Let me know if you have questions / issues. Dave E.