Publish readings from a Geiger counter using Microsoft Azure Service Bus and BeagleBoard xm

This little device is a Geiger counter. It measures ionizing radiation, which is radiation that carries enough energy to liberate electrons from atoms or molecules.
In a nutshell, it measures RADIOACTIVITY. You can get a Geiger counter here: https://www.sparkfun.com/products/11345

Radioactivity is everywhere, not only in nuclear power plants (but certainly more).
Bananas are radioactive, cigarettes too! In fact many things around our daily life emit low levels of radioactivity, Check some examples here: http://listverse.com/2013/02/06/10-things-you-probably-didnt-know-were-radioactive/

image

To measure radiation, we use the Geiger-Müller tube developed in 1928. It is a metal tube that contains a gas, usually Neon at low pressure. The Tube itself works as a cathode and the anode is inserted in the center of the tube. A high voltage of a few hundred volts is applied to the terminals and voila! The ionising radiation that passes through the gas ionises some of its molecules and create a natural effect called “avalanche”, which creates a tiny short circuit between the terminals and also an audible CLICK sound.
This electrical signal can easily be filtered and read by a microcontroller. Every time the gas detects a radioactive particle or wave it will tell the microcontroller and the microcontroller will send a serial signal to the computer.

What can we do with that information? Well, we can count the clicks and tell how many per second or per minute take place. The more detections, the higher the ionising radiation around the tube.

If you want to have your Counter running at home or at your nuclear power plant without the need of an entire power hungry PC , you can use a Raspberry Pi, BeagleBone,  or any other similar integrated linux computer.

In this example I am using my BeagleBoard xm. It is similar to the RasPi and the BBB but it features some extra connectors such as an onboard Serial port (connector). The serial port can be used to get the console output, it is a great way to get an OS console without using SSH.

Introduction

The goal is to have the BBxm read the Geiger counter serial data, interpret the data to generate useful radiation dose information (such as uSv/hr) and publish the results using the Microsoft Azure Service Bus infrastructure.

Getting Started

Once you have your BBxm ready (use this if you need help) you can make some basic general updates:

sudo apt-get update

In case you need the default username and password  USER: ubuntu  PASSWORD: temppwd

We will use python with Azure, so make sure you install Python and the SDK.

curl https://bootstrap.pypa.io/get-pip.py | sudo python

If that did not work, then you must install curl first by typing:

sudo apt-get install curl

We want to interact with the serial port in order to listen to our counter, so I instlled Minicom:

sudo apt-get install minicom

Click here for more info on Minicom.

 

Now it is time to connect your Geiger counter to your USB port. Make sure you don’t touch the Geiger tube clear as the 350 to 500V can give you a little zap.

To find out what port it is using:

dmesg | grep tty

image

I only have the counter connected so for me the port is ttyUSB0.

You can also use:

ls -la /dev/tty*

image

The last line shows my counter at /dev/ttyUSB0

 

Lets configure the serial port for the counter. We want 9600 baud. The parity and bit is standard so we can leave that as default:

sudo minicom -s

You enter a settings “window” like this:

image

Go to Serial port setup, hit Enter.

image

Hit A to select the port and type the correct name, in my case is /dev/ttUSB0

Hit Enter to go back and now hit E to select baudrate, you will see this:

image

Select option C, and hit Enter to confirm.

image

That’s it!, you should be able to read the incoming data form the counter as 1s and 0s.
Exit the minicom setup.

 

Next, the serial port is configured in the BBxm but we want it to interact with our python script, so we need to install the serial libraries for python, I chose pyserial:

easy_install -U pyserial

Finally, it is time to create our python code.
Create a new folder to store your .py file.

Go home:

cd

List the folders you have:

ls

Choose one or create a new directory:

mkdir name_of_directory

Finally create your .py file:

touch geigercounter.py

Open the file to add the code:

nano geigercounter.py

Here is my code in pieces, first import the libraries:

import serial
import time
import datetime
from azure.servicebus import *

-serial is for communication with counter.
-time is used to count minutes because I want “counts per minute” or CPM.
-datetime is to know the date and time of the counts detected.
-azure.servicebus is used to send the results to the cloud.

Then choose your serial port and settings:

#select serial port, baudrate (default = 9600), timeout (default = 1)
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)                    #for BeagleBoard xm
#ser = serial.Serial('/dev/tty.usbserial-A9VB6QRM', 9600, timeout=1)      #for Macbook

The next step is creating a queue in Azure Service bus, where the messages will be reported.

Go here to set your Azure Service Bus account and create queues.

bus_service = ServiceBusService(service_namespace='sensornetwork', account_key='s8S0GXhTNLOHrfsEdww/QE0+UYZIVxss8TK7CQkg29Y=', issuer='owner')
queuename = 'taskqueue'
#bus_service.create_queue(queuename)      #uncomment to create a queue, only once

Change your namespace, account key and issuer to your own. Name your queue ‘geiger’ or whatever you prefer.
Create the queue first by uncommenting the last line. This is needed only once.

Now comes the data analysis:

a=b=c=z=minutes=0
d=0.0
while True:
    timeout = time.time() + 60
    while True:
        x = ser.read()  # read one byte
        if x is '0':
            d=d+1
            #print '.'
        elif x is '1':
            d=d+1
            #print '.'
        if time.time() > timeout:
            minutes = minutes + 1
            break
    z=z+d
    print "CPM = " ,d, " total= ", z, " minutes: ", minutes
    if d > 0:
        now = datetime.now()
        msg = Message("CPM Geiger: " + str(d) + " equivalent to: "+ str(round((d/108),3))+" uSv/hr" + "  date and time: "+ str(now) [:19], location = 'Boldrewood b176')
        bus_service.send_queue_message(queuename, msg)
        print 'Message sent to the queue:', queuename
        print 'Body:' , msg.body
    d=0.0

The counter sends sometimes strange data that appears as a blank characters in my terminal, so I decided to filter it only to 1s or 0s. I used if…elif for each. Either 1 or 0 means a detection in the tube, I still don’t know what the difference is…
I print the raw results to make sure its working.
Use variable ‘d’ as a threshold value to trigger the reporting script. Since I want to know everything, I set it to d>0 which is pretty much every single time.

The “msg” contains the information to be sent to the Service bus. I included the CPM and the equivalent radiation dose. For this specific Geiger tube it is:

108 CPM is 1 uSv/hr   (that is 1 microSievert per hour)
This value only applies to my specific tube model, make sure yours is the same.
I used the following links to get the equivalency:

General information about radiation doses here.
This are the specs of my Geiger tube.
More useful info for converting radiation values.

Now save the file by CTRL-X and run it:

python geigercounter.py

 

The data will be uploaded every minute or so (it is in fact not exactly every minute, since it takes a bit of time to read the slow serial data).

To read the data form the ServiceBus you can setup your computer using this tutorial. Since you already did it previousily all you need is to do is to create a read.py file in your computer or wherever you want to read the data from.

To test your servicebus you can use the python file called recv.py shown below:

from azure.servicebus import *

#bus_service = ServiceBusService(service_namespace='mynamespace', account_key='mykey', issuer='myissuer')
bus_service = ServiceBusService(service_namespace='sensornetwork', account_key='s8S0GXhTNLOHrfsEdww/QE0+UYZIVxss8TK7CQkg29Y=', issuer='owner')

while True:
#msg = bus_service.read_delete_queue_message('taskqueue')
    msg = bus_service.receive_queue_message ('taskqueue') #peek_lock=True

#print Message.location
    print 'Body: ',(msg.body)

#msg.delete()

Notice the last line is a commented msg.delete()
This means that the messages in the queue will not be deleted once read.
If you want to read each message once and never see them again, then uncomment it.
Also If you don’t want to receive all the messages at once, remove the while True:

These are my results in only half an hour:

Body:  CPM Geiger: 10.0 equivalent to: 0.093 uSv/hr  date and time: 2014-08-20 23:28:10
Body:  CPM Geiger: 9.0 equivalent to: 0.083 uSv/hr  date and time: 2014-08-20 23:29:11
Body:  CPM Geiger: 3.0 equivalent to: 0.028 uSv/hr  date and time: 2014-08-20 23:30:11
Body:  CPM Geiger: 9.0 equivalent to: 0.083 uSv/hr  date and time: 2014-08-20 23:31:12
Body:  CPM Geiger: 8.0 equivalent to: 0.074 uSv/hr  date and time: 2014-08-20 23:32:13
Body:  CPM Geiger: 14.0 equivalent to: 0.13 uSv/hr  date and time: 2014-08-20 23:33:14
Body:  CPM Geiger: 8.0 equivalent to: 0.074 uSv/hr  date and time: 2014-08-20 23:34:14
Body:  CPM Geiger: 8.0 equivalent to: 0.074 uSv/hr  date and time: 2014-08-20 23:35:15
Body:  CPM Geiger: 9.0 equivalent to: 0.083 uSv/hr  date and time: 2014-08-20 23:36:16
Body:  CPM Geiger: 4.0 equivalent to: 0.037 uSv/hr  date and time: 2014-08-20 23:37:17
Body:  CPM Geiger: 7.0 equivalent to: 0.065 uSv/hr  date and time: 2014-08-20 23:38:18
Body:  CPM Geiger: 7.0 equivalent to: 0.065 uSv/hr  date and time: 2014-08-20 23:39:19
Body:  CPM Geiger: 10.0 equivalent to: 0.093 uSv/hr  date and time: 2014-08-20 23:40:20
Body:  CPM Geiger: 7.0 equivalent to: 0.065 uSv/hr  date and time: 2014-08-20 23:41:20
Body:  CPM Geiger: 9.0 equivalent to: 0.083 uSv/hr  date and time: 2014-08-20 23:42:21
Body:  CPM Geiger: 11.0 equivalent to: 0.102 uSv/hr  date and time: 2014-08-20 23:43:22
Body:  CPM Geiger: 12.0 equivalent to: 0.111 uSv/hr  date and time: 2014-08-20 23:44:23
Body:  CPM Geiger: 8.0 equivalent to: 0.074 uSv/hr  date and time: 2014-08-20 23:45:23
Body:  CPM Geiger: 8.0 equivalent to: 0.074 uSv/hr  date and time: 2014-08-20 23:46:24
Body:  CPM Geiger: 7.0 equivalent to: 0.065 uSv/hr  date and time: 2014-08-20 23:47:25
Body:  CPM Geiger: 12.0 equivalent to: 0.111 uSv/hr  date and time: 2014-08-20 23:48:26
Body:  CPM Geiger: 12.0 equivalent to: 0.111 uSv/hr  date and time: 2014-08-20 23:49:27
Body:  CPM Geiger: 8.0 equivalent to: 0.074 uSv/hr  date and time: 2014-08-20 23:50:28
Body:  CPM Geiger: 3.0 equivalent to: 0.028 uSv/hr  date and time: 2014-08-20 23:51:29
Body:  CPM Geiger: 8.0 equivalent to: 0.074 uSv/hr  date and time: 2014-08-20 23:52:29
Body:  CPM Geiger: 4.0 equivalent to: 0.037 uSv/hr  date and time: 2014-08-20 23:53:30
Body:  CPM Geiger: 5.0 equivalent to: 0.046 uSv/hr  date and time: 2014-08-20 23:54:31
Body:  CPM Geiger: 12.0 equivalent to: 0.111 uSv/hr  date and time: 2014-08-20 23:55:32
Body:  CPM Geiger: 4.0 equivalent to: 0.037 uSv/hr  date and time: 2014-08-20 23:56:33
Body:  CPM Geiger: 8.0 equivalent to: 0.074 uSv/hr  date and time: 2014-08-20 23:57:33
Body:  CPM Geiger: 11.0 equivalent to: 0.102 uSv/hr  date and time: 2014-08-20 23:58:34
Body:  CPM Geiger: 8.0 equivalent to: 0.074 uSv/hr  date and time: 2014-08-20 23:59:35

Conclusions:
The Geiger counter is an easy and fun way to interact with sensors. The BBxm and any linux embedded PC for that matter are a great way to read sensors and add them to the world of internet of things.
Microsoft Azure Service bus lets you upload data easily without the hassle of opening ports and dealing with firewall restrictions.

Future work:
Add different sensors to other USB ports and try different ways of logging data.
Try Azure Service Bus Topics to create topics and subscriptions to filter only certain messages with specific data.

Written by Martin Garcia (plusmartin.com) & Steven Johnston
21 August 2014

Devices used:

-Sparkfun Geiger Counter
-BeagleBoard xm running Ubuntu (any Linux system should work)
-USB to serial adapter (optional but handy)
-5V power supply at least 1 or 2 Amps
-miniUSB cable

Language:
-Python and the Azure SDK for Python components: http://azure.microsoft.com/en-us/documentation/articles/python-how-to-install/

Leave a Reply

Your email address will not be published. Required fields are marked *