Building a Wireless
            Mesh Networked
              Thermometer
                                                    Michael Pigg
                                                    June 3, 2010




This work is licensed under the Creative Commons Attribution-
Noncommercial-Share Alike 3.0 United States License.
Chariot Solutions

Practical, smart software development
powered by Java, open source and
emerging technologies
An engineer* walks
                 into a room ...


*Software guy with old, dusty electrical technology degree, really
and notices it’s too
      cold.
He walks into another
 and notices it’s too
       warm.
Hire someone to fix it?
No Way!
Build a system to
monitor temperature in
   multiple rooms.
Must have... Stories!
• The system should be able to collect
  temperature data from at least 12 locations
• The system should record collected
  temperatures in a database for later analysis
• The system shall not require ripping into walls
  to run wires
• Sensors shall not require ugly power bricks
• The system should be able to control external
  devices
Temp       Temp
     Temp                        Output
             Module     Module
    Module                       Control
                                 Module
 Temp
Module          Ether



                Control
                System
Sensor
                Output Voltage


                        Analog to
 Temp                    Digital
Module                  Convertor
         Digital Value of Voltage



                        Wireless
                       Transmitter
Sensor Requirements
• Low current draw
• Can operate from 2 AA batteries
• Output a nice linear voltage with a positive
  correlation to temperature
• Come in a through-hole (not surface
  mount) package
MCP9700

• 6 uA current draw (sweet!)
• Output ranges from 0V to 2V
• Comes in TO-92 package (3-pin through-
  hole)
Voltage to Temperature
            Tc = (V - 0.5) / 0.01

  Output (V)                 Temp (deg C)
      0                             -50
     0.5                            0
     0.75                           25
     0.9                            40
     1.2                            70
Establishing a Reference

• ADCs need a reference voltage
• MCP9700 outputs 0-2V
• As a practical matter, 1.2V = 70 deg C (158
  F) is much warmer than I hope to measure
• So a 1.2 V voltage reference should do
LM285LP


• 1.235 V nominal shunt reference
• requires only10µA to provide consistent
  voltage
Digi XBee
• Wireless 2.4GHz module based on
  802.15.4 spec
• 100 ft indoor range
• Operates on 2.8 to 3.4 V
 • 50mA operational
 • 50uA standby
• Built-in Analog-to-Digital converter (ADC)
Major Temp Module
       Parts
• XBee
• MCP9700
• LM285 (1.2 V Reference)
• 2 AA batteries and holder
• Case - Hammond 1593P series
• SchmartBoard 2mm proto board
n PA
                   i
               ned
                 ig
             Des



Top-secret
Prototype
Wireless
                       Module     Vref
  Temp
 Sensor
(under board)                     Text


Super Soldering Job*



                                   Professional Wiring*




*Not so much
Temp        Temp
      Temp    Module      Module
     Module


 Temp             Ether
Module


                   Control
                   System
XBee, meet Computer
•   Droids makes a nice
    XBee to USB board

•   This will allow software
    to communicate with
    XBee network

•   Driver for USB chip is
    built into Linux, can be
    installed for Mac or
    Windows
Control System




XBee   USB
Speaking XBee

• XBee supports two modes
 • AT - Acts like a modem with AT
    commands (hello, 1995)
 • API - Uses a byte protocol documented
    in the XBee product manual
XBee Interface
              Protocol
  • XBee operating in API mode
  • Basic frame format
 Start       Length         API-specific
                                          Checksum
Delimiter    2 bytes, MSB      data         1 byte
   0x7E          first          n bytes
Module Address
           Command
 • Command ID = MY
 • This command uses the ‘AT’ command API,
    which has an ID of 0x08
 Start        Length        API-specific data    Checksum
Delimiter    2 bytes, MSB
                                 n bytes           1 byte
  0x7E           first




                      Frame       AT           Parameter
         API ID
                        ID      Command          Value
Request Address
Start    Length     API    Fr. ID      Command          Checksum

0x7E    0x00 0x04   0x08   0x01     0x4D (M) 0x59 (Y)     0x50




                    Correlation                   0xFF - Sum
                        ID                       of data bytes
Response
Start    Length          API      Fr. ID       Command

0x7E    0x00 0x04        0x08      0x01     0x4D (M) 0x59 (Y)



                             Same as
                             request

                  Address        Checksum

                    0x0001         0x4F
This is looking ugly
Java: Who needs
unsigned numbers?
Apache Mina
• Java library for asynchronous I/O
• Helps with
 • Implementing custom protocol coders
    and decoders
  • Dealing with partial packets
• Provides a serial port transport so that we
  don’t have to deal with those details
XBeeLib

• Implementation of XBee API protocol
 • Written in Java
 • Based on Apache Mina
 • Open source, BSD license
 • http://kenai.com/projects/xbeelib
XBeeLib
                                xb Shell   xmon Shell
                               Command     Command

         XBeeService


      Mina Core



                  Mina Serial Connector
USB
                         RXTX
XBeeLib
                                  xb Shell   xmon Shell
                                 Command     Command

         XBeeService


      Mina Core
         Command Encoder



                  Mina Serial Connector
USB
                            RXTX
XBeeLib
                                   xb Shell    xmon Shell
                                  Command      Command

         XBeeService


      Mina Core
                                Command Response Decoder
         Command Encoder          IO Data Frame Decoder




                  Mina Serial Connector
USB
                            RXTX
Control System
                              OSGi
                              contai
                               ner




             XBeeLib
XBee
       (transport handling)
OSGi

• A runtime framework for Java
• Software is deployed as bundles
• An application is composed of one or more
  bundles
OSGi = Services

• The core idea is to encourage a modular
  approach to building applications
 • Only classes explicitly declared as shared
    can be seen by other bundles
 • Provides mechanism for bundles to
    provide services to other bundles
Apache Felix


• Apache implementation of OSGi standard
• Provides a run-time platform for the
  monitoring software
Felix Shell
• Provides an interactive console for the
  OSGi runtime environment
• Implementing a shell command
 • Create class that implements Command
 • Register an instance of the class with the
    OSGi service registry
Demo XBeeLib
pHomeNet

• Provides application-level services
 • Monitoring sensors
 • Acting on observations
 • Viewing collected observations
• http://kenai.com/projects/phomenet
If an XBee transmits
    but nobody is
      listening...
Listening to XBees
• xbee-listener bundle listens for incoming
    samples from XBee modules
• Attempts to find a handler for the sample
    •   Handlers implement XbeeAnalogDataTransfomer

•   If sample can be transformed to observation, call
    ObservationRecorder to persist in database

•   Post an OSGi event about the observation
Control System           OSGi
                                           Service
                                           Registry




                Xbee Listener


             Observation Recorder



                             Derby

                         EclipseLink JPA


                      XBeeLib
XBee   USB
                (transport handling)
Making sense of it all

• temp-sensor bundle implements
  XbeeAnalogDataTransformer for
  temperature modules
• Decodes samples to temperature
  observations
• Provides ability to configure instances of
  temperature sensors
Control System           OSGi
                                              Service
                                              Registry


             Temperature Module Services


                    Xbee Listener


                Observation Recorder



                                Derby

                            EclipseLink JPA


                         XBeeLib
XBee   USB
                   (transport handling)
Demo temp-sensor
    bundle
Let’s be controlling

• Switch bundle supports modules that turn
  outputs on/off
• Listen for observation events posted by
  xbee-listener bundle
• Provide ability to configure instances of
  switch modules
Control System           OSGi
                                              Service
               Control Module Handler         Registry


             Temperature Module Services


                    Xbee Listener


                Observation Recorder



                                Derby

                            EclipseLink JPA


                         XBeeLib
XBee   USB
                   (transport handling)
Demo xbee-switch
    bundle
pHomeNet Status

• Mostly a research project to date, despite
  it’s motivation
• TODOs
 • Expand support for controllable devices
 • Expand sensor support (One Wire
    devices, external temperature probes)
Resources
•   XBee 802.15.4 OEM module

    •   http://www.digi.com/products/wireless/point-multipoint/xbee-series1-
        module.jsp#overview

•   Droids XBee USB board

    •   http://www.droids.it/990_002.html

•   MCP9700 Temperature Sensor

    •   http://www.microchip.com/wwwproducts/Devices.aspx?
        dDocName=en022289

Building a Wireless Mesh Network Temperature Sensor

  • 1.
    Building a Wireless Mesh Networked Thermometer Michael Pigg June 3, 2010 This work is licensed under the Creative Commons Attribution- Noncommercial-Share Alike 3.0 United States License.
  • 2.
    Chariot Solutions Practical, smartsoftware development powered by Java, open source and emerging technologies
  • 3.
    An engineer* walks into a room ... *Software guy with old, dusty electrical technology degree, really
  • 4.
  • 5.
    He walks intoanother and notices it’s too warm.
  • 6.
  • 7.
  • 8.
    Build a systemto monitor temperature in multiple rooms.
  • 9.
    Must have... Stories! •The system should be able to collect temperature data from at least 12 locations • The system should record collected temperatures in a database for later analysis • The system shall not require ripping into walls to run wires • Sensors shall not require ugly power bricks • The system should be able to control external devices
  • 10.
    Temp Temp Temp Output Module Module Module Control Module Temp Module Ether Control System
  • 11.
    Sensor Output Voltage Analog to Temp Digital Module Convertor Digital Value of Voltage Wireless Transmitter
  • 12.
    Sensor Requirements • Lowcurrent draw • Can operate from 2 AA batteries • Output a nice linear voltage with a positive correlation to temperature • Come in a through-hole (not surface mount) package
  • 13.
    MCP9700 • 6 uAcurrent draw (sweet!) • Output ranges from 0V to 2V • Comes in TO-92 package (3-pin through- hole)
  • 14.
    Voltage to Temperature Tc = (V - 0.5) / 0.01 Output (V) Temp (deg C) 0 -50 0.5 0 0.75 25 0.9 40 1.2 70
  • 15.
    Establishing a Reference •ADCs need a reference voltage • MCP9700 outputs 0-2V • As a practical matter, 1.2V = 70 deg C (158 F) is much warmer than I hope to measure • So a 1.2 V voltage reference should do
  • 16.
    LM285LP • 1.235 Vnominal shunt reference • requires only10µA to provide consistent voltage
  • 17.
    Digi XBee • Wireless2.4GHz module based on 802.15.4 spec • 100 ft indoor range • Operates on 2.8 to 3.4 V • 50mA operational • 50uA standby • Built-in Analog-to-Digital converter (ADC)
  • 19.
    Major Temp Module Parts • XBee • MCP9700 • LM285 (1.2 V Reference) • 2 AA batteries and holder • Case - Hammond 1593P series • SchmartBoard 2mm proto board
  • 21.
    n PA i ned ig Des Top-secret Prototype
  • 22.
    Wireless Module Vref Temp Sensor (under board) Text Super Soldering Job* Professional Wiring* *Not so much
  • 23.
    Temp Temp Temp Module Module Module Temp Ether Module Control System
  • 24.
    XBee, meet Computer • Droids makes a nice XBee to USB board • This will allow software to communicate with XBee network • Driver for USB chip is built into Linux, can be installed for Mac or Windows
  • 25.
  • 26.
    Speaking XBee • XBeesupports two modes • AT - Acts like a modem with AT commands (hello, 1995) • API - Uses a byte protocol documented in the XBee product manual
  • 27.
    XBee Interface Protocol • XBee operating in API mode • Basic frame format Start Length API-specific Checksum Delimiter 2 bytes, MSB data 1 byte 0x7E first n bytes
  • 28.
    Module Address Command • Command ID = MY • This command uses the ‘AT’ command API, which has an ID of 0x08 Start Length API-specific data Checksum Delimiter 2 bytes, MSB n bytes 1 byte 0x7E first Frame AT Parameter API ID ID Command Value
  • 29.
    Request Address Start Length API Fr. ID Command Checksum 0x7E 0x00 0x04 0x08 0x01 0x4D (M) 0x59 (Y) 0x50 Correlation 0xFF - Sum ID of data bytes
  • 30.
    Response Start Length API Fr. ID Command 0x7E 0x00 0x04 0x08 0x01 0x4D (M) 0x59 (Y) Same as request Address Checksum 0x0001 0x4F
  • 31.
  • 32.
  • 33.
    Apache Mina • Javalibrary for asynchronous I/O • Helps with • Implementing custom protocol coders and decoders • Dealing with partial packets • Provides a serial port transport so that we don’t have to deal with those details
  • 34.
    XBeeLib • Implementation ofXBee API protocol • Written in Java • Based on Apache Mina • Open source, BSD license • http://kenai.com/projects/xbeelib
  • 35.
    XBeeLib xb Shell xmon Shell Command Command XBeeService Mina Core Mina Serial Connector USB RXTX
  • 36.
    XBeeLib xb Shell xmon Shell Command Command XBeeService Mina Core Command Encoder Mina Serial Connector USB RXTX
  • 37.
    XBeeLib xb Shell xmon Shell Command Command XBeeService Mina Core Command Response Decoder Command Encoder IO Data Frame Decoder Mina Serial Connector USB RXTX
  • 38.
    Control System OSGi contai ner XBeeLib XBee (transport handling)
  • 39.
    OSGi • A runtimeframework for Java • Software is deployed as bundles • An application is composed of one or more bundles
  • 40.
    OSGi = Services •The core idea is to encourage a modular approach to building applications • Only classes explicitly declared as shared can be seen by other bundles • Provides mechanism for bundles to provide services to other bundles
  • 41.
    Apache Felix • Apacheimplementation of OSGi standard • Provides a run-time platform for the monitoring software
  • 42.
    Felix Shell • Providesan interactive console for the OSGi runtime environment • Implementing a shell command • Create class that implements Command • Register an instance of the class with the OSGi service registry
  • 43.
  • 44.
    pHomeNet • Provides application-levelservices • Monitoring sensors • Acting on observations • Viewing collected observations • http://kenai.com/projects/phomenet
  • 45.
    If an XBeetransmits but nobody is listening...
  • 46.
    Listening to XBees •xbee-listener bundle listens for incoming samples from XBee modules • Attempts to find a handler for the sample • Handlers implement XbeeAnalogDataTransfomer • If sample can be transformed to observation, call ObservationRecorder to persist in database • Post an OSGi event about the observation
  • 47.
    Control System OSGi Service Registry Xbee Listener Observation Recorder Derby EclipseLink JPA XBeeLib XBee USB (transport handling)
  • 48.
    Making sense ofit all • temp-sensor bundle implements XbeeAnalogDataTransformer for temperature modules • Decodes samples to temperature observations • Provides ability to configure instances of temperature sensors
  • 49.
    Control System OSGi Service Registry Temperature Module Services Xbee Listener Observation Recorder Derby EclipseLink JPA XBeeLib XBee USB (transport handling)
  • 50.
  • 51.
    Let’s be controlling •Switch bundle supports modules that turn outputs on/off • Listen for observation events posted by xbee-listener bundle • Provide ability to configure instances of switch modules
  • 52.
    Control System OSGi Service Control Module Handler Registry Temperature Module Services Xbee Listener Observation Recorder Derby EclipseLink JPA XBeeLib XBee USB (transport handling)
  • 53.
  • 54.
    pHomeNet Status • Mostlya research project to date, despite it’s motivation • TODOs • Expand support for controllable devices • Expand sensor support (One Wire devices, external temperature probes)
  • 55.
    Resources • XBee 802.15.4 OEM module • http://www.digi.com/products/wireless/point-multipoint/xbee-series1- module.jsp#overview • Droids XBee USB board • http://www.droids.it/990_002.html • MCP9700 Temperature Sensor • http://www.microchip.com/wwwproducts/Devices.aspx? dDocName=en022289

Editor's Notes