Android Tutorial [IoT] XBee (ZigBee) Communication between Android and Raspberry Pi

Digi XBee modules are a great solution for wireless communication. Using two or more XBee modules you can create a wide area wireless network. The maximum range depends on the antenna and the configuration. Outdoors, it can reach several kilometers.


The bandwidth is relatively low so the messages should be short.

In this example we will control the Raspberry Pi (B4J) with an Android tablet (B4A). The XBee modules are connected to the Android tablet and RPi board with two XBee USB adapters.

SS-2016-01-20_11.57.28.jpg



The USB adapter is based on a FTDI USB to serial chip. This means that we will use felUsbDriver library in B4A and jSerial library in B4J.
The messages will be managed by AsyncStreamsText. AsyncStreamsText is useful in cases where we cannot use AsyncStreams in prefix mode. It will correctly build the messages and raise the NewText event whenever a complete message is available.

Note that in the B4A project the AsyncStreamsText module is slightly modified to allow it to work with felUsbSerial DataAvailable event (the change is trivial).

The messages are made of a comma separated string with the target, led or switch number and the state.

The XBee modules are configured in AT (application transparent) mode.

One is configured to be the coordinator and the other is configured as a router.

SS-2016-01-20_12.08.17.png


All the other settings are the default settings. This means that the router sends messages to the coordinator and the coordinator broadcasts messages to all devices (note that there are more efficient configurations for two modules).

The code is very simple. The B4J program is about 40 lines. The B4A program is a bit longer because of the UI.
 

Attachments

  • RPiXBee.zip
    2 KB · Views: 1,425
  • AndroidXBee.zip
    10.6 KB · Views: 1,874

microbox

Active Member
Licensed User
Longtime User
Hi Erel, thanks for this. Now I really need to play with my RPI :). BTW is android device required rooted?
 

walterf25

Expert
Licensed User
Longtime User
Digi XBee modules are a great solution for wireless communication. Using two or more XBee modules you can create a wide area wireless network. The maximum range depends on the antenna and the configuration. Outdoors, it can reach several kilometers.


The bandwidth is relatively low so the messages should be short.

In this example we will control the Raspberry Pi (B4J) with an Android tablet (B4A). The XBee modules are connected to the Android tablet and RPi board with two XBee USB adapters.

SS-2016-01-20_11.57.28.jpg



The USB adapter is based on a FTDI USB to serial chip. This means that we will use felUsbDriver library in B4A and jSerial library in B4J.
The messages will be managed by AsyncStreamsText. AsyncStreamsText is useful in cases where we cannot use AsyncStreams in prefix mode. It will correctly build the messages and raise the NewText event whenever a complete message is available.

Note that in the B4A project the AsyncStreamsText module is slightly modified to allow it to work with felUsbSerial DataAvailable event (the change is trivial).

The messages are made of a comma separated string with the target, led or switch number and the state.

The XBee modules are configured in AT (application transparent) mode.

One is configured to be the coordinator and the other is configured as a router.

SS-2016-01-20_12.08.17.png


All the other settings are the default settings. This means that the router sends messages to the coordinator and the coordinator broadcasts messages to all devices (note that there are more efficient configurations for two modules).

The code is very simple. The B4J program is about 40 lines. The B4A program is a bit longer because of the UI.
Wow, i'm getting excited to start playing around with my Electronics stuff again, haven't done anything in a long time, i have a few of this Xbee modules laying around, i might have just finally found a use for them, Thanks Erel.

Walter
 

microbox

Active Member
Licensed User
Longtime User
No. It uses the USB host feature of Android 4+ devices.
I'm used of using bluetooth and OTG cables with rooted device to connect to mcircontroller device..this will be very interesting for me to learn :). Xbee looks connected to a usb adapter before samsung tablet?
 

microbox

Active Member
Licensed User
Longtime User
That's correct. This is a USB adapter for Samsung Galaxy Tab 2.
Can you give a link any basic tutorial.. like the basic device and cables required? I have an arduino Uno and arduino Mega2560 that I can test for this particular setup. :)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
In this example you use the ZigBee's like a slow Serial2Wireless adapter?
This example demonstrates how you can use XBee to communicate between two or more devices.
Go over the XBee video. It talks about several use cases which are not possible without a specialized RF network as this one.

The range is a very important factor. The fact that the network maximum range is several kilometers makes it possible to do all kinds of interesting solutions. Think about agriculture solutions for example where you put all kinds of sensors or controllers in the field.

@microbox please start a new thread for this (make sure to search for usbserial first).
 

Aldo85ita

New Member
Question (I hope to be not too far from this topic): is it possible to control Xbee GPIO from a remote tablet (removing raspberry from your application)? It seems that I need to send special AT commands to do this. So, the real question is: can an android application send AT commands on a remote Xbee? Can we send AT commands to USB serial adapter?
 
Top