{"id":620,"date":"2015-07-28T11:12:45","date_gmt":"2015-07-28T10:12:45","guid":{"rendered":"http:\/\/axotron.se\/blog\/?p=620"},"modified":"2015-11-28T17:30:57","modified_gmt":"2015-11-28T16:30:57","slug":"interfacing-to-an-xbee-module-from-python","status":"publish","type":"post","link":"https:\/\/axotron.se\/blog\/interfacing-to-an-xbee-module-from-python\/","title":{"rendered":"Interfacing to an XBee Module from Python"},"content":{"rendered":"<p>In a <a href=\"http:\/\/axotron.se\/blog\/interfacing-an-xbee-module-to-a-frdm-board\/\" target=\"_blank\">previous post<\/a>, I described how to talk to an XBee ZigBee module from a FRDM development platform. In this post, I describe how to do it from Python on e.g. a Windows PC.<\/p>\n<p>First, one obviously has to have a Python environment set up on the computer. I use Python 2.7 and the <a href=\"http:\/\/ipython.org\/index.html\" target=\"_blank\">IPython interactive shell<\/a>. I will not describe how to install that, but it should not be too hard to do using the instructions on the IPython website.<\/p>\n<p>Then we need to install two modules to enable communication with the XBee. The first one is simply called xbee and can be found <a href=\"https:\/\/pypi.python.org\/pypi\/XBee\" target=\"_blank\">here<\/a>. Download, unpack (using e.g. 7-zip), start a cmd window, cd to the folder with the downloaded files and install it by typing:<\/p>\n<pre>python setup.py install<\/pre>\n<p>The second is pyserial. Download it from <a href=\"https:\/\/pypi.python.org\/pypi\/pyserial\" target=\"_blank\">here<\/a> and install it in a similar manner as described above. This module mentions that it is intended for 32-bit windows, but it has worked fine for me on 64-bit Windows 7.<\/p>\n<p>I use a\u00a0<a href=\"https:\/\/www.sparkfun.com\/products\/11812\" target=\"_blank\">SparkFun XBee Explorer USB board <\/a>to interface to the XBee module. This provides an FTDI serial port for communication with the XBee. I soldered an LED with a series resistor between DIO1 and GND to provide easy verification that I was able to control the I\/Os of the XBee. The XBee needs to be in API mode. If it is not, the XCTU program from Digi can be used to upload a proper firmware to the module.<\/p>\n<p>Here is the program I wrote to test communication with the XBee:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n#! \/usr\/bin\/python\r\n\r\n# Demo to talk to an XBee ZigBee device\r\n# Per Magnusson, 2015-07-28\r\n \r\nfrom xbee import ZigBee\r\nimport serial\r\nimport serial.tools.list_ports\r\nimport time\r\nimport sys\r\n\r\n# Look for COM port that might have an XBee connected\r\nportfound = False\r\nports = list(serial.tools.list_ports.comports())\r\nfor p in ports:\r\n    # The SparkFun XBee Explorer USB board uses an FTDI chip as USB interface\r\n    if &quot;FTDIBUS&quot; in p&#x5B;2]:\r\n        print &quot;Found possible XBee on &quot; + p&#x5B;0]\r\n        if not portfound:\r\n            portfound = True\r\n            portname = p&#x5B;0]\r\n            print &quot;Using &quot; + p&#x5B;0] + &quot; as XBee COM port.&quot;\r\n        else:\r\n            print &quot;Ignoring this port, using the first one that was found.&quot;\r\n\r\nif portfound:\r\n    ser = serial.Serial(portname, 9600)\r\nelse:\r\n    sys.exit(&quot;No serial port seems to have an XBee connected.&quot;)\r\n\r\n# Flash the LED attached to DIO1 of the XBee\r\ntry:\r\n    xbee = ZigBee(ser)\r\n    print &quot;XBee test&quot;\r\n\r\n    xbee.at(command='D1', parameter='\\x05') # Pin 1 high\r\n    resp = xbee.wait_read_frame()\r\n    print resp\r\n\r\n    time.sleep(1)\r\n    xbee.at(command='D1', parameter='\\x04') # Pin 1 low\r\n    resp = xbee.wait_read_frame()\r\n    print resp\r\n\r\n    # Try another AT command\r\n    xbee.at(command='ID')\r\n    resp = xbee.wait_read_frame()\r\n    print resp\r\n    print &quot;Done&quot;\r\n    ser.close()\r\nexcept:\r\n    print &quot;Error!&quot;\r\n    ser.close()\r\n\r\nraw_input(&quot;Press Enter to continue...&quot;)\r\n<\/pre>\n<p>The program has some bells and whistles. First it looks at all the serial ports it can find and selects the first one that could be the XBee (indicated by the text FTDIBUS in the description of the port). It then tries to set up a connection to the ZigBee and issues three local AT commands. The first turns the LED on (then it waits for 1 second), the second turns the LED off and then it issues the ATID command. The response from these commands are printed. At last the serial port is closed and the program waits for the user to press enter to exit the program.<\/p>\n<p>The output may look like this:<\/p>\n<pre>In [3]: %run xbee_prog.py\r\nFound possible XBee on COM3\r\nUsing COM3 as XBee COM port.\r\nXBee test\r\n{'status': '\\x00', 'frame_id': '\\x01', 'command': 'D1', 'id': 'at_response'}\r\n{'status': '\\x00', 'frame_id': '\\x01', 'command': 'D1', 'id': 'at_response'}\r\n{'status': '\\x00', 'frame_id': '\\x01', 'parameter': '\\x00\\x00\\x00\\x00\\x00\\x00\\x0\r\n0\\x01', 'command': 'ID', 'id': 'at_response'}\r\nDone\r\nPress Enter to continue...\r\n\r\nIn [4]:<\/pre>\n<h4>Stupid mistake<\/h4>\n<p>While writing this program I made a stupid mistake that prevented it from working. I happened to name the program xbee.py and this made the line<\/p>\n<pre>from xbee import ZigBee\r\n<\/pre>\n<p>import the program itself, instead of the installed module. The error message was: ImportError: cannot import name ZigBee. Renaming the program from xbee.py to xbee_prog.py solved this issue.<\/p>\n<p>Now that I realize that Python imported the program itself instead of the module, it is obvious why it could not find ZigBee inside it, but it took a while before I figured that one out.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a previous post, I described how to talk to an XBee ZigBee module from a FRDM development platform. In this post, I describe how to do it from Python on e.g. a Windows PC. First, one obviously has to have a Python environment set up on the computer. I use Python 2.7 and the &hellip; <a href=\"https:\/\/axotron.se\/blog\/interfacing-to-an-xbee-module-from-python\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Interfacing to an XBee Module from Python<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-620","post","type-post","status-publish","format-standard","hentry","category-electronics"],"_links":{"self":[{"href":"https:\/\/axotron.se\/blog\/wp-json\/wp\/v2\/posts\/620","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/axotron.se\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/axotron.se\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/axotron.se\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/axotron.se\/blog\/wp-json\/wp\/v2\/comments?post=620"}],"version-history":[{"count":5,"href":"https:\/\/axotron.se\/blog\/wp-json\/wp\/v2\/posts\/620\/revisions"}],"predecessor-version":[{"id":733,"href":"https:\/\/axotron.se\/blog\/wp-json\/wp\/v2\/posts\/620\/revisions\/733"}],"wp:attachment":[{"href":"https:\/\/axotron.se\/blog\/wp-json\/wp\/v2\/media?parent=620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/axotron.se\/blog\/wp-json\/wp\/v2\/categories?post=620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/axotron.se\/blog\/wp-json\/wp\/v2\/tags?post=620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}