Thursday, April 24, 2014

Interfacing XBee with OpenHAB

Purpose of this article is to provide you a kick start for interfacing XBee modules in to openHAB.
Things you need;
1. Preferably Ubuntu machine for openHAB development
2. WINE setup on Ubuntu machine or Windows machine for running X-CTU (XBee configuration utility)
3. XBee Series 2 modules (Series 1 would also fine. But configurations would slightly differ)
4. XBee Explore USB - 1(and XBee 3.3 v power source) or 2 (Both can be powered up from USB)
5. 1 hour of good time :)

Setting up development environment

For setting up Eclipse IDE for OpenHAB, you could follow the instructions at OpenHAB Eclipse IDE setup. Basically it can be installed using 3 methods; install the required plug-ins in Eclipse, install both Eclipse + required plug-ins from Yoxos installer, download the pre-packaged Eclipse + required plug-ins. I used the 3rd method on 64 bit Ubuntu installation. (Note: All 3 methods failed on 32 bit Windows 7)

Tips in case the build fails:
1. Eclipse > Window > Preferences > Java > Compiler > Compiler compliance level to 1.7.
2. Eclipse > Window > Preferences > Installed JREs > Execution Environments > Set 1.7 as compatible environment for each execution environment.
3. Eclipse > Window > Preferences > Plug-in Development > Target Platform > Select openHAB and Reload it.
4. Make sure you have generated the required code by executing org.openhab.model.codegen. 
5. Do a maven clean install
6. Clean the complete workspace

Binding for XBee modules

Original OpenHAB source tree does not include the bindings required for XBee. So we shall pull it from another branch (diaoulael-xbee).  A checked out modified version can be found at openhab_ext

1. Just download the source code, then copy the content of /bundles/binding/org.openhab.binding.xbee in to previously cloned original source tree's /bundles/binding/.
2. Using a text editor, add following XML tag to /bundles/binding/pom.xml, inside the <modules> element.
<module>org.openhab.binding.xbee</module> 
3. Import the new project into Eclipse workspace (openHAB)
4. In Eclipse Run/Debug configurations, Eclipse Application > openHAB runtime > Plug-ins, set org.openhab.binding.xbee Auto start 'true' and start level 'default'

Configurations of XBee modules

One going to be interfaced with the openHAB server machine has to be configured as a coordinator and other as an End device.
1. Download and install X-CTU Next gen Xbee configuration too. I used a Windows machine for XBee configurations.
2. Mount a XBee module on XBee Exlorer USB and add it in X-CTU.
3. Upload the ZNet Coordinator API latest firmware 
4. Configure following setting
     a) Set AP mode to 2
     b) Set serial settings 9600 bps, No flow control, No parity
5. Mount the next XBee module
6. Upload ZNet router/End device API latest firmware
     a) Set AP mode to 2
     b) Set serial settings 9600 bps, No flow control, No parity
     c) Disable all IO lines except DIO-0 to Digital input
   d) Set IR (sampling interval) to 1388 (5 seconds). So it will sample the DIO-0 (pin number 20) and transmit IOSample packet each 5 seconds.

Letting openHAB know about XBee and how to bind it

1. Connect XBee module which was configured as the coordinator to openHAB server machine (running on Ubuntu).
2. Connect/Disconnect to find out the USB device interface assigned to Xbee Explorer USB. For my case it was /tty/USB0
3. Make sure your user account is provided with dialing out permissions 
sudo adduser $USER dialout 
and logout/login
4. In Eclipse openHAB workspace, distribution/openhabhome/configurations/openhab_default.cfg, add following.

################################ XBee Binding #########################################
# Serial port of XBee interface
# Valid values are e.g. COM1 for Windows and /dev/ttyS0 or /dev/ttyUSB0 for Linux
xbee:serialPort=/dev/ttyUSB0

#Baudrate (optional)
xbee:baudRate=9600
5. In Eclipse openHAB workspace, distribution/openhabhome/configurations/items/demo.items, add following. Here I am going to introduce a contact (binary two state "Open" and "Closed") item named Window_FF_Bed2 under "FF_Bed" location and "Windows" groups. Item would appear in web interface under First floor > Bed Room > Bed Room2.

Contact Window_FF_Bed2            "Bedroom2 [MAP(en.map):%s]"         (FF_Bed, Windows)     { xbee="<znetrxiosampleresponse@0013A20040A2206C#D0:" }

Replace the highlighted 64 bit address with your XBee end device's address which can be queried from API/AT commands or printed in the backside of the module.

Running things together

1. Mount the XBee coordinator on XBee Explorer USB and interface it to the openHAB server machine
2. Mount the XBee end device on another XBee Explorer USB and power it up(or simply provide it 3.3v). No need to provide any signaling interface as XBee modules is programmed to sample DIO 0 (pin 20) and transmit the status each 5 seconds by itself.
In Eclipse > Run > Run configurations > openHAB runtime > Run.
2. Once the openHAB runtime is fully started, you should be able to browse 
http://192.168.0.3:8080/openhab.app?sitemap=demo
Replace highlighted IP with your local machine's IP.
3. Navigate to First floor > Bed room, Observe the Bedroom2 status. Initially this is set to "open"
4. Now short the pin 10 (GND) and pin 20 (DIO 0) to emulate a window being closed. Observe the status of Bedroom2 is changed from "open" to "closed". As you make the DIO pin voltage zero and open, changes are reflected in UI (remember we set the sampling to happen each 5 seconds). Hurray!


 
    





36 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Thank you. This is such a help.

    ReplyDelete
  3. May I know how to pass string as cmd(command) to the END DEVICE ?

    ReplyDelete
    Replies
    1. Current XBee binding implementation (I used https://code.google.com/r/diaoulael-xbee/) has an empty XBee transmit handler in binding/org.openhab.binding.xbee/src/main/java/org/openhab/binding/xbee/internal/XBeeBinding.java,

      ie.

      protected void internalReceiveCommand(String itemName, Command command)

      There you need to develop a code for converting and transmitting a XBee message. I may post a different blog post as the time permits. Quite a busy time these days...

      Delete
  4. https://code.google.com/r/diaoulael-xbee/ how to get code from this link plase help me

    ReplyDelete
    Replies
    1. Thanks for asking about. It seems that repository has been moved out. I found an archived repo at http://paoloquinci-xb.googlecode.com/hg-history/99568c4ecf4386fafc5950787cbc0753dfe758f2/bundles/binding/org.openhab.binding.xbee/src/main/java/org/openhab/binding/xbee/.

      Delete
    2. Hi,
      Thank you,Thanks a lot for reply and code.

      Delete
  5. Hi,
    Sorry to disturb you once again.In the above link I can find binding but I cant download that binding.Please if you have the zip file ,can you send it to me, through git or to my mail directly.please help me.

    ReplyDelete
    Replies
    1. A modified version can be found at https://bitbucket.org/pwkalana9/openhab_ext

      Delete
  6. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  7. Hi,
    Thank you sir hank you very very much for the whole code.please keep on helping people like me,if I get doubt will ask please support me.Nice tutorial Thank you once again.

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. Hi,
    I am trying to do same thing using Raspberry Pi.Can I run the above xbee binding in Raspberry Pi ? I got this doubt because Raspberry Pi uses ARM processor.so wether i need to change anything in the code.Please help me.
    Thank you,

    ReplyDelete
    Replies
    1. Yes. I tried this originally on Raspberry Pi. Since OpenHAB is Java based, as long as you have a JRE(Java Runtime Environment), it suppose to work independent of the OS and processor.

      Delete
  10. Hi,
    To work with Raspberry pi wether all the steps you have explained are similar or any change we should do.
    Thank you.("Thank you very much for supporting me")

    ReplyDelete
    Replies
    1. Should be similar, as long as you are using an linux image on Raspberry Pi. Otherwise, some slight changes into OS specific steps.

      Delete
  11. Hi,
    wether xbee device support clustering?

    ReplyDelete
  12. I mean wether it will support Z-stack cluster?

    ReplyDelete
    Replies
    1. XBee device supports clustering. However, currently used API only provides basic communication between the nodes as I recall. If you need higher level clustering, you may need to modify the XBee API.

      Delete
  13. Ok Thank you very much for Knowledge transfer.

    ReplyDelete
  14. Hi,
    I bought all the component you have told.I started to do this project in Raspberry pi.I install Raspbian os.The problem is all the open hab bindings are in .jar file and file structure when i extract it.
    jar file contains MET-INF,org,OSGI-INF,readme.txt.

    but xbee binding you have written has file structure.
    lib, MET-INF,src,target,OSGI-INF,build.properties,pom.xml
    I know this is beacuase you used eclipse but same binding how can i use ,if i convert your binding to .jar file is that will work?i think no so what should i do.please help me.

    Thank you.

    ReplyDelete
  15. Hi,
    Please help me 1 or 2 steps away.I am running above binding in eclipse itself.I configured xbee devices too.
    But I am getting following error..



    RXTX fhs_lock() Error: opening lock file: /var/lock/LCK..ttyUSB0: File exists.
    �� g testRead() Lock file failed
    RXTX fhs_lock() Error: opening lock file: /var/lock/LCK..ttyUSB0: File exists.
    �� g testRead() Lock file failed
    RXTX fhs_lock() Error: opening lock file: /var/lock/LCK..ttyUSB0: File exists.
    open: locking has failed for /dev/ttyUSB0

    XBee connection failed: {} exception





    ReplyDelete
  16. when I debug I found the that its going to exception in

    XbeeBinding.java class, connect method,
    this line xbeeConnector = new XBeeSerialConnector(serialPort, baudRate);

    Iam not getting what is the problem please help me.

    (I am using linux system for eclipse)
    and port coordinator port detected as ttyUSB0.

    ReplyDelete
  17. I have done this too sudo adduser $USER dialout

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. I assume you replaced $USER with your username, and did log out and log in again to the compute to take the changes into effect. Also you can try with sudo chmod 666 ttyUSB0 to provide the permission explicitly.

      Delete
  18. Hi,
    I have done all you have mentioned above.When i run the code and launch in browser its showing open but when i short 10 and 20th pin its not showing closed (bathroom 2)

    ReplyDelete
    Replies
    1. There could be many reasons for something to not work it as supposed to. I would suggest you to debug and verify the gateway received the message at XBeeConnector.

      Delete
  19. Hi,
    Thank you for supporting till end.This is the only site helped me lot in openHab.I wont forget you in my life Thank you v much.

    ReplyDelete
  20. Hi,
    whether this binding supports zigbee too.Why i got this doubt because when i look into the jar file in xbee it contains zigbee so?is this binding supports zigbee.

    ReplyDelete
    Replies
    1. ZigBee is the standard. XBee is one popular RF module produced by Digi Inc. Their XBee modules support Zigbee protocol, which is used in this binding. On other words, you may call it is XBee.Zigbee binding.

      Delete
  21. Hi,
    Ho Thank you for the information I miss understood this before.Now I am clear.

    ReplyDelete
  22. Hi,
    Have you worked on openhab mqtt.If yes have you made any tutorials like this.

    ReplyDelete

My Linkedin profile