{"id":1821,"date":"2015-03-06T00:00:09","date_gmt":"2015-03-06T08:00:09","guid":{"rendered":"http:\/\/192.168.3.4\/?p=1821"},"modified":"2018-01-09T06:49:48","modified_gmt":"2018-01-09T14:49:48","slug":"xbee-arduino-and-rpi","status":"publish","type":"post","link":"https:\/\/www.cloudacm.com\/?p=1821","title":{"rendered":"XBee, Arduino, and RPi"},"content":{"rendered":"<p><strong>Introduction &#8211; <a href=\"https:\/\/www.youtube.com\/watch?v=y1tFQMjc-IE\" target=\"_blank\">over the garden wall<\/a><br \/>\n<\/strong><\/p>\n<p>In mid July of 2014, I had posted about a new version of RPi and XBee.\u00a0 In this post will cover XBee wireless technology.\u00a0 We will also cover how <a href=\"http:\/\/www.faludi.com\/bwsn\/tmp36-instructions-simple-sensor-network\/\" target=\"_blank\">XBee can be used<\/a> to link <a href=\"http:\/\/garagelab.com\/profiles\/blogs\/tutorial-humidity-and-temperature-sensor-with-arduino\" target=\"_blank\">sensors<\/a> to the <a href=\"http:\/\/apartmentarduino.blogspot.com\/2012\/01\/step-three-interfacing-rht03.html\" target=\"_blank\">RPi<\/a>.\u00a0 Next, we go through\u00a0linking the Arduino to the RPi.\u00a0 In reminiscence, a newer offering of the RPi has just been made available to the public.\u00a0 I may do a brief overview of the new features, but that will be secondary.<\/p>\n<p><strong>Purpose &#8211; the wire dogs can&#8217;t get through<br \/>\n<\/strong><\/p>\n<p>One issue with bench setups is they don&#8217;t scale well in the real world.\u00a0 Most of the problem has to due with connections.\u00a0 There was a project I was developing and the biggest challenge was cabling.\u00a0 Using a wireless connection was the only viable option.\u00a0 Some barriers take too much effort and can hinder completion.\u00a0 Using XBee broke down the wall.<\/p>\n<p><strong>Details &#8211; cobblestones<br \/>\n<\/strong><\/p>\n<p>In this post I&#8217;ll be logging temperature and humidity readings from a DHT22 sensor.\u00a0 You can find <a href=\"https:\/\/www.adafruit.com\/product\/385\" target=\"_blank\">them online<\/a> at Adafruit.\u00a0 The sensor will be wired with an Arduino Uno board that runs a sketch and outputs the readings serially to a XBee.\u00a0 I&#8217;ll be using an <a href=\"https:\/\/www.adafruit.com\/product\/126\" target=\"_blank\">wireless modem adapter<\/a> for the XBee that I purchased from Adafruit.\u00a0 Since this will be point to point, I have 2 XBee and modem adapters.\u00a0 I&#8217;m using the <a href=\"https:\/\/www.adafruit.com\/product\/968\" target=\"_blank\">ZB Series 2<\/a> to keep costs down.\u00a0 There are higher output models for greater range, but I won&#8217;t need that for this project.\u00a0 On the other end of the wireless connection, I&#8217;ll wire the XBee serial and power rails to a <a href=\"https:\/\/www.adafruit.com\/products\/1754\" target=\"_blank\">PI T-Cobbler<\/a>.\u00a0 This makes it easier to connect jumpers to the bread board, without the ribbon cable getting in the way.<\/p>\n<p><a href=\"http:\/\/192.168.3.4\/wp-content\/uploads\/2015\/03\/XBee-wireless-modem-adapter-pinout.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-1946\" src=\"http:\/\/192.168.3.4\/wp-content\/uploads\/2015\/03\/XBee-wireless-modem-adapter-pinout-300x184.jpg\" alt=\"XBee wireless modem adapter pinout\" width=\"300\" height=\"184\" srcset=\"https:\/\/www.cloudacm.com\/wp-content\/uploads\/2015\/03\/XBee-wireless-modem-adapter-pinout-300x184.jpg 300w, https:\/\/www.cloudacm.com\/wp-content\/uploads\/2015\/03\/XBee-wireless-modem-adapter-pinout-100x61.jpg 100w, https:\/\/www.cloudacm.com\/wp-content\/uploads\/2015\/03\/XBee-wireless-modem-adapter-pinout-150x92.jpg 150w, https:\/\/www.cloudacm.com\/wp-content\/uploads\/2015\/03\/XBee-wireless-modem-adapter-pinout-200x122.jpg 200w, https:\/\/www.cloudacm.com\/wp-content\/uploads\/2015\/03\/XBee-wireless-modem-adapter-pinout-450x275.jpg 450w, https:\/\/www.cloudacm.com\/wp-content\/uploads\/2015\/03\/XBee-wireless-modem-adapter-pinout-600x367.jpg 600w, https:\/\/www.cloudacm.com\/wp-content\/uploads\/2015\/03\/XBee-wireless-modem-adapter-pinout.jpg 750w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>This is a good time to mention the logic differences between the Arduino Uno and the RPi.\u00a0 The Uno operates at 5V logic levels, while the RPi operates at 3.3V levels.\u00a0 If we were to directly connect the Uno to the RPi, something would get bricked.\u00a0 Here comes another benefit of using XBee and the modem adapter.\u00a0 The XBee will only operate on 3.3V levels, however the adapter regulates to that voltage from a 5V source.\u00a0 So, we can connect the XBee modem adapter directly to either the Uno or RPi.\u00a0 Be sure to correctly link the power based on the logic level of your system.<\/p>\n<p><a href=\"http:\/\/192.168.3.4\/wp-content\/uploads\/2015\/03\/XBee-Uno-RPi-Interface.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-2002\" src=\"http:\/\/192.168.3.4\/wp-content\/uploads\/2015\/03\/XBee-Uno-RPi-Interface-300x226.png\" alt=\"XBee Uno RPi Interface\" width=\"300\" height=\"226\" srcset=\"https:\/\/www.cloudacm.com\/wp-content\/uploads\/2015\/03\/XBee-Uno-RPi-Interface-300x226.png 300w, https:\/\/www.cloudacm.com\/wp-content\/uploads\/2015\/03\/XBee-Uno-RPi-Interface-1024x773.png 1024w, https:\/\/www.cloudacm.com\/wp-content\/uploads\/2015\/03\/XBee-Uno-RPi-Interface-100x75.png 100w, https:\/\/www.cloudacm.com\/wp-content\/uploads\/2015\/03\/XBee-Uno-RPi-Interface-150x113.png 150w, https:\/\/www.cloudacm.com\/wp-content\/uploads\/2015\/03\/XBee-Uno-RPi-Interface-200x151.png 200w, https:\/\/www.cloudacm.com\/wp-content\/uploads\/2015\/03\/XBee-Uno-RPi-Interface-450x340.png 450w, https:\/\/www.cloudacm.com\/wp-content\/uploads\/2015\/03\/XBee-Uno-RPi-Interface-600x453.png 600w, https:\/\/www.cloudacm.com\/wp-content\/uploads\/2015\/03\/XBee-Uno-RPi-Interface-900x679.png 900w, https:\/\/www.cloudacm.com\/wp-content\/uploads\/2015\/03\/XBee-Uno-RPi-Interface.png 2040w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Here is a wiring diagram of the setup I&#8217;ve used.\u00a0 The pin numbering for the adapter doesn&#8217;t reflect that actual pins used, pay attention to that.\u00a0 There weren&#8217;t any special considerations other than paying attention to the voltage applied to what pin of the XBee adapter.\u00a0 I can&#8217;t say it enough, you&#8217;ll smoke your equipment if you hook up the wrong voltage.\u00a0 Need I say it again?<\/p>\n<p>The Uno sketch used was taken from the <a href=\"https:\/\/github.com\/adafruit\/DHT-sensor-library\" target=\"_blank\">Adafruit DHT sensor library<\/a>.\u00a0 The only thing I did was select the pin to use.\u00a0 I verified that I had readings on the serial monitor of the Uno IDE before moving forward.\u00a0 This is good to do.\u00a0 If something doesn&#8217;t talk with the XBee or RPi, I&#8217;ll know it isn&#8217;t the Uno or sensor.<\/p>\n<p>There are 3 options for sending data from XBee to XBee, digital, analog, or serial.\u00a0 I won&#8217;t go into all the details on all the inner workings of the XBee here, so please watch this to get an introduction on the principles.\u00a0 Consider <a href=\"http:\/\/www.tunnelsup.com\/donate.html\" target=\"_blank\">giving Richee a donation<\/a> for his hard work.\u00a0 I had to relearn the XCTU interface, it has significantly changed since the tutorial videos.\u00a0 However, the information is still relevant.<\/p>\n<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/odekkumB3WQ\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>The method used in this post will be serial, so we&#8217;ll operate the XBee units in AT mode.\u00a0 I verified my wireless communication worked with 2 Uno boards first.\u00a0 Again, this gives me confidence that I don&#8217;t have a problem with the sensor, Uno, or XBee devices.\u00a0\u00a0 Now onto the Raspberry Pi.<\/p>\n<p>This turned out to be more easier than I thought when I found <a href=\"https:\/\/www.coolcomponents.co.uk\/article\/the-stalker-sensor-platform-part-5-logging-data-using-raspberry-pi\" target=\"_blank\">this site<\/a>.\u00a0 There it is, all spelled out for you.\u00a0 I had most of the work done, so I skipped ahead to step 3.\u00a0 I&#8217;m going to summarize the steps I did do here.\u00a0 Here are the commands I ran.<\/p>\n<p>1. Install the python serial library, I already had it.<\/p>\n<pre><code>sudo apt-get install python-serial<\/code><\/pre>\n<p>2. Restart the RPi<\/p>\n<pre><code>sudo shutdown -r now<\/code><\/pre>\n<p>3. Edit the cmdline file so the serial link will work.<\/p>\n<pre><code>sudo nano \/boot\/cmdline.txt<\/code><\/pre>\n<p>*** REMOVE THESE LINES ***<\/p>\n<pre><code>console=ttyAMA0,115200\r\nkgdboc=ttyAMA0,115200<\/code><\/pre>\n<p>*** SAVE AND EXIT ***<\/p>\n<p>4. Edit the inittab file so the serial link will work.<\/p>\n<pre><code>sudo nano \/etc\/inittab<\/code><\/pre>\n<p>*** COMMENT OUT THIS LINE ***<\/p>\n<pre><code>T0:23:respawn:\/sbin\/getty -L ttyAMA0 115200 vt100<\/code><\/pre>\n<p>*** SAVE AND REBOOT ***<\/p>\n<p>5. Create the python script.<\/p>\n<pre><code>nano serialin.py<\/code><\/pre>\n<p>*** ENTER THESE LINES ***<\/p>\n<pre><code>import serial\r\nport = serial.Serial(\"\/dev\/ttyAMA0\", baudrate=9600, timeout=3.0)\r\nwhile True:\r\n    rcv = port.readline()\r\n    print(\"received\" + repr(rcv))<\/code><\/pre>\n<p>*** SAVE AND EXIT ***<\/p>\n<p>6. Run the python script and watch the readings in the terminal.<\/p>\n<pre><code>python serialin.py<\/code><\/pre>\n<p>The readings can now be logged on the RPi by changing the python script a bit, I used the one posted on Cool Components, thanks to the blokes across the pond.\u00a0 That&#8217;s it, the RPi is reading sensor readings gathered from the Uno board over a wireless connection and logging it to a file.<\/p>\n<pre><code>import serial\r\nport = serial.Serial(\"\/dev\/ttyAMA0\", baudrate=9600, timeout=3.0)\r\ndef filewrite(rcv):\r\n   logfile = open(\"templog.txt\", \"a\")\r\n   logfile.write(rcv)\r\n   logfile.close\r\nwhile True:\r\n   rcv = port.readline()\r\n   print(\"received\" + repr(rcv))\r\n   filewrite(rcv)<\/code><\/pre>\n<p><strong>Relations &#8211; rapid and flexible<br \/>\n<\/strong><\/p>\n<p>The nice thing about having a mobile wireless sensor ready to use is it&#8217;s ready.\u00a0 In the time it takes me to walk and place the sensor, I&#8217;m done.\u00a0 This sort of thing would take longer with wired connections, if I wanted to be discreet and tidy about it.\u00a0 There are times when the idea to monitor a condition evaporates before hardware is put in place.\u00a0 With the number of sensors available, connecting them shouldn&#8217;t be an issue.\u00a0 Nor will it be an issue to use a micro controller like the Uno or a micro processor like the RPi.\u00a0 Go build it!<\/p>\n<p><strong>Summary &#8211; now you&#8217;re talking<br \/>\n<\/strong><\/p>\n<p>In this post I covered how to link a RPi and a sensor polling Arduino using a wireless link provided by XBee devices.\u00a0 The wireless link provided a fast, safe, and effective way to interface the Arduino and RPi.\u00a0 With this information, you will have the means by which to develop and deploy these devices in a broader range of applications.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction &#8211; over the garden wall In mid July of 2014, I had posted about a new version of RPi and XBee.\u00a0 In this post will cover XBee wireless technology.\u00a0 We will also cover how XBee can be used to link sensors to the RPi.\u00a0 Next, we go through\u00a0linking the Arduino to the RPi.\u00a0 In reminiscence, a newer offering of the RPi has just been made available to the public.\u00a0 I may do a brief overview of the new features,&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.cloudacm.com\/?p=1821\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,6,3,8],"tags":[],"class_list":["post-1821","post","type-post","status-publish","format-standard","hentry","category-arduino","category-raspberry-pi","category-rd","category-xbee"],"_links":{"self":[{"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/posts\/1821","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1821"}],"version-history":[{"count":48,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/posts\/1821\/revisions"}],"predecessor-version":[{"id":2004,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=\/wp\/v2\/posts\/1821\/revisions\/2004"}],"wp:attachment":[{"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cloudacm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}