#!/usr/bin/python3 -u import RPi.GPIO as GPIO import time, sys sys.path.append('/home/ghz/wxlib') import wxlib as wx wx_dir = "/home/ghz/particle_weather" count = 0 time0 = time1 = time.time() GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP) def trigger_count(channel): global count, time0, time1 time1 = time.time() count += 1 time2 = time1 - time0 if(time2 > 60): ts = time.strftime("%FT%TZ", time.gmtime()) dat_string = "%s\t%.2f cpm\tpi_temp: %.2f °C\n" %\ (ts, count/(60/time2), float(wx.pi_temp_read()) / 1000) wx.write_out_dat_stamp_iso(ts, 'particle.dat', dat_string, wx_dir) count = 0 time0 = time.time() try: GPIO.add_event_detect(17, GPIO.FALLING, callback=trigger_count, bouncetime=20) except: # as is so often the case, after one random upgrade this breaks with systemd starting it up, but runs normally otherwise. # i'm guess something about the random order of brining up systems in the quest for faster boot times fucks it up. # so if it fails wait for a bit and hope systemd brought up all the prereqs by then. fookin ell. time.sleep(4) GPIO.add_event_detect(17, GPIO.FALLING, callback=trigger_count, bouncetime=20) try: while True: time.sleep(1) except KeyboardInterrupt: GPIO.cleanup() # clean up on ctrl c.