<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[MJRoBot.org - Medium]]></title>
        <description><![CDATA[Explorations on Physical Computing, IoT, Robotics and Data Science - Medium]]></description>
        <link>https://medium.com/mjrobot-org?source=rss----9c5924ccca32---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>MJRoBot.org - Medium</title>
            <link>https://medium.com/mjrobot-org?source=rss----9c5924ccca32---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 06:28:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/mjrobot-org" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Physical Computing — Scratch 2.0 for Raspberry Pi]]></title>
            <link>https://medium.com/mjrobot-org/physical-computing-scratch-2-0-for-raspberry-pi-c15a456e04a6?source=rss----9c5924ccca32---4</link>
            <guid isPermaLink="false">https://medium.com/p/c15a456e04a6</guid>
            <category><![CDATA[education-technology]]></category>
            <category><![CDATA[raspberry-pi]]></category>
            <category><![CDATA[scratch-programming]]></category>
            <category><![CDATA[physical-computing]]></category>
            <dc:creator><![CDATA[Marcelo Rovai]]></dc:creator>
            <pubDate>Wed, 04 Jul 2018 14:36:11 GMT</pubDate>
            <atom:updated>2018-07-04T16:14:03.705Z</atom:updated>
            <cc:license>http://creativecommons.org/publicdomain/zero/1.0/</cc:license>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/620/0*jmYrAka5g9ehS8bc" /></figure><p>This is one of my four tutorials with a focus on Scratch and Physical Computing. Regarding this topic, I have already published on Hackster:</p><ul><li><a href="https://www.hackster.io/mjrobot/electronic-playground-with-arduino-and-scratch-2-aaa53e">Electronic Playground with Arduino and Scratch 2.o</a></li><li><a href="https://www.hackster.io/mjrobot/physical-computing-scratch-for-arduino-72661a">Physical Computing — Scratch for Arduino</a> (Scratch 1.0)</li><li><a href="https://www.hackster.io/mjrobot/physical-computing-scratch-for-raspberry-pi-270b69">Physical Computing — Scratch for Raspberry Pi</a> (Scratch 1.4)</li></ul><p>Here, we will explore exciting projects, using the latest version of Scratch available for Raspberry Pi, the 2.0.</p><p>In short,</p><p>We will learn:</p><ul><li>What is Scratch 2.0 and its differences with Scratch 1.4</li><li>How to interact with LEDs, Push-Buttons and a Buzzer</li><li>How to create Special Blocks</li><li>How to handle with variables</li><li>How to integrate animations and the Physical world.</li></ul><p>Let’s go!</p><h4>1. Bill of Material — BoM</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/0*iA7sJt1nF9ladnsA" /></figure><p>For the experiences in this tutorial, you will need at least the items:</p><ul><li>Raspberry Pi (V2 or V3)</li><li>LEDs (Red, Yellow, and Green)</li><li>1 Push-button</li><li>1 Active Buzzer</li><li>Resistors: 3 x 220 ohm</li><li>Cables</li><li>Full Breadboard</li></ul><h4>2. The Scratch 2.0</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/0*sTcD141pX3g2E5yr" /></figure><p>As we know, Scratch is a great tool to teach beginners how to code, but before starting our tutorial, I really recommend that you go familiarize yourself with the Scratch 2.0 language, following some basic tutorials. Doing that will help you to better understand how to use a Raspberry Pi for Physical Computing.</p><p>There are 2 ways to do that:</p><ul><li><a href="https://scratch.mit.edu/">Go to Scratch Official Website</a>, or</li><li><a href="https://www.raspberrypi.org/">Go to Official Raspberry Foundation site</a>.</li></ul><p>An offline Scratch 2 version is currently delivered bundled with the Raspbian OS release, with great features that will help you to create a professional codification.</p><p><strong>Opening the program:</strong></p><p>When you have the Pixel desktop in front of you, go to left up corner Application Menu (the “Raspberry Pi logo”) and open it. You will find as one of the options: “<strong><em>Programming</em></strong>”.</p><ul><li>Open it and click on “<strong><em>Scratch 2</em></strong>” (note that the old version Scratch 1.4 (named as “<strong><em>Scratch</em></strong>”) is also there.</li><li>Once you open the Scratch program, you will get the screen shown above.</li><li>Click on “<strong><em>More Blocks</em></strong>”</li><li>Click on “<strong><em>Add an extension</em></strong>”. The option “Pi GPIO” Library will be available. Select it and click [<strong><em>OK</em></strong>]</li><li>The PI GPIO library will be installed and the “green led” shows that it is running properly.</li><li>2 blocks will be now available to be used with your Physical Computing programs.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/248/0*W2RHolpfvSHrQVyd" /></figure><p>The first block:</p><pre>set gpio &lt;GPIO number&gt; to [output high]</pre><p>above has 3 possible parameters:</p><ul><li><em>output high</em></li><li><em>output low</em></li><li><em>input</em></li></ul><p>The second block:</p><pre><strong>gpio &lt;</strong><em>GPIO number</em><strong>&gt; is high?</strong></pre><p>will return:</p><ul><li><em>“True</em>” or “<em>False</em>” depending on GPIO digital level (“True = 1” and “False = 0”).</li></ul><p>Scratch 2.0 also allows the creation of custom blocks, allowing code to be encapsulated and used (possibly multiple times) in a project. We will see how to do it in the examples.</p><h4>3. The GPIO Pin Numbering</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/0*hUBjOvSCL9zD0dis" /></figure><p>We will not cover here how to prepare the Raspberry Pi to be used in this tutorial, but If you need any help, please refer to this link: <a href="https://www.raspberrypi.org/learning/noobs-install/">Installing Raspbian with NOOBS.</a></p><p>Our Raspberry Pi will be running with a Raspbian version of the Operational System, having PIXEL, as its desktop environment (GUI).</p><p>Once we will interact with the physical world it is good to remember that our gateway to it will be the <em>Raspberry Pi GPIO connector</em>. The above photo shows the connector, where I have drawn some fictional components in red and bellow its graphical representation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*KcxQTQ3KkH6f-f0J" /></figure><p><strong>Pin Numbering</strong></p><p>Important to mention that we will use Broadcom (BCM) pin numbering for the GPIO pins, as opposed to physical (BOARD) numbering. Any pin marked “GPIO” (Yellow) in the above diagram can be used as a pin number. For example, if a LED was attached to “GPIO13” (physical pin 33) you should specify the pin number as <strong>13 </strong>and not “33”.</p><h4>4. Blinking a LED</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/0*IY4R9H0EKkL-BFeD" /></figure><p>It’s time for the “Hello World” of Physical Computing, the “blinking of a LED”!</p><p>Let’s start connecting a LED to GPIO 13 of our Raspberry Pi (RPi, for short).</p><p>For starting, drag 2 new “set gpio” blocks to the SCRIPTS AREA,</p><pre>set gpiot &lt;13&gt; to [output high]</pre><pre>set gpio &lt;13&gt; to [output low]</pre><p>Clicking on those blocks you will turn ON and OFF the LED</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/1*rAqXBEzENy6LAiFOMBWqug.png" /></figure><p>Now, let’s create a real blink code using Scratch 2.</p><ul><li>Start your program when the “Green Flag” is clicked, for that you will need the block:</li><li><strong>When “green flag” clicked</strong>(it is the first block in the “Event menu” — colored in brown)</li><li>Drag the loop block “<strong>Forever</strong>” (you will find it on “Control menu” — colored in yellow)</li><li>Inside the loop Forever, put the blocks that you used on the before.</li><li>if you run your code, you will see that nothing happens, or better, the LED will be ON and OFF so quickly that you will not realize it. So, you must add some delay, let’s say 1 second after each block.</li></ul><p>And that’s it!</p><p>The above screenshot above will show you the final program and bellow a video with our LED blinking:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F7OOrejVsJx4%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D7OOrejVsJx4&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F7OOrejVsJx4%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/03f9620f77318bd4d52c523d7e99405a/href">https://medium.com/media/03f9620f77318bd4d52c523d7e99405a/href</a></iframe><h4>5. Using Custom Blocks — Multiples LEDs Blinking</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*Xj1AGgPQmay5wFg3" /></figure><p>Now, that we know how to control one LED, let’s install 2 new ones, with different colors:</p><ul><li>RED</li><li>YELLOW</li><li>GREEN</li></ul><p>Repeat what you did at last step, connecting the LEDs as shown above:</p><ul><li>RED ==&gt; GPIO 13</li><li>YELLOW ==&gt; GPIO 19</li><li>GREEN ==&gt; GPIO 26</li></ul><p>Suppose that we want now the 3 LEDs blinking independently at different frequencies. The first thing that you can do is to copy the set of block developed on the last step, changing the GPIO number and the delays for each one of the blocks, let’s say:</p><ul><li>Blink Block for GPIO 13 ==&gt; delay: 1 second</li><li>Blink Block for GPIO 19 ==&gt; delay: 2 seconds</li><li>Blink Block for GPIO 26 ==&gt; delay: 4 seconds</li></ul><p>It would work, but let’s use the opportunity here and introduce a new feature of Scratch 2, the <strong>“Custom Blocks”.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/1*TxV2wSX-MmNbQ8mFtrCEZA.png" /></figure><p>First, let’s create a custom block naming it “setup”, that will be used to turn OFF all LEDs</p><ul><li>Go to “More Blocks” menu</li><li>Select “Make a Block”</li><li>Enter the name “setup” and click OK</li></ul><p>You will see that a new block will appear “<strong>define [</strong><em>setup</em><strong>]</strong>”, below it, we can add 3 blocks setting the GPIOs to LOW as shown at above print screen. Now, this custom block “setup” (that it is, in fact, a “function”), can be called anytime in the program. Let’s call it at starting, under the green flag, so anytime time that the program run, we will guarantee that the LEDs will be OFF.</p><p>Now, we will create a more elaborate custom block. We will create a type that can receive parameters allowing further generalization and reuse of code blocks. The idea is to create a generic “Blink block”, where the parameters will be the GPIO number and the time. So, our final block will be like:</p><pre><strong>blink &lt;</strong><em>GPIO</em><strong>&gt; &lt;</strong><em>time</em><strong>&gt;</strong></pre><p>So, let&#39;s do the following:</p><ul><li>Go to “More Blocks” menu</li><li>Select “Make a Block”</li><li>Enter the name “Blink” and click at sub-menu “Options”</li><li>Click on the first option: “Add number input” and type “GPIO”</li><li>Click again on same option and type “time”</li><li>Enter OK</li></ul><p>Now, a new block will appear:</p><pre><strong>define [</strong><em>Blink</em><strong>] [</strong><em>GPIO</em><strong>] [</strong><em>time</em><strong>]</strong></pre><p>Under this block we will add the blocks that we used for the blink, but, instead of specific numbers, we will add the “blue” and generic parameters: “GPIO” and “time”</p><p>Now if we want to blink the LEDs independently it is enough to use blocks like those:</p><pre>Blink &lt;13&gt; &lt;1&gt;<br>Blink &lt;16&gt; &lt;2&gt;<br>Blink &lt;26&gt; &lt;4&gt;</pre><p>The code for this example is here: <a href="https://github.com/Mjrovai/Scratch-Raspberry/blob/master/Examples%20Scratch_2/Multiple%20blinks/blink_v2.sb2">blink_V2.sb2</a></p><h4>6. Introducing a Push-Button and Variables</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/0*TcbjOuSTjelBj7mS" /></figure><p>Using a push-button with the Raspberry Pi and Scratch 2 is a “straight forward task”. For that we will will implement a “direct logic”:</p><ul><li>If Push-Button is pressed (closed) ==&gt; GPIO = “1 “ (3.3V)</li><li>If Push-Button not pressed (open) ==&gt; GPIO = “0” (0V)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*-dFiO0J9JoTHvbNi" /></figure><p>So, let’s connect the button as shown in above diagram.</p><blockquote><em>WARNING: you must use the 3.3V output (physical pin number #1). The RPi does not support 5V on its GPIOs</em></blockquote><p>For reading our push-button, we will create a new variable “button” to be associated with its state:</p><ul><li>If Push-button is PRESSED ==&gt; “button = ON”</li><li>If Push-Button is NOT PRESSED ==&gt; “button= OFF”</li></ul><p>The blocks to that will be:</p><pre>if &lt;20&gt; is high?&gt; then<br>    set[button] to &lt;ON&gt;<br>else<br>    set[button] to &lt;OFF&gt;</pre><p>We will include those blocks on a &quot;new block&quot; that we will call: “gpioStatus” and by the way, we will do the same for LEDs, creating variables associated to each one of them:</p><ul><li>red</li><li>yellow</li><li>green</li></ul><p>And will associate values to those variables, and we will act on correspondent GPIO accordingly:</p><ul><li>If Red LED should be ON ==&gt; variable “red = ON” ==&gt; GPIO 13 ==&gt; “high”</li><li>If Yellow LED should be ON ==&gt; variable “yellow = ON” ==&gt; GPIO 19 ==&gt; “high”</li><li>If Green LED should be ON ==&gt; variable “green = ON” ==&gt; GPIO 26 ==&gt; “high”</li></ul><p>And of course, the opposite for LEDs off.</p><p>All above logic should also be included at “gpioStatus” function, that will be running during all the time. So, we will not need to be worried about GPIOs anymore. Instead, we will use the variables to build our program logic.</p><p>For example, let’s create a program that:</p><ul><li>Run the LEDs in a fixed sequence on a given frequency as a normal traffic light:</li><li>RED</li><li>YELLOW</li><li>GREEN</li></ul><p>And so on. If a button is pressed, the frequency is changed, let’s say around 3 times faster.</p><p>The first thing to do is to create a new variable, named “time” that will have a value, let’s say:</p><ul><li>1 second if the variable “button” is OFF and</li><li>0.3 seconds if “button” is ON.</li></ul><p>As you can see, we are in fact controlling time’s value actuating on a physical push-button, but here only “variables” are used.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*-bO4dXy6RhkhJH7P" /></figure><p>We can do the same for the LEDs sequence. We will set red, yellow and green variables, ON or OFF, keeping them ON using the variable “time” now. The above print screen shows you how the program looks like.</p><p>The below video can give you a real idea about what’s going on:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FpMhgKqkJGlo%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DpMhgKqkJGlo&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FpMhgKqkJGlo%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/8febac7089790ccc3b22d2c4c72b4bfb/href">https://medium.com/media/8febac7089790ccc3b22d2c4c72b4bfb/href</a></iframe><p>Here the code used in the example:<strong> </strong><a href="https://github.com/Mjrovai/Scratch-Raspberry/blob/master/Examples%20Scratch_2/Button%20and%20LEDs/3LED_Button_V2.sb2">3LED_button_V2.db2</a></p><h4>7. Let’s Animate!</h4><p>One of the great features of Scratch is the animation. So, let’s introduce some very simple animation on our projects.</p><p>Let’s change the last project a little. Now, The traffic lights will perform its sequence only when the push button is pressed. If not pressed, all lights must be off. The code is very simple. Try to do it before you see my solution.</p><p>OK, but let’s make the things more interesting. Let’s create a new sprite a “Blue Button” (I left with its default name, sprite 1. You can change it) that will have 2 possible costumes:</p><ul><li>button OFF</li><li>button ON</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*TjHO-DldYTzWXX1m" /></figure><p>I used the internal editor to create simple costumes. We will want that this sprite always appears at Stage, but with its costume “button OFF” if the push-button is released (variable button = OFF) and of course, with the costume “button ON” if the push-button is pressed. One simple block that should run all the time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*Ufqqe43Gfx5OpyWM" /></figure><p>Now let&#39; s create a Traffic Light that will show the LED status on the Stage. We can do it or creating a new Sprite, or creating new backdrops. We will do the second on, so you can see 2 different examples of animation creation here.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*CDgS1dpsgolh0dtY" /></figure><p>With the paint editor, I created 3 empty circles, that I called “OFF”. Coping this backdrop 3 times, I filled one of the circles with one of the LEDs color (Red, Yellow, and Green) in this sequence. I name them (SURPRISE!): RED, YELLOW, and GREEN.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*jFrYSn9hzV46oWKZ" /></figure><p>So, at Backdrop”s Script tab I included a piece of code that will run forever, changing the backdrop according to LED variables (see above). Very simple and straight forward.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*J95vC6u5pfRm9lUw" /></figure><p>And that’s it! As you can see, the final project is a group of various simple parts, what’s make it at end also very simple to understand.</p><blockquote><em>NOTE: The secret here is to be ORGANIZED with the code.</em></blockquote><p>Below a video with our project and animation running:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FUHZl-Id_6ZE%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DUHZl-Id_6ZE&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FUHZl-Id_6ZE%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/520aa95ceebcbfccccb563bbc6b9db2a/href">https://medium.com/media/520aa95ceebcbfccccb563bbc6b9db2a/href</a></iframe><p>The code can be downloaded from here: <a href="https://github.com/Mjrovai/Scratch-Raspberry/blob/master/Examples%20Scratch_2/Traffic%20Light/trafific%20Lights%20V2.sb2">Traffic Lights V2.sb2</a></p><h4>8. It’s Show Time!</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/586/0*UNNgggBMoykC0gtW" /></figure><p>On the last step, we had a lot of fun creating simple animation and mixing physical and virtual things together. Let’s now, do a more elaborate project.</p><p>Before we enter in the animation part of it, let’s complete the HW. This project will have a pedestrian Traffic Light and so, we will include a Buzzer that will produce a sound (“Beep”), during the time allowed for pedestrians crossing a street. The buzzer will be installed at GPIO 16. AS we did before, to simplify things, let’s create a new variable “beep” that will activate GPIO 16 High or Low. The logic will be included at gpioStatus, same we did for the LEDs.</p><p>Let’s take our little friend, the MJRoBot and show him London, the birthplace of Raspberry Pi!</p><p>For starting, we must define the general specification regarding our project:</p><ul><li>Our little friend will start its journey around London, moving right to left across all stage area.</li><li>When he touches the left Stage’s border, we must change for the next London’s backdrop.</li><li>When he reaches Abbey Road, he must stop because there is a pedestrian cross (Yes! The famous one!)</li><li>Suppose that was installed there, a traffic light to protect tourists. The robot will ask us to press the push-button for him, so he can go across the street, same as Paul, John, George, and Ringo did years ago) but in an opposite direction ;-)</li></ul><p>Let’s this time create two new sprites:</p><ul><li>A Traffic Light (in opposite for using several backdrops) with 3 costumes (red, yellow and green)</li><li>The little robot, the MJRoBot that has 2 costumes to help him to walk (or something that resembles walking). ;-)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bYR7xd2tNjizCS-LyTtBMA.png" /></figure><p>We will have 4 backdrops. 3 for London surrounds and the most important one: Abbey Road.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sUu1bmSF0zwKgWWmYZqSBA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kMjGyVqoykHnnxu4umV2Fg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nvJxtEpta6YJiNVKzzkAjg.png" /></figure><p>That’s it!</p><p>Try to code it by yourself and only after, compare your solution with mine! There are several different ways to do that.</p><p>Enjoy it!</p><p>A quick video about the final project working:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FXSeheTfW_ok%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DXSeheTfW_ok&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FXSeheTfW_ok%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/c983e5a8b5f0363074efe457d53d75ea/href">https://medium.com/media/c983e5a8b5f0363074efe457d53d75ea/href</a></iframe><p>My code solution here: <a href="https://github.com/Mjrovai/Scratch-Raspberry/blob/master/Examples%20Scratch_2/Walking%20By%20London/MJRoBot%20around%20London_V1.sb2">MJRoBot around London V1.sb2</a></p><h4>9. Conclusion</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/443/0*i-JBf3pRzuduw6o3" /></figure><p>As always, I hope this project can help others find their way in the exciting world of electronics, robotics and IoT!</p><p>Please visit my <a href="https://github.com/Mjrovai/Scratch-Raspberry/tree/master/Examples%20Scratch_2">GitHub for updated files: Scratch 2.0 — Raspberry Pi</a>.</p><p>For more projects, please visit my blog: <a href="https://mjrobot.org/">MJRoBot.org</a></p><p>Saludos from the south of the world!</p><p>See you at my next project!</p><p>Thank you,</p><p>Marcelo</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c15a456e04a6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mjrobot-org/physical-computing-scratch-2-0-for-raspberry-pi-c15a456e04a6">Physical Computing — Scratch 2.0 for Raspberry Pi</a> was originally published in <a href="https://medium.com/mjrobot-org">MJRoBot.org</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[IoT Made Easy: ESP-MicroPython-MQTT-ThingSpeak]]></title>
            <link>https://medium.com/mjrobot-org/iot-made-easy-esp-micropython-mqtt-thingspeak-a349ad78fc70?source=rss----9c5924ccca32---4</link>
            <guid isPermaLink="false">https://medium.com/p/a349ad78fc70</guid>
            <category><![CDATA[esp8266]]></category>
            <category><![CDATA[esp32]]></category>
            <category><![CDATA[iot]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[micropython]]></category>
            <dc:creator><![CDATA[Marcelo Rovai]]></dc:creator>
            <pubDate>Fri, 15 Jun 2018 20:51:27 GMT</pubDate>
            <atom:updated>2018-06-15T20:51:27.164Z</atom:updated>
            <cc:license>http://creativecommons.org/publicdomain/zero/1.0/</cc:license>
            <content:encoded><![CDATA[<h4>Using MQTT protocol, we will get captured data from sensors, logging them to an IoT service, ThingSpeak.com and to a mobile App, Thingsview.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MnjfiP1pOkQRtvl7GTaZKA.png" /></figure><h3>1. Introduction</h3><p>In my previous article, <a href="https://medium.com/@rovai/micropython-on-esp-using-jupyter-6f366ff5ed9">MicroPython on ESP using Jupyter</a>, we learned how to install and run MicroPython on an ESP device. Using Jupyter Notebook as our development environment, we also learned how to read from sensors (Temperature, Humidity, and Luminosity), using several communication protocols and methods as: Analog, Digital, 1-Wire and I2C, this last one to display our captured data on an OLED display.</p><p>Now, on this article and using an MQTT protocol, we will get all captured data, send them to an IoT service, <em>ThingSpeak.com</em> and to a mobile App, <em>Thingsview</em>, where is possble to log and play with them.</p><p>Here, a general overview regrading our project:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*M10grG85terQk6gS" /></figure><h3>2. The Hw</h3><p>The Hw that will be used, is basically the same used on the previous project: <a href="https://medium.com/@rovai/micropython-on-esp-using-jupyter-6f366ff5ed9">Micropython on ESP Using Jupyter</a>. Please, refer to it for a detailled HW explanation (the exception is the servo, that will not be used in this project).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*bCWqVIQABWFv92Qh" /></figure><p>Above you can see the full HW. Connect the devices as shown.</p><h3>3. Micropython, REPL and Jupyter Notebook</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/808/1*EkXSGEX69pZ_iQipI5d6-Q.png" /></figure><p>At this point , you should have a Micropython interpreter loaded on your ESP device and so, it is possible to program it using any of the available IDEs, like:</p><ul><li>REPL /Ampy</li><li>Jupyter Notebook (*)</li><li>Mu</li><li>ESPCut (Windows only)</li><li>… etc</li></ul><p>On my article <a href="https://medium.com/@rovai/micropython-on-esp-using-jupyter-6f366ff5ed9">Micropython on ESP Using Jupyter</a>, I have detailed how to download and install the MicroPython interpreter, ESPTool to manage ESP devices and how to use Jupyter Notebook as a <em>Development Environment.</em> Feel free to use what is more comfortable for you.</p><blockquote>(*) I usually make all development on Jupyter Notebook, and once I get the final code, I copy them to Geany, saving it as an python script. The script is upload on ESP using Ampy. I found this method very easy to create more complex and professional projects.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/452/1*HaKx_KlIMOBczSJrRyvvOA.png" /></figure><h3>4. Sensors</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/516/0*_Ns35MP4aqKO4pMk" /></figure><p>Let’s install the libraries, define GPIOs, create objects and functions for all sensors individually:</p><p><strong>A. DHT (Temperature and Humidity)</strong></p><p>Install the DHT library and create an object:</p><pre>from dht import DHT22<br>from machine import Pin<br>dht22 = DHT22(Pin(12))</pre><p>Create a function to read DHT sensor:</p><pre>def readDht():<br>    dht22.measure()<br>    return dht22.temperature(), dht22.humidity()</pre><p>Test DHT function:</p><pre>print (readDht())</pre><p>The result should be a tuple as the one below:</p><pre>(17.7, 43.4)</pre><p><strong>B. DS18B20 (External Temperature)</strong></p><p>Install the libraries and create an object:</p><pre>import onewire, ds18x20<br>import time</pre><pre># Define the pin to be used with 1-wire bus ==&gt; pin 2 (D4)<br>dat = Pin(2)</pre><pre># create the onewire object<br>ds = ds18x20.DS18X20(onewire.OneWire(dat))</pre><p>Scan for devices on the bus</p><pre>sensors = ds.scan()<br>print(&#39;found devices:&#39;, sensors)</pre><p>The printed result is not really important, what we will need is the first detected sensor: <em>sensors[0]</em>. And now, we can build a function to read sensor data:</p><pre>def readDs():<br>    ds.convert_temp()<br>    time.sleep_ms(750)<br>    return ds.read_temp(sensors[0])</pre><p>It is always important to test the sensor using the created function</p><pre>print(readDs())</pre><p>If you get a temperature value, your code is correct:</p><pre>17.5</pre><p><strong>C. LDR (Luminosity)</strong></p><p>The LDR will use the analog pin of our ESP (it is only one in the case of ESP8266 and several to ESP32).</p><p>Same as done before:</p><pre># import library<br>from machine import ADC</pre><pre># Define object<br>adc = ADC(0)</pre><p>A simple function: <em>adc.read() </em>can be used to read the ADC value. But remember that the internal ADC will convert voltages between 0 and 3.3V in correspondent digital values, varying from 0 to 1023. Once we are interested in “Luminosity”, we will consider Max light as the maximum captured value from the sensor (in my case 900) and minimum light that in my case is 40. Having those values we can “map” the value from [40 to 900] to [0 to 100%] of luminosity. For that, we will create a new function:</p><pre>def readLdr():<br>    lumPerct = (adc.read()-40)*(10/86) <br>    return round(lumPerct)</pre><p>You should test the function using <em>print (readLDR())</em>. The result should be an integer between 0 and 100.</p><p><strong>D. Push-Button (Digital Input)</strong></p><p>Here we are using a Push-Button as a “digital sensor”, but it could be an “echo” of an actuator (A pump that was turned ON/OFF, for example).</p><pre># define pin 13 as an input and activate an internal Pull-up resistor:<br>button = Pin(13, Pin.IN, Pin.PULL_UP)</pre><pre># Function to read button state:<br>def readBut():<br>    return button.value()</pre><p>You can test the button using the function <em>print(readBut())</em>. Whithout pressing it, the result should be “1”. Pressing the button, the result should be “0”.</p><h3>5. Capturing and Displaying Locally All Sensor Data</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*rCF-00yjEeCEa88j" /></figure><p>Now that we have created one function for each sensor, let’s create another one, responsable for reading all of them at the same time:</p><pre>def colectData():<br>    temp, hum, = readDht()<br>    extTemp = readDs()<br>    lum = readLdr()<br>    butSts = readBut()<br>    return temp, hum, extTemp, lum, butSts</pre><p>Now, using:</p><pre>print(colectData())</pre><p>Will result in a tuple that includes all captured data from sensors:</p><pre>(17.4, 45.2, 17.3125, 103, 1)</pre><p>We can also optionally, show those data on a local display:</p><pre># import library and create object i2c<br>from machine import I2C<br>i2c = I2C(scl=Pin(5), sda=Pin(4))</pre><pre># import library and create object oled<br>import ssd1306<br>i2c = I2C(scl=Pin(5), sda=Pin(4))<br>oled = ssd1306.SSD1306_I2C(128, 64, i2c, 0x3c)</pre><pre># create a function:<br>def displayData(temp, hum, extTemp, lum, butSts):<br>    oled.fill(0)<br>    oled.text(&quot;Temp:    &quot; + str(temp) + &quot;oC&quot;, 0, 4)<br>    oled.text(&quot;Hum:     &quot; + str(hum) + &quot;%&quot;,0, 16)<br>    oled.text(&quot;ExtTemp: &quot; + str(extTemp) + &quot;oC&quot;, 0, 29)<br>    oled.text(&quot;Lumin:   &quot; + str(lum) + &quot;%&quot;, 0, 43)<br>    oled.text(&quot;Button:  &quot; + str(butSts), 0, 57<br>    oled.show()</pre><pre># display data using the function<br>displayData(temp, hum, extTemp, lum, butSts)</pre><p>As an option, we can use the LED to be ON when we start reading sensors, going OFF after data are displayed. This will help to confirm that the program is working when we have the ESP disconnected from PC and running autonomously.</p><p>So, the “main function would be:</p><pre>def main():<br>    led.on()<br>    temp, hum, extTemp, lum, butSts = colectData()<br>    displayData(temp, hum, extTemp, lum, butSts)<br>    led.off()</pre><p>So, executing main(), we will get the sensor data displayed on OLED as shown in the last picture.</p><h3>6. Running the Local Station Code on ESP Start-up</h3><p>If you have using Jupyter Notebook to create and test the functions so far, it is to time to have all that were developed so far on a single file script to be executed by our ESP autonomously.</p><p>Let’s open any text editor and past on it all code (I like to use Geany):</p><pre>from machine import Pin<br>import time</pre><pre># LED<br>led = Pin(0, Pin.OUT)</pre><pre># DHT<br>from dht import DHT22<br>dht22 = DHT22(Pin(12))<br>def readDht():<br>    dht22.measure()<br>    return dht22.temperature(), dht22.humidity()</pre><pre># DS18B20<br>import onewire, ds18x20<br>dat = Pin(2)<br>ds = ds18x20.DS18X20(onewire.OneWire(dat))<br>sensors = ds.scan()<br>def readDs():<br>    ds.convert_temp()<br>    time.sleep_ms(750)<br>    return round(ds.read_temp(sensors[0]), 1)</pre><pre># LDR<br>from machine import ADC<br>adc = ADC(0)<br>def readLdr():<br>    lumPerct = (adc.read()-40)*(10/86)<br>    return round(lumPerct)</pre><pre># Push Button<br>button = Pin(13, Pin.IN, Pin.PULL_UP)<br>def readBut():<br>    return button.value()</pre><pre># Read all data:<br>def colectData():<br>    temp, hum, = readDht()<br>    extTemp = readDs()<br>    lum = readLdr()<br>    butSts = readBut()<br>    return temp, hum, extTemp, lum, butSts</pre><pre># I2C / OLED<br>from machine import I2C<br>import ssd1306<br>i2c = I2C(scl=Pin(5), sda=Pin(4))<br>oled = ssd1306.SSD1306_I2C(128, 64, i2c, 0x3c)<br>def displayData(temp, hum, extTemp, lum, butSts):<br>    oled.fill(0)<br>    oled.text(&quot;Temp:    &quot; + str(temp) + &quot;oC&quot;, 0, 4)<br>    oled.text(&quot;Hum:     &quot; + str(hum) + &quot;%&quot;,0, 16)<br>    oled.text(&quot;ExtTemp: &quot; + str(extTemp) + &quot;oC&quot;, 0, 29)<br>    oled.text(&quot;Lumin:   &quot; + str(lum) + &quot;%&quot;, 0, 43)<br>    oled.text(&quot;Button:  &quot; + str(butSts), 0, 57)<br>    oled.show()</pre><pre># Main function<br>def main():<br>    led.on()<br>    temp, hum, extTemp, lum, butSts = colectData()<br>    displayData(temp, hum, extTemp, lum, butSts)<br>    led.off()</pre><pre>&#39;&#39;&#39;------ run main function --------&#39;&#39;&#39;<br>main()</pre><p>Save it, for example as <em>localData.py.</em></p><p>To run this code directly on your terminal you will need Ampy.</p><p>First, on Terminal let’s inform Ampy our Serial port:</p><pre>export AMPY_PORT=/dev/tty.SLAB_USBtoUART</pre><p>Now, we can see the files that are inside our ESP root directory:</p><pre>ampy ls</pre><p>As a response, we will get <em>boot.py</em>, that is the first file that will run in the system.</p><p>Now, let’s use Ampy to load our python Script LocalData.py as /main.py, so our script will run just after boot:</p><pre>ampy put localData.py /main.py</pre><p>If we use the command <em>amp ls</em> now, you will see 2 files inside the ESP: <em>boot.py</em> and <em>main.py</em></p><p>Resetting your ESP, will make the program localData.py (uploaded as /main.py) run automatically, displaying the sensor data on display.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*qK7fh1aMIbvCCCri" /></figure><p>The above Terminal print screen shows what we have done.</p><p>With above code, the display will be shown only once, but we can define a loop on <em>main()</em> function, that will show data on every defined time interval (PUB_TIME_SEC), and for example, until we press the button:</p><pre># loop getting data until button is pressed<br>while button.value():<br>    led.on()<br>    temp, hum, extTemp, lum, butSts = colectData()<br>    displayData(temp, hum, extTemp, lum, butSts)<br>    led.off()<br>    time.sleep(PUB_TIME_SEC)</pre><p>The variable PUB_TIME_SEC must be declared by the time that you want your samples.</p><p>To enhance more our code, would be good to inform that we will go out from the loop, for that we will define 2 new general functions, one for clear the display and another to blink the LED on a certain number of times.</p><pre># Clear display :<br>def displayClear():<br>    oled.fill(0)<br>    oled.show()</pre><pre># create a blink function<br>def blinkLed(num):<br>    for i in range(0, num):<br>    led.on()<br>    sleep(0.5)<br>    led.off()<br>    sleep(0.5)</pre><p>So, we can now, rewrite our main() function:</p><pre>while button.value():<br>    led.on()<br>    temp, hum, extTemp, lum, butSts = colectData()<br>    displayData(temp, hum, extTemp, lum, butSts)<br>    led.off()<br>    time.sleep(PUB_TIME_SEC)<br>    blinkLed(3)<br>    displayClear()</pre><p>The final code can be downloaded from my GitHub:<a href="https://github.com/Mjrovai/Python4DS/blob/master/Micropython/IoT_TS_MQTT/localData.py"> localData.py</a> and also the Jupyter Notebook used for development of full code: <a href="https://github.com/Mjrovai/Python4DS/blob/master/Micropython/IoT_TS_MQTT/LocalData%20Development.ipynb">Jupyter Local Data Development.ipynb</a>.</p><h3>7: Connecting the ESP to Local WiFi</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*GsPmMnh5w20pLlPG" /></figure><p>The network module is used to configure the WiFi connection. There are two WiFi interfaces, one for the station (when the ESP8266 connects to a router) and one for the access point (for other devices to connect to the ESP8266). Here, our ESP will be connected to local network via a router. Let’s call the library and define our network credentials:</p><pre>import network<br>WiFi_SSID = &quot;YOUR SSID&quot;<br>WiFi_PASS = &quot;YOUR PASSWORD&quot;</pre><p>The function below can be used to connect the ESP to your local network:</p><pre>def do_connect():<br>    wlan = network.WLAN(network.STA_IF)<br>    wlan.active(True)<br>    if not wlan.isconnected():<br>        print(&#39;connecting to network...&#39;)<br>        wlan.connect(WiFi_SSID, WiFi_SSID)<br>        while not wlan.isconnected():<br>            pass<br>    print(&#39;network config:&#39;, wlan.ifconfig())</pre><p>Running the function, you can get as a result the IP address:</p><pre>do_connect()</pre><p>The result will be:</p><p>network config: (‘10.0.1.2’, ‘255.255.255.0’, ‘10.0.1.1’, ‘10.0.1.1’)</p><p>Were, in my case, 10.0.1.2, is the ESP IP address.</p><h3>8. The ThingSpeak</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7fo1QyG0jYc-zBY1.jpg" /></figure><p>At this point, we have learned how to capture data from all sensors, displaying them on our OLED. Now, it is time to see how to send those data to an IoT platform, the <em>ThingSpeak.com</em>.</p><blockquote><strong>“ThingSpeak</strong> is an <a href="https://en.wikipedia.org/wiki/Open_source">open source</a> <a href="https://en.wikipedia.org/wiki/Internet_of_Things">Internet of Things</a> (IoT) application to store and retrieve data from things, using REST and MQTT APIs. ThingSpeak enables the creation of sensor logging applications, location tracking applications, and a social network of things with status updates”</blockquote><p>Let’s begin!</p><p>First, you must have an account at ThinkSpeak.com. Next, follow the instructions to create a Channel and take note of your <strong>Channel ID</strong> and <strong>Write API Key.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*NuaGl0GUbTWgdC6Vpa4qMA.png" /></figure><p>Above you can see the 5 fields that will be used on our Channel.</p><h3>9. MQTT Protocol and ThingSpeak Connection</h3><p>MQTT is a publish/subscribe architecture that is developed primarily to connect bandwidth and power-constrained devices over wireless networks. It is a simple and lightweight protocol that runs over TCP/IP sockets or WebSockets. MQTT over WebSockets can be secured with SSL. The publish/subscribe architecture enables messages to be pushed to the client devices without the device needing to continuously poll the server.</p><p>The MQTT broker is the central point of communication, and it is in charge of dispatching all messages between the senders and the rightful receivers. A client is any device that connects to the broker and can publish or subscribe to topics to access the information. A topic contains the routing information for the broker. Each client that wants to send messages publishes them to a certain topic, and each client that wants to receive messages subscribes to a certain topic. The broker delivers all messages with the matching topic to the appropriate clients.</p><p>ThingSpeak™ has an MQTT broker at the URL <em>mqtt.thingspeak.com</em> and port 1883. The ThingSpeak broker supports both MQTT publish and MQTT subscribe.</p><p>In our case, we will use: <em>MQTT Publish.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/530/0*Fi8M5h2DXa6HgdS4" /></figure><p>The figure above describes the topic structure. The Write API Key is required to publish. The broker acknowledges a correct CONNECT request with CONNACK.</p><p>The MQTT protocol is supported in a built-in library in the Micropython. This protocol can be used to send data from your ESP8266, over WIFI, to a free cloud database, as ThingSpeak.</p><p>We will use the <em>umqtt.simple</em> library:</p><pre>from umqtt.simple import MQTTClient</pre><p>And knowing our SERVER ID, it is possible to create our MQTT client object:</p><pre>SERVER = &quot;mqtt.thingspeak.com&quot;<br>client = MQTTClient(&quot;umqtt_client&quot;, SERVER)</pre><p>Now, having your ThingSpeak credentials on hand:</p><pre>CHANNEL_ID = &quot;YOUR CHANNEL ID&quot;<br>WRITE_API_KEY = &quot;YOUR KEY HERE&quot;</pre><p>Let’s create our MQTT “Topic”:</p><pre>topic = &quot;channels/&quot; + CHANNEL_ID + &quot;/publish/&quot; + WRITE_API_KEY</pre><p>Get the data to be sent to ThingSpeak IoT Service, using the created function and associate its response to specific data variables:</p><pre>temp, hum, extTemp, lum, butSts = colectData()</pre><p>With those variables updated, we can create our “MQTT Payload”:</p><pre>payload &quot;field1=&quot;+str(temp)+&quot;&amp;field2=&quot;+str(hum)+&quot;&amp;field3=&quot;+str(extTemp)+&quot;&amp;field4=&quot;+str(lum)+&quot;&amp;field5=&quot;+str(butSts)</pre><p>And that’s it! We are ready to send data to ThinsSpeak, simply using the 3 lines of code below:</p><pre>client.connect()<br>client.publish(topic, payload)<br>client.disconnect()</pre><p>Now, if you go to your channel page (as mine below) you will see that each one of the 5 fields will have data related to your sensors.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/537/0*OAQjoHkzQF8icK-S" /></figure><h3>10. Sensor Data Logger</h3><p>Now, that we know that with only a few lines of code it is possible to upload data to an IoT service, let’s create a loop function to do it automatically at a regular interval of time (similar to what we have done with “Local data”).</p><p>Using the same variable (PUB_TIME_SEC) declared before, a simple main() function can be created to continuously capture data, logging them on our channel:</p><pre>while True:<br>    temp, hum, extTemp, lum, butSts = colectData()<br>    payload = &quot;field1=&quot;+str(temp)+&quot;&amp;field2=&quot;+str(hum)+&quot;&amp;field3=&quot;+str(extTemp)+&quot;&amp;field4=&quot;+str(lum)+&quot;&amp;field5=&quot;+str(butSts)<br>    client.connect()<br>    client.publish(topic, payload)<br>    client.disconnect()<br>    time.sleep(PUB_TIME_SEC)</pre><p>Note that only the “payload” must be updated, once “topic” is related to our channel credential and will not change.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/469/0*r-kIDbGuiKeUlO75" /></figure><p>Looking for your ThingSpeak channel page, you will observe that the data will be loading continuously to each field.</p><blockquote>Cover the LDR, put your hand on temp/hum sensors, press the button, etc. and see how the channel will be automatically “logging” those data for future analysis.</blockquote><p>Usually, for Data Logging, we should try to use as less power as possible, so, we would not use the LED or a local display. Also, it is common with ESP devices, to put them on “deep sleep”, where the microprocessor will stay on its state of minimum energy until it is time to capture data and send them to the IoT platform.</p><p>But, once here the idea is to learn, let’s also include the display and LED as we did before. Doing that, our “logger” function will be:</p><pre>while button.value():<br>    led.on()<br>    temp, hum, extTemp, lum, butSts = colectData()<br>    displayData(temp, hum, extTemp, lum, butSts)<br>    led.off()<br>    payload = &quot;field1=&quot;+str(temp)+&quot;&amp;field2=&quot;+str(hum)+&quot;&amp;field3=&quot;+str(extTemp)+&quot;&amp;field4=&quot;+str(lum)+&quot;&amp;field5=&quot;+str(butSts)<br>    client.connect()<br>    client.publish(topic, payload)<br>    client.disconnect()<br>    time.sleep(PUB_TIME_SEC)</pre><pre>blinkLed(3)<br>displayClear()</pre><p>The complete microPython script can be found here: <a href="https://github.com/Mjrovai/Python4DS/blob/master/Micropython/IoT_TS_MQTT/dataLoggerTS_EXT.py">dataLoggerTS_EXT.py</a> and the Jupyter notebook that was used for development can be also found here: <a href="https://github.com/Mjrovai/Python4DS/blob/master/Micropython/IoT_TS_MQTT/IoT%20-%20ThingSpeak%20-%20Data%20Logger%20EXT.ipynb">IoT ThingSpeak Data Logger EXT.ipynb</a>.</p><p>To upload the script on ESP, on your terminal use the command:</p><pre>ampy put dataLoggerTS.py /main.py</pre><p>And press the ESP reset button.</p><p>You will have the ESP capturing data and logging them on ThingSpeak.com until the bottom is kept pressed (wait for the LED to blink 3 times and the OLED turns off).</p><h3>11. The ThingView App</h3><p>The data logged can be view directly on ThingSpeak.com site or via an APP, for example, ThingsView!</p><p>ThingView is an APP developed by <a href="http://www.cinetica-tech.com/">CINETICA</a>, that enables you to visualize your ThingSpeak channels in an easy way, just enter the channel ID and you are ready to go.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/287/0*7aYHs_PGNoFpvUS2" /></figure><p>For public channels, the application will respect your windows settings: color, timescale, chart type and the number of results. The current version supports line and column charts, the spline charts are displayed as line charts.</p><p>For private channels, the data will be displayed using the default settings, as there is no way to read the private windows settings with the API key only.</p><p>The ThingView APP can be download for <a href="https://play.google.com/store/apps/details?id=com.cinetica_tech.thingview&amp;hl=en">ANDROID</a> and <a href="https://itunes.apple.com/us/app/thingview/id1284878579?mt=8">IPHONE</a>.</p><h3>12: Conclusion</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/442/0*CH2kM7JG-RIgS2YE" /></figure><p>As always, I hope this project can help others find their way into the exciting world of electronics!</p><p>For details and final code, please visit my GitHub depository: <a href="https://github.com/Mjrovai/Python4DS/tree/master/Micropython/IoT_TS_MQTT">IoT_TS_MQTT</a></p><p>For more projects, please visit my blog: <a href="https://mjrobot.org/">MJRoBot.org</a></p><p>Saludos from the south of the world!</p><p>See you in my next article!</p><p>Thank you,</p><p>Marcelo</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a349ad78fc70" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mjrobot-org/iot-made-easy-esp-micropython-mqtt-thingspeak-a349ad78fc70">IoT Made Easy: ESP-MicroPython-MQTT-ThingSpeak</a> was originally published in <a href="https://medium.com/mjrobot-org">MJRoBot.org</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[MicroPython on ESP Using Jupyter Notebook]]></title>
            <link>https://medium.com/mjrobot-org/micropython-on-esp-using-jupyter-notebook-40797b247f9b?source=rss----9c5924ccca32---4</link>
            <guid isPermaLink="false">https://medium.com/p/40797b247f9b</guid>
            <category><![CDATA[micropython]]></category>
            <category><![CDATA[physical-computing]]></category>
            <category><![CDATA[esp8266]]></category>
            <category><![CDATA[iot]]></category>
            <category><![CDATA[computer-science]]></category>
            <dc:creator><![CDATA[Marcelo Rovai]]></dc:creator>
            <pubDate>Fri, 15 Jun 2018 20:48:22 GMT</pubDate>
            <atom:updated>2018-06-15T20:48:22.231Z</atom:updated>
            <cc:license>http://creativecommons.org/publicdomain/zero/1.0/</cc:license>
            <content:encoded><![CDATA[<h4>Let’s play with MicroPython on an ESP using a Jupyter Notebook in order to get data from sensors and taking action in a physical world.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/0*5kCdBJOYis2texeO" /></figure><h3>1. Introduction</h3><p>On a previous article, we explored how to control a Raspberry Pi using Jupyter Notebook:<a href="https://medium.com/@rovai/physical-computing-using-jupyter-notebook-fb9e83e16760"> Physical Computing Using Jupyter Notebook</a></p><p>It was a great experience, and once the project worked very well I thought, “how about to also test Jupyter Notebook on an ESP8266 (or even on ESP32) using MicroPython?”.</p><p>As we all know, the Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Uses include data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and much more. For “much more”, we have also explored “Physical Computing”.</p><p>So far on my projects, I have mostly explored IoT and physical computing projects using ESP8266–01, 8266–12E (NodeMCU) and ESP32 programmed by an Arduino IDE, using its C/C++ type language. But another great tool to be used on programming those devices is MicroPython:</p><blockquote>MicroPython is a lean and efficient implementation of the Python 3 programming language that includes a small subset of the Python standard library and is optimised to run on microcontrollers and in constrained environments. It aims to be as compatible with normal Python as possible to allow you to transfer code with ease from the desktop to a microcontroller or embedded system.</blockquote><p>Also, I think that using Jupyter Notebook to program an ESP device using MicroPython, can be a great tool to teach Physical Computing to kids and also help scientists to quickly access real-world playing with sensors on acquiring data.</p><p>This is what we will try to accomplish in this tutorial:</p><ul><li>Outputting a digital signal to Turn On/Off a LED</li><li>Reading a digital input from a button</li><li>Output a PWM signal to fade a LED</li><li>Controlling a Servo Motor position using a PWM output</li><li>Reading Analog signal (Luminosity using LDR )</li><li>Reading Temperature vai 1-Wire (DS18B20)</li><li>Reading Temperature and Humidity (DHT22)</li><li>Displaying data using an OLED via I2C bus.</li></ul><h3>2. Installing MicroPython</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*-Ss5UFp7hNI7tAmK" /></figure><p>The first thing to be done with a fresh NodeMCU (or ESP32), is to erase what is loaded in its memory, “flashing” a new firmware, that will be the MicroPython interpreter.</p><p>A. Getting the new FW:</p><p>Go to the site: <a href="http://micropython.org/download#esp8266">MicroPython downloads</a> and download the appropriated FW for your device:</p><p>For example, for ESP8266, the latest version is:</p><pre>esp8266-20180511-v1.9.4.bin (Latest 01Jun18)</pre><p>(you can find details how to install the FW <a href="http://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/intro.html#deploying-the-firmware">here</a>)</p><p>The ideal is to create a directory where you will work with MicroPython. For example, in case of a mac, starting from your root directory:</p><pre>cd Documents<br>mkdir MicroPython<br>cd MicroPython</pre><p>B. Move the downloaded ESP8266 firmware to this recently created directory.</p><blockquote>At this point: Connect the NodeMCU or ESP32 to your PC using the serial USB cable.</blockquote><p>C. Check where is the serial port that is being used by your device using the command:</p><pre>ls /dev/tty.*</pre><p>In my case, I got:</p><pre>/dev/tty.SLAB_USBtoUART</pre><p>D. Install <em>esptool </em>(tool used to flash/erase FW on devices)</p><pre>pip install esptool</pre><p>E. Erase the NodeMCU flash:</p><pre>esptool.py --port /dev/tty.SLAB_USBtoUART erase_flash</pre><p>F. Flash the new FW:</p><pre>esptool.py --port /dev/tty.SLAB_USBtoUART --baud 460800 write_flash --flash_size=detect 0 esp8266-20180511-v1.9.4.bin</pre><p>Once you have the Firmware installed, you can play with REPL* on Terminal using the command “Screen”:</p><pre>screen  /dev/tty.SLAB_USBtoUART 115200<br>&gt;&gt;&gt; print (‘hello ESP8266’)<br>&gt;&gt;&gt; hello ESP8266</pre><p>If you are at REPL, use:</p><p>[Ctrl+C] to break a pgm and</p><p>[Ctrl+A] [K] [Y] to quit and return to the terminal.</p><p>* REPL stands for “<em>Read Evaluate Print Loop”</em>, and is the name given to the interactive MicroPython prompt that you can access on the ESP8266. You can learn more about REPL <a href="http://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/repl.html">here</a>.</p><h3>3. Installing Jupyter MicroPython Kernel</h3><p>To interact with a MicroPython ESP8266 or ESP32 over its serial REPL, we will need to install a specific Jupyter Kernel.</p><blockquote>This is necessary to be done only once.</blockquote><p>From <a href="http://jupyter.org/documentation">Jupyter Documentation website</a>, we can list all “ Community-maintained kernels”. There, we will be sent to :</p><p><a href="https://github.com/goatchurchprime/jupyter_micropython_kernel/">Jupyter MicroPython Kernel</a></p><p>Once we have Python 3 installed on our machine (in my case it is a Mac), clone the repository to a directory using the shell command (ie on a command line):</p><pre>git clone <a href="https://github.com/goatchurchprime/jupyter_micropython_kernel.git">https://github.com/goatchurchprime/jupyter_micropython_kernel.git</a></pre><p>Next, install the library (in editable mode) into Python3 using the shell command:</p><pre>pip install -e jupyter_micropython_kernel</pre><p>This creates a small file pointing to this directory in the python/../site-packages directory and makes it possible to “git update” the library later as it gets improved.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fdxyl9JOfCFBhKcjVGo60A.png" /></figure><blockquote>Things can go wrong here, and you might need “pip3” or “sudo pip” if you have numerous different versions of python installed.</blockquote><p>Install the kernel into Jupyter itself using the shell command:</p><pre>python -m jupyter_micropython_kernel.install</pre><p>This creates the small file “.local/share/jupyter/kernels/micropython/kernel.json” that jupyter uses to reference it’s kernels.</p><p>To find out where your kernelspecs are stored, you can type:</p><pre>jupyter kernelspec list</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*ojccuaMkq2kcxXUL" /></figure><p>The Terminal PrintScreen show you the list of kernels that I have installed on my machine. Note that in my case I installed the MicroPython kernel using PIP3 command and so, the Kernel is not in the same directory that the other ones (I got an error when trying to install my kernel using PIP).</p><p>Now run Jupyter notebooks:</p><pre>jupyter notebook</pre><p>In the notebook click the “New” Notebook button in the upper right, you should see the kernel display name listed: “MicroPython — USB”.</p><p>On the first cell, you will need to define the port and baud rate that will be used (115200 works fine):</p><pre>%serialconnect to --port=/dev/tty.SLAB_USBtoUART --baud=115200</pre><p>As a response, the cell will return:</p><pre>Connecting to --port=/dev/tty.SLAB_USBtoUART --baud=115200<br>Ready.</pre><p>And that’s it! When “Ready” appears, you should be able to execute MicroPython commands by running the cells.</p><p>Let’s try:</p><pre>print (&#39;hello esp8266&#39;)</pre><p>You should receive the response of your ESP8266 as on output on the cell:</p><pre>hello esp8266</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*gbcYDEWtICM9-CLB" /></figure><h3>4. Blinking a LED</h3><p>As usual, let’s start our journey to Physical computing, “blinking a LED”.</p><p>Below the typical pinout of NodeMCU (ESP8266–12E V1.0):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/616/0*A4mTcGH6sXBbpv6c" /></figure><blockquote>The available pins are: 0, 1, 2, 3, 4, 5, 12, 13, 14, 15, 16, which correspond to the actual GPIO pin numbers of ESP8266 chip. Note that many end-user boards use their own adhoc pin numbering (marked e.g. D0, D1, …).</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/501/0*n0Jc0yrz2JSTlCrS" /></figure><p>Install a LED on NodeMCU pin 0 (D3) and test it, turning it ON and OFF:</p><pre># import library to deal with pins:<br>from machine import Pin</pre><pre># define pin 0 as output<br>led = Pin(0, Pin.OUT)</pre><pre># define value of &quot;led&quot; as &quot;1&quot; or &quot;True&quot; to turn on the LED<br>led.value(1)</pre><pre># define value of &quot;led&quot; as &quot;0&quot; or &quot;False&quot; to turn off the LED<br>led.value(0)</pre><pre># also you can use .on() or .off methods to control the pin:<br>led.on()<br>led.off()</pre><p>Now, Let’s import a time library and blink the LED:</p><pre>from time import sleep</pre><pre>for i in range(5):<br>    led.on()<br>    sleep(0.5)<br>    led.off()<br>    sleep(0.5)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*qKKsL1DxziJu_LN8" /></figure><h3>5: Input Digital Signal</h3><p>The simple sensor data that you can read on a NodeMCU can be got with a push-button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/583/0*j7HFrH_kD5f6wSlw" /></figure><p>Let’s install a push-button between pin 13 (D7) and GND as shown in the diagram.</p><p>Our push-button will be connected in a way that pin 13 normal state will be “High” (so we will use an internal Pull-up resistor to guarantee this state). When pressed, pin 13 will be “Low”.</p><pre># define pin 13 as an input and activate an internal Pull-up resistor:</pre><pre>button = Pin(13, Pin.IN, Pin.PULL_UP)</pre><pre># Read button state:<br>print(button.value())</pre><p>When you run the above cell (w/o pressing the button), the result will be:</p><pre>1</pre><p>Pressing the button, run the cell again:</p><pre># Read button state:<br>print(button.value())</pre><p>The result is now:</p><pre>0</pre><p>Note that stop pressing the button does not return the “cell value” to “1”. To see “1”, you must run the cell again.</p><p>Let’s now do a small program to turn ON the LED only if the button is pressed:</p><pre>print (button.value())<br>if button.value() == 0:<br>    led.on()<br>else:<br>    led.off()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*Ov5lMmSP5DXbxaFk" /></figure><h3>6. PWM (pulse Width Modulation)</h3><p>PWM can be enabled on all pins except Pin(16). There is a single frequency for all channels, with a range between 1 and 1000 (measured in Hz). The duty cycle is between 0 and 1023 inclusive.</p><p>Start calling the appropriate library:</p><pre>from machine import Pin, PWM</pre><p>several commands are available:</p><pre>pwm0 = PWM(Pin(0))      # create PWM object from a pin<br>pwm0.freq()             # get current frequency<br>pwm0.freq(1000)         # set frequency<br>pwm0.duty()             # get current duty cycle<br>pwm0.duty(200)          # set duty cycle<br>pwm0.deinit()           # turn off PWM on the pin</pre><p>Or you can set configure the pin at once:</p><pre>pwm2 = PWM(Pin(2), freq=500, duty=512)</pre><p>Let’s dimming the LED connected to Pin 0 from OFF to ON:</p><pre>from machine import Pin, PWM<br>pwm0 = PWM(Pin(0), freq=1000, duty=0)<br>for i in range (0,1023,20):<br>    pwm0.duty(i)<br>    sleep(0.1)<br>    pwm0.duty(0)<br>    pwm0.deinit()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XxsJJCD68nABlwpu3RIz-g.png" /></figure><p>And how about to control a Servo Motor?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/501/0*3FajZQG27r4j8HjJ" /></figure><p>Let’s install a small hobby servo on our NodeMCU as shown in the diagram. Note that I am connecting the Servo VCC to NodeMCU +3.3V. This is OK for this tutorial, but on real projects, you must connect the Servo VCC to an external +5V Power supply (do not forget to connect the GNDs to NodeMCU GND).</p><p>The servo data pin will be connected to NodeMCU pin 14 (D5).</p><p>Servos usually work with frequency of 50Hz and then a duty cycle between about 40 and 115 will position them from 0 to 180 degrees respectively. A duty cycle of 77 will position the servo on its center value (90 degrees).</p><pre>servo = PWM(Pin(14), freq=50)</pre><p>Test the servo on different positions:</p><pre># Minimum position (angle 0)<br>servo.duty(40)</pre><pre># Maximun position (angle 180)<br>servo.duty(40)</pre><pre># center position (angle 90)<br>servo.duty(40)</pre><p>You can also create a simple swapping program to test your servo:</p><pre># swipping servo<br>step = 2<br>for i in range (40, 115, step):<br>    servo.duty(i)<br>    sleep (0.1)<br>    step = -1*step</pre><pre>for i in range (115, 40, step):<br>    servo.duty(i)<br>    sleep (0.1)<br>    servo.duty(77)</pre><p>Below the result:</p><p>I am not using the sonar here, so I will leave to you to develop a code to use it. It’s simple alheady! Try it!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*GZ3GtTDYgAMGv2rG" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Aq3rqG40RK9pDwFusIVmPA.png" /></figure><h3>7. Analog Input (measuring Luminosity)</h3><p>The ESP8266 has a single pin A0 which can be used to read analog voltages and convert them to a digital value. You can construct such an ADC pin object using:</p><pre>from machine import ADC<br>adc = ADC(0)</pre><p>Next, you can read the value of A0 pin using:</p><pre>adc.read()</pre><p>The Analog pin could be used to read, for example, a variable value got from a potentiometer as a voltage divider. This can be translated as an output for dimming the LED or move the servo to a specific position. You can try it based on what we have learned so far.</p><p>Another useful example is to capture data from an analog sensor, as temperature (LM35), ultraviolet (UV) radiation or luminosity using an LDR (“Light Dependent Resistor).</p><p>An LDR decreases its resistance when the luminosity increase. So, you can create a voltage divider with an LDR and a resistor as shown in the diagram.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/493/0*g386p0EFHzefLuQ8" /></figure><p>Reading the analog voltage directly over the resistor, we will get a signal directly proportional to luminosity.</p><p>Leave the sensor exposed to light and read the ADC value.</p><p>Now, cover the sensor and get a lower value.</p><p>In my case:</p><ul><li>Maximum Light ==&gt; adc value &gt; 850</li><li>Minimun Light ==&gt; adc value &lt; 40</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*2ORKVgHJ6oO52MkD" /></figure><h3>8. Controlling 1-Wire Devices</h3><p>The 1-wire bus is a serial bus that uses just a single wire for communication (in addition to wires for ground and power). The DS18B20 temperature sensor is a very popular 1-wire device, and here we show how to use the “onewire” module to read from such a device.</p><p>For the following code to work you need to have at least one DS18B20 temperature sensor with its data line connected to GPIO 2 (D4).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*xO5ReB5mdWH9n6gQ" /></figure><p>You must also power the sensors and connect a 4.7k Ohm resistor between the data pin and the power pin as shown in the diagram.</p><p>Import the libraries:</p><pre>import onewire, ds18x20</pre><p>Define which pin the 1-wire device will be connected. In our case ==&gt; pin 2 (D4)</p><pre>dat = Pin(2)</pre><p>Create the onewire object:</p><pre>ds = ds18x20.DS18X20(onewire.OneWire(dat))</pre><p>Scan for devices on the bus. Remember that you can have multiple devices connected at same bus.</p><pre>sensors = ds.scan()<br>print(&#39;found devices:&#39;, sensors)</pre><p>“sensors” is an array with the address of all 1-wire sensors connected. We will use “sensors[0]” to point to our sensor.</p><p>Note that you must execute the <em>convert_temp()</em> function to initiate a temperature reading, then wait at least 750ms before reading the value (do not forget to import time library). To read the value use: <em>ds.read_temp(sensors[0]):</em></p><pre>ds.convert_temp()<br>time.sleep_ms(750)<br>print(ds.read_temp(sensors[0]))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*RdpKyrM26QgzuS_o" /></figure><h3>9. Reading Temperature and Humidity, Using DHT Sensor</h3><p>DHT (Digital Humidity &amp; Temperature) sensors are low-cost digital sensors with capacitive humidity sensors and thermistors to measure the surrounding air. They feature a chip that handles analog to digital conversion and provides a digital interface using only a single data wire. Newer sensors additionally provide an I2C interface.</p><p>The DHT11 (blue) and DHT22 (white) sensors provide the same digital interface, however, the DHT22 requires a separate object as it has a more complex calculation. DHT22 have 1 decimal place resolution for both humidity and temperature readings. DHT11 have whole number for both. A custom protocol is used to get the measurements from the sensor. The payload consists of a humidity value, a temperature value, and a checksum.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*6Ib8M4kq9NYLEvKp" /></figure><p>Connect the DHT22 as shown in the diagram. The data pin will be connected to NodeMCU pin 12 (D6).</p><p>To use the DHT interface, construct the objects referring to their data pin. Start calling the library:</p><pre>from dht import DHT22</pre><p>Define the appropriated pin and construct the object:</p><pre>data = DHT22(Pin(12))</pre><p>Get the Temperature and Humidity values:</p><pre>data.measure()<br>temp = data.temperature()<br>hum = data.humidity()</pre><pre>print(&#39;Temp: {}oC&#39;.format(temp))<br>print(&#39;Hum:  {}%&#39;.format(hum))</pre><p>The DHT11 can be called no more than once per second and the DHT22 once every two seconds for most accurate results. Sensor accuracy will degrade over time. Each sensor supports a different operating range. Refer to the product datasheets for specifics.</p><blockquote>DHT22 sensors are now sold under the name AM2302 and are otherwise identical.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*fTZEwIJgH0cRB7cM" /></figure><h3>10. I2C — Using an OLED Display</h3><p>I2C is a two-wire protocol for communicating between devices. At the physical level it consists of 2 wires:</p><ul><li>SCL and SDA, the clock and data lines respectively.</li></ul><p>I2C objects are created attached to a specific bus. They can be initialized when created or initialized later on.</p><p>First, let’s import the library:</p><pre>from machine import I2C</pre><p>Considering a device on Pin 4 (SDA) and Pin 5 (SCL), let’s create an i2c object:</p><pre>i2c = I2C(scl=Pin(5), sda=Pin(4))</pre><p>Now, you should scan the I2C bus for eventual devices there. The function below will do this, returning the number of connected devices and its address:</p><pre>def scanI2c():<br>    print(&#39;Scan i2c bus...&#39;)<br>    devices = i2c.scan()</pre><pre>if len(devices) == 0:<br>        print(&quot;No i2c device !&quot;)<br>    else:<br>        print(&#39;i2c devices found:&#39;,len(devices))</pre><pre>for device in devices:<br>        print(&quot;Decimal address: &quot;,device,&quot; | Hexa address: &quot;,hex(device))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*Mi3oxL07xu0otzY1" /></figure><p>Let’s install an I2C OLED display on our NodeMCU as shown in the diagram. The display is the SSD 1306 (128 x 64).</p><p>Running the scan function:</p><pre>scanI2c()</pre><p>We will get as a result that 1device was found at address 0x3c.</p><p>This address will be used for an oled object creation as below:</p><pre>import ssd1306<br>i2c = I2C(scl=Pin(5), sda=Pin(4))<br>oled = ssd1306.SSD1306_I2C(128, 64, i2c, 0x3c)</pre><p>Some methods to manage the display:</p><pre>poweroff(), turns off the screen. Convenient for battery operation.</pre><pre>contrast(), to adjust the contrast</pre><pre>invert(), invert the colors of the screen (finally white and black!)</pre><pre>show(), to refresh the view</pre><pre>fill(), to fill the screen in black (1) or white (0)</pre><pre>pixel(), to turn on a particular pixel</pre><pre>scroll(), scroll the screen.</pre><pre>text(), to display on text at the indicated x, y position</pre><pre>Draw lines hline(), vline() or any line line()</pre><pre>Draw a rect rect rectangle() or rectangle filled fill_rect()</pre><p>Let’s test our display:</p><pre>oled.fill(0)<br>oled.text(&quot;Hello esp8266&quot;, 0, 0)<br>oled.show()</pre><p>And now, let’s display the DHT22 sensor data on put OLED:</p><pre>data.measure()<br>temp = data.temperature()<br>hum = data.humidity()</pre><pre>oled.fill(0)<br>oled.text(&quot;Temp:  &quot; + str(temp) + &quot;oC&quot;, 0, 0)<br>oled.text(&quot;Hum:   &quot; + str(hum) + &quot;%&quot;,0, 16)<br>oled.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*3p_PoN0qRoC-KlPZ" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/479/0*ImwANSr-XvZROeUc" /></figure><h3>11. Going Further</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*kdYO0TaSQbin25nH" /></figure><p>This article deliveres the bits and pieces for you to build a more robust project, using MicroPython as a programming language and Jupyter Notebook as a tool for quick development and analysis.</p><p>Of course, if you want to run a program written in MicroPython on a NodeMCU independent of your PC and Jupyter, you must save your code as a “main.py” file for example, in any text editor and download it on your device using: “<a href="https://github.com/adafruit/ampy"> Ampy</a>”, that is a utility developed by Adafruit, to interact with a MicroPython board over a serial connection.</p><p>Ampy is meant to be a simple command line tool to manipulate files and run code on a MicroPython board over its serial connection. With ampy you can send files from your computer to a MicroPython board’s file system, download files from a board to your computer, and even send a Python script to a board to be executed.</p><p>Installation:</p><pre>sudo pip3 install adafruit-ampy</pre><p>Ampy is made to talk to a MicroPython board over its serial connection. You will need your board connected and any drivers to access it serial port installed. Then, for example, to list the files on the board run a command like:</p><pre>ampy --port /dev/tty.SLAB_USBtoUART ls</pre><p>For convenience, you can set an AMPY_PORT environment variable which will be used if the port parameter is not specified. For example on Linux or OSX:</p><pre>export AMPY_PORT=/dev/tty.SLAB_USBtoUART</pre><p>So, from now one, you can simplify commands:</p><p>List internal NodeMCU files:</p><pre>ampy ls</pre><p>You can read a file installed on your nodeMCU using:</p><pre>ampy get boot.py</pre><p>Once you create a file using your text editor (nano for example), you can install it on your NodeMCU using:</p><pre>ampy put main.py</pre><p>Now, when you press “Reset” button on your NodeMcu, the program that will run first is “main.py”.</p><p>For Windows, please see the Adafruit instructions <a href="https://github.com/adafruit/ampy">here</a>.</p><h3>12. Conclusion</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/442/0*eCJH5xcWnGfaXZGd" /></figure><p>As always, I hope this project can help others find their way into the exciting world of electronics!</p><p>For details and final code, please visit my GitHub depository: <a href="https://github.com/Mjrovai/Python4DS/tree/master/Micropython">Pyhon4DS/Micropython</a></p><p>For more projects, please visit my blog: <a href="https://mjrobot.org/">MJRoBot.org</a></p><p>Saludos from the south of the world!</p><p>See you in my next article!</p><p>Thank you,</p><p>Marcelo</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=40797b247f9b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mjrobot-org/micropython-on-esp-using-jupyter-notebook-40797b247f9b">MicroPython on ESP Using Jupyter Notebook</a> was originally published in <a href="https://medium.com/mjrobot-org">MJRoBot.org</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Physical Computing using Jupyter Notebook]]></title>
            <link>https://medium.com/mjrobot-org/physical-computing-using-jupyter-notebook-57bd5fa39c8b?source=rss----9c5924ccca32---4</link>
            <guid isPermaLink="false">https://medium.com/p/57bd5fa39c8b</guid>
            <category><![CDATA[physical-computing]]></category>
            <category><![CDATA[raspberry-pi]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Marcelo Rovai]]></dc:creator>
            <pubDate>Fri, 15 Jun 2018 20:44:23 GMT</pubDate>
            <atom:updated>2018-06-15T20:44:22.518Z</atom:updated>
            <cc:license>http://creativecommons.org/publicdomain/zero/1.0/</cc:license>
            <content:encoded><![CDATA[<h4>Learn how to install Jupyter Notebook on a Raspberry Pi, and directly on it, read sensors and act on actuators.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aA_RwrQw9PGcxvDNc6wsBA.png" /></figure><p>We all know that Jupyter Notebook is a fantastic tool, or better, an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text.</p><p>Jupyter Notebook is largely used in Data Science, cleaning and transforming data, doing numerical simulation, statistical modeling, data visualization, machine learning, and much more!</p><blockquote>But, how about to use Jupyter Notebooks to control Raspberry Pi GPIOs?</blockquote><p>That’s is what we will do in this tutorial. We will learn how to install Jupyter Notebook on a Raspberry Pi, and directly on it, we will read sensors and act on actuators.</p><h3>1. Introduction</h3><p>The diagram gives us an an overview about the project:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*NGHh8GP9z_yG2PY-." /></figure><p>And the movie can give you a hint of what is possible to get when the Pi meets Jupyter!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FupqNI1aL_4Q%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DupqNI1aL_4Q&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FupqNI1aL_4Q%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/d88e1a2ab499a0bffdd97413889aa0c8/href">https://medium.com/media/d88e1a2ab499a0bffdd97413889aa0c8/href</a></iframe><h3>2. The HW</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/625/0*752a4yaGRq-8UVQu." /></figure><h4>Bill of Material</h4><ul><li>Raspberry Pi V3</li><li>DHT22 Temperature and Relative Humidity Sensor</li><li>DS18B20 Waterproof Temperature Sensor</li><li>BMP180 Barometric Pressure, Temperature and Altitude Sensor</li><li>LEDS (3x)</li><li>Push Button (1x)</li><li>Resistor 4K7 ohm (2x)</li><li>Resistor 1K ohm (1x)</li><li>Resistor 330 ohm (3x)</li><li>Protoboard and cables</li></ul><h4>The Electrical Diagram</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/533/0*7GUSO0qm3pG3A468." /></figure><p>Let’s follow the above electrical diagram and connect all 3 sensors, buttons and LEDs to Raspberry Pi.</p><p>The sensors used here in this project, are the same used on my tutorial:</p><p><a href="https://www.hackster.io/mjrobot/iot-weather-station-with-rpi-and-esp8266-f382d7">IoT Weather Station With RPi and ESP8266</a></p><p>I suggest that you give a look on that tutorial, where I explain one by one, how to install the sensors and their libraries, testing them individually before running the complete program.</p><h3>3. Installing Jupyter</h3><p>To install Jupyter on your Raspberry (that will run with Python 3), open Terminal and enter with following commands:</p><pre>sudo pip3 install jupyter</pre><pre>sudo ipython3 kernelspec install-self</pre><p>Now on your terminal, run the command:</p><pre>jupyter notebook</pre><p>And that’s it!!!! Amazing! very simple and easy. The Jupyter Notebook will be running as a server on:</p><pre>http:localhost:8888</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*iegmd3I6tYM5X0uF." /></figure><p>But, this is not really important for you to know, because, automatically your default browser will be opened on that address, running a “Home Page”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/439/0*PSzkGR21n4Rk2HXe." /></figure><p>To stop the server and close the “kernels” (the Jupyter notebooks), you must use [Ctrl] + [C] from your keyboard.</p><p>From now one, any time that you start your Pi and want use Jupyter Notebook, just type the command “Jupyter notebook” on your terminal and <strong>keep it running all the time.</strong> This is very important! If you need to use the terminal for another task, as run a program, for example, open a new Terminal window.</p><h3>4. Running Python Scripts on Jupyter Notebook</h3><p>We are assuming that you are familiar with Jupyter Notebook. It will work on your Pi exactly as it works on your desktop.</p><blockquote>There are “tons” of good Jupyter Notebook tutorials available on the web, just goggle it and you certainly will find one more suitable for you.</blockquote><p>Let’s play with a notebook that I have already created for this tutorial.</p><p>Download it from my GitHub: <a href="https://github.com/Mjrovai/Python4DS/blob/master/RaspberryPi/Weather%20Station%20Sensors%20-%20Actuator.ipynb">Weather Station Sensors — Actuators</a></p><p>Save the notebook on your Raspberry Pi. In my case, I save it on my main directory (see the homepage printscreen above). Observe that exist 4 notebooks in my main directory (notebooks have a “small notebook” as an icon). Just click on it. The notebook will be loaded and will appear as the one above. If you see the homepage again, you will realize that the “notebook icon” is now “green”, that means that it is running.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/427/0*TU1tOrWvKtq-RgxO." /></figure><p>Also, note that there is a picture of the HW at first cell of my notebook. For the picture to appear correctly, you must or have it in the same directory (that is my case) or change its path on the notebook if you uses another directory to store the picture.</p><blockquote>You can enter with Unix commands directlly on a Notebook cell, same as you do in your Terminal</blockquote><p>For example, the command:</p><pre>ls</pre><p>Will list all your files in the current directory where the notebook is running, as you can see here:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*Gu1Bj422_qf0ycJY." /></figure><h3>5. Initialization (1): Creating a Python Script on Jupyter Notebook and Importing Libraries</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*4PIGgHenJSRH0xFe." /></figure><p>So, the idea is to start writing your script in the same way (and order) that you are used to do with a Python IDE.</p><p>I will split my notebook into 3 phases, where we will do:</p><ol><li>Initialization</li></ol><ul><li>Import Libraries</li><li>Define GPIOs</li><li>Initialize GPIOs</li><li>Create important functions</li></ul><p>2. Reading Sensors</p><p>3. Reading and Acting on GPIOs (Button and LEDs)</p><p>Important to note that usually, you will run the Initialization phase only once. After that, you can play with your sensors and GPIOs, running only the pertinent cells created for Phase 2 and 3, as in this example.</p><blockquote>For phase 1 better understand, you can follow the introduction movie.</blockquote><h3>6. Initialization (2): Defining and Initializing GPIOs</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*Kl-HBprYa3OQ0MiV." /></figure><h3>7. Initialization (3): Functions for Display Data</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*j670relEGUDSYGFV." /></figure><h3>8. Initialization (4): Functions to Handling Sensors</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/518/0*7LKl6d2mjr5slX43." /></figure><h3>9. Initialization (5): Functions for Handling GPIOs</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/565/0*2suY81Bin-uIfmfp." /></figure><h3>10. Getting Sensor Data and Handling With GPIOs</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/571/0*N_0l-8vImTJIcXsN." /></figure><p>Running the Cell [11] you will get all sensors data:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FzhzwlB6wb_E%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DzhzwlB6wb_E&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FzhzwlB6wb_E%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/a00b01b7eda2307e0cbc84b334574a1a/href">https://medium.com/media/a00b01b7eda2307e0cbc84b334574a1a/href</a></iframe><p><strong>Widgets</strong></p><p>Here it is important to call attention to the “Widgets”.</p><p>Widgets are eventful python objects that have a representation in the browser, often as a control like a slider, textbox, etc. You can use widgets to build interactive GUIs for your notebooks. You can also use widgets to synchronize stateful and stateless information between Python and JavaScript.</p><p>Here in this example, we are using widgets (“Sliders”) to on cell [12}, control in REAL TIME the state of actuators, in this case, turning on or off the LEDs. Widgets are great to add more dynamic behavior on Jupyter Notebooks.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F2Xb9SsJ2c-U%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D2Xb9SsJ2c-U&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F2Xb9SsJ2c-U%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/bb31c9dddbef5e79195ae0aef5fc546b/href">https://medium.com/media/bb31c9dddbef5e79195ae0aef5fc546b/href</a></iframe><p>As you saw, on the code, we must run the cell [11] any time that we want an update value of sensors, but of course, a widget could also be implemented here to do it automatically, or by pressing a button, for example. I will leave it as a challenge for you! ;-)</p><p><strong>Installation</strong></p><p>In order to use Widgets, you must install the Ipywidgets library. For that use below commands:</p><pre>sudo pip3 install ipywidgets<br>jupyter nbextension enable --py widgetsnbextension</pre><p>After installation, only call the library on your Jupyter Notebook.</p><p>Note that we have done it, in the beginning, at initialization phase :</p><pre># widget library<br>from ipywidgets import interactive<br>import ipywidgets as widgets<br>from IPython.display import display</pre><p>This Widget, “interactive”, is simple to be implemented and very powerful. You can learn more about Interactive on this link: <a href="http://ipywidgets.readthedocs.io/en/latest/examples/Using%20Interact.html">Interactive Widget</a>.</p><h3>11. DataScience, Machine Learning, Physical Computing, Computer Vision, IoT and … the Future!</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*khhyNy_mJQiiTPl0k_3N4w.jpeg" /></figure><p>I am starting new journeys on this fascinating world of technology. Everything that we see today has data related and knowing better those data can be key to relevant projects that can help people. You will see more and more, here and on my blog <a href="https://mjrobot.org/">MJRoBot.org</a>, tutorials where DataScience is mixed with Physical Computing, Computer Vision, Machine Learning and IoT.</p><p>As an example, I played with data from a previous IoT project, the <a href="https://www.hackster.io/mjrobot/ardufarmbot-part-2-remote-station-an-iot-implementation-6ccc29">ArduFarmBot</a> :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B8uuBamKVMudZvLwqpI9EA.png" /></figure><p>Using the data that was logged on a IoT Platform, the ThingSpeak and appliyng Machine Learn on those data, was possible to predict (in fact confirm) when the lamp and the pump should be turned on.</p><p>The results were amazing!!!!!!</p><p>You can see the Jupyter Notebook and the final report on my GitHub: <a href="https://github.com/Mjrovai/Python4DS/tree/master/ArduFarmBot_Data_Analysis">ArduFarmBot_Data_Analysis</a></p><p>That’s all, folks! ;-)</p><h3>12. Conclusion</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/442/0*5QxSMwTFp2H-Ozxo." /></figure><p>As always, I hope this project can help others find their way into the exciting world of electronics!</p><p>For details and final code, please visit my GitHub depository: <a href="https://github.com/Mjrovai/Python4DS/tree/master/RaspberryPi">Pyhon4DS/RaspberryPi</a></p><p>For more projects, please visit my blog: <a href="https://mjrobot.org/">MJRoBot.org</a></p><p>Stay tuned! Next tutorial we will send data from a remote weather station to a central one, based on a Raspberry Pi Flask Web server:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/270/0*TJglbXpPe5bYdAe2." /></figure><p>Saludos from the south of the world!</p><p>See you in my next tutorial!</p><p>Thank you,</p><p>Marcelo</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=57bd5fa39c8b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mjrobot-org/physical-computing-using-jupyter-notebook-57bd5fa39c8b">Physical Computing using Jupyter Notebook</a> was originally published in <a href="https://medium.com/mjrobot-org">MJRoBot.org</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[When COZMO the Robot Meets Raspberry Pi]]></title>
            <link>https://medium.com/mjrobot-org/when-cozmo-the-robot-meets-raspberry-pi-ad1bb48077d5?source=rss----9c5924ccca32---4</link>
            <guid isPermaLink="false">https://medium.com/p/ad1bb48077d5</guid>
            <category><![CDATA[robotics]]></category>
            <category><![CDATA[cozmo]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[raspberry-pi]]></category>
            <dc:creator><![CDATA[Marcelo Rovai]]></dc:creator>
            <pubDate>Fri, 15 Jun 2018 20:40:13 GMT</pubDate>
            <atom:updated>2018-06-15T20:40:13.216Z</atom:updated>
            <cc:license>http://creativecommons.org/publicdomain/zero/1.0/</cc:license>
            <content:encoded><![CDATA[<h4>COZMO is a great tinny robot that can be fully programmed using Python. Imagine what you can do, also integrating Cozmo with a Raspberry Pi!</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UyI4sLA5fxeuDzBKd4W9bg.jpeg" /></figure><h3>1. Introduction:</h3><p><a href="https://www.anki.com/en-us/cozmo">COZMO</a>, is a small robot that can be fully programmed using Python. The robot is manufactured by <a href="https://www.anki.com/en-us">ANKI</a>.</p><p>When you start to discover COZMO, you will learn that this little guy is really smart that evolves the more you play with him. Better than words, the below movie can give a taste of Cozmo:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FKJ0iSJ-9dwY%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DKJ0iSJ-9dwY&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FKJ0iSJ-9dwY%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/dee0f9edadb4040b324ec7db31ce6bf6/href">https://medium.com/media/dee0f9edadb4040b324ec7db31ce6bf6/href</a></iframe><p>But, as explained on Cozmo’s website,</p><blockquote>“Cozmo’s unprecedented combination of advanced robotics hardware and software are part of what makes him an innovative consumer experience. But it’s also what makes him, in conjunction with the Cozmo SDK, a groundbreaking education platform that’s expressive, motivating, and engaging”</blockquote><p>And that’s is what we will develop here in this tutorial. Using the Cozmo’s SDK integrated with Raspberry Pi, we will open a fantastic window, integrating Robotics and Physical Computing!</p><p>The below video will give you a taste of what we will explore here:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F6sJqK_QfhIU%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D6sJqK_QfhIU&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F6sJqK_QfhIU%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/2424f24ffae3a77ae286c160a67e7a80/href">https://medium.com/media/2424f24ffae3a77ae286c160a67e7a80/href</a></iframe><h3>2. A Little Bit of Cozmo’s Technology</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/0*zOsdHq5GO8nHDlyd." /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*jlXF_VXbNCdLb4mBdZ_ZKA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*bqzBZf9dSiQ3dOtLGReLgQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Bl0NPfxvT829E1HJje1aTA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TrrBaodvsnoXeEMAjQj_HA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pL6u2SM9n8DzF_d8xtmzcA.png" /></figure><h3>3. Cozmo and Raspberry Pi — Initial Setup</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/0*2rEOM9bpeRQ-_30s." /></figure><p>In order to program Cozmo using Python, we will use its SDK. To do that, we must have <a href="https://play.google.com/store/apps/details?id=com.anki.cozmo&amp;hl=en">Cozmo Mobile App </a>installed on our mobile device and that device must be tethered to Rpi via USB cable.</p><p>Prerequisites:</p><ul><li>Raspberry Pi (preferible RPi 3)</li><li>Raspbian (I used the latest version 4.9)</li><li>Python 3.5.1 or later (the last one installed on RPI is 3.5.5)</li><li>WiFi connection</li><li>An Android mobile device with the Cozmo app installed, connected to the Rpi via USB cable</li></ul><p>The above picture shows the general idea.</p><h3>4. Mobile Device Setup</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/0*gTsoefBdGdGrR_2b." /></figure><p>Android devices require the installation of Android Debug Bridge (ADB) in order to run the Cozmo SDK. This is required for the RPi to communicate with the Android mobile device over a USB cable and runs automatically when required.</p><p>First of all, you must enable <strong><em>USB Debugging</em></strong> on your phone. To do that,</p><p>Go to <em>Settings/General/About device</em>:Tap seven (7) times on the <em>Build Number</em>.This will open another link: <em>Developer Option</em>, that is hidden by default. Then, under <em>Settings -&gt; Developer Options</em>, <strong>enable</strong><em>USB debugging</em>.</p><p>Install the ADB (Android Debug Bridge) on RPi with the command:</p><pre>sudo apt-get install android-tools-adb</pre><p>Now connect your phone to one of RPi USB connectors.</p><ul><li>When the <strong>Allow USB Debugging?</strong> popup displays on your phone,</li><li>Tap <strong>OK</strong>.</li></ul><p>At the Rpi command line, type below command to confirm that your device shows:</p><pre>adb devices</pre><p>At least one device should show in the result, for example:</p><pre>List of devices attached</pre><pre>31455667754d65432 device</pre><p>If you have not authorized your phone yet, the word “unauthorized” will appear in front of device (see RPi terminal Print Screen above).</p><h3>5. SDK Installation</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/0*AdRC06H82wddg8sn." /></figure><p>If you have the latest version of <em>Raspbian</em> installed, you have already Python3 and Pip3 installed and configured in your RPi.</p><p>Now, its time to SDK Installation:</p><p>To install the SDK, type the following into the Terminal window:</p><pre>pip3 install --user &#39;cozmo[camera]&#39;</pre><blockquote>Note that the [camera] option adds support for processing images from Cozmo’s camera.</blockquote><p>The RPi terminal Print Screen shows the result after a successful installation.</p><h3>6. Downloads SDK Examples</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/0*buwvyb4IH0gKev7Q." /></figure><p>Now you must download some examples to start playing with Cozmo SDK. Open your RPi Browser and go to this link:</p><pre><a href="http://cozmosdk.anki.com/1.2.1/cozmo_sdk_examples_1.2.1.tar.gz">http://cozmosdk.anki.com/1.2.1/cozmo_sdk_examples_1.2.1.tar.gz</a></pre><p>A compressed file will be downloaded on your RPi Downloads directory. Choose a proper directory to extract the files. In my case, I extracted them at:</p><pre>/Documents</pre><p>See the Print Screen of RPi File Manager. There you will find among the subdirectories:</p><pre>tutorials</pre><p>There you willl find great examples to start playing with Cozmo and RPi using Python language.</p><h3>7. Running the First Program: Hello World!</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/0*xguMUxSrJzB9sKdU." /></figure><p>Let’s run our first Cozmo program, using its SDK on an RPi! You can run the program on Terminal, for that type:</p><pre>cd tutorials/01_basics</pre><p>And execute the program <em>01_hello_world.py</em> using Python 3:</p><pre>python3 01_hello_world.py</pre><p>Or you can open the Python3 IDE or “Thonny IDE” (that I prefer). In this case, once you had open Thonny, go to “SDK examples directory”:</p><pre>/Documents/cozmo_sdk_examples_1.2.1/tutorials/01_basics</pre><p>And open the program:</p><pre>01_hello_world.py</pre><p>The above Print Screen shows Thommy IDE. Run the program. Cozmo should say “Hello World!”, as shown in the movie:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FjfJ2nswCbH0%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DjfJ2nswCbH0&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FjfJ2nswCbH0%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/f06bcf040d5fd768b8304b243a986210/href">https://medium.com/media/f06bcf040d5fd768b8304b243a986210/href</a></iframe><h3>8. The Complete IPI</h3><p>There are a lot of things to explore using the Cozmo SDK. For starting, go to Tutorial file and play with the other examples there. We will not go deeper here because our goal is to interact Cozmo with Raspberry Pi.</p><p>You can find a full description of Cozmo IPI at <a href="http://cozmosdk.anki.com/docs/api.html">COZMO IPI.</a></p><p>Only as an example, the below code:</p><pre>&#39;&#39;&#39; Drive a Square &#39;&#39;&#39;</pre><pre>import cozmo<br>from cozmo.util import degrees, distance_mm, speed_mmps</pre><pre>def cozmo_program(robot: cozmo.robot.Robot):<br>    robot.say_text(&quot;Hello marcelo, let&#39;s drive a  \<br>                    square&quot;).wait_for_completed()<br>    for _ in range (4):<br>        robot.drive_straight(distance_mm(150), \<br>          speed_mmps(50)).wait_for_completed()<br>        robot.say_text(&quot;Left&quot;).wait_for_completed()<br>        robot.turn_in_place(degrees(90)).wait_for_completed()<br>        robot.say_text(&quot;and finished!&quot;).wait_for_completed()</pre><pre>cozmo.run_program(cozmo_program)</pre><p>The movie shows the result:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FlqW1wYEXOE4%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DlqW1wYEXOE4&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FlqW1wYEXOE4%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/27be089986ce31a84c739f2a451b3c9c/href">https://medium.com/media/27be089986ce31a84c739f2a451b3c9c/href</a></iframe><h3>9. Physical Computing With Raspberry Pi</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/0*j-Gwg0jsTB_Kt_vz." /></figure><p>Some time ago, I published here a tutorial: <a href="https://www.hackster.io/mjrobot/playing-with-electronics-rpi-gpio-zero-library-tutorial-f984c9">Playing With Electronics: Raspberry GPIO Zero Library</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/0*f38nPtoEJYTMFkqK." /></figure><p>Using the GPIO Zero Library and a Raspberry Pi is a very simple way to learn electronics. With a very few code in Python you will control actuators, read sensors, etc. If you are not familiar with Physical computing, I suggest that you give a look at my tutorial.</p><p>OK, saying that! Let’s put together a simple circuit together our Raspberry Pi, where we will have as output 3 LEDs and a Push-Button as an input. The above diagram shows how to do it. Pay attention to GPIO numbers where the components are connected. As usual, let’s “blink a LED” (The Physical Computing version of “Hello World”), so we will confirm that everything is OK:</p><p>Open your Terminal, Python IDE or Thonny IDE and type the following code:</p><pre>from gpiozero import LEDfrom time import sleep<br>led = LED(13)<br>while True:<br>    led.on()<br>    sleep(1)<br>    led.off()<br>    sleep(1)</pre><p>Run the program and if everything is OK, your RED LED should blink.</p><h3>10. New “Hello World” With Cozmo and RPi</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/0*cxW5VeXBFkPSFRab." /></figure><p>Now that we have our RPi “talking” with a LED and the Cozmo, but separately. It is time to do it at the same time.</p><p>Enter with below program in your IDE:</p><pre>import cozmo<br>from cozmo.util import degrees<br>from time import sleep<br>from gpiozero import LED</pre><pre>led = LED(13)</pre><pre>def cozmo_program(robot: cozmo.robot.Robot):<br>    robot.set_head_angle(degrees(30)).wait_for_completed()<br>    robot.say_text(&quot;Hello Raspbery pi&quot;).wait_for_completed()<br>    robot.set_lift_height(30.0).wait_for_completed()<br>    hello_rpi()<br>    robot.set_head_angle(degrees(0)).wait_for_completed()<br>    robot.set_lift_height(0.0).wait_for_completed()</pre><pre>def hello_rpi():<br>    for _ in range(3):<br>    led.on()<br>    sleep(1)<br>    led.off()<br>    sleep(1)</pre><pre>cozmo.run_program(cozmo_program)</pre><p>Now we have a specific function created to blink the Red LED 3 times. This function will be called by Cozmo, after he salute the RPI. The below movie shows the result:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FVvo00AmX-8U%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DVvo00AmX-8U&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FVvo00AmX-8U%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/1cec9cdbdf349e66c9a8841f5ca4dc64/href">https://medium.com/media/1cec9cdbdf349e66c9a8841f5ca4dc64/href</a></iframe><h3>11. Triggering Cozmo With External Events</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/482/0*VJ-M0Xv81vb_HWVl." /></figure><p>Let’s now, create an external and physical event, that will trigger a reaction on Cozmo. We will press a button and Cozmo will confirm this action to us as shown below:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FJOEHCIktyas%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DJOEHCIktyas&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FJOEHCIktyas%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/b21fe78acbe194a8966e58229e217bde/href">https://medium.com/media/b21fe78acbe194a8966e58229e217bde/href</a></iframe><p>Very cool, isn’t? The code is very simple:</p><pre>import cozmo<br>from cozmo.util import degrees<br>from gpiozero import LED, Button<br>from signal import pause</pre><pre>led = LED(13)<br>button = Button(20)</pre><pre>def cozmo_program(robot: cozmo.robot.Robot):<br>    button.wait_for_press()<br>    led.on()<br>    robot.set_head_angle(degrees(30)).wait_for_completed()<br>    robot.say_text(&quot;Hello Raspbery pi. Button <br>                    pressed&quot;).wait_for_completed() \<br>    robot.set_head_angle(degrees(0)).wait_for_completed()<br>    led.off()</pre><pre>cozmo.run_program(cozmo_program)</pre><h3>12. More Interaction</h3><p>Let’s mixed actions form an external user and Cozmo, where he will move and use its backpack lights. This example is only to show that from now on, the sky is the limit!</p><blockquote>OPS! The men went to the Moon already! So, no limit, folks!</blockquote><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F6sJqK_QfhIU%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D6sJqK_QfhIU&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F6sJqK_QfhIU%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/2424f24ffae3a77ae286c160a67e7a80/href">https://medium.com/media/2424f24ffae3a77ae286c160a67e7a80/href</a></iframe><p>The code for the above example can be seen here:</p><pre>from gpiozero import LED, Button<br>from time import sleep</pre><pre>red = LED(13)<br>yellow = LED(19)<br>green = LED(26)<br>button = Button(20)</pre><pre>def cozmo_program(robot: cozmo.robot.Robot):<br>    robot.say_text(&quot;Hello Raspbery pi. Press the \<br>                   button&quot;).wait_for_completed()<br>    <br>    button.wait_for_press()<br>    robot.drive_straight(distance_mm(150), \<br>                  speed_mmps(50)).wait_for_completed()<br>    robot.turn_in_place(degrees(90)).wait_for_completed()<br>    red.on()<br>    robot.set_all_backpack_lights(cozmo.lights.red_light)<br>    robot.say_text(&quot;red&quot;).wait_for_completed()</pre><pre>button.wait_for_press()<br>    red.off()<br>    yellow.on()  <br>    robot.set_center_backpack_lights(cozmo.lights.white_light)<br>    robot.say_text(&quot;yellow&quot;).wait_for_completed()<br>    sleep(1)</pre><pre>button.wait_for_press()<br>    yellow.off()<br>    green.on()<br>    robot.set_all_backpack_lights(cozmo.lights.green_light)<br>    robot.say_text(&quot;green&quot;).wait_for_completed()<br>    sleep(1)</pre><pre>button.wait_for_press()<br>    green.off()<br>    robot.set_all_backpack_lights(cozmo.lights.off_light)<br>    robot.say_text(&quot;bye, bye Raspbery pi&quot;).wait_for_completed()<br>    robot.turn_in_place(degrees(90)).wait_for_completed()<br>    robot.drive_straight(distance_mm(150),  \<br>                        speed_mmps(50)).wait_for_completed()</pre><pre>robot.turn_in_place(degrees(180)).wait_for_completed()<br> <br>cozmo.run_program(cozmo_program)</pre><h3>13. Conclusion</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/443/0*ziqL3hwpcoDlREUH." /></figure><p>As always, I hope this project can help others find their way into the exciting world of electronics!</p><p>For more projects, please visit my blog: <a href="https://mjrobot.org/">MJRoBot.org</a></p><p>Saludos from the south of the world!</p><p>See you at my next tutorial!</p><p>Thank you</p><p>Marcelo</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ad1bb48077d5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mjrobot-org/when-cozmo-the-robot-meets-raspberry-pi-ad1bb48077d5">When COZMO the Robot Meets Raspberry Pi</a> was originally published in <a href="https://medium.com/mjrobot-org">MJRoBot.org</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Automatic Vision Object Tracking]]></title>
            <link>https://medium.com/mjrobot-org/automatic-vision-object-tracking-2dc6b4acaff5?source=rss----9c5924ccca32---4</link>
            <guid isPermaLink="false">https://medium.com/p/2dc6b4acaff5</guid>
            <category><![CDATA[raspberry-pi]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[opencv]]></category>
            <category><![CDATA[robotics]]></category>
            <category><![CDATA[computer-vision]]></category>
            <dc:creator><![CDATA[Marcelo Rovai]]></dc:creator>
            <pubDate>Fri, 15 Jun 2018 20:37:08 GMT</pubDate>
            <atom:updated>2018-06-15T20:37:07.981Z</atom:updated>
            <cc:license>http://creativecommons.org/publicdomain/zero/1.0/</cc:license>
            <content:encoded><![CDATA[<h4>A pan/tilt servo device helping a camera to automatically track color objects using vision.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/0*l7cBuKfSs58E34BV." /></figure><h3>1. Introduction</h3><p>On a previous tutorial, we explored how to <a href="https://www.hackster.io/mjrobot/pan-tilt-multi-servo-control-b67791">control a Pan/Tilt Servo device</a> in order to position a PiCam. Now we will use our device to help the camera to automatically tracking color objects as you can see below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*EWNnVYMo3DsouKX8." /></figure><p>This is my first experience with OpenCV and I must confess, I am in love with this fantastic “Open Source Computer Vision Library”.</p><p><a href="https://opencv.org/">OpenCV</a> is free for both academic and commercial use. It has C++, C, Python and Java interfaces and supports Windows, Linux, Mac OS, iOS and, Android. On my series of OpenCV tutorials, we will be focusing on Raspberry Pi (so, Raspbian as OS) and Python. OpenCV was designed for computational efficiency and with a strong focus on real-time applications. So, it’s perfect for Physical computing projects!</p><h3>2. Installing OpenCV 3 Package</h3><p>I am using a Raspberry Pi V3 updated to the last version of Raspbian (Stretch), so the best way to have OpenCV installed, is to follow the excellent tutorial developed by Adrian Rosebrock: <a href="https://www.pyimagesearch.com/2017/09/04/raspbian-stretch-install-opencv-3-python-on-your-raspberry-pi/">Raspbian Stretch: Install OpenCV 3 + Python on your Raspberry Pi</a>.</p><p>I tried several different guides to install OpenCV on my Pi. Adrian’s tutorial is the best. I advise you to do the same, following his guideline step-by-step.</p><p>Once you finished Adrian’s tutorial, you should have an OpenCV virtual environment ready to run our experiments on your Pi.</p><p>Let’s go to our virtual environment and confirm that OpenCV 3 is correctly installed.</p><p>Adrian recommends run the command “source” each time you open up a new terminal to ensure your system variables have been set up correctly.</p><pre>source ~/.profile</pre><p>Next, let’s enter on our virtual environment:</p><pre>workon cv</pre><p>If you see the text (cv) preceding your prompt, then you are in the <em>cv virtual</em>environment:</p><pre>(cv) pi@raspberry:~$</pre><p>Adrian calls the attention that the <strong><em>cv Python virtual environment</em></strong> is entirely independent and sequestered from the default Python version included in the download of Raspbian Stretch. So, any Python packages in the global site-packages directory will not be available to the cv virtual environment. Similarly, any Python packages installed in site-packages of cv will not be available to the global install of Python.</p><p>Now, enter in your Python interpreter:</p><pre>python</pre><p>and confirm that you are running the 3.5 (or above) version</p><p>Inside the interpreter (the “&gt;&gt;&gt;” will appear), import the OpenCV library:</p><pre>import cv2</pre><p>If no error messages appear, the OpenCV is correctly installed ON YOUR PYTHON VIRTUAL ENVIRONMENT.</p><p>You can also check the OpenCV version installed:</p><pre>cv2.__version__</pre><p>The 3.3.0 should appear (or a superior version that can be released in future). The above Terminal PrintScreen shows the previous steps.</p><h3>3. Testing Your Camera</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*jZqMTb-YJ-tYzkuP." /></figure><p>Once you have OpenCV installed in your RPi let’s test if your camera is working properly.</p><p>I am assuming that you have a PiCam already installed on your Raspberry Pi.</p><p>Enter the below Python code on your IDE:</p><pre>import numpy as np<br>import cv2<br>cap = cv2.VideoCapture(0)</pre><pre>while(True):<br>    ret, frame = cap.read()<br>    frame = cv2.flip(frame, -1) # Flip camera vertically<br>    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)<br>    <br>    cv2.imshow(&#39;frame&#39;, frame)<br>    cv2.imshow(&#39;gray&#39;, gray)<br>    if cv2.waitKey(1) &amp; 0xFF == ord(&#39;q&#39;):<br>        break</pre><pre>cap.release()<br>cv2.destroyAllWindows()</pre><p>The above code will capture the video stream that will be generated by your PiCam, displaying it both, in BGR color and Gray mode.</p><p>Note that I rotated my camera vertically due the way it is assembled. If it is not your case, comment or delete the “flip” command line.</p><p>You can alternatively download the code from my GitHub: <a href="https://github.com/Mjrovai/OpenCV-Object-Face-Tracking/blob/master/simpleCamTest.py">simpleCamTest.py</a></p><p>To execute, enter the command:</p><pre>python simpleCamTest.py</pre><p>To finish the program, you must press the key [q] tor [Ctrl] + [C] on your keyboard</p><p>The picture shows the result.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*pqbSHPObaRuUzftJ." /></figure><p>To know more about OpenCV, you can follow the tutorial: <a href="https://pythonprogramming.net/loading-video-python-opencv-tutorial/">loading -video-python-opencv-tutorial</a></p><h3>4. Color Detection in Python With OpenCV</h3><p>One thing that we will try to accomplish, will be the detection and tracking of a certain color object. For that, we must understand a little bit more about how OpenCV interpret colors.</p><p>Henri Dang wrote a great tutorial about <a href="https://henrydangprg.com/2016/06/26/color-detection-in-python-with-opencv/">Color Detection in Python with OpenCV</a>.</p><p>Usually, our camera will work with RGB color mode, which can be understood by thinking of it as all possible colors that can be made from three colored lights for red, green, and blue. We will work here with BGR (Blue, Green, Red) instead.</p><p>As described above, with BGR, a pixel is represented by 3 parameters, blue, green, and red. Each parameter usually has a value from 0–255 (or O to FF in hexadecimal). For example, a pure blue pixel on your computer screen would have a B value of 255, a G value of 0, and a R value of 0.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*MaBRY76pPYp9RaG0." /></figure><p>OpenCV works with HSV (Hue, Saturation, Value) color model, that it is an alternative representation of the RGB color model, designed in the 1970s by computer graphics researchers to more closely align with the way human vision perceives color-making attributes:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*w2nAGGNOqvmVoi1Q." /></figure><p>Great. So, if you want to track a certain color using OpenCV, you must define it using the HSV Model.</p><h4>Example</h4><p>Let’s say that I must track a yellow object as the plastic box shown ay above picture. The ease part is to find its BGR elements. You can use any design program to find it (I used PowerPoint).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*U9YtcUNNUJuMgp9_." /></figure><p>In my case I found:</p><ul><li>Blue: 71</li><li>Green: 234</li><li>Red: 213</li></ul><p>Next, we must convert the BGR (71, 234, 213) model to an HSV model, that will be defined with upper and lower range boundaries. For that, let’s run the below code:</p><pre>import sys<br>import numpy as np<br>import cv2</pre><pre>blue = sys.argv[1]<br>green = sys.argv[2]<br>red = sys.argv[3]  </pre><pre>color = np.uint8([[[blue, green, red]]])<br>hsv_color = cv2.cvtColor(color, cv2.COLOR_BGR2HSV)<br>hue = hsv_color[0][0][0]<br>print(&quot;Lower bound is :&quot;),<br>print(&quot;[&quot; + str(hue-10) + &quot;, 100, 100]\n&quot;)<br>print(&quot;Upper bound is :&quot;),<br>print(&quot;[&quot; + str(hue + 10) + &quot;, 255, 255]&quot;)</pre><p>You can alternatively download the code from my GitHub: <a href="https://github.com/Mjrovai/OpenCV-Object-Face-Tracking/blob/master/bgr_hsv_converter.py">bgr_hsv_converter.py</a></p><p>To execute, enter the below command having as parameters the BGR values found before:</p><pre>python bgr_hsv_converter.py 71 234 213</pre><p>The program will print the upper and lower boundaries of our object color.</p><p>In this case:</p><pre>lower bound: [24, 100, 100]</pre><p>and</p><pre>upper bound: [44, 255, 255]</pre><p>The Terminal PrintScreen shows the result.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*kwJWAzeXfCMtfIrN." /></figure><p>Last, but not least, let’s see how OpenCV can “mask” our object once we have determined its color:</p><pre>import cv2<br>import numpy as np</pre><pre># Read the picure - The 1 means we want the image in BGR<br>img = cv2.imread(&#39;yellow_object.JPG&#39;, 1) </pre><pre># resize imag to 20% in each axis<br>img = cv2.resize(img, (0,0), fx=0.2, fy=0.2)</pre><pre># convert BGR image to a HSV image<br>hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) </pre><pre># NumPy to create arrays to hold lower and upper range <br># The “dtype = np.uint8” means that data type is an 8 bit integer<br>lower_range = np.array([24, 100, 100], dtype=np.uint8) <br>upper_range = np.array([44, 255, 255], dtype=np.uint8)</pre><pre># create a mask for image<br>mask = cv2.inRange(hsv, lower_range, upper_range)</pre><pre># display both the mask and the image side-by-side<br>cv2.imshow(&#39;mask&#39;,mask)<br>cv2.imshow(&#39;image&#39;, img)</pre><pre># wait to user to press [ ESC ]<br>while(1):<br>  k = cv2.waitKey(0)<br>  if(k == 27):<br>    break</pre><pre>cv2.destroyAllWindows()</pre><p>You can alternatively download the code from my GitHub: <a href="https://github.com/Mjrovai/OpenCV-Object-Face-Tracking/blob/master/colorDetection.py">colorDetection.py</a></p><p>To execute, enter the below command having in your directory a photo with your target object (in my case: yellow_object.JPG):</p><pre>python colorDetection.py</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*5Y45ZVvLf-6eR3XH." /></figure><p>The above picture will show the original image (“image”) and how the object will appear (“mask”) after that the mask is applied.</p><h3>5. Object Movement Tracking</h3><p>Now that we know how to “select” our object using a mask, let’s track its movement in real time using the camera. For that, I based my code on <a href="https://www.pyimagesearch.com/2015/09/14/ball-tracking-with-opencv/">Adrian Rosebrock’s Ball Tracking with OpenCV tutoria</a>l.</p><p>I strongly suggest that you read Adrian’s tutorial in detail.</p><p>First, confirm if you have the <em>imutils library</em> installed. it is Adrian’s collection of OpenCV convenience functions to make a few basic tasks (like resizing or flip screen) much easier. If not, enter with below command to install the library on your Virtual Python environment:</p><pre>pip install imutils</pre><p>Next, download the code <a href="https://github.com/Mjrovai/OpenCV-Object-Face-Tracking/blob/master/ball_tracking.py">ball_tracking.py</a> from my GitHub, and execute it using the command:</p><pre>python ball_traking.py</pre><p>As a result, you will see something similar to the gif below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*IJslMc33QrepSY95." /></figure><p>Basically, it is the same code as Adrian’s unless the “video vertical flip”, that I got with the line:</p><pre>frame = imutils.rotate(frame, angle=180)</pre><p>Also, note that the mask boundaries used were the one that we got in the previous step.</p><h3>6. Testing the GPIOs</h3><p>Now that we have played with the basics of OpenCV, let’s install a LED to our RPi and start to interact with our GPIOs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*NR-X9UCx9qmbjBrm." /></figure><p>Follow the above electrical diagram: The LED’s cathode will be connected to GPIO 21 and its anode to GND via a 220-ohm resistor.</p><p>Let’s test our LED inside our Virtual Python Environment.</p><p>Remember that its possible that RPi.GPIO is not installed in your Python virtual environment! To fix this issue, once you are there (remember to confirm that the (cv) is in your terminal), you need to use pip to install it into your virtual environment:</p><pre>pip install RPi.GPIO</pre><p>Let’s use the python script to execute a simple test :</p><pre>import sys<br>import time<br>import RPi.GPIO as GPIO</pre><pre># initialize GPIO and variables<br>redLed = int(sys.argv[1])<br>freq = int(sys.argv[2])<br>GPIO.setmode(GPIO.BCM)<br>GPIO.setup(redLed, GPIO.OUT)<br>GPIO.setwarnings(False)</pre><pre>print(&quot;\n [INFO] Blinking LED (5 times) connected at GPIO {0} at every {1} second(s)&quot;.format(redLed, freq))<br>for i in range(5):<br>    GPIO.output(redLed, GPIO.LOW)<br>    time.sleep(freq)<br>    GPIO.output(redLed, GPIO.HIGH)<br>    time.sleep(freq)</pre><pre># do a bit of cleanup<br>print(&quot;\n [INFO] Exiting Program and cleanup stuff \n&quot;)<br>GPIO.cleanup()</pre><p>This code will receive as arguments a GPIO number and the frequency in seconds that our LED should blink. The LED will blink 5 times and the program will be terminated. Note that before terminate, we will liberate the GPIOs.</p><p>So, to execute the script, you must enter as parameters, <em>LED GPIO</em>, and <em>frequency</em>.</p><p>For example:</p><pre>python LED_simple_test.py 21 1</pre><p>The above command will blink 5 times the red LED connected to “GPIO 21” every “1” second.</p><p>The file <a href="https://github.com/Mjrovai/OpenCV-Object-Face-Tracking/blob/master/GPIO_LED_test.py">GPIO_LED_test.py</a> can be downloaded from my GitHub</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*c09K9ImNkENzfrqq." /></figure><p>The above Terminal print screen shows the result (and of course you should confirm that the LED is blinking.</p><p>Now, let’s work with OpenCV and some basic GPIO stuff.</p><h3>7. Recognizing Colors and GPIO Interaction</h3><p>Let’s start integrating our OpenCV codes with GPIO interaction. We will start with the last OpenCV code and we will integrate the GPIO-RPI library on it, so we will turn on the red LED anytime that our colored object is found by the camera. The code used in this step was based on Adrian’s great tutorial <a href="https://www.pyimagesearch.com/2016/05/09/opencv-rpi-gpio-and-gpio-zero-on-the-raspberry-pi/">OpenCV, RPi.GPIO, and GPIO Zero on the Raspberry Pi</a>:</p><p>The first thing to do is to “create” our LED, connecting it to the specific GPIO:</p><pre>import RPi.GPIO as GPIO<br>redLed = 21<br>GPIO.setmode(GPIO.BCM)<br>GPIO.setwarnings(False)<br>GPIO.setup(redLed, GPIO.OUT)</pre><p>Second, we must initialize our LED (turned off):</p><pre>GPIO.output(redLed, GPIO.LOW)<br>ledOn = False</pre><p>Now, inside the loop, where the “circle” is created when the object is found, we will turn on the LED:</p><pre>GPIO.output(redLed, GPIO.HIGH)<br>ledOn = True</pre><p>Let’s download the complete code from my GitHub: <a href="https://github.com/Mjrovai/OpenCV-Object-Face-Tracking/blob/master/object_detection_LED.py">object_detection_LED.py</a></p><p>Run the code using the command:</p><pre>python object_detection_LED.py</pre><p>Here the result. Note the LED (left inferior corner) goes on everytime that the object is detected:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*onGY9YyDNbgN-Vjp." /></figure><p>Try it with different objects (color and format). You will see that once the color match inside the mask boundaries, the LED is turned on.</p><p>The video below shows some experiences. Note that only yellow objects that stay inside the color range will be detected, turning the LED on. Objects with different colors are ignored.</p><p>We are only using the LED here as explained in the last step. I had my Pan Tilt already assembled when I did the video, so ignore it. We will handle with PAN/TILT mechanism in next step.</p><h3>8. The Pan Tilt Mechanism</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*cLfYYL85fPMG89kt." /></figure><p>Now that we have played with the basics of OpenCV and GPIO, let’s install our Pan/tilt mechanism.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*9buxAYZ7-RQldG0C." /></figure><blockquote><em>For details, please visit my tutorial: </em><a href="https://www.instructables.com/id/Pan-Tilt-Multi-Servo-Control/"><em>Pan-Tilt-Multi-Servo-Control</em></a></blockquote><p>The servos should be connected to an external 5V supply, having their data pin (in my case, their yellow wiring) connect to Raspberry Pi GPIO as below:</p><ul><li>GPIO 17 ==&gt; Tilt Servo</li><li>GPIO 27 ==&gt; Pan Servo</li></ul><p>Do not forget to connect the GNDs together ==&gt; Raspberry Pi — Servos — External Power Supply)</p><p>You can have as an option, a resistor of 1K ohm in series, between Raspberry Pi GPIO and Server data input pin. This would protect your RPi in case of a servo problem.</p><p>Let’s also use the opportunity and test our servos inside our Virtual Python Environment.</p><p>Let’s use Python script to execute some tests with our drivers:</p><pre>from time import sleep<br>import RPi.GPIO as GPIO<br>GPIO.setmode(GPIO.BCM)<br>GPIO.setwarnings(False)</pre><pre>def setServoAngle(servo, angle):<br>	pwm = GPIO.PWM(servo, 50)<br>	pwm.start(8)<br>	dutyCycle = angle / 18. + 3.<br>	pwm.ChangeDutyCycle(dutyCycle)<br>	sleep(0.3)<br>	pwm.stop()</pre><pre>if __name__ == &#39;__main__&#39;:<br>	import sys<br>	servo = int(sys.argv[1])<br>	GPIO.setup(servo, GPIO.OUT)<br>	setServoAngle(servo, int(sys.argv[2]))<br>	GPIO.cleanup()</pre><p>The core of above code is the function setServoAngle(servo, angle). This function receives as arguments, a servo GPIO number, and an angle value to where the servo must be positioned. Once the input of this function is “angle”, we must convert it to an equivalent duty cycle.</p><p>To execute the script, you must enter as parameters, <em>servo GPIO</em>, and <em>angle</em>.</p><p>For example:</p><pre>python angleServoCtrl.py 17 45</pre><p>The above command will position the servo connected on GPIO 17 (“tilt”) with 45 degrees in “elevation”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/364/0*5rURGlTPGM-k1c5E." /></figure><p>The file <a href="https://github.com/Mjrovai/OpenCV-Object-Face-Tracking/blob/master/angleServoCtrl.py">angleServoCtrl.py </a>can be downloaded from my GitHub</p><h3>9. Finding the Object Realtime Position</h3><p>The idea here will be to position the object in the middle of the screen using the Pan/Tilt mechanism. The bad news is that for starting we must know where the object is located in real time. But the good news is that it is very easy, once we already have the object center’s coordinates.</p><p>First, let’s take the “object_detect_LED” code used before and modify it to print the x,y coordinates of the founded object.</p><p>Download from my GitHub the code: <a href="https://github.com/Mjrovai/OpenCV-Object-Face-Tracking/blob/master/Object_Tracking/objectDetectCoord.py">objectDetectCoord.py</a></p><p>The “core” of the code is the portion where we find the object and draw a circle on it with a red dot in its center.</p><pre># only proceed if the radius meets a minimum size<br>if radius &gt; 10:<br>	# draw the circle and centroid on the frame,<br>	# then update the list of tracked points<br>	cv2.circle(frame, (int(x), int(y)), int(radius),<br>		(0, 255, 255), 2)<br>	cv2.circle(frame, center, 5, (0, 0, 255), -1)<br>			<br>	# print center of circle coordinates<br>	mapObjectPosition(int(x), int(y))<br>			<br>	# if the led is not already on, turn the LED on<br>	if not ledOn:<br>		GPIO.output(redLed, GPIO.HIGH)<br>		ledOn = True</pre><p>Let’s “export” the center coordinates to <em>mapObjectPosition(int(x), int(y))</em> function in order to print its coordinates. Below the function:</p><pre>def mapObjectPosition (x, y):<br>    print (&quot;[INFO] Object Center coordenates at X0 = {0} and Y0 =  {1}&quot;.format(x, y))</pre><p>Running the program, we will see at our terminal, the (x, y) position coordinates, as shown above. Move the object and observe the coordinates. We will realize that x goes from 0 to 500 (left to right) and y goes from o to 350 (top to down). See the above pictures.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*KlTqM5nedcAVLQln." /></figure><p>Great! Now we must use those coordinates as a starting point for our Pan/Tilt tracking system</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*-udqFJ5gRQNdBqde." /></figure><h3>10. Object Position Tracking System</h3><p>We want that our object stays always centered on the screen. So, let’s define for example, that we will consider our object “centered” if:</p><ul><li>220 &lt; x &lt; 280</li><li>160 &lt; y &lt; 210</li></ul><p>Outside of those boundaries, we must move our Pan/Tilt mechanism to compensate deviation. Based on that, we can build the function <em>mapServoPosition(x, y)</em> as below. Note that the “x” and “y” used as parameters in this function are the same that we have used before for printing central position:</p><pre># position servos to present object at center of the frame<br>def mapServoPosition (x, y):<br>    global panAngle<br>    global tiltAngle<br>    if (x &lt; 220):<br>        panAngle += 10<br>        if panAngle &gt; 140:<br>            panAngle = 140<br>        positionServo (panServo, panAngle)<br>    if (x &gt; 280):<br>        panAngle -= 10<br>        if panAngle &lt; 40:<br>            panAngle = 40<br>        positionServo (panServo, panAngle)<br>    if (y &lt; 160):<br>        tiltAngle += 10<br>        if tiltAngle &gt; 140:<br>            tiltAngle = 140<br>        positionServo (tiltServo, tiltAngle)<br>    if (y &gt; 210):<br>        tiltAngle -= 10<br>        if tiltAngle &lt; 40:<br>            tiltAngle = 40<br>        positionServo (tiltServo, tiltAngle)</pre><p>Based on the (x, y) coordinates, servo position commands are generated, using the function <em>positionServo(servo, angle). </em>For example, suppose that y position is “50”, what means that our object is almost in the top of the screen, that can be translated that out “camera sight” is “low” (let’s say a tilt angle of 120 degrees) So we must “decrease” Tilt angle (let’s say to 100 degrees), so the camera sight will be “up” and the object will go “down” on screen (y will increase to let’s say, 190).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*ge-flqm6YV3bHizd." /></figure><p>The above diagram shows the example in terms of geometry.</p><p>Think how the Pan camera will operate. note that the screen is not mirroring, what means that if you move the object to “your left”, it will move on screen for “your right”, once you are in opposition to the camera.</p><p>The function positionServo(servo, angle) can be written as:</p><pre>def positionServo (servo, angle):<br>    os.system(&quot;python angleServoCtrl.py &quot; + str(servo) + &quot; &quot; + str(angle))<br>    print(&quot;[INFO] Positioning servo at GPIO {0} to {1} degrees\n&quot;.format(servo, angle))</pre><p>We will be calling the script shown before for servo positioning.</p><p>Note that angleServoCtrl.py must be in the same directory as objectDetectTrac.py</p><p>The complete code can be download from my GitHub: <a href="https://github.com/Mjrovai/OpenCV-Object-Face-Tracking/blob/master/Object_Tracking/objectDetectTrack.py">objectDetectTrack.py</a></p><p>Below gif shows an example of our project working:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*CsE_19QURjVVXlWg." /></figure><h3>11. Conclusion</h3><p>As always, I hope this project can help others find their way into the exciting world of electronics!</p><p>For details and final code, please visit my GitHub depository: <a href="https://github.com/Mjrovai/OpenCV-Object-Face-Tracking">OpenCV-Object-Face-Tracking</a></p><p>For more projects, please visit my blog: <a href="https://mjrobot.org/">MJRoBot.org</a></p><p>Below a glimpse of my next tutorial, where we will explore “Face track and detection”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*3i4UQyVSQRHR2rc8." /></figure><p>Saludos from the south of the world!</p><p>See you in my next tutorial!</p><p>Thank you,</p><p>Marcelo</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2dc6b4acaff5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mjrobot-org/automatic-vision-object-tracking-2dc6b4acaff5">Automatic Vision Object Tracking</a> was originally published in <a href="https://medium.com/mjrobot-org">MJRoBot.org</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Raspberry Pi Cam Pan-Tilt Controlled Over Local Internet]]></title>
            <link>https://medium.com/mjrobot-org/raspberry-pi-cam-pan-tilt-controlled-over-local-internet-65b5087a9bc5?source=rss----9c5924ccca32---4</link>
            <guid isPermaLink="false">https://medium.com/p/65b5087a9bc5</guid>
            <category><![CDATA[cameras]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[servo]]></category>
            <category><![CDATA[flask]]></category>
            <category><![CDATA[raspberry-pi]]></category>
            <dc:creator><![CDATA[Marcelo Rovai]]></dc:creator>
            <pubDate>Fri, 15 Jun 2018 20:31:22 GMT</pubDate>
            <atom:updated>2018-06-15T20:31:21.962Z</atom:updated>
            <cc:license>http://creativecommons.org/publicdomain/zero/1.0/</cc:license>
            <content:encoded><![CDATA[<h4>Pan-Tilt Camera position controlled over local internet, using Flask and Python.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/864/0*nLEwrxmtDA3yvI9R." /></figure><h3>1. Introduction</h3><p>On previous tutorials we explored:</p><ul><li>How to stream to the internet a video captured by a camera: <a href="https://medium.com/@rovai/video-streaming-web-server-2153c4ce825">VIDEO STREAMING WEB SERVER</a></li><li>How to position a camera (or anything) using: <a href="https://medium.com/@rovai/pan-tilt-multi-servo-control-62f723d03f26">PAN-TILT MULTI SERVO CONTROL</a></li></ul><p>In this tutorial, we will combine what we have learned before, controlling our camera position thru internet, as shown in the example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*42VGdTgr_4XJhoKa." /></figure><p>The above gif shows the camera controlled by buttons, pre-programmed with fixed Pan/Tilt angles. In this tutorial, we will also explore other alternatives to control the camera position thru internet.</p><p>Below the block diagram of our project:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*HR5yrJFcXaz_cVKy." /></figure><h3>2. Installing the PiCam</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*OHtDAqBY1N2aGOy2." /></figure><p>With your RPi turned-off, install the Camara on its special port as shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*rP50GH1gdTpvhHXa." /></figure><p>Turn on your Pi and go to <em>Raspberry Pi Configuration Tool</em> at main menu and verify if Camera Interface is enabled:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/492/0*Oi-vWBLmkyuI0ie5." /></figure><p>If you needed to Enabled it, press [OK] and reboot your Pi. Make a simple test to verify if everything is OK:</p><pre>raspistill -o /Desktop/image.png</pre><p>You will realize that an image icon appears on your Rpi desktop. Click on it to open. If an image appears, your Pi is ready to stream video! If you want to know more about the camera, visit the link: <a href="https://projects.raspberrypi.org/en/projects/getting-started-with-picamera">Getting started with picamera</a>.</p><h3>3. Instaling Flask</h3><p>There are several ways to stream video. The best (and “lighther”) way to do it that I found was with Flask, as developed by Miguel Grinberg. For a detailed explanation about how Flask does this, please see his great tutorial: <a href="https://blog.miguelgrinberg.com/post/flask-video-streaming-revisited">flask-video-streaming-revisited</a>.</p><p>On my tutorial:<a href="https://medium.com/@rovai/python-webserver-with-flask-and-raspberry-pi-398423cc6f5d"> Python WebServer With Flask and Raspberry Pi</a>, we learned in more details how Flask works and how to implement a web-server to capture data from sensors and show their status on a web page. Here, on the first part of this tutorial, we will do the same, only that the data to be sent to our front end, will be a <em>video stream</em>.</p><h4>Creating a web-server environment:</h4><p>The first thing to do is to install Flask on your Raspberry Pi. If you do not have it yet, go to the Pi Terminal and enter:</p><pre>sudo apt-get install python3-flask</pre><p>The best when you start a new project is to create a folder where to have your files organized. For example:</p><p>From home, go to your working directory:</p><pre>cd Documents</pre><p>Create a new folder, for example:</p><pre>mkdir camWebServer</pre><p>The above command will create a folder named “camWebServer”, where we will save our python scripts:</p><pre>/home/pi/Documents/camWebServer</pre><p>Now, on this folder, let’s create 2 sub-folders: <em>static</em> for CSS and eventually JavaScript files and <em>templates </em>for HTML files<em>.</em> Go to your newer created folder:</p><pre>cd camWebServer</pre><p>And create the 2 new sub-folders:</p><pre>mkdir static</pre><p>and</p><pre>mkdir templates</pre><p>The final directory “tree”, will look like:</p><pre>├── Documents<br>       ├── camWebServer<br>               ├── templates<br>               └── static</pre><p>OK! With our environment in place let’s create our <em>Python WebServer Application to stream video.</em></p><h3>4. Creating the Video Streaming Server</h3><p>First, download Miguel Grinberg’s picamera package: <a href="https://github.com/Mjrovai/Video-Streaming-with-Flask/blob/master/camWebServer/camera_pi.py">camera_pi.py</a> and save it on created directory camWebServer. This is the heart of our project, Miguel did a fantastic job!</p><p>Now, using Flask, let’s change the original Miguel’s web Server application (<em>app.py</em>), creating a specific python script to render our video. We will call it <a href="https://github.com/Mjrovai/Video-Streaming-with-Flask/blob/master/camWebServer/appCam.py">appCam.py</a>:</p><pre>from flask import Flask, render_template, Response</pre><pre># Raspberry Pi camera module (requires picamera package, developed by Miguel Grinberg)<br>from camera_pi import Camera</pre><pre>app = Flask(__name__)</pre><pre>@app.route(&#39;/&#39;)<br>def index():<br>    &quot;&quot;&quot;Video streaming home page.&quot;&quot;&quot;<br>    return render_template(&#39;index.html&#39;)</pre><pre>def gen(camera):<br>    &quot;&quot;&quot;Video streaming generator function.&quot;&quot;&quot;<br>    while True:<br>        frame = camera.get_frame()<br>        yield (b&#39;--frame\r\n&#39;<br>               b&#39;Content-Type: image/jpeg\r\n\r\n&#39; + frame + b&#39;\r\n&#39;)</pre><pre>@app.route(&#39;/video_feed&#39;)<br>def video_feed():<br>    &quot;&quot;&quot;Video streaming route. Put this in the src attribute of an img tag.&quot;&quot;&quot;<br>    return Response(gen(Camera()),<br>                    mimetype=&#39;multipart/x-mixed-replace; boundary=frame&#39;)</pre><pre>if __name__ == &#39;__main__&#39;:<br>    app.run(host=&#39;0.0.0.0&#39;, port =80, debug=True, threaded=True)</pre><p>The above script streams your camera video on an index.html page as below:</p><pre>&lt;html&gt;<br>  &lt;head&gt;<br>    &lt;title&gt;MJRoBot Lab Live Streaming&lt;/title&gt;<br>    &lt;link rel=&quot;stylesheet&quot; href=&#39;../static/style.css&#39;/&gt;<br>  &lt;/head&gt;<br>  &lt;body&gt;<br>    &lt;h1&gt;MJRoBot Lab Live Streaming&lt;/h1&gt;<br>    &lt;h3&gt;&lt;img src=&quot;{{ url_for(&#39;video_feed&#39;) }}&quot; width=&quot;90%&quot;&gt;&lt;/h3&gt;<br>    &lt;hr&gt;<br>    &lt;p&gt; @2018 Developed by MJRoBot.org&lt;/p&gt;<br>  &lt;/body&gt;<br>&lt;/html&gt;</pre><p>The most important line of index.html is:</p><pre>&lt;img src=&quot;{{ url_for(&#39;video_feed&#39;) }}&quot; width=&quot;50%&quot;&gt;</pre><p>There is where the video will be “feed” to our web page.</p><p>You must also include the style.css file on the static directory to get the above result in terms of style.</p><p>All the files can be downloaded from my GitHub: <a href="https://github.com/Mjrovai/Video-Streaming-with-Flask/tree/master/camWebServer">camWebServer</a></p><p>Only to be sure that everything is in the right location, let’s check our environment after all updates:</p><pre>├── Documents<br>       └── camWebServer<br>               ├── camera_pi.py<br>               ├── appCam.py<br>               ├── templates<br>               |     └── index.html<br>               └── static<br>                     └── style.css</pre><p>Now, run the python script on the Terminal:</p><pre>sudo python3 appCam.py</pre><p>Go to any browser in your network and enter with<em> </em><em>http://YOUR_RPI_IP </em>(for example, in my case: 10.0.1.27)</p><p>NOTE: If you are not sure about your RPi IP address, run on your terminal:</p><pre>ifconfig</pre><p>at wlan0: section you will find it.</p><p>The results:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*ny2zWB5jovVOtOiN." /></figure><p>That’s it! From now it is only a matter to sophisticate a page, embedded your video on another page etc.</p><h3>5. The Pan Tilt Mechanism</h3><p>Now that we have the camera working and our Flask WebServer streaming its video, let’s install our Pan/tilt mechanism to position the camera remotely.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*w4G5Ymj1Z4HIZkEf." /></figure><p>For details, please visit my tutorial: <a href="https://www.hackster.io/mjrobot/pan-tilt-multi-servo-control-b67791">Pan-Tilt-Multi-Servo-Control</a></p><p>The servos should be connected to an external 5V supply, having their data pin (in my case, their yellow wiring) connect to Raspberry Pi GPIO as below:</p><ul><li>GPIO 17 ==&gt; Tilt Servo</li><li>GPIO 27 ==&gt; Pan Servo</li></ul><p>Do not forget to connect the GNDs together ==&gt; Raspberry Pi — Servos — External Power Supply)</p><p>You can have as an option, a resistor of 1K ohm in series, between Raspberry Pi GPIO and Server data input pin. This would protect your RPi in case of a servo problem.</p><p>Let’s also use the opportunity and test our servos inside our Virtual Python Environment.</p><p>Let’s use Python script to execute some tests with our drivers:</p><pre>from time import sleep<br>import RPi.GPIO as GPIO<br>GPIO.setmode(GPIO.BCM)<br>GPIO.setwarnings(False)</pre><pre>def setServoAngle(servo, angle):<br>	pwm = GPIO.PWM(servo, 50)<br>	pwm.start(8)<br>	dutyCycle = angle / 18. + 3.<br>	pwm.ChangeDutyCycle(dutyCycle)<br>	sleep(0.3)<br>	pwm.stop()</pre><pre>if __name__ == &#39;__main__&#39;:<br>	import sys<br>	servo = int(sys.argv[1])<br>	GPIO.setup(servo, GPIO.OUT)<br>	setServoAngle(servo, int(sys.argv[2]))<br>	GPIO.cleanup()</pre><p>The core of above code is the function setServoAngle(servo, angle). This function receives as arguments, a servo GPIO number, and an angle value to where the servo must be positioned. Once the input of this function is “angle”, we must convert it to an equivalent duty cycle.</p><p>To execute the script, you must enter as parameters, <em>servo GPIO</em>, and <em>angle</em>.</p><p>For example:</p><pre>sudo python angleServoCtrl.py 17 45</pre><p>The above command will position the servo connected on GPIO 17 (“tilt”) with 45 degrees in “elevation”. A similar command could be used for Pan Servo control (position to 45 degrees in “azimuth”:</p><pre>sudo python angleServoCtrl.py 27 45</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/364/0*5343I-r2oEolgYYj." /></figure><p>The file <a href="https://github.com/Mjrovai/OpenCV-Object-Face-Tracking/blob/master/angleServoCtrl.py">angleServoCtrl.py </a>can be downloaded from my GitHub</p><h3>6. Controlling Camera Position Via Web — Using Buttons</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/416/0*p7scf9qfhTVPxusO." /></figure><p>Let’s start creating a new directory calling it:</p><pre>mkdir PanTiltControl1</pre><p>On this directory we should have the following environment and files:</p><pre>├── Documents<br>       └── PanTiltControl1<br>               ├── camera_pi.py<br>	       ├── angleServoCtrl.py<br>               ├── appCamPanTilt1.py<br>               ├── templates<br>               |     └── index.html<br>               └── static<br>                     └── style.css</pre><p>The file <a href="https://github.com/Mjrovai/WebCam-Pan-Tilt-Control-via-Flask/blob/master/PanTiltControl1/camera_pi.py">camera_pi.py</a> is Miguel’s script and<a href="https://github.com/Mjrovai/WebCam-Pan-Tilt-Control-via-Flask/blob/master/PanTiltControl1/angleServoCtrl.py"> angleServoCtrl.py</a>, both used before. You can download both from my GitHub, clicking on correspondent links.</p><p>Now we need the <a href="https://github.com/Mjrovai/WebCam-Pan-Tilt-Control-via-Flask/blob/master/PanTiltControl1/appCamPanTilt1.py">appCamPanTilt1.py</a>, the <a href="https://github.com/Mjrovai/WebCam-Pan-Tilt-Control-via-Flask/blob/master/PanTiltControl1/templates/index.html">index.html</a> and <a href="https://github.com/Mjrovai/WebCam-Pan-Tilt-Control-via-Flask/blob/master/PanTiltControl1/static/style.css">style.css</a>. You can download those files from my GitHub, clicking on correspondent links. Pay attention to its correct position on your directory.</p><p>Let’s see the index.html:</p><pre>&lt;html&gt;<br>  &lt;head&gt;<br>    &lt;title&gt;MJRoBot Lab Live Streaming&lt;/title&gt;<br>    &lt;link rel=&quot;stylesheet&quot; href=&#39;../static/style.css&#39;/&gt;<br>  &lt;/head&gt;<br>  &lt;body&gt;<br>    &lt;h1&gt;MJRoBot Lab Live Streaming&lt;/h1&gt;<br>    &lt;h3&gt;&lt;img src=&quot;{{ url_for(&#39;video_feed&#39;) }}&quot; width=&quot;80%&quot;&gt;&lt;/h3&gt;<br>	&lt;hr&gt;<br>	&lt;h4&gt; PAN:  <br>		&lt;a href=&quot;/pan/30&quot;class=&quot;button&quot;&gt;30&lt;/a&gt;<br>		&lt;a href=&quot;/pan/45&quot;class=&quot;button&quot;&gt;45&lt;/a&gt;<br>		&lt;a href=&quot;/pan/60&quot;class=&quot;button&quot;&gt;60&lt;/a&gt;<br>		&lt;a href=&quot;/pan/75&quot;class=&quot;button&quot;&gt;75&lt;/a&gt;<br>		&lt;a href=&quot;/pan/90&quot;class=&quot;button&quot;&gt;90&lt;/a&gt;<br>		&lt;a href=&quot;/pan/105&quot;class=&quot;button&quot;&gt;105&lt;/a&gt;<br>		&lt;a href=&quot;/pan/120&quot;class=&quot;button&quot;&gt;120&lt;/a&gt;<br>		&lt;a href=&quot;/pan/135&quot;class=&quot;button&quot;&gt;135&lt;/a&gt;<br>		&lt;a href=&quot;/pan/150&quot;class=&quot;button&quot;&gt;150&lt;/a&gt;<br>		==&gt; Angle: [ {{ panServoAngle }} ]<br>	&lt;/h4&gt;<br>		&lt;h4&gt; TILT: <br>		&lt;a href=&quot;/tilt/30&quot;class=&quot;button&quot;&gt;30&lt;/a&gt;<br>		&lt;a href=&quot;/tilt/45&quot;class=&quot;button&quot;&gt;45&lt;/a&gt;		<br>		&lt;a href=&quot;/tilt/60&quot;class=&quot;button&quot;&gt;60&lt;/a&gt;<br>		&lt;a href=&quot;/tilt/75&quot;class=&quot;button&quot;&gt;75&lt;/a&gt;		<br>		&lt;a href=&quot;/tilt/90&quot;class=&quot;button&quot;&gt;90&lt;/a&gt;<br>		&lt;a href=&quot;/tilt/105&quot;class=&quot;button&quot;&gt;105&lt;/a&gt;		<br>		&lt;a href=&quot;/tilt/120&quot;class=&quot;button&quot;&gt;120&lt;/a&gt;<br>		&lt;a href=&quot;/tilt/135&quot;class=&quot;button&quot;&gt;135&lt;/a&gt;		<br>		&lt;a href=&quot;/tilt/150&quot;class=&quot;button&quot;&gt;150&lt;/a&gt;<br>		==&gt; Angle: [ {{ tiltServoAngle }} ]<br>	&lt;/h4&gt;<br>	&lt;hr&gt;<br>	&lt;p&gt; @2018 Developed by MJRoBot.org&lt;/p&gt;<br>  &lt;/body&gt;<br>&lt;/html&gt;</pre><p>The index.html was created from the previous file, used when we streamed our video. The new bunch of lines on this new file is related to each one of the buttons that appear on the page.</p><p>Let’s analyze one of them:</p><pre>&lt;a href=&quot;/pan/30&quot;class=&quot;button&quot;&gt;30&lt;/a&gt;</pre><p>This is a simple HTML hyperlink TAG, that we have styled as a button (the button style is described in style.css). When we click on this link, we generate a “GET /&lt;servo&gt;/&lt;angle&gt;”, where &lt;servo&gt; is “pan” and &lt;angle&gt; is “30 degrees”. Those parameters will be passed to the Web Server App (appCamPanTilt1.py).</p><p>Let’s see this part of code on appCamPanTilt1.py:</p><pre>@app.route(&quot;/&lt;servo&gt;/&lt;angle&gt;&quot;)<br>def move(servo, angle):<br>	global panServoAngle<br>	global tiltServoAngle<br>	if servo == &#39;pan&#39;:<br>		panServoAngle = int(angle)<br>		os.system(&quot;python3 angleServoCtrl.py &quot; + str(panPin) + &quot; &quot; + str(panServoAngle))<br>	if servo == &#39;tilt&#39;:<br>		tiltServoAngle = int(angle)<br>		os.system(&quot;python3 angleServoCtrl.py &quot; + str(tiltPin) + &quot; &quot; + str(tiltServoAngle))<br>	<br>	templateData = {<br>      &#39;panServoAngle&#39;	: panServoAngle,<br>      &#39;tiltServoAngle&#39;	: tiltServoAngle<br>	}<br>	return render_template(&#39;index.html&#39;, **templateData)</pre><p>In this example, “servo” is equal to “pan”, and the 2 lines below will be executed:</p><pre>panServoAngle = int(angle)<br>os.system(&quot;python3 angleServoCtrl.py &quot; + str(panPin) + &quot; &quot; + str(panServoAngle))</pre><p>What we are doing here is the same as we did when we tested the servo position on Pi Terminal. PanPin will be translated by “27” and panServoAngle to “30”. The app will generate the command:</p><pre>python3 angleServoCtrl 27 30</pre><blockquote>Note that we do not need use “sudo” in this case, because the app was already started using “sudo”.</blockquote><p>The video shows the project working and how the GET requests appear on Pi Terminal:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FlQ5vusgtE1g%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DlQ5vusgtE1g&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FlQ5vusgtE1g%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/981bf2f8948c3a35a223e89314e2684a/href">https://medium.com/media/981bf2f8948c3a35a223e89314e2684a/href</a></iframe><h3>7. Using Incremental — Decremental Angle Buttons</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/399/0*VVqEQoc2eNp1Yjn0." /></figure><p>Sometimes what we only need is a few buttons to move our servos in steps:</p><ul><li>Pan: Left / Right</li><li>Tilt: Up / Down</li></ul><p>We can also use +/- buttons (Incremental — decremental angle), your choice. Let’s create a new directory:</p><pre>mkdir PanTiltControl2</pre><p>On this directory we should have the following environment and files:</p><pre>├── Documents<br>       └── PanTiltControl2<br>               ├── camera_pi.py<br>	       ├── angleServoCtrl.py<br>               ├── appCamPanTilt2.py<br>               ├── templates<br>               |     └── index.html<br>               └── static<br>                     └── style.css</pre><p>The files <a href="https://github.com/Mjrovai/WebCam-Pan-Tilt-Control-via-Flask/blob/master/PanTiltControl1/camera_pi.py">camera_pi.py</a> and<a href="https://github.com/Mjrovai/WebCam-Pan-Tilt-Control-via-Flask/blob/master/PanTiltControl1/angleServoCtrl.py"> angleServoCtrl.py</a> are the same used before. You can download both from my GitHub, clicking on correspondent links or use the ones that you have downloaded before.</p><p>Now we need the <a href="https://github.com/Mjrovai/WebCam-Pan-Tilt-Control-via-Flask/blob/master/PanTiltControl2/appCamPanTilt2.py">appCamPanTilt2.py</a>, the <a href="https://github.com/Mjrovai/WebCam-Pan-Tilt-Control-via-Flask/blob/master/PanTiltControl2/templates/index.html">index.html</a> and <a href="https://github.com/Mjrovai/WebCam-Pan-Tilt-Control-via-Flask/blob/master/PanTiltControl1/static/style.css">style.css</a>. You can download those files from my GitHub, clicking on correspondent links. Pay attention to its correct position on your directory.</p><p>Let’s see the NEW index.html:</p><pre>&lt;html&gt;<br>  &lt;head&gt;<br>    &lt;title&gt;MJRoBot Lab Live Streaming&lt;/title&gt;<br>    &lt;link rel=&quot;stylesheet&quot; href=&#39;../static/style.css&#39;/&gt;<br>  &lt;/head&gt;<br>  &lt;body&gt;<br>    &lt;h1&gt;MJRoBot Lab Live Streaming&lt;/h1&gt;<br>    &lt;h3&gt;&lt;img src=&quot;{{ url_for(&#39;video_feed&#39;) }}&quot; width=&quot;80%&quot;&gt;&lt;/h3&gt;<br>	&lt;hr&gt;<br>	&lt;h4&gt; PAN Angle: &lt;a href=&quot;/pan/-&quot;class=&quot;button&quot;&gt;-&lt;/a&gt; [ {{ panServoAngle }} ] &lt;a href=&quot;/pan/+&quot;class=&quot;button&quot;&gt;+&lt;/a&gt; &lt;/h4&gt;<br>	&lt;h4&gt; TILT Angle: &lt;a href=&quot;/tilt/-&quot;class=&quot;button&quot;&gt;-&lt;/a&gt; [ {{ tiltServoAngle }} ] &lt;a href=&quot;/tilt/+&quot;class=&quot;button&quot;&gt;+&lt;/a&gt; &lt;/h4&gt; <br>	&lt;hr&gt;<br>	&lt;p&gt; @2018 Developed by MJRoBot.org&lt;/p&gt;<br>  &lt;/body&gt;<br>&lt;/html&gt;</pre><p>The index.html is very similar to the previous one. The bunch of lines used on the last index.html was replaced by only 2 lines, where we will only have now 4 buttons Pan [+], Pan [-], Tilt [+] and Tilt [-].</p><p>Let’s analyze one of the 4 buttons:</p><pre>&lt;a href=&quot;/pan/-&quot;class=&quot;button&quot;&gt;-&lt;/a&gt;</pre><p>This is also a simple HTML hyperlink TAG, that we have styled as a button (the button style is described in style.css). When we click on this link, we generate a “GET /&lt;servo&gt;/&lt;Increment or decrement angle&gt;”, where &lt;servo&gt; is “pan” and &lt;-&gt; is “decrease angle”. Those parameters will be passed to the Web Server App (appCamPanTilt2.py).</p><p>Let’s see this part of code on appCamPanTilt2.py:</p><pre>@app.route(&quot;/&lt;servo&gt;/&lt;angle&gt;&quot;)<br>def move(servo, angle):<br>	global panServoAngle<br>	global tiltServoAngle<br>	if servo == &#39;pan&#39;:<br>		if angle == &#39;+&#39;:<br>			panServoAngle = panServoAngle + 10<br>		else:<br>			panServoAngle = panServoAngle - 10<br>		os.system(&quot;python3 angleServoCtrl.py &quot; + str(panPin) + &quot; &quot; + str(panServoAngle))<br>	if servo == &#39;tilt&#39;:<br>		if angle == &#39;+&#39;:<br>			tiltServoAngle = tiltServoAngle + 10<br>		else:<br>			tiltServoAngle = tiltServoAngle - 10<br>		os.system(&quot;python3 angleServoCtrl.py &quot; + str(tiltPin) + &quot; &quot; + str(tiltServoAngle))<br>	<br>	templateData = {<br>      &#39;panServoAngle&#39;	: panServoAngle,<br>      &#39;tiltServoAngle&#39;	: tiltServoAngle<br>	}<br>	return render_template(&#39;index.html&#39;, **templateData)</pre><p>In this example, “servo” is equal to “pan”, the lines below will be executed:</p><pre>if angle == &#39;+&#39;:<br>	panServoAngle = panServoAngle + 10<br>else:<br>	panServoAngle = panServoAngle - 10<br>os.system(&quot;python3 angleServoCtrl.py &quot; + str(panPin) + &quot; &quot; + str(panServoAngle))</pre><p>Once the “angle” is equal to “-”, we will decrease 10 from panServoAngle and pass this parameter to our command. Suppose that the actualpanServoAngle is 90. The new parameter will be 80.</p><p>So, PanPin will be translated by “27” and panServoAngle to “80”. The app will generate the command:</p><pre>python3 angleServoCtrl 27 80</pre><blockquote>Note that we do not need use “sudo” in this case, because the app was already started using “sudo”.</blockquote><p>The gif shows the webpage working :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*3XGmz_Lhq02TBwwQ." /></figure><h3>8. Using “POST” Approach</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/396/0*NzCi0G354ukwqSCh." /></figure><p>Sometimes could be interesting to send specific angle commands like:</p><ul><li>Pan Angle ==&gt; 35 degrees</li><li>Tilt Angle ==&gt; 107 degrees</li></ul><p>What means that or you will define your increment to “1” on the last step, what will take a lot of time to reach the required position, or you create a POST from your webpage. Let’s explore this last possibility.</p><p>Let’s again create a new directory:</p><pre>mkdir PanTiltControl3</pre><p>On this directory we should have the following environment and files:</p><pre>├── Documents<br>       └── PanTiltControl3<br>               ├── camera_pi.py<br>	       ├── angleServoCtrl.py<br>               ├── appCamPanTilt3.py<br>               ├── templates<br>               |     └── index.html<br>               └── static<br>                     └── style.css</pre><p>The files <a href="https://github.com/Mjrovai/WebCam-Pan-Tilt-Control-via-Flask/blob/master/PanTiltControl1/camera_pi.py">camera_pi.py</a> and<a href="https://github.com/Mjrovai/WebCam-Pan-Tilt-Control-via-Flask/blob/master/PanTiltControl1/angleServoCtrl.py"> angleServoCtrl.py</a> are the same used before. You can download both from my GitHub, clicking on correspondent links or use the ones that you have downloaded before.</p><p>Now we need the <a href="https://github.com/Mjrovai/WebCam-Pan-Tilt-Control-via-Flask/blob/master/PanTiltControl3/appCamPanTilt3.py">appCamPanTilt3.py</a>, the <a href="https://github.com/Mjrovai/WebCam-Pan-Tilt-Control-via-Flask/blob/master/PanTiltControl3/templates/index.html">index.html</a> and <a href="https://github.com/Mjrovai/WebCam-Pan-Tilt-Control-via-Flask/blob/master/PanTiltControl1/static/style.css">style.css</a>. You can download those files from my GitHub, clicking on correspondent links. Pay attention to its correct position on your directory.</p><p>Let’s see the NEW index.html:</p><pre>&lt;html&gt;<br>  &lt;head&gt;<br>    &lt;title&gt;MJRoBot Lab Live Streaming&lt;/title&gt;<br>    &lt;link rel=&quot;stylesheet&quot; href=&#39;../static/style.css&#39;/&gt;<br>  &lt;/head&gt;<br>  &lt;body&gt;<br>    &lt;h1&gt;MJRoBot Lab Live Streaming&lt;/h1&gt;<br>    &lt;h3&gt;&lt;img src=&quot;{{ url_for(&#39;video_feed&#39;) }}&quot; width=&quot;80%&quot;&gt;&lt;/h3&gt;<br>    &lt;p&gt; Enter Pan Tilt Servo Angle:<br>	&lt;form method=&quot;POST&quot;&gt;<br>		PAN:  &lt;input type=&quot;text&quot; name=&quot;panServoAngle&quot; value= {{panServoAngle}} size=&quot;3&quot;&gt;<br>		TILT: &lt;input type=&quot;text&quot; name=&quot;tiltServoAngle&quot; value= {{tiltServoAngle}} size=&quot;3&quot;&gt;<br>		&lt;input type=&quot;submit&quot;&gt;<br>	&lt;/form&gt;<br>   &lt;/p&gt;<br>   &lt;hr&gt;<br>	&lt;p&gt; @2018 Developed by MJRoBot.org&lt;/p&gt;<br>  &lt;/body&gt;<br>&lt;/html&gt;</pre><p>The index.html now is a little bit different from the previous one. We will create here a “Form”, which method will be POST. An HTML form input TAG, will be used to pass as parameters the Pan/Tilt angle values digited by users. Those parameters will be passed to the Web Server App (appCamPanTilt3.py) when the button “submit” is pressed.</p><p>Let’s see this part of code on appCamPanTilt3.py:</p><pre>@app.route(&#39;/&#39;, methods=[&#39;POST&#39;])<br>def my_form_post():<br>	global panServoAngle<br>	global tiltServoAngle<br>	panNewAngle = int(request.form[&#39;panServoAngle&#39;])<br>	if (panNewAngle != panServoAngle):<br>		panServoAngle = panNewAngle<br>		os.system(&quot;python3 angleServoCtrl.py &quot; + str(panPin) + &quot; &quot; + str(panServoAngle))<br>	tiltNewAngle = int(request.form[&#39;tiltServoAngle&#39;])<br>	if (tiltNewAngle != tiltServoAngle):<br>		tiltServoAngle = tiltNewAngle<br>		os.system(&quot;python3 angleServoCtrl.py &quot; + str(tiltPin) + &quot; &quot; + str(tiltServoAngle))<br>	templateData = {<br>      &#39;panServoAngle&#39;	: panServoAngle,<br>      &#39;tiltServoAngle&#39;	: tiltServoAngle<br>	}<br>	return render_template(&#39;index.html&#39;, **templateData)</pre><p>What we will do is to check which angle was changed generating the correspondent command similar what were done before.</p><h3>9. Conclusion</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/442/0*kmZyW_1et5RTnq11." /></figure><p>As always, I hope this project can help others find their way into the exciting world of electronics!</p><p>For details and final code, please visit my GitHub depository: <a href="https://github.com/Mjrovai/WebCam-Pan-Tilt-Control-via-Flask">WebCam-Pan-Tilt-Control-via-Flask</a></p><p>For more projects, please visit my blog: <a href="https://mjrobot.org/">MJRoBot.org</a></p><p>Below a glimpse of my next tutorial, where we will explore how to control our Pan-Tilt, but not with buttons, but with our face! ;-)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*9TtxpoPBe1rJ2Y-W." /></figure><p>Saludos from the south of the world!</p><p>See you in my next tutorial!</p><p>Thank you,</p><p>Marcelo</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=65b5087a9bc5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mjrobot-org/raspberry-pi-cam-pan-tilt-controlled-over-local-internet-65b5087a9bc5">Raspberry Pi Cam Pan-Tilt Controlled Over Local Internet</a> was originally published in <a href="https://medium.com/mjrobot-org">MJRoBot.org</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Pan-Tilt Multi Servo Control]]></title>
            <link>https://medium.com/mjrobot-org/pan-tilt-multi-servo-control-282adaa13a73?source=rss----9c5924ccca32---4</link>
            <guid isPermaLink="false">https://medium.com/p/282adaa13a73</guid>
            <category><![CDATA[physical-computing]]></category>
            <category><![CDATA[robotics]]></category>
            <category><![CDATA[raspberry-pi]]></category>
            <category><![CDATA[servo]]></category>
            <dc:creator><![CDATA[Marcelo Rovai]]></dc:creator>
            <pubDate>Fri, 15 Jun 2018 20:22:20 GMT</pubDate>
            <atom:updated>2018-06-15T20:22:19.749Z</atom:updated>
            <cc:license>http://creativecommons.org/publicdomain/zero/1.0/</cc:license>
            <content:encoded><![CDATA[<h4>Multiple servos control, using Python and a PAN/TILT mechanism construction to PiCam positioning.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/0*PiguMNf0t0qXkltH." /></figure><h3>1. Introduction</h3><p>On this tutorial, we will explore how to control multiple servos using Python on a Raspberry Pi. Our goal will be a PAN/TILT mechanism to position a camera (a PiCam).</p><p>Here you can see how our final project will work:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*4vIdVxlktYxsP4ef." /></figure><h3>2. How PWM Works</h3><p>The Raspberry Pi has no analog output, but we can simulate this, using a PWM (Pulse Width Modulation) approach. What we will do is to generate a digital signal with a fixed frequency, where we will change the pulse train width, what will be “translated” as an “average” output voltage level as shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/561/0*WMEN1K2dAN5i4wqv." /></figure><p>We can use this “average” voltage level to control a LED brightness for example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*8aKMKfT-FwswIeyx." /></figure><p>Note that what matters here is not the frequency itself, but the “Duty Cycle”, that is the relation between the time that the puls is “high” divided by the wave period. For example, suppose that we will generating a 50Hz pulse frequency on one of our Raspberry Pi GPIO. The period (p) will the inverse of frequency or 20ms (1/f). If we want that our LED with a “half” bright, we must have a Duty Cycle of 50%, that means a “pulse” that will be “High” for 10ms.</p><p>This principle will be very important for us, to control our servo position, once the “Duty Cycle” will define the servo position as shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/0*p9On-Z7oRknCXAz1." /></figure><h3>3. Installing the Hw</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*9DJ9lFRwQqKg4JNE." /></figure><p>The servos will be connected to an external 5V supply, having their data pin (in my case, their yellow wiring) connect to Raspberry Pi GPIO as below:</p><ul><li>GPIO 17 ==&gt; Tilt Servo</li><li>GPIO 27 ==&gt; Pan Servo</li></ul><p>Do not forget to connect the GNDs together ==&gt; Raspberry Pi — Servos — External Power Supply)</p><blockquote>You can have as an option, a resistor of 1K ohm between Raspberry Pi GPIO and Server data input pin. This would protect your RPi in case of a servo problem.</blockquote><h3>4. Servos Calibration</h3><p>The first thing to do it is to confirm the main characteristics of your servos. In my case, I am using a <a href="http://akizukidenshi.com/download/ds/towerpro/SG90.pdf">Power Pro SG90</a>.</p><p><a href="https://halckemy.s3.amazonaws.com/uploads/attachments/415505/SG90.pdf">SG90.pdf</a></p><p>From its datasheet, we can consider:</p><ul><li>Range: 180o</li><li>Power Supply: 4.8V (external 5VDC as a USB power supply works fine)</li><li>Working frequency: 50Hz (Period: 20 ms)</li><li>Pulse width: from 1ms to 2ms</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*nawllUNfn9l5_Nua." /></figure><p>In theory, the servo will be on its</p><ul><li>Initial<strong> Position</strong> (0 degrees) when a pulse of <strong>1ms</strong> is applied to its data terminal</li><li><strong>Neutral Position</strong> (90 degrees) when a pulse of 1.5 ms is applied to its data terminal</li><li>Final Position (180 degrees) when a pulse of 2 ms is applied to its data terminal</li></ul><p>To program a servo position using Python will be very important to know the correspondent “Duty Cycle” for the above positions, let’s do some calculation:</p><ul><li>Initial Position ==&gt; (0 degrees) Pulse width ==&gt; 1ms ==&gt; Duty Cycle = 1ms/20ms ==&gt; 2.0%</li><li>Neutral Position (90 degrees) Pulse width of 1.5 ms ==&gt; Duty Cycle = 1.5ms/20ms ==&gt; 7.5%</li><li>Final Position (180 degrees) Pulse width of 2 ms ==&gt; Duty Cycle = 2ms/20ms ==&gt; 10%</li></ul><p>So the Duty Cycle should vary on a range of 2 to 10 %.</p><p>Let’s test the servos individually. For that, open your Raspberry terminal and launch your Python 3 shell editor as “sudo” (because of you should be a “super user” to handle with GPIOs) :</p><pre>sudo python3</pre><p>On Python Shell:</p><pre>&gt;&gt;&gt;</pre><p>Import the RPI.GPIO module and call it GPIO:</p><pre>import RPi.GPIO as GPIO</pre><p>Define which pin-numbering schemes you want to use (BCM or BOARD). I did this test with BOARD, so the pins that I used were the physical pins (GPIO 17 = Pin 11 and GPIO 27 Pin 13). Was easy for me to identify them and not make mistakes during the test (In the final program I will use BCM). Choose the one of your preference:</p><pre>GPIO.setmode(GPIO.BOARD)</pre><p>Define the servo pin that you are using:</p><pre>tiltPin = 11</pre><p>If Instead, you have used BCM scheme, the last 2 commands should be replaced by:</p><pre>GPIO.setmode(GPIO.BCM)<br>tiltPin = 17</pre><p>Now, we must specify that this pin will be an “output”:</p><pre>GPIO.setup(tiltPin, GPIO.OUT)</pre><p>And, what will be the frequency generated on this pin, that for our servo will be 50Hz:</p><pre>tilt = GPIO.PWM(tiltPin, 50)</pre><p>Now, let’s start generating a PWM signal on the pin with an initial duty cycle (we will keep it “0”):</p><pre>tilt = start(0)</pre><p>Now, you can enter different duty cycle values, observing the movement of your servo. Let’s start with 2% and see what happens (we spect that the servo goes to “zero position”):</p><pre>tilt.ChangeDutyCycle(2)</pre><p>In my case, the servo went to zero position but when I changed the duty cycle to 3% i observed that the servo stayed in the same position, starting to move with duty cycles greater than 3%. So, 3% is my initial position (o degrees). The same happened with 10%, my servo went above this value, topping its end on 13%. So for this particular servo, the result was:</p><ul><li>0 degree ==&gt; duty cycle of 3%</li><li>90 degrees ==&gt; duty cycle of 8%</li><li>180 degrees ==&gt; duty cycle of 13%</li></ul><p>After you finish your tests, you must stop the PWM and clean up the GPIOs:</p><pre>tilt= stop()<br>GPIO.cleanup()</pre><p>The Terminal print screen shows the result for my tilt servo:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*BlKsDBoSBzQ5CpAh." /></figure><p>And here the result for the second servo, Pan:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/0*QrHNgB4FMZZW0cE_." /></figure><p>Note that both have similar results (Your range can be different).</p><h3>5. Creating a Python Script</h3><p>The PWM commands to be sent to our servo are in “duty cycles” as we saw on the last step. But usually, we must use “angle” in degrees as a parameter to control a servo. So, we must convert “angle” that is a more natural measurement to us in duty cycle as understandable by our Pi.</p><p>How to do it? Very simple! We know that duty cycle range goes from 3% to 13% and that this is equivalent to angles that will range from 0 to 180 degrees. Also, we know that those variations are linear, so we can construct a proportional schema as shown here:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/377/0*QpKC8-0KHTIuOH4N." /></figure><p>So, given an angle, we can have a correspondent duty cycle:</p><pre>dutycycle = angle/18 + 3</pre><p>Keep this formula. We will use it in the next code.</p><p>Let’s create a Python script to execute the tests. Basically, we will repeat what we did before on Python Shell:</p><pre>from time import sleep<br>import RPi.GPIO as GPIO<br>GPIO.setmode(GPIO.BCM)<br>GPIO.setwarnings(False)</pre><pre>def setServoAngle(servo, angle):<br>	pwm = GPIO.PWM(servo, 50)<br>	pwm.start(8)<br>	dutyCycle = angle / 18. + 3.<br>	pwm.ChangeDutyCycle(dutyCycle)<br>	sleep(0.3)<br>	pwm.stop()</pre><pre>if __name__ == &#39;__main__&#39;:<br>	import sys<br>	servo = int(sys.argv[1])<br>	GPIO.setup(servo, GPIO.OUT)<br>	setServoAngle(servo, int(sys.argv[2]))<br>	GPIO.cleanup()</pre><p>The core of above code is the function setServoAngle(servo, angle). This function receives as arguments, a servo GPIO number, and an angle value to where the servo must be positioned. Once the input of this function is “angle”, we must convert it to duty cycle in percentage, using the formula developed before.</p><p>When the script is executed, you must enter as parameters, servo GPIO, and angle.</p><p>For example:</p><pre>sudo python3 angleServoCtrl.py 17 45</pre><p>The above command will position the servo connected on GPIO 17 with 45 degrees in “elevation”.</p><p>The file <a href="https://github.com/Mjrovai/RPi-Pan-Tilt-Servo-Control/blob/master/PanTiltControl/angleServoCtrl.py">angleServoCtrl.py</a> can be downloaded from my GitHub</p><h3>6. The Pan-Tilt Mechanism</h3><p>The “Pan” servo will move “horizontally” our camera (“azimuth angle”) and our “Tilt” servo will move it “vertically” (elevation angle).</p><p>The below picture shows how the Pan/Tilt mechanism works:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/510/0*PvtFZpeMu7kYv9UV." /></figure><p>During our development we will not go to “extremes” and we will use our Pan/Tilt mechanism from 30 to 150 degrees only. This range will be enough to be used with a camera.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/364/0*km6IhYOoTE8rcYbs." /></figure><h3>7. The Pan-Tilt Mechanism — Mechanical Construction</h3><p>Let’s now, assembly our 2 servos as a Pan/Tilt mechanism. You can buy a Pan-Tilt platform mechanism as the one showed before or build your own according to your necessities.</p><p>One example can be the one that I built, only strapping the servos one to another, and using small metal pieces from old toys as shown bellow:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/383/0*GcALd-kIwf5s5BeC." /></figure><h3>8. Electrical Pan/Tilt Assembly</h3><p>Once you have your Pan/Tilt mechanism assembled, follow the photos for full electrical connection.</p><ul><li>Turn off your Pi.</li><li>Do all electrical connections.</li><li>Double check it.</li><li>Power on your Pi first.</li><li>If everything is OK, power your servos.</li></ul><p>We will not explore on this tutorial how to set-up the camera, this will be explained on next tutorial.</p><h3>9. The Python Script</h3><p>Let’s create a Python Script to control both servos simultaneously:</p><pre>from time import sleep<br>import RPi.GPIO as GPIO<br>GPIO.setmode(GPIO.BCM)<br>GPIO.setwarnings(False)</pre><pre>pan = 27<br>tilt = 17</pre><pre>GPIO.setup(tilt, GPIO.OUT) # white =&gt; TILT<br>GPIO.setup(pan, GPIO.OUT) # gray ==&gt; PAN</pre><pre>def setServoAngle(servo, angle):<br>	assert angle &gt;=30 and angle &lt;= 150<br>	pwm = GPIO.PWM(servo, 50)<br>	pwm.start(8)<br>	dutyCycle = angle / 18. + 3.<br>	pwm.ChangeDutyCycle(dutyCycle)<br>	sleep(0.3)<br>	pwm.stop()</pre><pre>if __name__ == &#39;__main__&#39;:<br>	import sys<br>	if len(sys.argv) == 1:<br>		setServoAngle(pan, 90)<br>		setServoAngle(tilt, 90)<br>	else:<br>		setServoAngle(pan, int(sys.argv[1])) # 30 ==&gt; 90 (middle point) ==&gt; 150<br>		setServoAngle(tilt, int(sys.argv[2])) # 30 ==&gt; 90 (middle point) ==&gt; 150<br>	GPIO.cleanup()</pre><p>When the script is executed, you must enter as parameters, Pan angle and Tilt angle. For example:</p><pre>sudo python3 servoCtrl.py 45 120</pre><p>The above command will position the Pan/Tilt mechanism with 45 degrees in “azimuth” (Pan angle) and 120 degrees of “elevation” (Tilt Angle). Note that if no parameters are entered, the default will be both, pan and tilt angles set up to 90 degrees.</p><p>Below you can see some tests:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*vbPo5PKrj_OTzsmJ." /></figure><p>The <a href="https://github.com/Mjrovai/RPi-Pan-Tilt-Servo-Control/blob/master/PanTiltControl/servoCtrl.py">servoCtrl.py</a> file can be downloaded from my GitHub.</p><h3>10. Loop Test of Servers</h3><p>Let’s now create a Python Script to automatically test the full range of servos:</p><pre>from time import sleep<br>import RPi.GPIO as GPIO<br>GPIO.setmode(GPIO.BCM)<br>GPIO.setwarnings(False)</pre><pre>pan = 27<br>tilt = 17</pre><pre>GPIO.setup(tilt, GPIO.OUT) # white =&gt; TILT<br>GPIO.setup(pan, GPIO.OUT) # gray ==&gt; PAN</pre><pre>def setServoAngle(servo, angle):<br>	assert angle &gt;=30 and angle &lt;= 150<br>	pwm = GPIO.PWM(servo, 50)<br>	pwm.start(8)<br>	dutyCycle = angle / 18. + 3.<br>	pwm.ChangeDutyCycle(dutyCycle)<br>	sleep(0.3)<br>	pwm.stop()</pre><pre>if __name__ == &#39;__main__&#39;:  <br>    for i in range (30, 160, 15):<br>        setServoAngle(pan, i)<br>        setServoAngle(tilt, i)<br>    <br>    for i in range (150, 30, -15):<br>        setServoAngle(pan, i)<br>        setServoAngle(tilt, i)<br>        <br>    setServoAngle(pan, 100)<br>    setServoAngle(tilt, 90)    <br>    GPIO.cleanup()</pre><p>The program will execute automatically a loop from 30 to 150 degrees in both angles.</p><p>Below the result:</p><p>I connected an oscilloscope only to illustrate the PWM theory as explained before.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*UiwX1tgYPypthspS." /></figure><p>The above code, <a href="https://github.com/Mjrovai/RPi-Pan-Tilt-Servo-Control/blob/master/PanTiltControl/servoTest.py">servoTest.py</a> can be downloaded from my GitHub.</p><h3>11. Conclusion</h3><p>As always, I hope this project can help others find their way into the exciting world of electronics!</p><p>For details and final code, please visit my GitHub depository: <a href="https://github.com/Mjrovai/RPi-Pan-Tilt-Servo-Control">RPi-Pan-Tilt-Servo-Control</a></p><p>For more projects, please visit my blog: <a href="https://mjrobot.org/">MJRoBot.org</a></p><p>Below a glimpse of my next tutorial:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*-mWONlFC4YtvS7C1." /></figure><p>Saludos from the south of the world!</p><p>See you in my next tutorial!</p><p>Thank you,</p><p>Marcelo</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=282adaa13a73" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mjrobot-org/pan-tilt-multi-servo-control-282adaa13a73">Pan-Tilt Multi Servo Control</a> was originally published in <a href="https://medium.com/mjrobot-org">MJRoBot.org</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Video Streaming Web Server]]></title>
            <link>https://medium.com/mjrobot-org/video-streaming-web-server-5315fd48899c?source=rss----9c5924ccca32---4</link>
            <guid isPermaLink="false">https://medium.com/p/5315fd48899c</guid>
            <category><![CDATA[raspberry-pi]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[cameras]]></category>
            <category><![CDATA[flask]]></category>
            <dc:creator><![CDATA[Marcelo Rovai]]></dc:creator>
            <pubDate>Fri, 15 Jun 2018 20:17:33 GMT</pubDate>
            <atom:updated>2018-06-15T20:17:32.837Z</atom:updated>
            <cc:license>http://creativecommons.org/publicdomain/zero/1.0/</cc:license>
            <content:encoded><![CDATA[<h4>Learn how to stream a video and integrate it with a Python/Flask Web Server.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*zOvtroeGInYkR4Lls5OJoQ.png" /></figure><h3>1. Introduction</h3><p>On my tutorial, <a href="https://medium.com/@rovai/from-data-to-graph-a-web-jorney-with-flask-and-sqlite-6c2ec9c0ad0">From Data to Graph: A Web Journey with Flask and SQLite</a>, we learned how to:</p><ul><li>Capture real data (air temperature and relative humidity) using a DHT22 sensor;</li><li>Load that data on a local database, built with SQLite;</li><li>Create graphics with historical data using Matplotlib;</li><li>Display data with animated “gages”, created with JustGage;</li><li>Make everything available online through a local web-server created with Python and Flask;</li></ul><p>Now we will learn how to stream a video and integrating it on a web server :</p><ul><li>Stream a video</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*W_nZn0u3ErmC47TV." /></figure><ul><li>Integrate the video on a Web Server.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/328/0*f0DRjGhGZdD1STah." /></figure><h3>2. Installing the Camera</h3><p>With your RPi turned-off, install the Camara on its special port as shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*0nioXJiaGCxOfOUO." /></figure><p>Turn on your Pi and go to <em>Raspberry Pi Configuration Tool</em> at main menu and verify if Camera Interface is enabled:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/492/0*Wpc_qCSH5od3uxU5." /></figure><p>If you needed to Enabled it, press [OK] and reboot your Pi.</p><p>Make a simple test to verify if everything is OK:</p><pre>raspistill -o /Desktop/image.png</pre><p>You will realize that an image icon appears on your RPi desktop. Click on it to open. If an image appears, your Pi is ready to stream video! If you want to know more about the camera, visit the link: <a href="https://projects.raspberrypi.org/en/projects/getting-started-with-picamera">Getting started with picamera</a>.</p><h3>3. Instaling Flask</h3><p>There are several ways to stream video. The best (and “lighther”) way to do it that I found was with Flask, as developed by Miguel Grinberg. For a detailed explanation about how Flask does this, please see his great tutorial: <a href="https://blog.miguelgrinberg.com/post/flask-video-streaming-revisited">flask-video-streaming-revisited</a>.</p><p>On my tutorial: <a href="https://medium.com/@rovai/python-webserver-with-flask-and-raspberry-pi-398423cc6f5d">Python WebServer With Flask and Raspberry Pi</a>, we learned in more details how Flask works and how to implement a web-server to capture data from sensors and show their status on a web page. Here, on the first part of this tutorial, we will do the same, only that the data to be sent to our front end, will be a <em>video stream</em>.</p><p><strong>Creating a web-server environment:</strong></p><p>The first thing to do is to install Flask on your Raspberry Pi. If you do not have it, go to the Terminal and enter:</p><pre>sudo apt-get install python3-flask</pre><p>It’s best when you start a new project is to create a folder where to have your files organized. For example…</p><p>From home, go to your working directory:</p><pre>cd Documents</pre><p>Create a new folder, for example:</p><pre>mkdir camWebServer</pre><p>The above command will create a folder named “camWebServer”, where we will save our python scripts:</p><pre>/home/pi/Documents/camWebServer</pre><p>Now, on this folder, let’s create 2 sub-folders: <em>static</em> for CSS and eventually JavaScript files and <em>templates </em>for HTML files<em>.</em> Go to your newer created folder:</p><pre>cd camWebServer</pre><p>And create the 2 new sub-folders:</p><pre>mkdir static</pre><p>and</p><pre>mkdir templates</pre><p>The final directory “tree”, will look like:</p><pre>├── Documents<br>       ├── camWebServer<br>               ├── templates<br>               └── static</pre><p>OK! With our environment in place let’s create our <em>Python WebServer Application to stream video.</em></p><h3>4. Creating the Video Streaming Server</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/529/0*vNRn1Lv6zyU3Fego." /></figure><p>First, download Miguel Grinberg’s picamera package: <a href="https://github.com/Mjrovai/Video-Streaming-with-Flask/blob/master/camWebServer/camera_pi.py">camera_pi.py</a> and save it on created directory camWebServer. This is the heart of our project, Miguel did a fantastic job!</p><p>Now, using Flask, let’s adapt the original Miguel’s web Server application (<em>app.py</em>), creating a specific python script to render our video. We will call it <a href="https://github.com/Mjrovai/Video-Streaming-with-Flask/blob/master/camWebServer/appCam.py">appCam.py</a>:</p><pre>from flask import Flask, render_template, Response</pre><pre># Raspberry Pi camera module (requires picamera package, developed by Miguel Grinberg)</pre><pre>from camera_pi import Camera<br>app = Flask(__name__)</pre><pre>@app.route(&#39;/&#39;)<br>def index():<br>    &quot;&quot;&quot;Video streaming home page.&quot;&quot;&quot;<br>    return render_template(&#39;index.html&#39;)</pre><pre>def gen(camera):<br>    &quot;&quot;&quot;Video streaming generator function.&quot;&quot;&quot;<br>    while True:<br>        frame = camera.get_frame()<br>        yield (b&#39;--frame\r\n&#39;<br>               b&#39;Content-Type: image/jpeg\r\n\r\n&#39; + frame + b&#39;\r\n&#39;)</pre><pre>@app.route(&#39;/video_feed&#39;)<br>def video_feed():<br>    &quot;&quot;&quot;Video streaming route. Put this in the src attribute of an img tag.&quot;&quot;&quot;<br>    return Response(gen(Camera()),<br>                    mimetype=&#39;multipart/x-mixed-replace; boundary=frame&#39;)</pre><pre>if __name__ == &#39;__main__&#39;:<br>    app.run(host=&#39;0.0.0.0&#39;, port =80, debug=True, threaded=True)</pre><p>The above script streams your camera video on an index.html page as below:</p><pre>&lt;html&gt;<br>  &lt;head&gt;<br>    &lt;title&gt;MJRoBot Lab Live Streaming&lt;/title&gt;<br>    &lt;link rel=&quot;stylesheet&quot; href=&#39;../static/style.css&#39;/&gt;<br>  &lt;/head&gt;<br>  &lt;body&gt;<br>    &lt;h1&gt;MJRoBot Lab Live Streaming&lt;/h1&gt;<br>    &lt;h3&gt;&lt;img src=&quot;{{ url_for(&#39;video_feed&#39;) }}&quot; width=&quot;90%&quot;&gt;&lt;/h3&gt;<br>    &lt;hr&gt;<br>    &lt;p&gt; @2018 Developed by MJRoBot.org&lt;/p&gt;<br>  &lt;/body&gt;<br>&lt;/html&gt;</pre><p>The most important line of index.html is:</p><pre>&lt;img src=&quot;{{ url_for(&#39;video_feed&#39;) }}&quot; width=&quot;50%&quot;&gt;</pre><p>There is where the video will be “feed” to our web page. You must also include the style.css file on the static directory to get the above result in terms of style. All the files can be downloaded from my GitHub: <a href="https://github.com/Mjrovai/Video-Streaming-with-Flask/tree/master/camWebServer">camWebServer</a></p><p>Only to be sure that everything is in the right location, let’s check our environment after all updates:</p><pre>├── Documents<br>       ├── camWebServer<br>               ├── camera_pi.py<br>               ├── appCam.py<br>               ├── templates<br>               |     ├── index.html<br>               └── static<br>                     ├── style.css</pre><p>Now, run the python script on the Terminal:</p><pre>&lt;p&gt;sudo python3 appCam.py&lt;/p&gt;</pre><p>Go to any browser in your network and enter with <em>http://YOUR_RPI_IP</em> (for example, in my case: 10.0.1.27)</p><p>NOTE: If you are not sure about your RPi IP address, run on your terminal:</p><pre>ifconfig</pre><p>at wlan0: section you will find it.</p><p>The results:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/0*hUXhHXN-hgtEg5ql." /></figure><p>That’s it! From here it is only a matter to sophisticate a page, embed your video on another page etc.</p><h3>5. Installing the Temperature &amp; Humidity Sensor</h3><p>Now it is time to see how to call a video scream form a web page. Let’s create a page that uses real data like air temperature and relative humidity. For that, we will use the old and good DHT22. The <a href="https://learn.adafruit.com/dht/overview">ADAFRUIT site</a> provides great information about those sensors. Below, some information retrieved from there:</p><h4>DHT22 overview</h4><p>The low-cost DHT temperature &amp; humidity sensors are very basic and slow but are great for hobbyists who want to do some basic data logging. The DHT sensors are made of two parts, a capacitive humidity sensor, and a thermistor. There is also a very basic chip inside that does some analog to digital conversion and spits out a digital signal with the temperature and humidity. The digital signal is fairly easy to be read using any microcontroller.</p><p>DHT22 Main characteristics:</p><p>Good for 0–100% humidity readings with 2–5% accuracy Good for -40 to 125°C temperature readings ±0.5°C accuracy No more than 0.5 Hz sampling rate (once every 2 seconds)</p><ul><li>Low cost</li><li>3 to 5V power and I/O</li><li>2.5mA max current use during conversion (while requesting data)</li><li>Body size 15.1mm x 25mm x 7.7mm</li><li>4 pins with 0.1&quot; spacing</li></ul><p>Once usually you will use the sensor on distances less than 20m, a 4K7 ohm resistor should be connected between Data and VCC pins. The DHT22 output data pin will be connected to Raspberry GPIO 16.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/0*QDqD_HLOwy07F9-S." /></figure><p>Check the above electrical diagram connecting the sensor to RPi pins as below:</p><ul><li>Pin 1 — Vcc ==&gt; 3.3V</li><li>Pin 2 — Data ==&gt; GPIO 16</li><li>Pin 3 — Not Connect</li><li>Pin 4 — Gnd ==&gt; Gnd</li></ul><p>Do not forget to install the 4K7 ohm resistor between Vcc and Data pins. Once the sensor is connected, we must also install its library on our RPi.</p><h4>Installing DHT Library:</h4><p>On your Raspberry, starting on /home, go to /Documents:</p><pre>cd Documents</pre><p>Create a directory to install the library and move to there:</p><pre>mkdir DHT22_Sensor<br>cd DHT22_Sensor</pre><p>On your browser, go to Adafruit GitHub: <a href="https://github.com/adafruit/Adafruit_Python_DHT">https://github.com/adafruit/Adafruit_Python_DHT</a></p><p>Download the library by clicking the download zip link to the right and unzip the archive on your Raspberry Pi recently created folder. Then go to the directory of the library (subfolder that is automatically created when you unzipped the file), and execute the command:</p><pre>sudo python3 setup.py install</pre><p>Open a test program (<a href="https://github.com/Mjrovai/RPI-Flask-SQLite/blob/master/suport_files/DHT22_test.py">DHT22_test.py</a>) from my GITHUB:</p><pre>import Adafruit_DHT<br>DHT22Sensor = Adafruit_DHT.DHT22<br>DHTpin = 16</pre><pre>humidity, temperature = Adafruit_DHT.read_retry(DHT22Sensor, DHTpin)</pre><pre>if humidity is not None and temperature is not None:<br>    print(&#39;Temp={0:0.1f}*C  Humidity={1:0.1f}%&#39;.format(temperature, humidity))<br>else:<br>    print(&#39;Failed to get reading. Try again!&#39;)</pre><p>Execute the program with the command:</p><pre>python3 DHT22_test.py</pre><p>The below Terminal print screen shows the result.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*rhyVAtLrwr3310VG." /></figure><h3>6. Creating a Web Server Application for Data and Video Display</h3><p>Let’s create another python WebServer using Flask, that will handle both, the data captured by sensors and a video stream.</p><p>Once our code is becoming more complex, I suggest <em>Geany as the IDE to be used, </em>once you can work simultaneously with different types of files (.py, .html and .css)</p><p>The code below is the python script to be used on our web-server:</p><pre>from flask import Flask, render_template, Response<br>app = Flask(__name__)</pre><pre># Raspberry Pi camera module (requires picamera package)<br>from camera_pi import Camera</pre><pre>import Adafruit_DHT<br>import time</pre><pre># get data from DHT sensor<br>def getDHTdata():		<br>	DHT22Sensor = Adafruit_DHT.DHT22<br>	DHTpin = 16<br>	hum, temp = Adafruit_DHT.read_retry(DHT22Sensor, DHTpin)<br>	<br>	if hum is not None and temp is not None:<br>		hum = round(hum)<br>		temp = round(temp, 1)<br>	return temp, hum</pre><pre>@app.route(&quot;/&quot;)<br>def index():<br>	timeNow = time.asctime( time.localtime(time.time()) )<br>	temp, hum = getDHTdata()<br>	<br>	templateData = {<br>      &#39;time&#39;: timeNow,<br>      &#39;temp&#39;: temp,<br>      &#39;hum&#39;	: hum<br>	}<br>	return render_template(&#39;index.html&#39;, **templateData)</pre><pre>@app.route(&#39;/camera&#39;)<br>def cam():<br>	&quot;&quot;&quot;Video streaming home page.&quot;&quot;&quot;<br>	timeNow = time.asctime( time.localtime(time.time()) )<br>	templateData = {<br>      &#39;time&#39;: timeNow<br>	}<br>	return render_template(&#39;camera.html&#39;, **templateData)<br>def gen(camera):<br>    &quot;&quot;&quot;Video streaming generator function.&quot;&quot;&quot;<br>    while True:<br>        frame = camera.get_frame()<br>        yield (b&#39;--frame\r\n&#39;<br>               b&#39;Content-Type: image/jpeg\r\n\r\n&#39; + frame + b&#39;\r\n&#39;)</pre><pre>@app.route(&#39;/video_feed&#39;)<br>def video_feed():<br>    &quot;&quot;&quot;Video streaming route. Put this in the src attribute of an img tag.&quot;&quot;&quot;<br>    return Response(gen(Camera()),<br>                    mimetype=&#39;multipart/x-mixed-replace; boundary=frame&#39;)</pre><pre>if __name__ == &#39;__main__&#39;:<br>    app.run(host=&#39;0.0.0.0&#39;, port =80, debug=True, threaded=True)</pre><p>You can get the python script <a href="https://github.com/Mjrovai/Video-Streaming-with-Flask/blob/master/camWebServer2/appCam2.py">appCam2.py</a> from my GitHub. What the above code does is:</p><p>With this request, the first thing done in the code is to read data from the sensor using the function <em>getDHTdata().Next, the actual time is retrieved from system</em>With the data on hand, our script returns to the webpage (<em>index.html</em>): <em>time</em>, <em>temp </em>and <em>hum</em> as a response to the previous request.</p><ul><li>Every time that someone “clicks”’on “/”, that is the main page (index.html) of our webpage a GET request is generated;</li></ul><p>Also, a page “/camera” can be called when the user wants to see the video stream. In this case, the 3 routs as shown in the last step are called and the <em>camera.html</em> is rendered.</p><p>So, let’s see the <em>index.html, camera.html </em>and <em>style.css</em> files that will be used to build our front-end:</p><p><strong>index.html</strong></p><pre>&lt;!doctype html&gt;<br>&lt;html&gt;<br>&lt;head&gt;<br>    &lt;title&gt;MJRoBot Lab Sensor Data&lt;/title&gt;<br>    &lt;link rel=&quot;stylesheet&quot; href=&#39;../static/style.css&#39;/&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>	&lt;h1&gt;MJRoBot Lab Sensor Data&lt;/h1&gt;<br>	&lt;h3&gt; TEMPERATURE   ==&gt;  {{ temp }} oC&lt;/h3&gt;<br>	&lt;h3&gt; HUMIDITY (Rel.) ==&gt;  {{ hum }} %&lt;/h3&gt;<br>	&lt;hr&gt;<br>	&lt;h3&gt; Last Sensors Reading: {{ time }} ==&gt; &lt;a href=&quot;/&quot;class=&quot;button&quot;&gt;REFRESH&lt;/a&gt;&lt;/h3&gt;	<br>	&lt;hr&gt;<br>	&lt;h3&gt; MJRoBot Lab Live Streaming ==&gt; &lt;a href=&quot;/camera&quot; class=&quot;button&quot;&gt;LIVE&lt;/a&gt;&lt;/h3&gt;<br>	&lt;hr&gt;	<br>	&lt;p&gt; @2018 Developed by MJRoBot.org&lt;/p&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;</pre><p>You can get the file <a href="https://github.com/Mjrovai/Video-Streaming-with-Flask/blob/master/camWebServer2/templates/index.html">index.html</a> from my GitHub.</p><p><strong>camera.html</strong></p><p>Basically, this page is the same that we created before, only that we are adding a button to return to the main page.</p><pre>&lt;html&gt;<br>  &lt;head&gt;<br>    &lt;title&gt;MJRoBot Lab Live Streaming&lt;/title&gt;<br>    &lt;link rel=&quot;stylesheet&quot; href=&#39;../static/style.css&#39;/&gt;<br>    &lt;style&gt;<br>	body {<br>		text-align: center;<br>	}<br>    &lt;/style&gt;<br>  &lt;/head&gt;<br>  &lt;body&gt;<br>    &lt;h1&gt;MJRoBot Lab Live Streaming&lt;/h1&gt;<br>    &lt;h3&gt;&lt;img src=&quot;{{ url_for(&#39;video_feed&#39;) }}&quot; width=&quot;80%&quot;&gt;&lt;/h3&gt;<br>    &lt;h3&gt;{{ time }}&lt;/h3&gt;<br>    &lt;hr&gt;<br>	&lt;h3&gt; Return to main page ==&gt; &lt;a href=&quot;/&quot;class=&quot;button&quot;&gt;RETURN&lt;/a&gt;&lt;/h3&gt;	<br>	&lt;hr&gt;<br>	&lt;p&gt; @2018 Developed by MJRoBot.org&lt;/p&gt;<br>  &lt;/body&gt;<br>&lt;/html&gt;</pre><p>You can get the file <a href="https://github.com/Mjrovai/Video-Streaming-with-Flask/blob/master/camWebServer2/templates/camera.html">camera.html </a>from my GitHub.</p><p><strong>style.css</strong></p><pre>body{<br>	background: blue;<br>	color: yellow;<br>	padding:1%<br>}</pre><pre>.button {<br>	font: bold 15px Arial;<br>	text-decoration: none;<br>	background-color: #EEEEEE;<br>	color: #333333;<br>	padding: 2px 6px 2px 6px;<br>	border-top: 1px solid #CCCCCC;<br>	border-right: 1px solid #333333;<br>	border-bottom: 1px solid #333333;<br>	border-left: 1px solid #CCCCCC;<br>}</pre><p>You can get the file <a href="https://github.com/Mjrovai/Video-Streaming-with-Flask/blob/master/camWebServer2/static/style.css">style.cs s</a> from my GitHub. Creating the Web Server environment. The files must be placed in your directory like this:</p><pre>├── camWebServer2<br>         ├── camera_pi.py<br>         └─── appCam2.py                <br>                   ├── templates<br>                   │       ├── index.html<br>                   │       ├── camera.html<br>                   └── static<br>                           └──── style.css</pre><p>Now, run the python script on the Terminal:</p><pre>sudo python3 appCam2.py</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/328/0*ZOi-Qan7a-yEVSrb." /></figure><h3>7. Adding Video Stream on a Previous Project</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/679/0*L4NMPGtLKC-zRiqQ." /></figure><p>Let’s include the video stream on the project developed on my previous tutorial: <a href="https://medium.com/@rovai/from-data-to-graph-a-web-jorney-with-flask-and-sqlite-6c2ec9c0ad0">From Data to Graph:A Web Journey with Flask and SQLite</a>.</p><p>As a reminder, on that project we logged data (temperature and humidity) on a database and presented the actual and historical data on a webpage:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*zGGOZ3vrT5Oor-I2." /></figure><p>We did this, using 2 python scripts, one to log data on a database (logDHT.py) and another to present data on a webPage via Flask WebServer (appDhtWebServer.py):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*oFU-eJ3QAMJk8xRq." /></figure><h4>Creating the web-server environment:</h4><p>Below I presented my directory view. Sensor_Database is under /Documents in my RPi:</p><pre>├── Sensors_Database<br>       ├── sensorsData.db<br>       ├── logDHT.py<br>       └── dhtWebHistCam<br>               ├── appDhtWebHistCam.py                 <br>               ├── camera_pi.py<br>               └─── appCam2.py                <br>                       ├── templates<br>                       │       ├── index.html<br>                       │       ├── camera.html<br>                       └── static<br>                               ├── justgage.js<br>		               ├── raphael-2.1.4.min.js<br>                               └──── style.css</pre><p>The files can be found on my GitHub: <a href="https://github.com/Mjrovai/Video-Streaming-with-Flask/tree/master/dhtWebHistCam">dhtWebHistCam</a></p><p>Below the result:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FJCiBgntCY6c%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DJCiBgntCY6c&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FJCiBgntCY6c%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ff484a1108a3b26fd084bf7f5f960c6/href">https://medium.com/media/8ff484a1108a3b26fd084bf7f5f960c6/href</a></iframe><h3>8. Conclusion</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/442/0*5zL-O55mw_URtSpG." /></figure><p>As always, I hope this tutorial can help others find their way into the exciting world of electronics!</p><p>For details and final code, please visit my GitHub depository: <a href="https://github.com/Mjrovai/Video-Streaming-with-Flask">Video-Streaming-with-Flask</a></p><p>For more projects, please visit my blog: <a href="https://mjrobot.org/">MJRoBot.org</a></p><p>Saludos from the south of the world!</p><p>See you at my next tutorial!</p><p>Thank you,</p><p>Marcelo</p><p><em>Originally published at </em><a href="https://www.hackster.io/mjrobot/video-streaming-web-server-ef11e3"><em>www.hackster.io</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5315fd48899c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mjrobot-org/video-streaming-web-server-5315fd48899c">Video Streaming Web Server</a> was originally published in <a href="https://medium.com/mjrobot-org">MJRoBot.org</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[From Data to Graph: a Web Jorney With Flask and SQLite]]></title>
            <link>https://medium.com/mjrobot-org/from-data-to-graph-a-web-jorney-with-flask-and-sqlite-8b967776c25f?source=rss----9c5924ccca32---4</link>
            <guid isPermaLink="false">https://medium.com/p/8b967776c25f</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[raspberry-pi]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[physical-computing]]></category>
            <dc:creator><![CDATA[Marcelo Rovai]]></dc:creator>
            <pubDate>Fri, 15 Jun 2018 19:53:46 GMT</pubDate>
            <atom:updated>2018-06-15T19:53:46.396Z</atom:updated>
            <cc:license>http://creativecommons.org/publicdomain/zero/1.0/</cc:license>
            <content:encoded><![CDATA[<h4>Capturing real data, logging them in a database, creating graphs and presenting them on a web page.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/0*-w9Ck_XvNFT3tfwO." /></figure><h3>1. Introduction</h3><p>On my previous tutorial, <a href="https://medium.com/@rovai/python-webserver-with-flask-and-raspberry-pi-398423cc6f5d">Python WebServer With Flask and Raspberry Pi</a>, we learned how to interact with the physical world, thru a web front-end page, built with Flask. So, the next natural step is collect data from the real world, having them available for us on a webpage. Very simple! But, what will happen if we want to know what was the situation on the day before, for example? Or make some kind of analyzing with those data? In those cases, we must have the data also stored in a database.</p><p>In short, on this new tutorial, we will:</p><p><em>Capture real data (air temperature and relative humidity) using a DHT22 sensor;</em></p><p><em>Load those data on a local database, built with SQLite;</em></p><p><em>Create graphicswith historical data using Matplotlib;</em></p><p><em>Display data with animated “gages”, created with JustGage;</em></p><p><em>Make everything available online through a local web-server created with Python and Flask;</em></p><p>The block diagram gives us an idea of the whole project:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*CQFvRn35FWPea4ZZ." /></figure><h3>2. Installing SQLite</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/0*TnE_814xmLMw1iwI." /></figure><p>OK, the general idea will be collect data from a sensor and store them in a database.</p><p><em>But what database “engine” should be used?</em></p><p>There are many options in the market and probably the 2 most used with Raspberry Pi and sensors are MySQL and SQLite. MySQL is very known but a little bit “heavy” for use on simple Raspberry based projects. Here, SQLite is probably the most suitable choice, because it is serverless, lightweight, opensource and supports most SQL code (its license is “Public Domain”). Another handy thing is that SQLite stores data in a single file which can be stored anywhere.</p><p><em>But, what’s SQLite?</em></p><p>SQLite is a relational database management system contained in a C programming library. In contrast to many other database management systems, SQLite is not a client-server database engine. Rather, it is embedded into the end program.</p><p>SQLite is a popular <em>public domain</em> choice as embedded database software for local/client storage in application software such as web browsers. It is arguably the most widely deployed database engine, as it is used today by several widespread browsers, operating systems, and embedded systems (such as mobile phones), among others. SQLite has bindings to many programming languages like Python, the one used on our project. (<a href="https://www.google.cl/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=16&amp;cad=rja&amp;uact=8&amp;ved=0ahUKEwi7u5HP99TYAhXBkpAKHQi3DX8QmhMIhwEwDw&amp;url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FSQLite&amp;usg=AOvVaw06SYiW5wZZ_LX2dkoF7YnS">More on Wikipedia</a>)</p><blockquote>We will not enter into too many details here, but the full SQLite documentation can be found at this link: <a href="https://www.sqlite.org/docs.html">https://www.sqlite.org/docs.html</a></blockquote><p>So, be it! Let’s install SQLite on our Pi</p><p><strong>Installation:</strong></p><p>Follow the below steps to create a database.</p><p>1. Install SQLite to Raspberry Pi using the command:</p><pre>sudo apt-get install sqlite3</pre><p>2. Create a directory to develop the project:</p><pre>mkdir Sensors_Database</pre><p>3. Move to this directory:</p><pre>cd mkdir Sensors_Database/</pre><p>3. Give a name and create a database like <em>databaseName.db</em> (in my case “sensorsData.db”):</p><pre>sqlite3 sensorsData.db</pre><p>A “shell” will appear, where you can enter with SQLite commands. We will return to it later.</p><pre>sqlite&gt;</pre><blockquote>Commands starts with a “.”, like “.help”, “.quit”, etc.</blockquote><p>4. Quit the shell to return to the Terminal:</p><pre>sqlite&gt; .quit</pre><p>The above Terminal print screen shows what was explained.</p><p>The “sqlite&gt;” above is only to ilustrated how the SQLite shell will appear. You do not need to type it. It will appear automatically.</p><h3>3. Create and Populating a Table</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/0*w_IDe3hDwFB3o9vW." /></figure><p>In order to log DHT sensor measured data on the database, we must create <em>a table </em>(a database can contain several tables). Our table will be named “DHT_data” and will have 3 columns, where we will log our collected data: Date and Hour (column name: <em>timestamp</em>), Temperature (column name: <em>temp</em>), and Humidity (column name: <em>hum</em>).</p><h4>Creating a table:</h4><p>To create a table, you can do it:</p><ul><li>Directly on the SQLite shell, or</li><li>Using a Python program.</li></ul><p>1. Using Shell:</p><p>Open the database that was created in the last step:</p><pre>sqlite3 sensorsData.db</pre><p>And entering with SQL statements:</p><pre>sqlite&gt; BEGIN;<br>sqlite&gt; CREATE TABLE DHT_data (timestamp DATETIME,  temp NUMERIC, hum NUMERIC);<br>sqlite&gt; COMMIT;</pre><p>All SQL statements must end with “;”. Also usually, those statements are written using capital letters. It is not mandatory, but a good practice.</p><p>2. Using Python</p><pre>import sqlite3 as lite<br>import sys<br>con = lite.connect(&#39;sensorsData.db&#39;)<br>with con: <br>    cur = con.cursor() <br>    cur.execute(&quot;DROP TABLE IF EXISTS DHT_data&quot;)<br>    cur.execute(&quot;CREATE TABLE DHT_data(timestamp DATETIME, temp NUMERIC, hum NUMERIC)&quot;)</pre><p>Open the above code from my GitHub: <a href="https://github.com/Mjrovai/RPI-Flask-SQLite/blob/master/suport_files/createTableDHT.py">createTableDHT.py</a></p><p>Run it on your Terminal:</p><pre>python3 createTableDHT.py</pre><p>Wherever the method used, the table should be created. You can verify it on SQLite Shell using the “.table” command. Open the database shell:</p><pre>sqlite3&gt; sensorsData.db</pre><p>In the shell, once you use the <em>.table</em> command, the created tables names will appear (in our case will be only one: “DHT_table”. Quit the shell after, using the <em>.quit </em>command.</p><pre>sqlite&gt; .table<br>DHT_data<br>sqlite&gt; .quit</pre><h4>Inserting data on a table:</h4><p>Let’s input on our database 3 sets of data, where each set will have 3 components each: (timestamp, temp, and hum). The component <em>timestamp</em> will be real and taken from the system, using the built-in function ‘now’ and <em>temp</em> and <em>hum</em> are dummy data in oC and % respectively.</p><blockquote><strong>Note</strong> that the time is in “UTC”, what is good because you don’t have to worry about issues related to daylight saving time and other matters. Should you want to output the date in localized time, just convert it to the appropriate time zone afterward.</blockquote><p>Same way was done with table creation, you can insert data manually via SQLite shell or via Python. At the shell, you would do it, data by data using SQL statements like this (For our example, you will do it 3 times):</p><pre>sqlite&gt; INSERT INTO DHT_data VALUES(datetime(&#39;now&#39;), 20.5, 30);</pre><p>And in Python, you would do the same but at once:</p><pre>import sqlite3 as lite<br>import sys<br>con = lite.connect(&#39;sensorsData.db&#39;)<br>with con:<br>    cur = con.cursor() <br>    cur.execute(&quot;INSERT INTO DHT_data VALUES(datetime(&#39;now&#39;), 20.5, 30)&quot;)<br>    cur.execute(&quot;INSERT INTO DHT_data VALUES(datetime(&#39;now&#39;), 25.8, 40)&quot;)<br>    cur.execute(&quot;INSERT INTO DHT_data VALUES(datetime(&#39;now&#39;), 30.3, 50)&quot;)</pre><p>Open the above code from my GitHub: <a href="https://github.com/Mjrovai/RPI-Flask-SQLite/blob/master/suport_files/insertTableDHT.py">insertTableDHT.py</a></p><p>Run it on Pi Terminal:</p><pre>python3 insertTableDHT.py</pre><p>To confirm that the above code worked, you can check the data in the table via shell, with the SQL statement:</p><pre>sqlite&gt; SELECT * FROM DHT_DATA;</pre><p>The above Terminal print screen shows how the table’rows will appear.</p><h3>4. Inserting and Verifying Data With Python</h3><p>For starting, let’s do the same we did before (input and retrieve data), but doing both with python and also printing the data on terminal:</p><pre>import sqlite3<br>import sys<br>conn=sqlite3.connect(&#39;sensorsData.db&#39;)<br>curs=conn.cursor()<br># function to insert data on a table<br>def add_data (temp, hum):<br>    curs.execute(&quot;INSERT INTO DHT_data values(datetime(&#39;now&#39;), (?), (?))&quot;, (temp, hum))<br>    conn.commit()<br># call the function to insert data<br>add_data (20.5, 30)<br>add_data (25.8, 40)<br>add_data (30.3, 50)<br># print database content<br>print (&quot;\nEntire database contents:\n&quot;)<br>for row in curs.execute(&quot;SELECT * FROM DHT_data&quot;):<br>    print (row)<br># close the database after use<br>conn.close()</pre><p>Open the above code from my GitHub: <a href="https://github.com/Mjrovai/RPI-Flask-SQLite/blob/master/suport_files/insertDataTableDHT.py">insertDataTableDHT.py</a> and run it on your Terminal:</p><pre>python3 insertDataTableDHT.py</pre><p>The Terminal print screen shows the result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/0*ZxlldgZvKvzIY4o_." /></figure><h3>5. DHT22 Temperature and Humidity Sensor</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/0*BLyXoX2uSykm6WW0." /></figure><p>So far we have created a table in our database, where we will save all data that a sensor will read. We have also entered with some dummy data there. Now it is time to use real data to be saved in our table, air temperature and relative humidity. For that, we will use the old and good DHTxx (DHT11 or DHT22). The <a href="https://learn.adafruit.com/dht/overview">ADAFRUIT site</a> provides great information about those sensors. Bellow, some information retrieved from there:</p><h4>Overview</h4><p>The low-cost DHT temperature &amp; humidity sensors are very basic and slow but are great for hobbyists who want to do some basic data logging. The DHT sensors are made of two parts, a capacitive humidity sensor, and a thermistor. There is also a very basic chip inside that does some analog to digital conversion and spits out a digital signal with the temperature and humidity. The digital signal is fairly easy to be read using any microcontroller.</p><h4>DHT11 vs DHT22</h4><p>We have two versions of the DHT sensor, they look a bit similar and have the same pinout, but have different characteristics. Here are the specs:</p><p>DHT11 (usually blue)</p><p>Good for 20–80% humidity readings with 5% accuracy Good for 0–50°C temperature readings ±2°C accuracy No more than 1 Hz sampling rate (once every second)</p><ul><li>Ultra low cost</li><li>3 to 5V power and I/O</li><li>2.5mA max current use during conversion (while requesting data)</li><li>Body size 15.5mm x 12mm x 5.5mm</li><li>4 pins with 0.1&quot; spacing</li></ul><p>DHT22 (usually white)</p><p>Good for 0–100% humidity readings with 2–5% accuracy Good for -40 to 125°C temperature readings ±0.5°C accuracy No more than 0.5 Hz sampling rate (once every 2 seconds)</p><ul><li>Low cost</li><li>3 to 5V power and I/O</li><li>2.5mA max current use during conversion (while requesting data)</li><li>Body size 15.1mm x 25mm x 7.7mm</li><li>4 pins with 0.1&quot; spacing</li></ul><p>As you can see, the DHT22 is a little bit more accurate and good over a slightly larger range. Both use a single digital pin and are ‘sluggish’ in that you can’t query them more than once every second (DHT11) or two (DHT22).</p><p>Both sensors will work fine to get Indoor information to be stored in our database.</p><p>The DHTxx has 4 pins (facing the sensor, pin 1 is the most left) :</p><ul><li>VCC (we can connect to external 5V or to 3.3V from RPi);</li><li>Data out;</li><li>Not Connected</li><li>Ground.</li></ul><p>We will use a DHT22 in our project.</p><p>Once usually you will use the sensor on distances less than 20m, a 4K7 ohm resistor should be connected between Data and VCC pins. The DHT22 output data pin will be connected to Raspberry GPIO 16.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/0*jd71xBDGXSmZ56f2." /></figure><p>Check above electrical diagram connecting the sensor to RPi pins as below:</p><ul><li>Pin 1 — Vcc ==&gt; 3.3V</li><li>Pin 2 — Data ==&gt; GPIO 16</li><li>Pin 3 — Not Connect</li><li>Pin 4 — Gnd ==&gt; Gnd</li></ul><p>Do not forget to Install the 4K7 ohm resistor between Vcc and Data pins. Once the sensor is connected, we must also install its library on our RPi. We will do this in the next step.</p><h3>6. Installing DHT Library</h3><p>On your Raspberry, starting on /home, go to /Documents:</p><pre>cd Documents</pre><p>Create a directory to install the library and move to there:</p><pre>mkdir DHT22_Sensor<br>cd DHT22_Sensor</pre><p>On your browser, go to Adafruit GITHub: <a href="https://github.com/adafruit/Adafruit_Python_DHT">https://github.com/adafruit/Adafruit_Python_DHT</a></p><p>Download the library by clicking the download zip link to the right and unzip the archive on your Raspberry Pi recently created folder. Then go to the directory of the library (subfolder that is automatically created when you unzipped the file), and execute the command:</p><pre>sudo python3 setup.py install</pre><p>Open a test program (<a href="https://github.com/Mjrovai/RPI-Flask-SQLite/blob/master/suport_files/DHT22_test.py">DHT22_test.py</a>) from my GITHUB:</p><pre>import Adafruit_DHT<br>DHT22Sensor = Adafruit_DHT.DHT22<br>DHTpin = 16</pre><pre>humidity, temperature = Adafruit_DHT.read_retry(DHT22Sensor, DHTpin)</pre><pre>if humidity is not None and temperature is not None:<br>    print(&#39;Temp={0:0.1f}*C  Humidity={1:0.1f}%&#39;.format(temperature, humidity))<br>else:<br>    print(&#39;Failed to get reading. Try again!&#39;)</pre><p>Execute the program with the command:</p><pre>python3 DHT22_test.py</pre><p>The Terminal print screen shows the result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/0*BXCOnI7svnmPv45e." /></figure><h3>7. Capturing Real Data</h3><p>Now that we have both, the sensor and our database all installed and configurated, it’s time to read and save real data.</p><p>For that, we will use the code:</p><pre>import time<br>import sqlite3<br>import Adafruit_DHT</pre><pre>dbname=&#39;sensorsData.db&#39;<br>sampleFreq = 2 # time in seconds</pre><pre># get data from DHT sensor<br>def getDHTdata():	<br>	DHT22Sensor = Adafruit_DHT.DHT22<br>	DHTpin = 16<br>	hum, temp = Adafruit_DHT.read_retry(DHT22Sensor, DHTpin)<br>	<br>	if hum is not None and temp is not None:<br>		hum = round(hum)<br>		temp = round(temp, 1)<br>		logData (temp, hum)</pre><pre># log sensor data on database<br>def logData (temp, hum):	<br>	conn=sqlite3.connect(dbname)<br>	curs=conn.cursor()<br>	curs.execute(&quot;INSERT INTO DHT_data values(datetime(&#39;now&#39;), (?), (?))&quot;, (temp, hum))<br>	conn.commit()<br>	conn.close()</pre><pre># display database data<br>def displayData():<br>	conn=sqlite3.connect(dbname)<br>	curs=conn.cursor()<br>	print (&quot;\nEntire database contents:\n&quot;)<br>	for row in curs.execute(&quot;SELECT * FROM DHT_data&quot;):<br>		print (row)<br>	conn.close()</pre><pre># main function<br>def main():<br>	for i in range (0,3):<br>		getDHTdata()<br>		time.sleep(sampleFreq)<br>	displayData()</pre><pre># Execute program <br>main()</pre><p>Open the above file from my GitHub: <a href="https://github.com/Mjrovai/RPI-Flask-SQLite/blob/master/suport_files/appDHT.py">appDHT.py</a>and run it on your Terminal:</p><pre>python3 appDHT.py</pre><p>The function <em>getDHTdata()</em> captures 3 samples of DHT sensor, test them for errors and if OK, save the data on database using the function <em>logData (temp, hum)</em>. The final part of code calls the function <em>displayData()</em> that prints the entire content of our table on Terminal.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/0*e1D9GFm97YKvpZTA." /></figure><p>The above print screen shows the result. Observe that the last 3 lines (rows) are the real data captured with this program and the 3 previous rows were the ones manually entered before.</p><p>In fact appDHT.py is not a good name. In general, “appSomething.py” is used with Python scripts on web servers as we will see further on this tutorial. But of course you can use it here.</p><h3>8. Capturing Data Automatically</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/565/0*fIN9brlkgWNrSa2Z." /></figure><p>At this point, what we must implement is a mechanism to read and insert data on our database automatically, our “Logger”.</p><p>Open a <strong>new Terminal window</strong> and enter with bellow Python code:</p><pre>import time<br>import sqlite3<br>import Adafruit_DHT</pre><pre>dbname=&#39;sensorsData.db&#39;<br>sampleFreq = 1*60 # time in seconds ==&gt; Sample each 1 min</pre><pre># get data from DHT sensor<br>def getDHTdata():	<br>	DHT22Sensor = Adafruit_DHT.DHT22<br>	DHTpin = 16<br>	hum, temp = Adafruit_DHT.read_retry(DHT22Sensor, DHTpin)<br>	if hum is not None and temp is not None:<br>		hum = round(hum)<br>		temp = round(temp, 1)<br>	return temp, hum</pre><pre># log sensor data on database<br>def logData (temp, hum):<br>	conn=sqlite3.connect(dbname)<br>	curs=conn.cursor()<br>	curs.execute(&quot;INSERT INTO DHT_data values(datetime(&#39;now&#39;), (?), (?))&quot;, (temp, hum))<br>	conn.commit()<br>	conn.close()</pre><pre># main function<br>def main():<br>	while True:<br>		temp, hum = getDHTdata()<br>		logData (temp, hum)<br>		time.sleep(sampleFreq)</pre><pre># ------------ Execute program <br>main()</pre><p>Or get it from my GitHub: <a href="https://github.com/Mjrovai/RPI-Flask-SQLite/blob/master/suport_files/logDHT.py">logDHT.py</a>. Run it on the Terminal:</p><pre>python3 logDHT.py</pre><p>What the main() function does is:</p><p>Call the function <em>getDHTdata()</em>, that will return the captured data by the DHT22 sensor. Take those data (temperature and humidity) and passing them to another function: <em>logData(temp, hum)</em> that insert them, together with actual date and time, to our table.And goes to sleep, waiting until the next scheduled time to capture data (defined by <em>sampleFreq</em>, which in this example is 1 minute).</p><p>Leave the Terminal window opened.</p><p>Until you kill the program with [Ctr+z], for example, the program will continuously capture data, feeding them in our database. I left it running for a while on a frequency of 1 minute for populating the database quicker, changing the frequency after few hours to 10 minutes.</p><p>There are other mechanisms much more efficient to perform this kind of “automatic logger” than using “time.sleep”, but the above code will work fine for our purpose here. Anyway, if you want to implement a better “scheduler”, you can use <em>Crontab</em>, which is a handy UNIX tool to schedule jobs. A good explanation of what Crontab is can be found in this tutorial: <a href="http://kvz.io/blog/2007/07/29/schedule-tasks-on-linux-using-crontab/">“Schedule Tasks on Linux Using Crontab”</a>, by Kevin van Zonneveld.</p><h3>9. Queries</h3><p>Now that our database is being fed automatically, we should find ways to work with all those data. We do it with queries!</p><p><strong>What is a query?</strong></p><p>One of the most important features of working with SQL language over databases is the ability to create “database queries”. In other words, queries extract data from a database formatting them in a readable form. A query must be written in <em>SQL language</em>, that uses a <em>SELECT</em> statement to select specific data.</p><p>We have in fact use it on a “broad way” on last step: “SELECT * FROM DHT_data”.</p><p><strong>Examples:</strong></p><p>Let’s create some queries over the data on the table that we have already created. For that, enter with below code:</p><pre>import sqlite3<br>conn=sqlite3.connect(&#39;sensorsData.db&#39;)<br>curs=conn.cursor()</pre><pre>maxTemp = 27.6</pre><pre>print (&quot;\nEntire database contents:\n&quot;)<br>for row in curs.execute(&quot;SELECT * FROM DHT_data&quot;):<br>    print (row)</pre><pre>print (&quot;\nDatabase entries for a specific humidity value:\n&quot;)<br>for row in curs.execute(&quot;SELECT * FROM DHT_data WHERE hum=&#39;29&#39;&quot;):<br>    print (row)<br>    <br>print (&quot;\nDatabase entries where the temperature is above 30oC:\n&quot;)<br>for row in curs.execute(&quot;SELECT * FROM DHT_data WHERE temp&gt;30.0&quot;):<br>    print (row)<br>    <br>print (&quot;\nDatabase entries where the temperature is above x:\n&quot;)<br>for row in curs.execute(&quot;SELECT * FROM DHT_data WHERE temp&gt;(?)&quot;, (maxTemp,)):<br>    print (row)</pre><p>Or get it from my GitHub: <a href="https://github.com/Mjrovai/RPI-Flask-SQLite/blob/master/suport_files/queryTableDHT.py">queryTableDHT.py</a>, and run it on Terminal:</p><pre>python3 queryTableDHT.py</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/355/0*6HOc5KoNHEc1WK7W." /></figure><p>You can see the result on the Terminal’s print screen above. Those are simple examples to give you an idea regarding queries. Take a time to understand the SQL statements in above code.</p><p>If you want to know more about SQL language, a good source is <a href="https://www.w3schools.com/sql/">W3School SQL Tutorial</a>.</p><h3>10. Last Data Entered on a Table:</h3><p>A very important query is the one to retrieve the <em>last data entered</em> (or logged) on a table. We can do it directly on the SQLite shell, with the command:</p><pre>sqlite&gt; SELECT * FROM DHT_data ORDER BY timestamp DESC LIMIT 1;</pre><p>Or running a simple python code as below:</p><pre>import sqlite3<br>conn = sqlite3.connect(&#39;sensorsData.db&#39;)<br>curs=conn.cursor()</pre><pre>print (&quot;\nLast Data logged on database:\n&quot;)<br>for row in curs.execute(&quot;SELECT * FROM DHT_data ORDER BY timestamp DESC LIMIT 1&quot;):<br>    print (row)</pre><p>You can see the result on the Terminal print screen:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/0*Y-D1sZDDK8AzMm_o." /></figure><p>Note that the result will appear as a “tuple of values”: (‘timestamp’, temp, hum).</p><p>The tuple returned the last row content of our table, which is formed with 3 elements on it:</p><ul><li>row[0] = timestamp [string]</li><li>row[1] = temp [float]</li><li>row[2] = hum [float]</li></ul><p>So, we can work better our code, to retrieve “clean” data from the table, for example:</p><pre>import sqlite3<br>conn=sqlite3.connect(&#39;sensorsData.db&#39;)<br>curs=conn.cursor()</pre><pre>print (&quot;\nLast raw Data logged on database:\n&quot;)<br>for row in curs.execute(&quot;SELECT * FROM DHT_data ORDER BY timestamp DESC LIMIT 1&quot;):<br>    print (str(row[0])+&quot; ==&gt; Temp = &quot;+str(row[1])+&quot;	Hum =&quot;+str(row[2]))</pre><p>Open the file from my GitHub: <a href="https://github.com/Mjrovai/RPI-Flask-SQLite/blob/master/suport_files/lastLogDataTableDHT.py">lastLogDataTableDHT.py</a> and run it on Terminal:</p><pre>python3 lastLogDataTableDHT.py</pre><p>You can see the result on the Terminal print screen:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TuVkSWl2vTSM7vEaiAZmUQ.png" /></figure><h3>11. A Web Front-end for Data Visualization</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/0*1RjNOJIiBc1fPDH3." /></figure><p>On my last tutorial: <a href="https://medium.com/@rovai/python-webserver-with-flask-and-raspberry-pi-398423cc6f5d">Python WebServer With Flask and Raspberry Pi</a>, we learned how to implement a web-server (using Flask) to capture data from sensors and show their status on a web page.</p><p>This is what we also want to accomplish here. The difference is in the data to be sent to our front end, that will be taken <strong>from a database</strong> and not directly from sensors as we did on that tutorial.</p><h4>Creating a web-server environment:</h4><p>The first thing to do is to install Flask on your Raspberry Pi. If you do not have it, go to the Terminal and enter:</p><pre>sudo apt-get install python3-flask</pre><p>The best when you start a new project is to create a folder where to have your files organized. For example:</p><p>From home, go to our working directory:</p><pre>cd Documents/Sensors_Database</pre><p>Create a new folder, for example:</p><pre>mkdir dhtWebServer</pre><p>The above command will create a folder named “dhtWebServer”, where we will save our python scripts:</p><pre>/home/pi/Documents/Sensor_Database/rpiWebServer</pre><p>Now, on this folder, let’s create 2 sub-folders: <em>static</em> for CSS and eventually JavaScript files and <em>templates </em>for HTML files<em>.</em> Go to your newer created folder:</p><pre>cd dhtWebServer</pre><p>And create the 2 new sub-folders:</p><pre>mkdir static</pre><p>and</p><pre>mkdir templates</pre><p>The final directory “tree”, will look like:</p><pre>├── Sensors_Database<br>       ├── sensorsData.db<br>       ├── logDHT.py<br>       ├── dhtWebSensor<br>               ├── templates<br>               └── static</pre><p>We will leave our created database on /Sensor_Database directory, so you will need to connect SQLite with “../sensorsData.db”.</p><p>OK! With our environment in place let’s assemble the parts and create our <em>Python WebServer Application</em>. The above diagram gives us an idea of what should be done!</p><h3>12. The Python WebServer Application</h3><p>Starting from the last diagram, let’s create a python WebServer using Flask. I suggest <em>Geany as the IDE to be used, </em>once you can work simultaneously with different types of files (.py, .html and .css).</p><p>The code below is the python script to be used on our first web-server:</p><pre>from flask import Flask, render_template, request<br>app = Flask(__name__)<br>import sqlite3</pre><pre># Retrieve data from database<br>def getData():<br>	conn=sqlite3.connect(&#39;../sensorsData.db&#39;)<br>	curs=conn.cursor()<br>	for row in curs.execute(&quot;SELECT * FROM DHT_data ORDER BY timestamp DESC LIMIT 1&quot;):<br>		time = str(row[0])<br>		temp = row[1]<br>		hum = row[2]<br>	conn.close()<br>	return time, temp, hum</pre><pre># main route <br>@app.route(&quot;/&quot;)<br>def index():	<br>	time, temp, hum = getData()<br>	templateData = {<br>		&#39;time&#39;: time,<br>		&#39;temp&#39;: temp,<br>		&#39;hum&#39;: hum<br>	}<br>	return render_template(&#39;index.html&#39;, **templateData)</pre><pre>if __name__ == &quot;__main__&quot;:<br>   app.run(host=&#39;0.0.0.0&#39;, port=80, debug=False)</pre><p>You can get the python script <a href="https://github.com/Mjrovai/RPI-Flask-SQLite/blob/master/dhtWebServer/appDhtWebServer.py">appDhtWebServer.py</a> from my GitHub. What the above code does is:</p><p>With this request, the first thing done in the code is to take data from the database using the function <em>time, temp, hum = getData().</em> This function is basically the same query that was used before to retrieve a data stored in the table. With the data on hand, our script returnsto the webpage (<em>index.html</em>): <em>time</em>, <em>temp </em>and <em>hum</em> as a response to the previous request.</p><ul><li>Every time that someone “clicks”’on “/”, that is the main page (index.html) of our webpage a GET request is generated;</li></ul><p>So, let’s see the <em>index.html</em> and <em>style.css</em> files that will be used to build our front-end:</p><h4>index.html</h4><pre>&lt;!DOCTYPE html&gt;<br>   &lt;head&gt;<br>      &lt;title&gt;DHT Sensor data &lt;/title&gt;<br>      &lt;link rel=&quot;stylesheet&quot; href=&#39;../static/style.css&#39;/&gt;<br>   &lt;/head&gt;<br>   &lt;body&gt;<br>	&lt;h1&gt;DHT Sensor Data &lt;/h1&gt;<br>	&lt;h3&gt; TEMPERATURE   ==&gt;  {{ tempLab  }} oC&lt;/h3&gt;<br>	&lt;h3&gt; HUMIDITY (Rel.) ==&gt;  {{ humLab  }} %&lt;/h3&gt;<br>	&lt;hr&gt;<br>	&lt;h3&gt; Last Sensors Reading: {{ time }} ==&gt; &lt;a href=&quot;/&quot;class=&quot;button&quot;&gt;REFRESH&lt;/a&gt;&lt;/h3&gt;	<br>	&lt;hr&gt;<br>	&lt;p&gt; @2018 Developed by MJRoBot.org&lt;/p&gt;<br>   &lt;/body&gt;<br>&lt;/html&gt;</pre><p>You can get the file <a href="https://github.com/Mjrovai/RPI-Flask-SQLite/blob/master/dhtWebServer/templates/index.html">index.html</a> from my GitHub.</p><h4>style.css</h4><pre>body{<br>	background: blue;<br>	color: yellow;<br>	padding:1%<br>}</pre><pre>.button {<br>	font: bold 15px Arial;<br>	text-decoration: none;<br>	background-color: #EEEEEE;<br>	color: #333333;<br>	padding: 2px 6px 2px 6px;<br>	border-top: 1px solid #CCCCCC;<br>	border-right: 1px solid #333333;<br>	border-bottom: 1px solid #333333;<br>	border-left: 1px solid #CCCCCC;<br>}</pre><p>You can get the file <a href="https://github.com/Mjrovai/RPI-Flask-SQLite/blob/master/dhtWebServer/static/style.css">style.css</a> from my GitHub. The files must be placed in your directory like this:</p><pre>├── Sensors_Database<br>       ├── sensorsData.db<br>       ├── logDHT.py<br>       ├── dhtWebSensor<br>               ├── appDhtWebSensor.py<br>               ├── templates<br>	       │      ├── index.html<br>               └── static<br>                      ├── style.css</pre><p>Now, run the python script on the Terminal:</p><pre>sudo python3 appDhtWebServer.py</pre><p>Go to any browser in your network and enter with <em>http://YOUR_RPI_IP</em> (for example, in my case: <a href="http://10.0.1.27/">http://10.0.1.27</a>)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/0*0CvvprcPXXwpXpWd." /></figure><p>The above print screen shows what you must see. NOTE: If you are not sure about your RPi Ip address, run on your terminal:</p><pre>ifconfig</pre><p>At wlan0: section you will find it. In my case: 10.0.1.27</p><h3>13. Making Our Web Front-End Fancier!</h3><p>Let’s introduce some Gages to present actual Temperature and Humidity values on a better way. Note that our Python script will not change, but using <a href="http://justgage.com/">JustGage </a>on our html/css files, will improve a lot how data will be presented.</p><h4>What is JustGage?</h4><p>JustGage is a handy JavaScript plugin for generating and animating nice &amp; clean gauges. It is based on Raphaël library for vector drawing, so it’s completely resolution independent and self-adjusting, working in almost any browser.</p><h4>Installation:</h4><ul><li>Download JustGage v1.2.2 + Examples from JustGage website ==&gt; <a href="http://justgage.com/download/justgage-1.2.2.zip">http://justgage.com/download/justgage-1.2.2.zip</a></li><li>Save the 2 .js files on /static/ directory</li><li>Use the new index.html below:</li></ul><p><a href="https://halckemy.s3.amazonaws.com/uploads/attachments/407327/justgage-1.2.2.zip">justgage-1.2.2.zip</a></p><pre>&lt;!doctype html&gt;<br>&lt;html&gt;<br>&lt;head&gt;<br>    &lt;title&gt;DHT Data Sensor&lt;/title&gt;<br>    &lt;link rel=&quot;stylesheet&quot; href=&#39;../static/style.css&#39;/&gt;<br>    &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot; /&gt;<br>    &lt;style&gt;<br>	body {<br>	    text-align: center;<br>	}<br>	#g1,<br>	#g2 {<br>	    width: 200px;<br>	    height: 160px;<br>	    display: inline-block;<br>	    margin: 1em;<br>	}<br>   &lt;/style&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>    &lt;h1&gt;DHT Sensor Data &lt;/h1&gt;<br>    &lt;div id=&quot;g1&quot;&gt;&lt;/div&gt;<br>    &lt;div id=&quot;g2&quot;&gt;&lt;/div&gt;<br>    &lt;hr&gt;<br>	&lt;h3&gt; Last Sensors Reading: {{ time }} ==&gt; &lt;a href=&quot;/&quot;class=&quot;button&quot;&gt;REFRESH&lt;/a&gt;&lt;/h3&gt;	<br>	&lt;hr&gt;<br>	&lt;p&gt; @2018 Developed by MJRoBot.org&lt;/p&gt;<br>    <br>    &lt;script src=&quot;../static/raphael-2.1.4.min.js&quot;&gt;&lt;/script&gt;<br>    &lt;script src=&quot;../static/justgage.js&quot;&gt;&lt;/script&gt;<br>    &lt;script&gt;<br>	var g1, g2;<br>	document.addEventListener(&quot;DOMContentLoaded&quot;, function(event) {<br>	    g1 = new JustGage({<br>		id: &quot;g1&quot;,<br>		value: {{temp}},<br>		valueFontColor: &quot;yellow&quot;,<br>		min: -10,<br>		max: 50,<br>		title: &quot;Temperature&quot;,<br>		label: &quot;Celcius&quot;<br>	});<br>	    g2 = new JustGage({<br>		id: &quot;g2&quot;,<br>		value: {{hum}},<br>		valueFontColor: &quot;yellow&quot;,<br>		min: 0,<br>		max: 100,<br>		title: &quot;Humidity&quot;,<br>		label: &quot;%&quot;<br>	});<br>      });<br>    &lt;/script&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;</pre><p>Download from my GitHub the file: <a href="https://github.com/Mjrovai/RPI-Flask-SQLite/blob/master/dhtWebServer/templates/index_gage.html">index_gage.html</a>, and rename it as index.html (do not forget of renaming the previous one with a different name if you want to keep it, for example, index_txt.html).</p><p>The final directory tree should look like as below:</p><pre>├── Sensors_Database       <br>       ├── sensorsData.db<br>       ├── logDHT.py<br>       ├── dhtWebServer<br>               ├── appDhtWebServer.py<br>               ├── templates<br>	       │      ├── index.html<br>               └── static<br>                      ├── style.css<br>                      ├── justgage.js<br>		      ├── raphael-2.1.4.min.js</pre><p>Press[Crl-C] on your Terminal to Quit appDhtWebServer.py and just start it again. When you refresh your browser you should see your website like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/545/0*wYnwmy4GPpaBUrVj." /></figure><p>Look the examples files that you downloaded from JustGage website. Try make changes on your gages. It is very simple.</p><h3>14. The Full Proccess</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/0*OblyxewYHZPqfw_D." /></figure><p>The above diagram resumes what we have accomplished so far: 2 separate scripts running in parallel, doing their tasks independently:</p><p>Capturing data with sensor and load them into a database (<em>logDHT.py</em>)Look for data on the database and present them on a web front-end (<em>appDhtWebServer.py</em>).</p><p>In general terms our project of capture data, saving them on a database and displaying those data on a webpage is finish. But make no sense to have a database with historical data and only use it for the last data captured. We must play with historical data and the most basic thing to do is presented them on a graph. Let’s go to it!</p><h3>15. Graphing the Historical Data</h3><p>A very good library to graph data is <a href="https://matplotlib.org/">Matplotlib</a><em>, </em>that is a Python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms.</p><p>To install <em>matplotlib</em>, run the command below on your Terminal:</p><pre>sudo apt-get install python3-matplotlib</pre><p>Before we start, let’s create a new environment, where we will save the new application to be developed: <em>appDhtWebHist.py</em> and its correspondent <em>index.html</em>and <em>style.css</em></p><pre>├── Sensors_Database<br>       ├── sensorsData.db<br>       ├── logDHT.py<br>       ├── dhtWebHist<br>               ├── appDhtWebHist.py<br>               ├── templates<br>	       │      ├── index.html<br>               └── static<br>                      ├── style.css</pre><p>Create the new 3 directories (<em>dhtWebHist; /templates</em> and <em>/static</em>) same as we did before and open from my GitHub the 3 files below:</p><p>1. <a href="https://github.com/Mjrovai/RPI-Flask-SQLite/blob/master/dhtWebHist/appDhtWebHist.py">appDhtWebHist.py</a></p><pre>from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas<br>from matplotlib.figure import Figure<br>import io<br>from flask import Flask, render_template, send_file, make_response, request<br>app = Flask(__name__)</pre><pre>import sqlite3<br>conn=sqlite3.connect(&#39;../sensorsData.db&#39;)<br>curs=conn.cursor()</pre><pre># Retrieve LAST data from database<br>def getLastData():<br>	for row in curs.execute(&quot;SELECT * FROM DHT_data ORDER BY timestamp DESC LIMIT 1&quot;):<br>		time = str(row[0])<br>		temp = row[1]<br>		hum = row[2]<br>	#conn.close()<br>	return time, temp, hum</pre><pre>def getHistData (numSamples):<br>	curs.execute(&quot;SELECT * FROM DHT_data ORDER BY timestamp DESC LIMIT &quot;+str(numSamples))<br>	data = curs.fetchall()<br>	dates = []<br>	temps = []<br>	hums = []<br>	for row in reversed(data):<br>		dates.append(row[0])<br>		temps.append(row[1])<br>		hums.append(row[2])<br>	return dates, temps, hums</pre><pre>def maxRowsTable():<br>	for row in curs.execute(&quot;select COUNT(temp) from  DHT_data&quot;):<br>		maxNumberRows=row[0]<br>	return maxNumberRows</pre><pre># define and initialize global variables<br>global numSamples<br>numSamples = maxRowsTable()<br>    if (numSamples &gt; 101):<br>numSamples = 100</pre><pre># main route<br>@app.route(&quot;/&quot;)<br>def index():<br>	time, temp, hum = getLastData()<br>	templateData = {<br>	  	&#39;time&#39;	: time,<br>		&#39;temp&#39;	: temp,<br>      		&#39;hum&#39;	: hum,<br>      		&#39;numSamples&#39;	: numSamples<br>	}<br>	return render_template(&#39;index.html&#39;, **templateData)</pre><pre>@app.route(&#39;/&#39;, methods=[&#39;POST&#39;])<br>def my_form_post():<br>    global numSamples<br>    numSamples = int (request.form[&#39;numSamples&#39;])<br>    numMaxSamples = maxRowsTable()<br>    if (numSamples &gt; numMaxSamples):<br>        numSamples = (numMaxSamples-1)<br>    time, temp, hum = getLastData()<br>    templateData = {<br>	  	&#39;time&#39;	: time,<br>      		&#39;temp&#39;	: temp,<br>      		&#39;hum&#39;	: hum,<br>      		&#39;numSamples&#39;	: numSamples<br>	}<br>    return render_template(&#39;index.html&#39;, **templateData)</pre><pre>@app.route(&#39;/plot/temp&#39;)<br>def plot_temp():<br>	times, temps, hums = getHistData(numSamples)<br>	ys = temps<br>	fig = Figure()<br>	axis = fig.add_subplot(1, 1, 1)<br>	axis.set_title(&quot;Temperature [°C]&quot;)<br>	axis.set_xlabel(&quot;Samples&quot;)<br>	axis.grid(True)<br>	xs = range(numSamples)<br>	axis.plot(xs, ys)<br>	canvas = FigureCanvas(fig)<br>	output = io.BytesIO()<br>	canvas.print_png(output)<br>	response = make_response(output.getvalue())<br>	response.mimetype = &#39;image/png&#39;<br>	return response</pre><pre>@app.route(&#39;/plot/hum&#39;)<br>def plot_hum():<br>	times, temps, hums = getHistData(numSamples)<br>	ys = hums<br>	fig = Figure()<br>	axis = fig.add_subplot(1, 1, 1)<br>	axis.set_title(&quot;Humidity [%]&quot;)<br>	axis.set_xlabel(&quot;Samples&quot;)<br>	axis.grid(True)<br>	xs = range(numSamples)<br>	axis.plot(xs, ys)<br>	canvas = FigureCanvas(fig)<br>	output = io.BytesIO()<br>	canvas.print_png(output)<br>	response = make_response(output.getvalue())<br>	response.mimetype = &#39;image/png&#39;<br>	return response</pre><pre>if __name__ == &quot;__main__&quot;:<br>   app.run(host=&#39;0.0.0.0&#39;, port=80, debug=False)</pre><p>A new function was created here: <em>getHistData (numSamples)</em>, that receives as a parameter the number of rows that should be taken from the database. Basically, it is very similar to getLastData(), where <em>numSamples</em> was “1”. Of course, now we must “append” the return array for all required rows.</p><p>In fact, we could use only this last function for both tasks.</p><p>The number of samples is set by default as 100, at the beginning (if there are more them 100 rows into the database) and also received it as an input from the webpage, during normal operation. When we receive the number of samples to be retrieved, we must also check if it is lower than the maximum number of rows in the database (otherwise we will get an error). The function <em>maxRowsTable()</em>, returns this number.</p><p>With the historical data in hand: <em>times, temps </em>and<em> hums</em> that are arrays, we must build the graphs saving them as a <em>.png ímage</em>. Those images will be the return for the routes:</p><p><em>@app.route(‘/plot/temp’)</em> and <em>@app.route(‘/plot/hum’).</em></p><p>The request for the images is done by<em> index.html, by the IMG TAG.</em></p><p>2. <a href="https://github.com/Mjrovai/RPI-Flask-SQLite/blob/master/dhtWebHist/templates/index.html">index.html</a></p><pre>&lt;!DOCTYPE html&gt;<br>   &lt;head&gt;<br>      &lt;title&gt;DHT Sensor data &lt;/title&gt;<br>      &lt;link rel=&quot;stylesheet&quot; href=&#39;../static/style.css&#39;/&gt;<br>   &lt;/head&gt;<br>   &lt;body&gt;<br>	&lt;h1&gt;DHT Sensor Data &lt;/h1&gt;<br>	&lt;h3&gt; TEMPERATURE   ==&gt;  {{ temp  }} oC&lt;/h3&gt;<br>	&lt;h3&gt; HUMIDITY (Rel.) ==&gt;  {{ hum  }} %&lt;/h3&gt;<br>	&lt;hr&gt;	<br>	&lt;h3&gt; Last Sensors Reading: {{ time }} ==&gt; &lt;a href=&quot;/&quot;class=&quot;button&quot;&gt;REFRESH&lt;/a&gt;&lt;/h3&gt;	<br>	&lt;hr&gt;<br>	&lt;h3&gt; HISTORICAL DATA &lt;/h3&gt;<br>	&lt;p&gt; Enter number of samples to retrieve:<br>	&lt;form method=&quot;POST&quot;&gt;<br>		&lt;input name=&quot;numSamples&quot; value= {{numSamples}}&gt;<br>		&lt;input type=&quot;submit&quot;&gt;<br>		&lt;/form&gt;&lt;/p&gt;<br>	&lt;hr&gt;<br>	&lt;img src=&quot;/plot/temp&quot; alt=&quot;Image Placeholder&quot; width=&quot;49%&quot;&gt;<br>	&lt;img src=&quot;/plot/hum&quot; alt=&quot;Image Placeholder&quot; width=&quot;49%&quot;&gt;<br>	&lt;p&gt; @2018 Developed by MJRoBot.org&lt;/p&gt;<br>   &lt;/body&gt;<br>&lt;/html&gt;</pre><p>3. <a href="https://github.com/Mjrovai/RPI-Flask-SQLite/blob/master/dhtWebHist/static/style.css">style.css</a></p><pre>body{<br>	background: blue;<br>	color: yellow;<br>	padding:1%<br>}</pre><pre>.button {<br>	font: bold 15px Arial;<br>	text-decoration: none;<br>	background-color: #EEEEEE;<br>	color: #333333;<br>	padding: 2px 6px 2px 6px;<br>	border-top: 1px solid #CCCCCC;<br>	border-right: 1px solid #333333;<br>	border-bottom: 1px solid #333333;<br>	border-left: 1px solid #CCCCCC;<br>}</pre><pre>img{<br>	display: display: inline-block<br>}</pre><p>The above print screen shows the result.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/363/0*6l3jVrYote6h80xJ." /></figure><h3>Step 16. Including Gage on History Webpage</h3><p>If instead of text, you want also to include gages to display the actual data, you must have the 2 .js files that you have used before on <a href="https://github.com/Mjrovai/RPI-Flask-SQLite/tree/master/dhtWebHist/static">/static </a>and change the index.html file on <a href="https://github.com/Mjrovai/RPI-Flask-SQLite/tree/master/dhtWebHist/templates">/templates:</a></p><p>Below how the directory tree looks like:</p><pre>├── Sensors_Database<br>       ├── sensorsData.db<br>       ├── logDHT.py<br>       ├── dhtWebHist<br>               ├── appDhtWebHist.py<br>               ├── templates<br>	       │      ├── index.html<br>               └── static<br>                      ├── style.css<br>                      ├── justgage.js<br>		      ├── raphael-2.1.4.min.js</pre><p>From my GitHub, open <a href="https://github.com/Mjrovai/RPI-Flask-SQLite/blob/master/dhtWebHist/templates/index_gage.html">index_gage.html</a> and rename it index.html. Replace the actual index.html (text version) and voilá! You will get a beautiful webpage, showing as gages the last captured data of temperature and humidity by the DHT22 and the historical graphs of those data.</p><p>Press[Crl-C] on your Terminal to Quit appDhtWebServer.py and just start it again. When you refresh your browser you must see:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/307/0*NdRD3hIOjTonFUoT." /></figure><h3>17. Retrieving Data by Time Instead of Samples</h3><p>So far we have build our graphics based on historical data, sending as a input parameter the numbers of samples to be retrieved from our database. Alternatively we could use as a parameter the number of past minutes that we want to show on a graph.</p><p>In order to do that, the first thing to know is the frequency of logged data on our database. Remember that this task is done for an independent program (in our case, <em>logDHT.py</em>). One simple way to finfd this frequency is to retrieve the last 2 data logged on database and subtracting their correspondent timeStamp data:</p><p>in general terms: <em>frequency = timeStamp(1) — timeStamp(0)</em></p><p>The function below does the work for us, using “datetime.striptime()”:</p><pre># Get sample frequency in minutes<br>def freqSample():<br>	times, temps, hums = getHistData (2)<br>	fmt = &#39;%Y-%m-%d %H:%M:%S&#39;<br>	tstamp0 = datetime.strptime(times[0], fmt)<br>	tstamp1 = datetime.strptime(times[1], fmt)<br>	freq = tstamp1-tstamp0<br>	freq = int(round(freq.total_seconds()/60))<br>	return (freq)</pre><p>Once we we have this frequency parameter in minutes, we will show it on index.html and asking for a “rangeTime” number of minutes to be send back to our server ==&gt; @app.route(‘/’, methods=[‘POST’]):</p><pre>@app.route(&#39;/&#39;, methods=[&#39;POST&#39;])<br>def my_form_post():<br>   global numSamples <br>   global freqSamples<br>   global rangeTime<br>   rangeTime = int (request.form[&#39;rangeTime&#39;])<br>   if (rangeTime &lt; freqSamples):<br>       rangeTime = freqSamples + 1<br>   numSamples = rangeTime//freqSamples<br>   numMaxSamples = maxRowsTable()<br>   if (numSamples &gt; numMaxSamples):<br>       numSamples = (numMaxSamples-1)</pre><p>The picture shows the result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/310/0*ezseDzEmK_Ua_2AA." /></figure><p><strong>Eliminating Possible errors when constructing the graphs:</strong></p><p>Ocasionally, strange (or corrupted) data can be storaged on database, jeopardizing our analysis. Those data can be verified (or cleaneed) on several places (like during the time sensor are capturing the data, etc). But once the script that display data is independent of the one that logged the data, let’s “cap” the maximum and minimum values of our sensors, before using the data to buit the graphs. This can be achieved with the function <em>testData(temps, hums)</em>:</p><pre># Test data for cleanning possible &quot;out of range&quot; values<br>def testeData(temps, hums):<br>	n = len(temps)<br>	for i in range(0, n-1):<br>		if (temps[i] &lt; -10 or temps[i] &gt;50):<br>			temps[i] = temps[i-2]<br>		if (hums[i] &lt; 0 or hums[i] &gt;100):<br>			hums[i] = temps[i-2]<br>	return temps, hums</pre><p>The scripts for this new version can be download from my GitHub: <a href="https://github.com/Mjrovai/RPI-Flask-SQLite/tree/master/dhtWebHist_v2">dhtWebHist_v2</a></p><h3>18. Conclusion</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/443/0*8o4CvhX_dCiVGtAz." /></figure><p>As always, I hope this project can help others find their way into the exciting world of electronics!</p><p>For details and final code, please visit my GitHub depository: <a href="https://github.com/Mjrovai/RPI-Flask-SQLite">RPI-Flask-SQLite</a></p><p>For more projects, please visit my blog: <a href="https://mjrobot.org/">MJRoBot.org</a></p><p>Saludos from the south of the world!</p><p>See you at my next tutorial!</p><p>Thank you,</p><p>Marcelo</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8b967776c25f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mjrobot-org/from-data-to-graph-a-web-jorney-with-flask-and-sqlite-8b967776c25f">From Data to Graph: a Web Jorney With Flask and SQLite</a> was originally published in <a href="https://medium.com/mjrobot-org">MJRoBot.org</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>