{"id":769,"date":"2016-01-07T20:03:13","date_gmt":"2016-01-07T19:03:13","guid":{"rendered":"http:\/\/axotron.se\/blog\/?p=769"},"modified":"2016-01-07T20:03:13","modified_gmt":"2016-01-07T19:03:13","slug":"problems-with-python-xbee-2-2-3-package","status":"publish","type":"post","link":"https:\/\/axotron.se\/blog\/problems-with-python-xbee-2-2-3-package\/","title":{"rendered":"Problems with Python XBee 2.2.3 package"},"content":{"rendered":"<p>This blog post describes an issue I am having with the XBee 2.2.3 Python package where the callbacks that are supposed to happen when new packets are received suddenly stop. I managed to find the reason for this as well as an at least temporary solution.<\/p>\n<h3>A bit of background<\/h3>\n<p>I just tried to add a second node to my small ZigBee network (now consisting of a coordinator and two routers) and ran into a bit of trouble. The new node has a new version of the XBee radio, namely XB24CZ7PIT-004. This has replaced the previous product XB24-ZB from Digi and it has a new firmware which simultaneously supports coordinator\/router\/end point. So one does not have to upload a firmware specific to the role the radio is playing in the network. Maybe it has other subtle differences as well. I do not know if the new radio version is relevant to the problems I am having, but I thought I&#8217;d better mention it.<\/p>\n<h3>The problem<\/h3>\n<p>I use a callback function to receive the data from the XBee, but the problem is that when I added the new XBee (as a router), I just got one or a few packets back from the coordinator (which is the one I talk to using Python) before callbacks stopped. I discovered that a new error callback feature had been added in 2.2.2, so I made use of this to try to troubleshoot:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n # Callback function that runs when XBee errors are detected\r\n def xbee_error(e):\r\n     print &quot;XBee error!&quot;\r\n     print e\r\n \r\n xbee = ZigBee(ser, callback = receive_data, error_callback = xbee_error)\r\n\r\n <\/pre>\n<p>This gave me the following printout before receive_data callbacks ceased:<\/p>\n<pre>XBee error!\r\n'Unrecognized response packet with id byte \\xa1'<\/pre>\n<p>So apparently a &#8220;new&#8221; kind of packet with frame type A1 has started to show up for some reason unknown to me. This turns out to be a &#8220;Route Record Indicator&#8221;. I have not looked into why this happens, but since the A1 frame type is not defined in the api_responses dictionary in zigbee.py and there is a new except-clause with a break in the definition of run() in base.py, I think the thread that receives data from the XBee and calls the callback function thereby terminates when such an unrecognized frame id is received.<\/p>\n<p>I do not think this is appropriate behavior in this case. The thread should not die just because an A1 frame ID appears. I solved it quickly and in an ugly manner by commenting out the troublesome break in base.py:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n\u00a0\u00a0\u00a0 def run(self):\r\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &quot;&quot;&quot;\r\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 run: None -&gt; None\r\n \r\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 This method overrides threading.Thread.run() and is automatically\r\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 called when an instance is created with threading enabled.\r\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &quot;&quot;&quot;\r\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 while True:\r\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 try:\r\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 self._callback(self.wait_read_frame())\r\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 except ThreadQuitException:\r\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Expected termintation of thread due to self.halt()\r\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break\r\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 except Exception as e:\r\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Unexpected thread quit.\r\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if self._error_callback:\r\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 self._error_callback(e)\r\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &lt;b&gt;#break # Commented out by Per M 2016-01-06&lt;\/b&gt;\r\n\r\n<\/pre>\n<p>There are probably better ways to solve this. Maybe the A1 and other possible frame IDs (see the API frame specifications section in the <a href=\"http:\/\/ftp1.digi.com\/support\/documentation\/90002002.pdf\" target=\"_blank\">XBee User Guide<\/a>) should be added to api_responses in zigbee.py.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This blog post describes an issue I am having with the XBee 2.2.3 Python package where the callbacks that are supposed to happen when new packets are received suddenly stop. I managed to find the reason for this as well as an at least temporary solution. A bit of background I just tried to add &hellip; <a href=\"https:\/\/axotron.se\/blog\/problems-with-python-xbee-2-2-3-package\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Problems with Python XBee 2.2.3 package<\/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-769","post","type-post","status-publish","format-standard","hentry","category-electronics"],"_links":{"self":[{"href":"https:\/\/axotron.se\/blog\/wp-json\/wp\/v2\/posts\/769","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=769"}],"version-history":[{"count":3,"href":"https:\/\/axotron.se\/blog\/wp-json\/wp\/v2\/posts\/769\/revisions"}],"predecessor-version":[{"id":772,"href":"https:\/\/axotron.se\/blog\/wp-json\/wp\/v2\/posts\/769\/revisions\/772"}],"wp:attachment":[{"href":"https:\/\/axotron.se\/blog\/wp-json\/wp\/v2\/media?parent=769"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/axotron.se\/blog\/wp-json\/wp\/v2\/categories?post=769"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/axotron.se\/blog\/wp-json\/wp\/v2\/tags?post=769"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}