{"id":575,"date":"2019-07-29T13:55:47","date_gmt":"2019-07-29T12:55:47","guid":{"rendered":"https:\/\/www.flat-planet.net\/?p=575"},"modified":"2019-07-29T13:55:48","modified_gmt":"2019-07-29T12:55:48","slug":"wifi-controlled-relay-and-openhab","status":"publish","type":"post","link":"https:\/\/flat-planet.net\/?p=575","title":{"rendered":"Wifi Controlled Relay and OpenHab"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Items used:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>LOLIN D1 mini V3.1.0 (ESP8266)<\/li><li>LOLIN Relay Shield V2.0.0<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; highlight: [4,5,6,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89]; title: ; notranslate\" title=\"\">\n\/\/ Load Wi-Fi library\n#include &lt;ESP8266WiFi.h&gt;\n\n\/\/ Replace with your network credentials\nconst char* ssid     = &quot;mySSID&quot;;\nconst char* password = &quot;VerySecret&quot;;\n\n\/\/ Set web server port number to 80\nWiFiServer server(80);\n\n\/\/ Variable to store the HTTP request\nString header;\n\n\/\/ Auxiliary variables to store the current output state\nString relayState = &quot;OFF&quot;;\n\/\/ we will have the relay on 5\nconst int relay = 5;\n\n\nvoid setup() {\n  Serial.begin(115200);\n  \/\/ Initialize the output variables as outputs\n  pinMode(relay, OUTPUT);\n  \/\/ set the relay off by default\n  digitalWrite(relay, LOW);\n  pinMode(LED_BUILTIN, OUTPUT);     \/\/ Initialize the LED_BUILTIN pin as an output\n  \/\/ we match the LED state to the relay state\n  digitalWrite(LED_BUILTIN, HIGH);  \/\/ High is off, low is on\n\n  \/\/ Connect to Wi-Fi network with SSID and password\n  Serial.print(&quot;Connecting to &quot;);\n  Serial.println(ssid);\n  WiFi.begin(ssid, password);\n  while (WiFi.status() != WL_CONNECTED) {\n    delay(500);\n    Serial.print(&quot;.&quot;);\n  }\n  \/\/ Print local IP address and start web server\n  Serial.println(&quot;&quot;);\n  Serial.println(&quot;WiFi connected.&quot;);\n  Serial.println(&quot;IP address: &quot;);\n  Serial.println(WiFi.localIP());\n  server.begin();\n}\nvoid loop(){\n  WiFiClient client = server.available();   \/\/ Listen for incoming clients\n\n  if (client) {                             \/\/ If a new client connects,\n    Serial.println(&quot;New Client.&quot;);          \/\/ print a message out in the serial port\n    String currentLine = &quot;&quot;;                \/\/ make a String to hold incoming data from the client\n    while (client.connected()) {            \/\/ loop while the client&#039;s connected\n      if (client.available()) {             \/\/ if there&#039;s bytes to read from the client,\n        char c = client.read();             \/\/ read a byte, then\n        Serial.write(c);                    \/\/ print it out the serial monitor\n        header += c;\n        if (c == &#039;\\n&#039;) {                    \/\/ if the byte is a newline character\n          \/\/ if the current line is blank, you got two newline characters in a row.\n          \/\/ that&#039;s the end of the client HTTP request, so send a response:\n          if (currentLine.length() == 0) {\n            \/\/ HTTP headers always start with a response code (e.g. HTTP\/1.1 200 OK)\n            \/\/ and a content-type so the client knows what&#039;s coming, then a blank line:\n            client.println(&quot;HTTP\/1.1 200 OK&quot;);\n            client.println(&quot;Content-type:text\/plain&quot;);\n            client.println(&quot;Connection: close&quot;);\n\n\n            \/\/ turns the GPIOs on and off\n            if (header.indexOf(&quot;GET \/relay\/on&quot;) &gt;= 0) {\n              Serial.println(&quot;Relay on&quot;);\n              relayState = &quot;ON&quot;;\n              digitalWrite(LED_BUILTIN, LOW);  \/\/ High is off, low is on\n              digitalWrite(relay, HIGH);\n            } else if (header.indexOf(&quot;GET \/relay\/off&quot;) &gt;= 0) {\n              Serial.println(&quot;Relay off&quot;);\n              relayState = &quot;OFF&quot;;\n              digitalWrite(relay, LOW);\n              digitalWrite(LED_BUILTIN, HIGH);  \/\/ High is off, low is on\n            }\n            else if (header.indexOf(&quot;GET \/relay\/status&quot;) &gt;= 0) {\n              Serial.print(&quot;Sending status: &quot;);\n              Serial.println(relayState);\n              \n              client.println();\n              client.print(relayState);\n              client.println();\n              \n            }\n            client.println();\n            break;\n          } else { \/\/ if you got a newline, then clear currentLine\n            currentLine = &quot;&quot;;\n          }\n        } else if (c != &#039;\\r&#039;) {  \/\/ if you got anything else but a carriage return character,\n          currentLine += c;      \/\/ add it to the end of the currentLine\n        }\n      }\n    }\n    \/\/ Clear the header variable\n    header = &quot;&quot;;\n    \/\/ Close the connection\n    delay(1);\n    client.stop();\n    Serial.println(&quot;Client disconnected.&quot;);\n    Serial.println(&quot;&quot;);\n  }\n}        \n  \n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Now for the OpenHab side<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Here, I&#8217;m making use of the HTTP binding (and the REGEX mapping), and we basically have 3 calls we need to handle<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li> On<\/li><li>Off<\/li><li>Status <\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">And so our items definition is simple to handle those 3 cases:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nSwitch MyRelay &quot;MyRelay&quot; &lt;switch&gt; ( gSwitches, gRelay ) { http=&quot;&gt;&#x5B;ON:GET:http:\/\/&lt;IP Address&gt;\/relay\/on] &gt;&#x5B;OFF:GET:http:\/\/&lt;IP Address&gt;\/relay\/off] &lt;&#x5B;http:\/\/&lt;IP Address&gt;\/relay\/status:10000:REGEX((.*))]&quot;}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">And that is basically it &#8211; I can now control the on\/off of my relays, and check on the current status.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion \/ Finishing Thoughts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Some obvious points to consider are:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Lack of security &#8211; anything that can reach the IPs of the relays can manipulate their state<\/li><li>Lack of rate limiting &#8211; a follow up from the 1st point is that we don&#8217;t do any cooldowns or rate limiting to prevent accident or maliciousness from repeatedly toggling the relays, potentially damaging the relay and worse, the appliance<\/li><li>Updating the relays &#8211; requires them to be disconnected and connected via USB to a PC\/programmer<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Items used: LOLIN D1 mini V3.1.0 (ESP8266) LOLIN Relay Shield V2.0.0 Now for the OpenHab side Here, I&#8217;m making use of the HTTP binding (and the REGEX mapping), and we basically have 3 calls we need to handle On Off Status And so our items definition is simple to handle those 3 cases: And that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28,379],"tags":[70,381,382,364,380],"series":[],"class_list":["post-575","post","type-post","status-publish","format-standard","hentry","category-arduino","category-openhab","tag-home-automation","tag-lolin","tag-microcontroller","tag-openhab","tag-relay"],"_links":{"self":[{"href":"https:\/\/flat-planet.net\/index.php?rest_route=\/wp\/v2\/posts\/575","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/flat-planet.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/flat-planet.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/flat-planet.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/flat-planet.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=575"}],"version-history":[{"count":6,"href":"https:\/\/flat-planet.net\/index.php?rest_route=\/wp\/v2\/posts\/575\/revisions"}],"predecessor-version":[{"id":581,"href":"https:\/\/flat-planet.net\/index.php?rest_route=\/wp\/v2\/posts\/575\/revisions\/581"}],"wp:attachment":[{"href":"https:\/\/flat-planet.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/flat-planet.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/flat-planet.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=575"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/flat-planet.net\/index.php?rest_route=%2Fwp%2Fv2%2Fseries&post=575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}