Sunday 29 April 2012

ProBee Based Home ZigBee Network - Part 3 - Coordinator Software Display

Welcome to part 3 of the ProBee Based Home ZigBee Network series. In this entry I will be looking at how to display the temperature reported from our ProBee based sensor node onto a webpage using a custom Python script and Open Energy Monitor's emoncms software. "Emoncms is a powerful open-source web-app for processing, logging and visualising energy, temperature and other environmental data."
Will will need to have a working sensor node network comprising of a co-ordinator and at least one sensor node, so make sure you have followed the part 2 of this series!
Additional things you will need:
  • A Web Server: A Linux based PC/server with Apache, MySQL Server and Python installed. I'll be using my Beagleboard running Ubuntu Server, but any major Linux distribution should be suitable (including running one from a virtual machine assuming your virtual machine has access to the ZigBee coordinator's USB serial interface).
  • Emoncms  : The latest version of emoncms installed on the server. You can get it here (at time of writing version 3 is that latest). Installing it is quite trivial once you have your Linux server setup, just follow the installation guide here.
This tutorial should work just as well with a Windows based server, but I will leave it up to the reader to investigate.

Flow of temperature data


So we need to do the following:
  1. Verify sensor node and network - Check what serial device our coordinator is connected to and verify we are receiving temperature data from the sensor node through the co-ordinator's serial interface.
  2. Verify  emoncms  Installation - Check that emoncms is working correctly and receiving sample commands from the browser.
  3. Create the serial data parsing script - Write a Python script to parse the serial data  outputted by the ProBee coordinator and send this data to emoncms for storage and display. The script is very basic, so if you don't know Python you could easily port it to your preferred language.
  4. Integration - Integrate it all so our temperature data is displayed on our web server.
  5. Run at start-up (optional) - I will show you how to get the serial data parsing script running automatically when your Ubuntu server is powered on.
Verify Sensor Node and Network
Plug your pre-configured ProBee coordinator (ProBee USB dongle) into your server and power on your ProBee sensor node (you should have followed part 2 to get these ready). 
We now need to connect to the ProBee USB dongle's USB serial port using a terminal application.
To find the device name for your USB dongle, use the dmesg command:
donal@server:~$ dmesg | grep FTDI
[ 62.209960] USB Serial support registered for FTDI USB Serial Device
[ 62.219909] ftdi_sio 1-2.2:1.0: FTDI USB Serial Device converter detected
[ 62.246856] usb 1-2.2: FTDI USB Serial Device converter now attached to ttyUSB0
[ 62.252075] ftdi_sio: v1.6.0:USB FTDI Serial Converters Driver
The Probee dongle uses an FTDI USB to serial interface chip, which in my case is attached to ttyUSB0.
Now use minicom (Linux equivalent of hyperterminal) to view the serial data (containing our sensor node temperature) coming from the co-ordinator. If you haven't use minicom, there is a good tutorial here.
You should get output similar to this:




Verify Emoncms Installation
Html post requests are used to send input data to the emoncms server. Our script will be creating  requests to send the temperature data to the server each time we receive new data from our temperature sensor.
Now to verify the emoncms installation: Browse to your emoncms installation (in my case http://mylocalserver/emoncms3/user/view), and click on the Account tab. Then click on the "try me" link which will send a sample html post request to the server. Then click on the Inputs tab, and you should see two new feeds in there that were updated several seconds ago.
Emoncms validation

Create Serial Data Parsing Script
The script will have to do the following:
  1. Open the serial port to the ProBee dongle.
  2. Wait for and read a line of data from the serial port.
  3. Parse that line and generate the html post request to the emoncms server.
  4. go back to step 

#! /usr/bin/python
"""
Simple script for parsing probee co-ordinator data and passing it on to Emoncms.
Donal Morrissey
http://donalmorrissey.blogspot.com/
"""
import re       # Used for parsing the data string.
import sys      # For exit
import serial   # Serial port interfacing
import requests # To send html post requests to Emoncms

PORT = '/dev/ttyUSB1'
BAUD_RATE = 115200

MY_EMONCMS_API_KEY = 'your_emoncms3_api_key'
URL_TO_MY_EMONCMS = 'http://your_server_address/emoncms3'

# Open serial port
try:
        ser = serial.Serial(PORT, BAUD_RATE)
        ser.open()
except:
        print "Could not open serial port: ", sys.exc_info()[0]
        sys.exit(2)


# Continuously read and print packets
while True:
    try:
        line = ser.readline();

        #Check the line is valid
        if not "++" in line: continue
        line = line.rstrip()

        # Split the line up, we know our raw temperature data will be located in location 2.
        r = re.compile('[|,]+')
        split_line =  r.split(line)

        # Extract the raw value as an int (our data is in location 2).
        raw_value =  int(split_line[2], 16)

        #Get the measured voltage from the raw ADC value.
        vout_mV = raw_value / 10

        temperature_degC = (vout_mV - 500) / 10

        #print temperature_degC

        # Now there is where the magic happens...
        # We send the new value to emoncms as a post request.
        requests.post(URL_TO_MY_EMONCMS+"/api/post?apikey=" + MY_EMONCMS_API_KEY +"&json={temperature_probee:"+str(temperature_degC)+"}")

    except KeyboardInterrupt:
        break

ser.close()


Integration
We are now ready to test the system. Open a browser and browse to your emoncms installation and click on the Inputs tab. Plug in your ZigBee co-ordinator (ProBee dongle), run your serial data parsing script and power on your sensor node.
You should now see the temperature input updating every 5 seconds on the Inputs tab. You can now add widgets to your dashboard and display various graphs, etc of this data.


Run at Start-Up
As the root user, save the following script to /etc/init.d/home_monitoring.sh, not forgetting to add in the full path to your script.
#! /bin/sh
# /etc/init.d/home_monitoring.sh

# Carry out specific functions when asked to by the system
case "$1" in
  start)
    echo "Starting receive_and_post_emon3_d.py "
    /<full path to this script>/receive_and_post_emon3_d.py &
    ;;
  stop)
    echo "Stopping receive_and_post_emon3_d.py"
    killall receive_and_post_emon3_d.py
    ;;
  *)
    echo "Usage: /etc/init.d/home_monitoring.sh {start|stop}"
    exit 1
    ;;
esac

exit 0


Make the script executable: by running the command: "chmod 755 /etc/init.d/home_monitoring.sh".
Now register the script so it will be called at startup/shutdown: "update-rc.d  home_monitoring.sh defaults".
You can test your script runs at startup by restarting your server, your emoncms dashboard should be continuously updated by your sensor node.


Conclusion
You should now have a basic home temperature monitoring system using ProBee based Sensor Node and Co-ordinator and emoncms.


This series:

10 comments:

  1. Thanks for these pieces. Although I am using Sena's ProBee ZE10 dev boards for my project prototyping, I find a lot of information useful here.

    Could you please explain how I can configure four of the ZigBee modules as star (1 ZC and 3 ZEDs) and as a mesh (1 ZC, 1 ZR and 2 ZEDs).

    After setting up the network, I was able to send broadcast messages to all the ZEDs from the ZC. Also, the ZEDs was sending periodic analog temperature data to the ZC. I was also able to send periodic analog temperature data from one ZED to another.

    ReplyDelete
    Replies
    1. Hi, it's very useful this topic but I don't know how to read in windows data with python like coodinator from SED in a similar network, can anyone to help me please, Marius

      Delete
    2. Hi Marius,
      Have you looked at using the pySerial python library? http://pyserial.sourceforge.net/ It will work in Windows.
      Donal

      Delete
  2. Awesome post. Gave me useful information on how to use the ProBee USB with my Raspberry Pi.
    Thanks

    ReplyDelete
    Replies
    1. Hi Jaycuse,
      Cheers for the feedback! I haven't tried using emoncms on my Raspberry Pi yet, please let me know how it goes!
      Donal

      Delete
  3. Hello,
    I´ve checked that you posted that some months ago, do you know if there is other modules cheapper than Probee and with the same features by now(because in almost 1 year market can change a lot in technology)?

    ReplyDelete
    Replies
    1. Hi ...
      Unfortunately I am not aware of cheaper modules than the Probee ones. There is a full list of 805.15.4 modules available on wikipedia here: http://en.wikipedia.org/wiki/Comparison_of_802.15.4_radio_modules
      Perhaps you could work through the list to find a suitable alternative.
      Best Regards,
      Donal

      Delete
  4. Thanks for providing such a social platform which give us variety of ideas to explore technical things. Zigbee is the leading standard for smart energy networks, home automation, sensor networks, health care, and many other exciting applications.

    ReplyDelete
    Replies
    1. Hi Amaan,
      Thank you for the feedback.
      Best Regards,
      Donal

      Delete
  5. This comment has been removed by a blog administrator.

    ReplyDelete