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

Discussion in 'Tutorials & Examples' started by Erel, Jan 20, 2016.

  1. Erel

    Erel Administrator Staff Member Licensed 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.


    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.


    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.

    Attached Files:

    rwblinn, MarcoRome, b4auser1 and 14 others like this.
  2. microbox

    microbox Active Member Licensed User

    Hi Erel, thanks for this. Now I really need to play with my RPI :). BTW is android device required rooted?
  3. walterf25

    walterf25 Well-Known Member Licensed User

    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.

  4. Erel

    Erel Administrator Staff Member Licensed User

    No. It uses the USB host feature of Android 4+ devices.
    BillMeyer and microbox like this.
  5. microbox

    microbox Active Member Licensed User

    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?
  6. Erel

    Erel Administrator Staff Member Licensed User

    That's correct. This is a USB adapter for Samsung Galaxy Tab 2.
  7. KMatle

    KMatle Expert Licensed User

    Ehm. Please don't bann me from the forum :D In this example you use the ZigBee's like a slow Serial2Wireless adapter?
  8. microbox

    microbox Active Member Licensed User

    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. :)
  9. Erel

    Erel Administrator Staff Member Licensed User

    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).
  10. Erel

    Erel Administrator Staff Member Licensed User

    Yesterday I attended a local conference about IoT. Two speakers described an industrial solution that they were involved with. Both of them used ZigBee to communicate between the sensors and the central computer.
  11. Aldo85ita

    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?
  12. Erel

    Erel Administrator Staff Member Licensed User

    I'm not an XBee expert.

    It doesn't matter whether you connect the XBee to a PC or an Android. You can do the same things.
    You should be able to enter command mode and configure the connected XBee module: Commands.pdf

    I don't think that you can configure remote XBees.
  13. Aldo85ita

    Aldo85ita New Member

    Yes, you're right: it seems that AT commands can be sent only to local Xbee module. Thanks
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice