Raspberry Pi XBee Network Experiment (openHAB,B4J,B4R)

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

Raspberry Pi XBee Network Experiment (openHAB,B4J,B4R)

Postby rwblinn » 22 Jan 2018, 15:29

Raspberry Pi XBee Network Experiment (openHAB2,B4J,B4R)
Version 20180122

Objectives
To build a XBee Mesh Network to control an Arduino UNO with LED connected.
Switching the LED is done via openHAB Basic-UI sitemap containing a single switch.
This experiment is a proof-of-concept for my Home Automation Solution to enable controlling & reading sensors in an own network.
(20180131) NOTE: Instead of using B4J for communication between openHAB and the Arduino, a new Node-RED based solution has been developed which is the way-forward to use (Node-RED is included in openHABian).

Image
Image

Hardware
  • 1x Raspberry Pi 3 Model B v1.2
  • 1x Arduino UNO
  • 2x XBee XB24-ZB Modules
  • 1x XBee Explorer USB - to configure the XBee Modules and rund as XBee Coordinator connected to the Raspberry Pi.
  • 1x XBee Shield Module - to act as the XBee Router connected to the Arduino.
  • 1x LED RED - connected to pin 11 of the Arduino and getting switched via openHAB.

Software
  • openHAB 2.2.0 running on the Raspberry Pi 3.
  • B4J for the BeeCoordinator running a non-ui application on the Raspberry Pi.
  • B4R for the Arduino program to control switching of the LED.

Download
Download B4R & B4J source code and the openHAB configuration files.
More information.

XBee Mesh Network
The XBee Network has 2 XBee Modules:
XBee Coordinator connected to the Raspberry Pi openHAB Server via an XBee Explorer USB.
Configuration: XBee XB24-Z7WIT-004 Serie ZB Coordinator AT 9600/8/N/1/N-AT

XBee Router connected to an Arduino UNO with XBee Shield Module.
Configuration: XBee XB24-Z7WIT-004 Serie ZB Router AT 9600/8/N/1/N-AT

Communication Protocol
A central role in communication, plays the BeeCoordinator.
The BeeCoordinator routes MQTT messages from openHAB to the Arduino and v.v.
The BeeCoordinator is written in B4J (TODO ADD LINK) (Non-UI Application), runs as a Process on the Raspberry Pi and communicates with the Arduino using the B4XSerializer.
The Arduino runs a programm written in B4R (TODO ADD LINK).
Communication Flow:
[openHAB] <> MQTT Subscribe/Publish <> BeeCoordinator <> B4XSerialized Message <> B4RSerializer <> [Arduino]

Arduino Program
The Arduino program is written in B4R.
Steps to flash the B4R program to the Arduino:
  • Disconnect the Arduino from the PC port.
  • Remove the two XBEE/USB jumpers from the XBee Shield Modul.
  • Connect the Arduino to the PC port.
  • Compile and flash the program.
  • Disconnect the Arduino from the PC port.
  • Set the two XBEE/USB jumpers to XBee (towards the inside of the Arduino).
  • Connect the Arduino to the PC port - to provide power to the Arduino.

BeeCoordinator
The BeeCoordinator handles MQTT messages between openHAB and the Arduino.
openHAB sends a MQTT message to trigger the state of the LED. The BeeCoordinator takes the message, converts the payload to byte and sends the data to the Arduino.
The communication between the Raspberry Pi XBee Module and the Arduino XBee Module is done via serial 9600/8/N1/N - AT.

Test MQTT (via Terminal) LED ON/OFF
Open a Terminal and start the BeeCoordinator (java -jar beecoordinator.jar) and wait for messages.
Open a terminal and publish message, using mosquitto to switch the LED ON

Code: Select all

mosquitto_pub -t expxbee/led/set -m 1

Switch OFF:

Code: Select all

mosquitto_pub -t expxbee/led/set -m 0


The BeeCoordinator logs:

Code: Select all

XBeeLEDSwitch v20180121
[INFO] Waiting...
[INFO] MQTT Connected: OK
[INFO] MQTT MessageArrived: expxbee/led/set=1
[INFO] Send LED State 1 to the Arduino.
[INFO] MQTT MessageArrived: expxbee/led/set=0
[INFO] Send LED State 0 to the Arduino.


openHAB Configuration
MQTT Configuration
MQTT install the MQTT Binding and MQTT Action.
MQTT configuration:
File: /etc/openhab2/services/mqtt.cfg.
Content:
Set the broker URL:

Code: Select all

openhabdev.url=tcp://localhost:1883


Item Configuration
File: /etc/openhab2/items/xbeeledswitch.items.
Content:

Code: Select all

Switch aExpXBee_Switch {mqtt=">[openhabdev:expxbee/led/set:command:ON:1],>[openhabdev:expxbee/led/set:command:OFF:0]"}

Notes:
Set the LED on by invoking command ON which is translated to 1 as required by the BeeCoordinator. For OFF send 0.

File: /etc/openhab2/sitemaps/xbeeledswitch.sitemap.
Content:

Code: Select all

sitemap xbeeledswitch label="Experiment XBee LED Switch"
{
        Switch  item=aXBeeSwitch
}


Test openHAB Configuration
Start the BeeCoordinator:
On the Raspberry Pi start the B4J Bridge in folder /home/pi/b4j by running sudo java -jar b4j-bridge.jar
Open the B4J IDE, load the project beecoordinator.b4j, connect to the B4J bridge using the Raspberry Pi IP address and run the application (F5)

openHAB load sitemap with URL http://192.168.N.NN:8080/basicui/app?si ... eledswitch
Switch the LED ON / OFF.

Watch the openHAB log (open a terminal and run ssh -p 8101 openhab@localhost with password habopen):

Code: Select all

openhab>log:tail
08:48:04.172 [INFO ] [del.core.internal.ModelRepositoryImpl] - Loading model 'xbeeledswitch.sitemap'
08:48:31.502 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'aXBeeSwitch' received command ON
08:48:31.558 [INFO ] [smarthome.event.ItemStateChangedEvent] - aXBeeSwitch changed from NULL to ON
08:48:45.220 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'aXBeeSwitch' received command OFF
08:48:45.246 [INFO ] [smarthome.event.ItemStateChangedEvent] - aXBeeSwitch changed from ON to OFF


Instead of KARAF, logging using tail:

Code: Select all

tail -f /var/log/openhab2/openhab.log -f /var/log/openhab2/events.log


ToDo
  • For the Home Automation version: Consider to use the B4XSerializer or Node-RED instead, to communicate between the B4J BeeCoordinator and B4R Arduino program.

Version
  • 20180122 - Experiment completed

Return to “Experiments”



Who is online