Tuesday, June 12, 2012

Raspberry Pi and Wireless Sensor

A few weeks ago, I blogged about at a 'shield' that plugs into the Raspberry Pi's GPIO port called the 'Slice of Pi'. This very low cost board also includes an X-Bee shaped socket into which RF modules can be plugged. The company behind the 'Slice of Pi' (Ciseco) is also behind a useful and low-cost range of RF devices.

In this blog entry, I used one of their wireless sensor modules (in this case temperature) with a pair of XRF RF modems to add remote wireless temperature sensing to the Raspberry Pi using the serial connection on the GPIO pins.

Here, a simple program (on the left) reads the temperature from the remote wireless sensor and displays it (in the right). Degrees C not F, its not that cold over here!


Here is the Raspberry Pi, with a Slice of Pi and an XRF module all plugged together sandwich style.

I plugged them together with the Pi turned off, as 'hot-swapping' makes me nervous.

The wireless temperature sensor is made up of two modules, the temperature sensor module, complete with Lithium battery, and another XRF module -- the same as the one attached to the Pi. These just plug together.


The temperature module sends the temperature as a string every 15 seconds. So all that is required of the Raspberry Pi is to catch the serial messages and display them in a Tkinter window. The sensor is microcontroller-based and quite smart, so you can do much more complicated things, including multiple sensors, sensor IDs and messages that indicate that the buttons on the sensor have been pressed.

The Python program to do this is listed in full below (20 lines of code). It requires the PySerial library which you must install. See the instructions in my earlier post  on linking the Pi to an Arduino.

From IDLE (use Python 2 not 3) select File->New and paste in the code below, then click 'Run Module'.

from Tkinter import *
import serial
import time

DEVICE = '/dev/ttyAMA0'
BAUD = 9600

root = Tk()

def updateTime() :
    ser = serial.Serial(DEVICE, BAUD)
    n = ser.inWaiting()
    if n == 4 :
        msg = ser.read(n)
    root.after(2000, updateTime)

v = StringVar()
Label(root, textvariable=v, font=("Helvetica", 240)).pack()

root.after(2000, updateTime)

These are great modules with long range and low power consumption. For the full specs, visit Ciseco's website.

Thanks to Ciseco for lending me the hardware - I will send it back, I promise!


About the Author
These are my books. Click on the image below to find out more about them.


Unknown said...

Very please to see this article - so much so I have been out and acquired the parts you list here! It is all now assembled however I am getting Permission denied when trying to open the serial port. I am running the latest Beta of the Wheezy. I wonder if this could be because the /dev/ttyAMA0 is in use as a console device?
pi@raspberrypi ~ $ sudo ps aux | grep AMA
root 2360 0.0 0.3 2044 728 ttyAMA0 Ss+ 12:38 0:00 /sbin/getty -L ttyAMA0 115200 vt100

Simon Monk said...

I get that too when I ps - but the example does work, honest!

what happens if you kill that thread?

anyone else getting / not getting this problem?

Unknown said...

I ended up editing the /etc/innitab to comment out the getty entry as follows

#Spawn a getty on Raspberry Pi serial line
#T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100

and /boot/cmdline.txt to remove the /dev/ttyAMA0 entries. My cmdline.txt now looks like this

dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait

Following a reboot I can connect to the serial port.

So progress - but not quite there yet. I can see communication on the pi (using miniterm.py) between the two XRF's. When I short out pins 2 and 3 on the remote end the characters are echoed back. Not sure why no temperature response yet - maybe the remote firmware is not the correct one for the temperature sensor. I am trying to get my head round the command syntax for these XRF modules so I can ask them who they are and what they can do.

LWK said...
This comment has been removed by the author.
LWK said...

Hi John

If the remote XRF is echoing back with pins 2&3 shorted then both XRF's are running the default SerilPassThru firmware.

You need to upload the Thermistor LLAP device firmware to one of the XRF's
Check out the Artices section of OpenMicros
The firmware's and instructions for uploading are in the XRF basics section, and the LLAP and LLAP devices sections have details on using the Thermistor firmware once you haven't on there.

hope this helps

Unknown said...

Following this thread with interest! I too have been shopping and "have the bits". Ciseco's site is excellent, but confusing in places. I'm currently stuck halfway through the setup process, using their "xrf_firmware" loader I've updated the XRF partway...

$ ./xrf_firmware -d /dev/ttyAMA0 -f Bootloader-V3d-24MHz.bin
Writing new firmware... etc...

Ending with:
<- OK
<> Sent 256 of 256 lines...

All OK, XRF successfully reprogrammed!

Waiting for device to settle...

<> Entering command modus
Timeout, no data received within 10 seconds

and that's it. Rebooting with or without a powerdown cycle has no effect, just timeouts...

SO, I now have an updated bootloader, but the XRF has gone deaf! Any suggestions?

Unknown said...

After purchasing a Sparkfun 3.3v FTDI module I was able to reprogram the remote XRF to have the Thermistor code using the XCM utility. Actually that utility helped to understand the format of the commands and responses to / from the remote XRF. I see that Ciseco site now has a method of doing this directly from the pi - but handy to have a FTDI module. And from the previous comment - it is also handy to be able to test and verify the firmware uploads in XCM. I know this can all be done on the pi manually - but the XCM tool is easy.

So - while nearly there I still have a small problem in that the tk window does not seem to update. It sits at --.- but when using miniterm I can see the responses coming back as follows -

pi@raspberrypi ~ $ python kits/pyserial-2.5/build/scripts-2.7/miniterm.py /dev/ttyAMA0
--- Miniterm on /dev/ttyAMA0: 9600,8,N,1 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---

I am new to python so unsure how to debug what is going wrong here - it looks like it should work. Oh - in case it is important - my pi is headless so I am using vncserver.

A tip for others building from new unconfigured XRF modules - it is really important to get the interval / sleep working - those cells do not last long otherwise!

Once I fitted a second CR2032 cell this was the sequence (In bold are my commands to tell the remote XRF to sleep then push a value once a minute...)




Every 10th response you get to see the battery voltage too..

When the battery level gets too low I think you should get a particular warning message - I missed that last time - All I saw were constantly repeating a--STARTED-- messages.

Unknown said...

Interestingly, the XRF thermistor software stops working properly at < -50C. It simply reports "-273" as the temperature. However it keeps sending battery readings. Miles at Open Micros suggested putting the ADC firmware on the thermistor, this causes it to generate an ADC reading which is proportional to temperature (typically 19763- at - 80C). SO if you're prepared to calibrate the thermistor yourself (the temperature of dry ice is -78.5C, so it's not too hard) you can convert these ADC readings into temperatures. To cope with these ultra low temperatures I'm running the XRF from 2 AA lithium batteries (apparently lithium batteries handle the cold best). I get ~ 2.6V reported after a day or two at -80C, reading once a minute).

Threlkeld said...

I am a complete novice about Python. I've hit a problem early on that means I should climb a steep learning curve - and I will mug up on it, I promise - but the temptation to take the easy way out and ask for help so that I can get the reward of seeing this project work is too much.

I'm using Raspian, and Python 2 accessed from IDLE on the desktop. I've gone through all the procedure of soldering the hardware and reprogramming the remote XRF with the thermistor, and installing the library. All that seems to have gone OK, and if I enter +++ in screen I get OK back from the XRF on the 'Slice of Pi'.

But the Python script fails at the line:
ser = serial.Serial(DEVICE, BAUD)
with the error:
'There's an error in your program:
Invalid syntax'

Where should I look? (I cut and pasted the program, so there should not be typos).

Threlkeld said...

I'm still struggling with this one, I'm afraid.
I can see the data coming in using screen or miniterm, and I've installed the python serial module which isn't enabled by default
sudo apt-get install python serial
but the script always fails at the line
ser = serial.Serial(DEVICE, BAUD)
with an invalid syntax error.
I'm using the latest version of Rasbian. Is this the reason?

(The script runs without this error in Kubuntu (Debian) on my laptop, but fails to import the readings from the serial port. It just puts up dashes instead of the digits)

Unknown said...

Try looking here:


There's a link on the bottom to a PDF which I wrote, try some of the code from that, works for me.
That monitor is currently running on the older (2012-4-18 ?) build, but it also works fine on the latest "official" Raspbian build from the raspberry pi website (2012-9-18).

Jari said...

Nice article!
I have xrf module on raspi and one thermistor sensor. When sensor is started I get info to the termina. a--STARTED.
Where and how I can give / change id to the sensor? I tried to change it with atid command but that changed the PANID and device does not found anymore from network..

Jari said...

I still get only a--STARTED message when sensor was powered up. What would be the reason for that?

I change Dallas sensor and 4.7k resistor to the to sensor, updated the firmaware but stil I got only this message. Teperature information does not come to the raspberry.. :/

Unknown said...

Dear Dr. Monk, is it possible to swap the temperature senso with a combine temperature / humidity senso? Like : http://arduino-info.wikispaces.com/PROJECT-Temp-Humidity-Display ? Cheers, Gert.

Unknown said...


Thanks for your article.

I did setup a system with 2 temperature sensor but having strange issue and can't find any solution yet.

I receive fancy temperature readings out of my sensors.
2 sensors are at 2 cm of each other.
i am receiving a difference of 2 or 3 degrees Celsius difference between them
one is giving me 24 degrees and the other 27.

The one at 24 degrees after approximately 5 or 6 intervals is giving me 27 degrees and then goes back slowly to the normal temperature (26 and then 24 again).

Does anyone here have the same reaction ?

What can i do to correct this issue ? i know there is a setting to define the 25° of the themistor (aXXRNOM10000) but don't know what i need to do with it, i typed this command and get the same temperature after.

Am i the only one to get weird temperature readings ?

Unknown said...

is it possible to connect atleast 10 of those thermistors ?

Unknown said...

hi, was looking to solve another problem and your code helped out thanks.

would you know if you code change the background color of a tkinter button if the temp went over under etc.....