Geiger counter



Sparkfun SEN-09848 Geiger counter (ce compteur est remplacé par le SEN-11345)

Usage

 * Générateur aléatoire
 * Drone PRI (Prévention des Risques Industriels)
 * RobAIR-Wifibot

Linux
screen /dev/ttyUSB0 9600

Processing
Code Processing pour visualiser les particules recues. /** * Geiger Counter * * Read data from the serial port ("0" "1") from SEN-09848 and change the color * of a rectangle when a switch connected to the Geiger Counter * SEN-09848 http://www.sparkfun.com/products/9848. */

import processing.serial.*;

Serial myPort; // Create object from Serial class int val;     // Data received from the serial port int fg=0; int serialPortPos=1; // 0 on my PC is the Modem port. int counter0=0; int counter1=0;

void setup { size(200, 200); String portName = Serial.list[serialPortPos]; myPort = new Serial(this, portName, 9600); }

void draw { if ( myPort.available > 0) {  // If data is available, val = myPort.read;        // read it and store it in val background(255);

// Set background to white if(val=='0') { counter0++; } else { counter1++; }   if(fg==0) { fg=204; } else { fg=0; }   fill(fg); text((counter0+counter1)+":"+counter0+":"+counter1,8,height-6); long timestamp=System.currentTimeMillis; println(timestamp+"="+new Date(timestamp)); } rect(50, 50, 100, 100); }

Python with MQTT and Mosquitto

 * 1) !/usr/bin/python
 * 2) publish the number of particles detected by an USB geiger counter evert second
 * 3) using MQTT
 * 4) Author: Didier Donsez
 * 5) 2013/08/20
 * 6) TODO
 * 7) threading.Timer(period, on_second, (a,b)).start => call on_second(a,b)
 * 8) improve concurrency control with  https://github.com/littlehedgehog/base/blob/master/atomicinteger.py
 * 1) threading.Timer(period, on_second, (a,b)).start => call on_second(a,b)
 * 2) improve concurrency control with  https://github.com/littlehedgehog/base/blob/master/atomicinteger.py

import serial import mosquitto import os import sys import threading


 * 1) python publishgeiger.py test.mosquitto.org /dev/tty.usbserial-A700eD9k geiger/particulesPerSecond

broker=sys.argv[1]
 * 1) broker = "test.mosquitto.org"

port = 1883

serialdev=sys.argv[2]
 * 1) = '/dev/tty.usbserial-A700eD9k'

topic=sys.argv[3]
 * 1) = 'geiger/particulesPerSecond'

period = 1.0

cpt = 0

def on_second: global period, cpt, topic print(cpt) mqttc.publish(topic, cpt) cpt = 0 timer = threading.Timer(period, on_second) timer.start


 * 1) MQTT callbacks

def on_connect(mosq, obj, rc): if rc == 0: print("Connected successfully.") else: raise Exception("Can not connect to MQTT broker")

def on_disconnect(mosq, obj, rc): print("Disconnected successfully.")

def on_publish(mosq, obj, mid): print("Message "+str(mid)+" published.")

def on_subscribe(mosq, obj, mid, qos_list): print("Subscribe with mid "+str(mid)+" received.")

def on_unsubscribe(mosq, obj, mid): print("Unsubscribe with mid "+str(mid)+" received.")

def cleanup: print "Ending and cleaning up" ser.close mqttc.disconnect
 * 1) called on exit
 * 2) close serial, disconnect MQTT

try: print "Connecting... ", serialdev #connect to serial port ser = serial.Serial(serialdev, 9600, timeout=20) except: print "Failed to connect serial" #unable to continue with no serial input raise SystemExit("Failed to connect serial")

try: ser.flushInput #create an mqtt client mypid = os.getpid client_uniq = "geigercounter-"+str(mypid) mqttc = mosquitto.Mosquitto(client_uniq)

#attach MQTT callbacks mqttc.on_connect = on_connect mqttc.on_disconnect = on_disconnect mqttc.on_publish = on_publish mqttc.on_subscribe = on_subscribe mqttc.on_unsubscribe = on_unsubscribe

#connect to broker mqttc.connect(broker, port, 60)

timer = threading.Timer(period, on_second) timer.start while mqttc.loop == 0: c = ser.read cpt = cpt + 1 pass

except (IndexError): print "No data received within serial timeout period" cleanup except (KeyboardInterrupt): print "Interrupt received" cleanup except (RuntimeError): print "uh-oh! time to die" cleanup
 * 1) handle list index error (i.e. assume no data received)
 * 1) handle app closure

Projets AIR

 * Drone PRI (Prévention des Risques Industriels) 2011
 * Drone roulant de Prévention des Risques Industriels 2012