log_geiger.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import time,json
  2. from datetime import datetime
  3. import RPi.GPIO as GPIO
  4. configfile="config.json"
  5. cf=open(configfile,"r")
  6. log_conf=json.load(cf)
  7. cf.close()
  8. parameter={"device":socket.gethostname(),"mean_count":5,"ring_length":10,"wait":0.5,"sigma":2,"cycle":10}
  9. for n in parameter:
  10. if n in log_conf:
  11. parameter[n]=log_conf[n]
  12. def tube_impulse_callback(channel): # threaded callback -- falling edge detected
  13. global counter # make counter global to be able to increment it
  14. counter+=1
  15. print "ping"
  16. bgeiger=False
  17. if "geiger" in log_conf:
  18. bgeiger=True
  19. GPIO.setmode(GPIO.BOARD) # use RaspPi board layout pin numbering
  20. GPIO.setup(32, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  21. counter = 0
  22. # when a falling edge is detected on port 12, regardless of whatever
  23. # else is happening in the program, the tube_impulse_callback will be run
  24. GPIO.add_event_detect(32, GPIO.FALLING, callback=tube_impulse_callback)
  25. if bgeiger:
  26. try:
  27. while True:
  28. currentMinute = datetime.now().minute
  29. while datetime.now().minute == currentMinute: # this minute..
  30. time.sleep(1) # .. wait while add_event_detect detects pulses
  31. print str(counter)+" cpm"
  32. counter=0 # reset counter
  33. except KeyboardInterrupt:
  34. GPIO.cleanup() # clean up GPIO on CTRL+C exit
  35. except:
  36. GPIO.cleanup() # clean up GPIO on normal exit