{"id":304,"date":"2018-02-21T03:25:20","date_gmt":"2018-02-20T20:25:20","guid":{"rendered":"http:\/\/class.ajarnsunny.com\/?page_id=304"},"modified":"2018-02-22T03:16:50","modified_gmt":"2018-02-21T20:16:50","slug":"web-server-and-gpios","status":"publish","type":"page","link":"http:\/\/class.ajarnsunny.com\/?page_id=304","title":{"rendered":"Web Server and GPIOs"},"content":{"rendered":"<h3 style=\"text-align: center;\"><span style=\"color: #000080;\"><strong>Web Server and GPIOs<\/strong><\/span><\/h3>\n<p><span style=\"color: #000080;\"><strong>Installation of Setuptools<\/strong><\/span><br \/>\nIn order to use packages that are <strong>not<\/strong> <strong>preinstalled<\/strong> in Python, such as the Twython package for Twitter or the WebOb package for the Webserver, we need first to install <strong>Setuptools<\/strong>. After installing Setuptools, we can download and then install the desired packages by using the <strong>install<\/strong> command. Please note that the procedure for installing Setuptools is different on Linux and window.<\/p>\n<p><em><strong>Installation of Setuptools on <span style=\"color: #000080;\">Linux<\/span> (for Raspberry Pi and Ubuntu):<\/strong><\/em><br \/>\n1. Download <a href=\"https:\/\/pypi.python.org\/packages\/ba\/2c\/743df41bd6b3298706dfe91b0c7ecdc47f2dc1a3104abeb6e9aa4a45fa5d\/ez_setup-0.9.tar.gz\"><strong>ez setup.py<\/strong><\/a> and save it on the Desktop.<br \/>\n2. Open the terminal and &#8220;go to the Desktop directory&#8221;, that is, use the cd command to change the directory to <strong>&#8230;\/Desktop<\/strong>.<br \/>\n3. In the terminal type:<br \/>\n<strong>sudo python3 setup.py install<\/strong><\/p>\n<p><strong>To know more:<\/strong><br \/>\nIf you want to know more about software packages available for Python look at the page https:\/\/pypi.python.org\/pypi.<\/p>\n<p><span style=\"color: #000080;\"><strong>Web Server<\/strong><\/span><br \/>\nBefore running the programs of this section we need to install the WebOb package. Make sure <strong>Setuptools<\/strong> is installed before trying to install <strong>WebOb<\/strong>.<\/p>\n<p><em><strong>Installation of the WebOb Package on Linux (for Raspberry Pi and Ubuntu)<\/strong><\/em><br \/>\n1. Go to the<strong><a href=\"https:\/\/pypi.python.org\/pypi\/WebOb\"> WebOb package page<\/a><\/strong> and download the latest version to the Desktop (you need to click on the green button to start the download). Expand the archive. After expanding the archive you should have on the desktop the folder WebOb-1.7.4<br \/>\n2. Open the terminal and &#8220;go to the WebOb-1.7.4 directory&#8221;, that is, use the cd command to change the directory to &#8230;\/Desktop\/WebOb-1.7.4<br \/>\n3. In the terminal type:<br \/>\n<strong>sudo python3 setup.py install<\/strong><\/p>\n<p><span style=\"color: #000080;\"><strong>Example 1<\/strong><\/span><br \/>\nTry the following program (<a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/WebHello.txt\">file Example 1<\/a>).<\/p>\n<p><span style=\"font-size: xx-small;\"><b><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"http:\/\/itschool2.mfu.ac.th\/chayapol\/RPi\/pict_Web%20Server\/Example1.png\" alt=\"\" width=\"568\" height=\"411\" \/><\/b><\/span><\/p>\n<p>Run the program and find the <strong>ip address<\/strong> of the computer on which the program is running. In the browser of another computer connected to the same network type:<br \/>\n<strong>http:\/\/ip address:8080<\/strong><\/p>\n<p>If you are testing the program using the browser <strong>on the same computer in which the program runs<\/strong>, you can write:<br \/>\n<strong>http:\/\/localhost:8080<\/strong><\/p>\n<p><span style=\"color: #000080;\"><strong>Example 2<\/strong><\/span><br \/>\nThe following program (<a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/WebTripleQuotes.txt\">file Example 2<\/a>) shows how to easily include inside a python program proper html code spanning several lines. In order to create a string that spans several lines just use the triple quote &#8220;&#8221;&#8221; as shown in this program.<\/p>\n<p><span style=\"font-size: xx-small;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"http:\/\/itschool2.mfu.ac.th\/chayapol\/RPi\/pict_Web%20Server\/PictExample2.png\" alt=\"\" width=\"523\" height=\"578\" \/><\/span><\/p>\n<p><span style=\"color: #000080;\"><strong>Example 3<\/strong><\/span><br \/>\nTry the following program (<a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/WebLikeYesNo.txt\">file Example 3<\/a>):<\/p>\n<p><span style=\"font-size: xx-small;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"http:\/\/itschool2.mfu.ac.th\/chayapol\/RPi\/pict_Web%20Server\/PictExample3.png\" alt=\"\" width=\"643\" height=\"645\" \/><\/span><\/p>\n<p><span style=\"color: #000080;\"><strong>Example 4<\/strong><\/span><br \/>\nTry the following program (<a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/WebNameSurname.txt\">file Example 4<\/a>):<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"http:\/\/itschool2.mfu.ac.th\/chayapol\/RPi\/pict_Web%20Server\/PictExample4.png\" alt=\"\" width=\"643\" height=\"644\" \/><\/p>\n<p><span style=\"color: #000080;\"><strong>Example 4.1<\/strong><\/span><br \/>\nMake the following connections (<a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/ex_4_1_A_pict.jpg\">see picture<\/a>):<\/p>\n<p>Connect <strong>PIN 25 (GROUND)<\/strong> of the Raspberry Pi to the <strong>GND<\/strong> of the experiment board.<br \/>\nConnect <strong>PIN 22<\/strong> of the Raspberry Pi to <strong>LED 0<\/strong> of the Lower Monitor on the experiment board.<\/p>\n<p>This program shows how to turn on and off <strong>LED 0<\/strong> remotely using the Web Server (<a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/ex4_1led.txt\">file example 4.1<\/a> and <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/ex_4_1_video.mp4\">video<\/a>).<\/p>\n<p>from webob import Request, Response<br \/>\nimport RPi.GPIO as GPIO # Import library for GPIO (pin) access<br \/>\nGPIO.setmode(GPIO.BOARD) # Specify that we use the BOARD numbers (pin numbers 1-26 on the connector)<br \/>\nGPIO.setwarnings(False) # Disable the warning &#8220;This channel is already in use&#8221;<\/p>\n<p>GPIO.setup(22, GPIO.OUT) # Configure pin 22 as an output<br \/>\nGPIO.output(22, 0) # Turn off the LED when the program starts<\/p>\n<p>class WebApp(object):<\/p>\n<p style=\"padding-left: 30px;\">def __call__(self, environ, start_response):<\/p>\n<p style=\"padding-left: 60px;\"># capture the request coming from the browser<br \/>\nreq = Request(environ)<br \/>\nprint(&#8220;**** Serving request ****&#8221;)<br \/>\n# Look for on_off_variable in the request:<br \/>\nx = req.params.get(&#8216;on_off_variable&#8217;) # save content of on_off_variable in x<br \/>\nprint(x)<\/p>\n<p style=\"padding-left: 60px;\">if x == &#8220;on&#8221;: # if button ON pressed&#8230;<br \/>\nGPIO.output(22, 1) # turn on LED<\/p>\n<p style=\"padding-left: 60px;\">elif x == &#8220;off&#8221;: # if button OFF pressed&#8230;<br \/>\nGPIO.output(22, 0) # turn off LED<\/p>\n<p style=\"padding-left: 60px;\">html = &#8220;&#8221;&#8221;<\/p>\n<p>Turn on or off the led:<br \/>\n&lt;form method=&#8221;get&#8221;&gt;<br \/>\n&lt;button type=&#8221;submit&#8221; name=&#8221;on_off_variable&#8221; value=&#8221;on&#8221;&gt;ON&lt;\/button&gt;<br \/>\n&lt;button type=&#8221;submit&#8221; name=&#8221;on_off_variable&#8221; value=&#8221;off&#8221;&gt;OFF&lt;\/button&gt;<br \/>\n&lt;\/form&gt;<br \/>\n&#8220;&#8221;&#8221;<\/p>\n<p style=\"padding-left: 60px;\"># create and return the response to the browser<br \/>\nresp = Response(html)<br \/>\nreturn resp(environ, start_response)<\/p>\n<p>application = WebApp()<\/p>\n<p># main program &#8211; the web server<br \/>\nif __name__ == &#8216;__main__&#8217;:<\/p>\n<p style=\"padding-left: 30px;\">from wsgiref.simple_server import make_server<br \/>\nport = 8080<br \/>\nhttpd = make_server(&#8221;, port, application)<br \/>\nprint(&#8216;Serving HTTP on port %s&#8230;&#8217;%port)<br \/>\nhttpd.serve_forever()<\/p>\n<p><strong>Assignment 4.A<\/strong><br \/>\nConnect also <strong>LED 2<\/strong> and <strong>LED 1<\/strong> to <strong>pins 26<\/strong> and <strong>24<\/strong> respectively (<a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/as_4_A.jpg\">picture<\/a>). Modify the previous assignment adding four more buttons in order to control <strong>LED 2<\/strong>, <strong>LED 1<\/strong> and <strong>LED 0<\/strong> as shown in this <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/02\/as_4_A_video.mp4\">video<\/a>.<\/p>\n<div id=\"themify_builder_content-304\" data-postid=\"304\" class=\"themify_builder_content themify_builder_content-304 themify_builder\">\n\n    <\/div>\n<!-- \/themify_builder_content -->","protected":false},"excerpt":{"rendered":"<p>Web Server and GPIOs Installation of Setuptools In order to use packages that are not preinstalled in Python, such as the Twython package for Twitter or the WebOb package for the Webserver, we need first to install Setuptools. After installing Setuptools, we can download and then install the desired packages by using the install command. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-304","page","type-page","status-publish","hentry","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=\/wp\/v2\/pages\/304"}],"collection":[{"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=304"}],"version-history":[{"count":14,"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=\/wp\/v2\/pages\/304\/revisions"}],"predecessor-version":[{"id":331,"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=\/wp\/v2\/pages\/304\/revisions\/331"}],"wp:attachment":[{"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}