{"id":820,"date":"2015-10-26T21:45:58","date_gmt":"2015-10-27T04:45:58","guid":{"rendered":"http:\/\/www.partofthething.com\/thoughts\/?p=820"},"modified":"2015-12-07T12:19:09","modified_gmt":"2015-12-07T20:19:09","slug":"data-acquisition-and-live-plotting-over-the-network-with-a-raspberry-pi-and-python","status":"publish","type":"post","link":"https:\/\/partofthething.com\/thoughts\/data-acquisition-and-live-plotting-over-the-network-with-a-raspberry-pi-and-python\/","title":{"rendered":"Data acquisition and live-plotting over the network with a Raspberry PI and Python"},"content":{"rendered":"<p>Sensors are fun!<em>\u00a0<\/em>I wanted to keep an eye on the temperature of one of my home physics projects and realized I needed a thermocouple and a convenient way to read it. This post is about how I got one and wrote a Python program to send the data from a Raspberry PI over the network to my laptop, which plots the results in real time.<\/p>\n<figure id=\"attachment_822\" aria-describedby=\"caption-attachment-822\" style=\"width: 660px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2015\/10\/thermocouple-in-fridge.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-822 size-large\" src=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2015\/10\/thermocouple-in-fridge-1024x768.jpg\" alt=\"Thermocouple in fridge\" width=\"660\" height=\"495\" srcset=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2015\/10\/thermocouple-in-fridge-1024x768.jpg 1024w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2015\/10\/thermocouple-in-fridge-300x225.jpg 300w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/a><figcaption id=\"caption-attachment-822\" class=\"wp-caption-text\">Thermocouple in fridge<\/figcaption><\/figure>\n<p>I bought a K-type thermocouple for several dollars that ranges from -100\u00b0C to 500\u00b0C. The Seebeck effect that makes it work only gives microvolts per degree so I needed a good amplifier for it. And since I wanted to read it on a Raspberry PI (which only has digital inputs), I&#8217;d need an analog-to-digital converter (ADC). Conveniently, <a href=\"https:\/\/www.adafruit.com\/products\/269\">the MAX31855<\/a> does amplification and ADC and is like $15, so I got one. Sweet. There&#8217;s even a <a href=\"https:\/\/github.com\/adafruit\/Adafruit_Python_MAX31855\">Python library<\/a> for it that makes it easy to access. Double-sweet.<\/p>\n<figure id=\"attachment_821\" aria-describedby=\"caption-attachment-821\" style=\"width: 660px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2015\/10\/MAX31855-thermocouple-amp.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-821 size-large\" src=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2015\/10\/MAX31855-thermocouple-amp-1024x768.jpg\" alt=\"The MAX31855 thermocouple amp and ADC\" width=\"660\" height=\"495\" srcset=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2015\/10\/MAX31855-thermocouple-amp-1024x768.jpg 1024w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2015\/10\/MAX31855-thermocouple-amp-300x225.jpg 300w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/a><figcaption id=\"caption-attachment-821\" class=\"wp-caption-text\">The MAX31855 thermocouple amp and ADC<\/figcaption><\/figure>\n<p><!--more--><\/p>\n<p>I decided to hook it up to the hardware SPI interface on the PI. This was supposed to be fastest so I figured, why not. On a RPi B+, that&#8217;s 3.3V -&gt; Vin, GND -&gt; GND, SCLK(GPIO11)-&gt;CLK, CE0 (GPIO 8)-&gt;CS, and MISO(GPIO 9)-&gt;DO. Easy enough. Then I just uncommented the Hardware SPI section in the Adafruit Python library example and got some readings. Excellent. It actually has a build-in temperature sensor as well so you can get its temperature and the temperature at the end of your thermocouple wire.<\/p>\n<figure id=\"attachment_823\" aria-describedby=\"caption-attachment-823\" style=\"width: 794px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2015\/10\/thermocouple-readings.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-823 size-full\" src=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2015\/10\/thermocouple-readings.png\" alt=\"first thermocouple readings\" width=\"794\" height=\"404\" srcset=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2015\/10\/thermocouple-readings.png 794w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2015\/10\/thermocouple-readings-300x153.png 300w\" sizes=\"auto, (max-width: 794px) 100vw, 794px\" \/><\/a><figcaption id=\"caption-attachment-823\" class=\"wp-caption-text\">first thermocouple readings<\/figcaption><\/figure>\n<p>At first I tried getting the Raspberry PI to plot it live. But frankly, it was just too slow to handle the live-plotting plus the encryption and forwarding of the X11 graphics over ssh (I even <a href=\"http:\/\/xmodulo.com\/how-to-speed-up-x11-forwarding-in-ssh.html\">dumbed down the encryption<\/a> since I was on a local network). Maybe a RPI2 could handle it, but the B+ was too slow. So I got the idea to send the data over the network to my laptop which could spend all the CPU it wanted on animating plots and updating and stuff. I had to learn some stuff about the <a href=\"https:\/\/docs.python.org\/2\/howto\/sockets.html\">Python socket module<\/a>, but it was really pretty straightforward. I made a client script that runs on the PI, reading the sensor and transmitting the data. My server script runs on the laptop and receives the data, plotting it live.<\/p>\n<h3>Plotting live data<\/h3>\n<figure id=\"attachment_824\" aria-describedby=\"caption-attachment-824\" style=\"width: 660px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2015\/10\/fridge2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-824 size-large\" src=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2015\/10\/fridge2-1024x721.png\" alt=\"Refridgerator temperature\" width=\"660\" height=\"465\" srcset=\"https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2015\/10\/fridge2-1024x721.png 1024w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2015\/10\/fridge2-300x211.png 300w, https:\/\/partofthething.com\/thoughts\/wp-content\/uploads\/2015\/10\/fridge2.png 1442w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/a><figcaption id=\"caption-attachment-824\" class=\"wp-caption-text\">Temperature in the refrigerator as the door is opened and then closed<\/figcaption><\/figure>\n<p>Plotting the live data isn&#8217;t too bad with matplotlib, though it was a bit tricky due to the ongoing network communication. First, just get a plot animation going using one of the examples of the FuncAnimation. <a href=\"http:\/\/matplotlib.org\/1.2.1\/examples\/animation\/animate_decay.html\">This decay one is particularly useful<\/a>. Once you have that running and fully understand it, then modify the data_gen method to give you data off the network as it becomes available.<\/p>\n<p>You&#8217;re going to need Threads. The real trick is to use a <a href=\"https:\/\/docs.python.org\/2\/library\/queue.html\">Queue <\/a>which I learned about in this great <a href=\"http:\/\/www.slideshare.net\/dabeaz\/an-introduction-to-python-concurrency\">presentation on Python concurrency<\/a>. My implementation is still not excellent (it hangs a lot), but it gets the job done. Check out my full code and improve it if you want. It&#8217;s <a href=\"https:\/\/github.com\/partofthething\/simpleDAQ\">hosted on github<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sensors are fun!\u00a0I wanted to keep an eye on the temperature of one of my home physics projects and realized I needed a thermocouple and a convenient way to read it. This post is about how I got one and wrote a Python program to send the data from a Raspberry PI over the network &hellip; <a href=\"https:\/\/partofthething.com\/thoughts\/data-acquisition-and-live-plotting-over-the-network-with-a-raspberry-pi-and-python\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Data acquisition and live-plotting over the network with a Raspberry PI and Python<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":""},"categories":[3,69],"tags":[],"class_list":["post-820","post","type-post","status-publish","format-standard","hentry","category-computers","category-electronics-and-physics"],"_links":{"self":[{"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/posts\/820","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/comments?post=820"}],"version-history":[{"count":4,"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/posts\/820\/revisions"}],"predecessor-version":[{"id":911,"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/posts\/820\/revisions\/911"}],"wp:attachment":[{"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/media?parent=820"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/categories?post=820"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/partofthething.com\/thoughts\/wp-json\/wp\/v2\/tags?post=820"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}