Geiger counter

From air
Jump to: navigation, search
(thumbnail)
Sparkfun's geiger counter
(thumbnail)
WifiBot with geiger counters and toxic gas sensors
(thumbnail)
On board Geiger counter (connected the USB port of the ARDrone v1)
(thumbnail)
Capture des particules détectés dans la nuit du 23 03 2011 à Grenoble (chaque trait vertical est le nombre de particules par seconde).


Geiger counter


Contents

Usage

Source code

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

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


import serial
import mosquitto
import os
import sys
import threading


# python publishgeiger.py test.mosquitto.org /dev/tty.usbserial-A700eD9k geiger/particulesPerSecond

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

port = 1883

serialdev=sys.argv[2]
# = '/dev/tty.usbserial-A700eD9k'

topic=sys.argv[3]
# = '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()


#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.")

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

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


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


Projets AIR

Personal tools
Namespaces

Variants
Actions
Navigation
multilingual version
Tools