{"id":2139,"date":"2014-05-10T11:02:37","date_gmt":"2014-05-10T07:02:37","guid":{"rendered":"http:\/\/nayarweb.com\/blog\/?p=2139"},"modified":"2014-05-10T11:08:57","modified_gmt":"2014-05-10T07:08:57","slug":"control-raspberry-pis-gpio-with-rest-qt-webiopi","status":"publish","type":"post","link":"https:\/\/nayarweb.com\/blog\/2014\/control-raspberry-pis-gpio-with-rest-qt-webiopi\/","title":{"rendered":"Control Raspberry Pi&#8217;s GPIO with REST (Qt, WebIOPi)"},"content":{"rendered":"<h3>Abstract<\/h3>\n<p>To control the GPIO pins of my <em>raspberry pi<\/em>, I am using WebIOPi framework. You can thus have a web page to remotely control the GPIO from a laptop, mobile phone or anything web capable on &#8216;http:\/\/192.168.1.x:8000&#8217; where x is the Raspberry Pi on your LAN. You get an interface like this:<\/p>\n<p><center><a href=\"https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2014\/05\/snapshot10.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-2148\" src=\"https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2014\/05\/snapshot10-296x300.png\" alt=\"snapshot10\" width=\"296\" height=\"300\" srcset=\"https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2014\/05\/snapshot10-296x300.png 296w, https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2014\/05\/snapshot10.png 452w\" sizes=\"auto, (max-width: 296px) 100vw, 296px\" \/><\/a><\/center>But if you want to create a real application to control the GPIO, you gotta need some API.<\/p>\n<h3>WebIOPi REST API learnt from bash<\/h3>\n<p>WebIOPi provides a REST API. Here&#8217;s the API reference: https:\/\/code.google.com\/p\/webiopi\/wiki\/RESTAPI<br \/>\nI&#8217;m assuming you have already installed WebIOPi on your Raspberry Pi and the <em>daemon<\/em> is running.<\/p>\n<p><strong>1.Install <em>curl<\/em> on my laptop. <\/strong><br \/>\n<code>$ sudo apt-get install curl<\/code><\/p>\n<p><strong>2. Testing a few commands to see if work. <\/strong>(-u username:password)<br \/>\n2.1 Making GPIO 4 as OUT<\/p>\n<blockquote><p>HTTP GET \/GPIO\/(gpioNumber)\/function<br \/>\nReturns &#8220;in&#8221; or &#8220;out&#8221;<\/p><\/blockquote>\n<p><code>$ curl -v -X POST -u webiopi:raspberry http:\/\/192.168.1.x:8000\/GPIO\/4\/function\/out<\/code><\/p>\n<p>The web interface displays the change automatically.<\/p>\n<p><center><a href=\"https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2014\/05\/snapshot11.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-2149\" src=\"https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2014\/05\/snapshot11-278x300.png\" alt=\"snapshot11\" width=\"278\" height=\"300\" srcset=\"https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2014\/05\/snapshot11-278x300.png 278w, https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2014\/05\/snapshot11.png 425w\" sizes=\"auto, (max-width: 278px) 100vw, 278px\" \/><\/a><\/center>2.2 Outputting voltage on GPIO 4<\/p>\n<blockquote><p>HTTP POST \/GPIO\/(gpioNumber)\/value\/(0 or 1)<br \/>\nReturns new value : 0 or 1<\/p><\/blockquote>\n<p><code>$ curl -v -X POST -u webiopi:raspberry http:\/\/192.168.1.x:8000\/GPIO\/4\/value\/1<\/code><\/p>\n<p><center><a href=\"https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2014\/05\/snapshot12.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-2150\" src=\"https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2014\/05\/snapshot12-276x300.png\" alt=\"snapshot12\" width=\"276\" height=\"300\" srcset=\"https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2014\/05\/snapshot12-276x300.png 276w, https:\/\/nayarweb.com\/blog\/wp-content\/uploads\/2014\/05\/snapshot12.png 419w\" sizes=\"auto, (max-width: 276px) 100vw, 276px\" \/><\/a><\/center>However, including the above commands in your custom application as a system call is not so desirable because it needs curl to be installed on the target machine and has to be Linux\/UNIX type.To make cross-platform apps which run on Linux, Windows and Android, I use Qt Framework.<\/p>\n<h3>Qt<\/h3>\n<p>To create the above call in Qt C++, it is done like this:<br \/>\n<code><br \/>\n<span style=\"color: #000080;\">#include<\/span><span style=\"color: #008000;\">&lt;QDebug&gt;<\/span><\/code><\/p>\n<pre><span style=\"color: #000080;\">#include<\/span><span style=\"color: #008000;\">&lt;QNetworkAccessManager&gt;<\/span><\/pre>\n<pre><span style=\"color: #000080;\">#include<\/span><span style=\"color: #008000;\">&lt;QUrl&gt;<\/span><\/pre>\n<pre><span style=\"color: #000080;\">#include<\/span><span style=\"color: #008000;\">&lt;QNetworkRequest&gt;<\/span><\/pre>\n<pre><span style=\"color: #000080;\">#include<\/span><span style=\"color: #008000;\">&lt;QNetworkReply&gt;<\/span><\/pre>\n<p><code> int main(int argc, char *argv[])<br \/>\n{<br \/>\n   int no = 4; \/\/ GPIO number<br \/>\n   int value = 1; \/\/ 1 or 0 for On and Off<br \/>\n<span style=\"color: #800080;\">\u00a0 \u00a0QNetworkAccessManager<\/span><span style=\"color: #000000;\">*<\/span><span style=\"color: #000000;\">manager<\/span><span style=\"color: #000000;\">=<\/span><span style=\"color: #808000;\">new<\/span><span style=\"color: #800080;\">QNetworkAccessManager<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #808000;\">this<\/span><span style=\"color: #000000;\">);<\/span><\/code><\/p>\n<pre><span style=\"color: #800080;\">   QUrl <\/span><span style=\"color: #000000;\">url<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #008000;\">\"http:\/\/192.168.1.6:8000\/GPIO\/\"<\/span><span style=\"color: #000000;\">+<\/span><span style=\"color: #800080;\">QString<\/span><span style=\"color: #000000;\">::<\/span><span style=\"color: #000000;\">number<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">no<\/span><span style=\"color: #000000;\">)\r\n<\/span><span style=\"color: #000000;\">+<\/span><span style=\"color: #008000;\">\"\/value\/\"<\/span><span style=\"color: #000000;\">+<\/span><span style=\"color: #800080;\">QString<\/span><span style=\"color: #000000;\">::<\/span><span style=\"color: #000000;\">number<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">value<\/span><span style=\"color: #000000;\">));<\/span><\/pre>\n<pre><span style=\"color: #000000;\">   url<\/span><span style=\"color: #000000;\">.<\/span><span style=\"color: #000000;\">setUserName<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #008000;\">\"webiopi\"<\/span><span style=\"color: #000000;\">);<\/span><\/pre>\n<pre><span style=\"color: #000000;\">   url<\/span><span style=\"color: #000000;\">.<\/span><span style=\"color: #000000;\">setPassword<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #008000;\">\"raspberry\"<\/span><span style=\"color: #000000;\">);<\/span><\/pre>\n<pre><span style=\"color: #800080;\">   QNetworkReply <\/span><span style=\"color: #000000;\">*<\/span><span style=\"color: #000000;\">reply <\/span><span style=\"color: #000000;\">= <\/span><span style=\"color: #000000;\">manager<\/span><span style=\"color: #000000;\">-&gt;<\/span><span style=\"color: #000000;\">post<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #800080;\">QNetworkRequest<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #000000;\">url<\/span><span style=\"color: #000000;\">),<\/span><span style=\"color: #008000;\">\"\"<\/span><span style=\"color: #000000;\">);<\/span><\/pre>\n<pre><span style=\"color: #000080;\">   qDebug<\/span><span style=\"color: #000000;\">() <\/span><span style=\"color: #000000;\">&lt;&lt; <\/span><span style=\"color: #000000;\">reply<\/span><span style=\"color: #000000;\">-&gt;<\/span><span style=\"color: #000000;\">readAll<\/span><span style=\"color: #000000;\">();\r\n<\/span>}\r\n\r\nYou just need to change the URL each time for different functions ;)<\/pre>\n<p>Next, i&#8217;m gonna create a UI in QML to control the GPIO so as to be able to be used to make a remote controlled car. Stay tuned \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Abstract To control the GPIO pins of my raspberry pi, I am using WebIOPi framework. You can thus have a web page to remotely control the GPIO from a laptop, mobile phone or anything web capable on &#8216;http:\/\/192.168.1.x:8000&#8217; where x is the Raspberry Pi on your LAN. You get an interface like this: But if &hellip; <a href=\"https:\/\/nayarweb.com\/blog\/2014\/control-raspberry-pis-gpio-with-rest-qt-webiopi\/\" class=\"continue-reading\">Continue reading <span class=\"screen-reader-text\">Control Raspberry Pi&#8217;s GPIO with REST (Qt, WebIOPi)<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[61,186,167,109,189],"class_list":["post-2139","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-linux","tag-qt","tag-raspberry-pi","tag-ubuntu","tag-webiopi"],"_links":{"self":[{"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/posts\/2139","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/comments?post=2139"}],"version-history":[{"count":15,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/posts\/2139\/revisions"}],"predecessor-version":[{"id":2157,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/posts\/2139\/revisions\/2157"}],"wp:attachment":[{"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/media?parent=2139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/categories?post=2139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nayarweb.com\/blog\/wp-json\/wp\/v2\/tags?post=2139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}