Raspberry Pi receives Arduino BMP180 Sensor Data via XBee Mesh Network (openHAB,Node-RED,C++)

Exploring openHAB & openHABian.
rwblinn
Site Admin
Posts: 224
Joined: 07 Oct 2017, 12:16

Raspberry Pi receives Arduino BMP180 Sensor Data via XBee Mesh Network (openHAB,Node-RED,C++)

Postby rwblinn » 20 Feb 2018, 18:14

openHAB2 Experiment Raspberry Pi receives Arduino BMP180 Sensor Data via XBee Mesh Network (openHAB,Node-RED,C++).
Version 20180220

Objectives
To read the Temperature & Airpressure from an BMP180 sensor connected to an Arduino and send the sensor data via an XBee Mesh Network to a Raspberry Pi running openHAB with Node-RED as Rules Engine.

Image

This solution can be used as a base for other sensor based solutions, like Weather Station, Gas Detector (i.e. CO2), Motion or Alarm Switch Detector.
It has been an exiting experience to build esp. handling various byte array conversions between Arduino (C++) and Node-RED (JavaScript).
More Information.

Concept
The Arduino reads in regular intervals (1s) the BMP180 sensor data Temperature and Pressure. The BMP180 is connected to the Arduino by using I2C pins (SDA,SCL).
The Arduino converts the data into an 9 byte payload (byte[0]=ID as char "R" 1 byte, bytes[1-4]=Temperature as long 4 bytes, Bytes[5-8]=Pressure as long 4 bytes) which are send via an XBee.
The XBee (Series 2 XB24-ZB Module) is placed on an Arduino XBee Shield. The XBee is configured in API-Mode as a Router with AP=2.
The data is send to an XBee (Series 2 XB24-ZB Module) connected to a Raspberry Pi via an XBee Explorer USB. The XBee is configured in API-Mode as a Coordinator with AP=2.
Node-RED received the data and converts the 9 byte payload into 2 messages Temperature and Pressure which are used to update openHAB items. The first byte holding the Arduino ID is just logged.
In openHAB 2 items holding the Temperature and Pressure are textual configured. The items are displayed via a simple sitemap in a browser via the openHAB Basic-UI.

Data Flow Example
Payload used: [52,108,7,0,0,3,143,1,0]
  • Arduino Byte Array send to Node-RED: byte[0]=ID as char "R" 1 byte, bytes[1-4]=Temperature as long 4 bytes, Bytes[5-8]=Pressure as long 4 bytes >
  • Node-RED receives Byte Array as Message Payload: [52, 108,7,0,0,3,143,1,0] >
  • Node-RED converts the Byte Array into 3 items: 1) ID="R" logged, 2) Message Temperature 1900, 3) Pressure 102147 >
  • Node-RED sends the 2 messages holding the Temperature & Pressure to openHAB >
  • openHAB updates the respective Items & Sitemap (Browser Window with URL http://192.168.N.NN:8080/basicui/app?sitemap=xbeebmp180)

Hardware
  • 1x Raspberry Pi 3 Model B v1.2 (XBee Coordinator)
  • 1x Arduino MEGA (XBee Router)
  • 2x XBee XB24-ZB Modules (S2) - used as XBee Coordinator (Raspberry Pi) and XBee Router (Arduino)
  • 1x XBee Explorer USB - to configure the XBee Modules and run as XBee Coordinator connected to the Raspberry Pi.
  • Two Arduino Shields tested:
    • 1x XBee Shield Module (SainSmart xBee V1.1) - to act as the XBee Router connected to the Arduino. This shield has jumpers to set USB or XBee mode.
    • 1x XBee Wireless SD Shield Module - to act as the XBee Router connected to the Arduino. This shield has a micro serial switch to set USB or XBee (Micro) mode.
  • 1x LED GREEN + R220Ohm - Arduino Pin 12 - Status indicator (flashing when receiving or sending data via XBee)
  • 1x LED RED + R220Ohm - Arduino Pin 11 - Error indicator

Software
  • Raspberry Pi Stretch Linux 4.9.59-v7+ ARM
  • openHAB 2.2.0.
  • Node-RED 0.17.5 with node.js 6.12.3
  • Node-RED add-on Dashboard UI 2.7.0.
  • Node-RED add-on node-red-contrib-xbee v1.0.0
  • Node-RED add-on openHAB Addon 1.1.3
  • Arduino IDE v1.8.4
  • Arduino Libraries XBee-Arduino, Adafruit_BMP085
Note:
This experiment uses a Raspberry Pi NOOBS installation with additional manual openHAB installation (to test setting up from scratch).
Logged in as user Pi. For editing openHAB textual configuration files, user openhab is used.

Download
Download Arduino sketch, openHAB configuration files and Node-RED flow.

Prepare
Raspberry Pi running openHAB (link, to lookup how to setup openHAB either manually or use openHABian) and Node-RED (link).

Additional libraries and addons required:
XBee-Arduino-Library by Andrew Rapp v0.6.0 (link)
Install via the Arduino IDE.

Adafruit_BMP085_Library by Adafruit (link)
Install via the Arduino IDE.

Raspberry Pi Node-RED Addons
node-red-contrib-xbee v1.0.0 (link)
Install: Open a terminal, login as user pi, $ cd ~/.node-red, $ sudo npm install node-red-contrib-xbee

openHAB Addon (link).
Install: Open a terminal, login as user pi, $ cd ~/.node-red, $ npm install node-red-contrib-openhab2

Hint: Must read details about the XBee API (link).

XBee Configuration
There are 2 XBee's in the Mesh Network.
The XBee Coordinator connected, via XBee Explorer USB, to the Raspberry Pi running openHAB and two XBee's attached to an Arduino XBee Shield.

XBee Coordinator
Product Family: XB24-ZB (Series 2 XBee), Function Set: ZigBee Coordinator API, Firmware: 21A7
MAC: 0013A20040DD2BB8,SH: 13A200,SL: 40DD2BB8,MY: 0,DH: 0,DL: FFFF,AP: 2,NI: Coordinator
Connected to the USB port (/dev/ttyUSB0) of the Raspberry Pi openHAB server.

XBee Router 1
Product Family: XB24-ZB (Series 2 XBee), Function Set: ZigBee Router API, Firmware: 23A7
MAC: 0013A20040DD2BCF,SH: 13A200,SL: 40DD2BCF,MY: 0,DH: 0,DL: FFFF,AP: 2,NI: Router1
Connected to the Arduino MEGA XBee shield.

IMPORTANT: Ensure ALL XBees have AP = 2 set!!!

The software XCTU is used for XBee configuration (URL=https://www.digi.com/products/xbee-rf-solutions/xctu-software/xctu]link[/URL).
Use the XBee Explorer for updating firmware, configuration and test the XBee Modules.

Arduino Wiring
BMP180 (I2C) = Arduino UNO or MEGA - used a MEGA for this experiment.

Code: Select all

3.3 = 3.3v   - Do not use 5v!
SDA = A4 (UNO), 20 (MEGA) - Data
SCL = A5 (UNO), 21 (MEGA) - Clock
GND = GND


Code: Select all

LED GREEN = Arduino
+ (long leg) + R220Ohm = 12
GND (short leg) = GND
LED RED = Arduino
+ (long leg) + R220Ohm = 11
GND (short leg) = GND


Image

Arduino Sketch (pseudo code)
The Arduino uses the libraries Adafruit_BMP085_Library and xbee-arduino, installed in the Arduino IDE (used v1.8.4).

Define the payload 9 byte array: byte[0]=ID as char "R" 1 byte, bytes[1-4]=Temperature as long 4 bytes, Bytes[5-8]=Pressure as long 4 bytes
Define the XBee Coordinator receiving SH & SL address (taken from the MAC address).
Continuously read the temperature & pressure
  • Both temperature & pressure are converted from long to a 4 byte array.
  • Build the 9 byte array payload used to transmit to the XBee Coordinator.
  • Send the data as 9 byte array to the XBee Coordinator connected to the Raspberry Pi.

NOTE Uploading Sketch
When uploading the sketch from the Arduino IDE, ensure to remove the Arduino Shield Jumpers (XBee Shield) or set the Micro Serial Switch to USB (Wireless SD Shield).

Node-RED & openHAB
Image

Node-RED Flow (pseudo code)
A xbee-rx node receives the data and sends the message payload every 5s, via a trigger node, to a function node.
The function node convert the message payload containing 9 bytes, i.e. [52,108,7,0,0,3,143,1,0], into:
  • ID: byte[0], i.e. char "R"
  • Temperature: bytes[1-4], i.e. 108,7,0,0 = 1900
  • Pressure: bytes[5-8], i.e. 3,143,1,0 = 102147
The output of the function node are two messages, message Temperature and Pressure.
Example Result Full Message Payload:
{"topic":"temperature","payload":18.2,"_msgid":"c837e3a.522762"}
{"topic":"pressure","payload":102141,"_msgid":"c837e3a.522762"}
The messages are send to 2 openhab2-out nodes which update the items resulting in an update of the sitemap in the browser window.

openHAB Configuration
Items (xbeebmp180.items)

Code: Select all

Group BMP180
Number nXBee180_Temperature "Temperature [%.1f C]" <temperature>
Number nXBee180_Pressure "Pressure [%.0f pa]" <pressure>


Sitemap (xbeebmp180.sitemap)

Code: Select all

sitemap xbeebmp180 label="Experiment XBee BMP180"
{
   Frame label="BMP180" {
      Text item=nXBee180_Temperature
      Text item=nXBee180_Pressure
   }
}


ToDo
  • Experiment completed.
  • Enhancements ideas: Node-RED Dashboard with gauges & graphs, openHAB rrd4j Charts.

Version
  • 20180220

Hints
Raspberry Pi USB Symlink for XBee Explorer USB
On the Raspberry Pi, the XBee Explorer is connected to an USB port.
To ensure the right USB Port is used, a symbolic link (symlink) is assigned for an USB Port.
Steps to create a symbolic link for the XBee Explorer USB device connected to one of the USB ports:
1. Do not plug in the XBee Explorer USB device.
2. List the devices: ls /dev/tty*
3. Plug in the XBee Explorer USB device to an USB port.
4. List the devices again to check out the new USB device: ls /dev/tty*
5. A new device should be listed, i.e: /dev/ttyUSB0

To define the symlink, certain USB device information are required; idVendor, idProduct.
Get detailed USB devices information (Using username "pi"):

Code: Select all

sudo lsusb -v

Seek for Future Technology Devices International with iManufacturer FTDI (See details below).
The data required to define the symlink are the idVendor, idProduct.
Create the symlink:

Code: Select all

cd /etc/udev/rules.d/
sudo nano /etc/udev/rules.d/99-usb-serial.rules
Add the line:
SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6015", SYMLINK+="ttyUSBxbee", MODE="0666"

Reboot the Raspberry Pi.
Login as user Pi and check the USB ports for /dev/ttyUSBxbee.
ls /dev/tty*
The output should contain /dev/ttyUSBxbee.

Code: Select all

...
/dev/ttyUSB0
/dev/ttyUSBxbee

Details USB device connected to /dev/ttyUSB0:

Code: Select all

Bus 001 Device 007: ID 0403:6015 Future Technology Devices International, Ltd Bridge(I2C/SPI/UART/FIFO)
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               2.00
  bDeviceClass            0 (Defined at Interface level)
  bDeviceSubClass         0
  bDeviceProtocol         0
  bMaxPacketSize0         8
  idVendor           0x0403 Future Technology Devices International, Ltd
  idProduct          0x6015 Bridge(I2C/SPI/UART/FIFO)
  bcdDevice           10.00
  iManufacturer           1 FTDI
  iProduct                2 FT231X USB UART
  iSerial                 3 DN01DJJ0
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength           32
    bNumInterfaces          1
    bConfigurationValue     1
    iConfiguration          0
    bmAttributes         0xa0
      (Bus Powered)
      Remote Wakeup
    MaxPower               90mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           2
      bInterfaceClass       255 Vendor Specific Class
      bInterfaceSubClass    255 Vendor Specific Subclass
      bInterfaceProtocol    255 Vendor Specific Protocol
      iInterface              2 FT231X USB UART
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x81  EP 1 IN
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x02  EP 2 OUT
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               0
Device Status:     0x0000
  (Bus Powered)

Return to “Experiments”



Who is online