Analyzing sensor readings with an XBee wireless connection

Measuring Ground Humidity

The XBee module with the router firmware has now been configured to take measurements. Seeed Studio offers solar kits [11] for its modules that include a solar module, a basic board with USB adapter, a battery, and two sensors for temperature and ground humidity (Figure 9). As soon as you plug in an XBee module that has been configured according to the instructions provided in this article and hook up the power, the setup transmits measurement values from the sensors once a second. At this point, it is a good idea to consider a small detail.

Figure 9: The solar kit from Seeed Studio lets you operate individual sensors autonomously.

The sensors in the Seeed Studio kit deliver a maximum of 3.3V. This amount of voltage is not a problem for the XBee data ports. However, some modules use a 1.2V internal reference value for analog measurements. Analog measurements range in value from 0 (0V applied) to 1023 (reference value voltage applied). Therefore, if the voltage exceeds the reference value, the measurement 03 FF is returned (i.e., 1023 or the reference value).

To map the 3.3V to 1.2V, you will need a simple voltage divider, which consists of two resistors. With a divider, you can split the 3.3V into 2.1 and 1.2V by soldering two resistors having values of 10kohm and 5kohm together and attaching them between the sensor output and GND. Then, you have a maximum of 1.1V at the point of contact. This means the module can maintain a fairly accurate reference value. A software correction cancels out any measurement errors.

Reception with a Raspberry Pi

To start receiving data, you should connect the coordinator module to the USB connection on the Raspberry Pi with the help of an XBee USB adapter. The data packets you receive can then be read via the serial USB interface of the Rasp Pi. For reading, you will need to open a console. You should install the special terminal program Screen and take a first look to determine whether everything is working:

sudo apt-get install screen
sudo screen /dev/ttyUSB0 9600

Because the data in API mode does not contain printable symbols, you will see strange-looking symbols on the screen.

The Python script in Listing 2 reads the serial USB interface, interprets the length of the data packet and analog measurement values, and outputs the data. The data packet is coded in the string

Listing 2

Reading and Writing the Serial USB

#!/usr/bin/python
import serial
import struct
port = serial.Serial('/dev/ttyUSB0', baudrate=9600, rtscts=True, timeout=0.75)
data_struct = struct.Struct('>BHBBBBBBBBBBBBBBBBBBHB')
while True:
  buf = port.read(24)
  if len(buf) == 24:
    frm = data_struct.unpack(buf)
    if frm[0] == 0x7E:
      print "Data packet is %i bytes long" % frm[1]
      print "Analog measurement: %i (of 1023)" % frm[20]
port.close
'>BHBBBBBBBBBBBBBBBBBBHB'

according to the I/O frame structure. The letter B indicates 1 byte and H indicates 2 bytes.

The Python script interprets the data read from a serial USB port appropriately and processes it. Figure 10 shows an example of output on a small display screen that can be purchased for around $20 [13]. The C library from Charles Hallard [14] regulates the control process. A port of the library to the Raspberry Pi exists.

Figure 10: Output of the measurement data on a small display screen.

Buy this article as PDF

Express-Checkout as PDF

Pages: 8

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content