{"id":161,"date":"2017-02-05T21:24:40","date_gmt":"2017-02-06T04:24:40","guid":{"rendered":"http:\/\/vinodsr.com\/myblog\/?p=161"},"modified":"2021-07-25T08:17:00","modified_gmt":"2021-07-25T15:17:00","slug":"my-experiments-with-iot","status":"publish","type":"post","link":"https:\/\/vinodsr.com\/myblog\/2017\/02\/my-experiments-with-iot\/","title":{"rendered":"My experiments with IoT &#8211; &#8220;Meeting with Pi&#8221;"},"content":{"rendered":"<table style=\"padding-left: 30px;\">\n<tbody style=\"padding-left: 30px;\">\n<tr style=\"padding-left: 30px;\">\n<td style=\"border-color: #454545; padding-left: 30px;\">\n<p style=\"padding-left: 30px;\"><strong>Ingredients<\/strong>: \u00a0Little bit of nodeJS, \u00bd teaspoon raspberry pi. \ud83d\ude42<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-family: 'Scope One';\"><span style=\"font-size: 36px;\">W<\/span>hen I heard the word IoT from one of my colleagues back in 2012, it was just like a piece of tech which seems to be very far from my world. Later when I started working with Arduino and raspberry pi in 2015, I realized the potential.<\/span><\/p>\n<h1><span style=\"font-family: 'Annie Use Your Telescope';\"><b>How it started<\/b><\/span><\/h1>\n<p><span style=\"font-family: 'Scope One';\">I am a graduate in computer science, with very little interest in electronics. During my college days, we had a paper on electronics and my score is &#8220;just pass&#8221;. My prime focus was behind programming languages and its concepts. Tried to learn many programming languages during the college days itself. It seems very easy for me to learn new languages as I tried to understand the concepts first, then the constructs.<\/span><\/p>\n<p><span style=\"font-family: 'Scope One';\">It was in 2015 when one of my colleagues\u00a0explained me about the working of raspberry pi. I was quite interested in it and there began the new phase of my learning.<\/span><\/p>\n<p><!--more--><\/p>\n<h1><strong><span style=\"font-family: 'Annie Use Your Telescope';\">The IoT Life\u00a0<\/span><\/strong><\/h1>\n<p><span style=\"font-family: 'Scope One';\">After hearing about raspberry pi, I decided to buy a pie. After a little research purchased a raspberry pi from amazon. My initial experiments included connecting the pi to my tv using HDMI and try different OS in it; not limited to raspbian ( default OS). Also tried berryboot, retro pie, XMBC in my PI. Used Pi for playing games as well as using it as a media hub.<\/span><\/p>\n<p><span style=\"font-family: 'Scope One';\">Sooner I realized that I was deviating from the purpose that I bought Pi. All these things anyone can do. I need to try something unique, something innovative. This thought had driven me to the things world.<\/span><\/p>\n<h1><strong><span style=\"font-family: 'Annie Use Your Telescope';\">Who can learn IOT?<\/span><\/strong><\/h1>\n<p><span style=\"font-family: 'Scope One';\">Anyone with fair knowledge of c++ \/ nodeJS, python, with a little common sense can create a world of things<\/span><\/p>\n<h1><strong><span style=\"font-family: 'Annie Use Your Telescope';\">Let&#8217;s begin<\/span><\/strong><\/h1>\n<p><span style=\"font-family: 'Scope One';\">The basics that you need to understand here is current flows in a closed loop and not in an open loop. If you place a led in a circuit between the positive and ground terminal, led turn on.<\/span><br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-198\" src=\"http:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/02\/Closed-Loop.png\" alt=\"\" width=\"828\" height=\"375\" srcset=\"https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/02\/Closed-Loop.png 828w, https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/02\/Closed-Loop-300x136.png 300w, https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/02\/Closed-Loop-768x348.png 768w\" sizes=\"auto, (max-width: 828px) 100vw, 828px\" \/><\/p>\n<p><span style=\"font-family: 'Scope One';\">In other words, binary one (1) can be used to represent where there is a voltage(+) and a binary zero(0) for ground.\u00a0These 1&#8217;s and 0&#8217;s are the basics of electronics circuit and it is the basic building block of anything (IOT).\u00a0<\/span><\/p>\n<h1><strong><span style=\"font-family: 'Annie Use Your Telescope';\">Basics of Raspberry Pi<\/span><\/strong><\/h1>\n<p><span style=\"font-family: 'Scope One';\">There are different types of raspberry pi like Pi 2, Pi 3, Pi Zero etc.. Each of these differs each other with specifications like chipset, RAM size, USB ports, display ports etc. But in general, they have a set of pins which is called as GPIO (General Purpose Input Output) pins. Apart from the GPIO pins, there are specific purpose pins like clock pin, Ground pins, 5V Pin, 3.3V Pin. these pins have a specific purpose, where we can programmatically set the GPIO pin to the high state or low state. \u00a0We can use c++, nodeJS, python for controlling the GPIO pins.\u00a0<\/span><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.jameco.com\/Jameco\/workshop\/circuitnotes\/raspberry_pi_circuit_note_fig2a.jpg\" alt=\"Image result for raspberry pi 2 model b pinout\" \/><\/p>\n<p style=\"padding-left: 30px;\"><span style=\"font-size: 10px; color: #808000;\">Image source :\u00a0http:\/\/www.jameco.com\/Jameco\/workshop\/circuitnotes\/raspberry-pi-circuit-note.html<\/span><\/p>\n<p><span style=\"font-family: 'Scope One';\">Each GPIO pin has a number associated with it. See the above pinout diagram for more details. <\/span><\/p>\n<p><span style=\"font-family: 'Scope One';\">For now, we will use a small nodeJS script to work with Pi with the help of the rpio library (<a href=\"https:\/\/github.com\/jperkin\/node-rpio\">https:\/\/github.com\/jperkin\/node-rpio<\/a>)<\/span><span style=\"font-family: 'Scope One';\">. <\/span><\/p>\n<p><span style=\"font-family: 'Scope One';\">To install rpio library\u00a0<\/span><\/p>\n<pre class=\"lang:sh decode:true\">npm install rpio<\/pre>\n<p><span style=\"font-family: 'Scope One';\">Below is a small nodeJS snippet to set the GPIO pin 12 ( GPIO 18 ) to high then to low 5 times every 500 milliseconds.\u00a0<\/span><\/p>\n<blockquote>\n<pre class=\"lang:js decode:true\">var rpio = require('rpio');\n\nrpio.open(12, rpio.OUTPUT, rpio.LOW);\n\nfor (var i = 0; i &lt; 5; i++) {\n\/* On for 1 second *\/\nrpio.write(12, rpio.HIGH);\nrpio.sleep(1);\n\n\/* Off for half a second (500ms) *\/\nrpio.write(12, rpio.LOW);\nrpio.msleep(500);\n}<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-199\" src=\"http:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/02\/PI-LED.png\" alt=\"\" width=\"1485\" height=\"1101\" srcset=\"https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/02\/PI-LED.png 1485w, https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/02\/PI-LED-300x222.png 300w, https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/02\/PI-LED-768x569.png 768w, https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/02\/PI-LED-1024x759.png 1024w\" sizes=\"auto, (max-width: 1485px) 100vw, 1485px\" \/><\/p><\/blockquote>\n<p><span style=\"font-family: 'Scope One';\">So if I connect a LED pin to the GPIO pin 12 ( GPIO, then LED will blink at an interval of 2 seconds<\/span><\/p>\n<p><span style=\"font-family: 'Scope One';\">wow! \u00a0We did our hello world on raspberry pi.<\/span><\/p>\n<p><span style=\"font-family: 'Scope One';\">Coming back to how it worked,\u00a0<\/span><\/p>\n<pre class=\"lang:js decode:true\">rpio.open(12, rpio.OUTPUT, rpio.LOW);\n<\/pre>\n<p><span style=\"font-family: 'Scope One';\">The above code initializes pin 12 to output mode and sets the default value as zero.<\/span><\/p>\n<pre class=\"lang:js decode:true\">rpio.write(12, rpio.HIGH);<\/pre>\n<p><span style=\"font-family: 'Scope One';\">The write method then keeps the pins high and low periodically, which turns the led blink.<\/span><\/p>\n<h1><strong><span style=\"font-family: 'Annie Use Your Telescope';\">Processing input<\/span><\/strong><\/h1>\n<p><span style=\"font-family: 'Scope One';\">Now we have seen how to do output, let&#8217;s see how to control our logic. \u00a0The same pin can be used as an input pin also.\u00a0It is the reverse of what we did for blinking. We need to get the input from a button and if it is clicked then the led needs to be turned on.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-203\" src=\"http:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/02\/PI-LED-BUTON.png\" alt=\"\" width=\"1485\" height=\"1101\" srcset=\"https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/02\/PI-LED-BUTON.png 1485w, https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/02\/PI-LED-BUTON-300x222.png 300w, https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/02\/PI-LED-BUTON-768x569.png 768w, https:\/\/vinodsr.com\/myblog\/wp-content\/uploads\/2017\/02\/PI-LED-BUTON-1024x759.png 1024w\" sizes=\"auto, (max-width: 1485px) 100vw, 1485px\" \/><\/p>\n<pre class=\"lang:js decode:true\">var rpio = require('rpio');\n\n\nrpio.open(15, rpio.INPUT, rpio.PULL_DOWN);\nrpio.open(12, rpio.OUTPUT, rpio.LOW);\n\nfunction pollcb(pin)\n{\n        var state = rpio.read(pin) ? rpio.HIGH :rpio.LOW ;\n        rpio.write(12,state);\n}\n\n\nrpio.poll(15, pollcb);\n<\/pre>\n<p><span style=\"font-family: 'Scope One';\">On clicking on the button, it will make the GPIO pin 15 high, which is captured as a interrupt and the logic is executed, which will turn the led on.<\/span><\/p>\n<p><span style=\"font-family: 'Scope One';\">As you see the way of initialization and usage is almost similar<\/span><\/p>\n<p><span style=\"font-family: 'Scope One';\">This way you can control almost any sensor using raspberry pi \/ Arduino.\u00a0<\/span><\/p>\n<p><span style=\"font-family: 'Scope One';\">I will be coming up with a hello world post for Arduino.\u00a0<\/span><\/p>\n<p><span style=\"font-family: 'Scope One';\">Please share your feedback through the comment box.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ingredients: \u00a0Little bit of nodeJS, \u00bd teaspoon raspberry pi. \ud83d\ude42 When I heard the word IoT from one of my colleagues back in 2012, it&hellip;<\/p>\n","protected":false},"author":1,"featured_media":203,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[56],"tags":[53,54,55,52,51],"class_list":["post-161","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-iot","tag-arduino","tag-experiments","tag-hello-world","tag-iot","tag-raspberry-pi","has-post-thumbnail-archive"],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/posts\/161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/comments?post=161"}],"version-history":[{"count":34,"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/posts\/161\/revisions"}],"predecessor-version":[{"id":371,"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/posts\/161\/revisions\/371"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/media\/203"}],"wp:attachment":[{"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/media?parent=161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/categories?post=161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vinodsr.com\/myblog\/wp-json\/wp\/v2\/tags?post=161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}