{"id":89,"date":"2017-04-27T19:04:39","date_gmt":"2017-04-27T17:04:39","guid":{"rendered":"https:\/\/dev.drun.net\/?p=89"},"modified":"2017-04-27T19:04:39","modified_gmt":"2017-04-27T17:04:39","slug":"customizing-sonoff-esp-switch-to-work-with-anything","status":"publish","type":"post","link":"https:\/\/dev.krasi.net\/?p=89","title":{"rendered":"Customizing Sonoff ESP switch to work with anything"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-92 size-large\" src=\"https:\/\/dev.krasi.net\/wp-content\/uploads\/2017\/04\/DSCF4415-1024x683.jpg\" alt=\"\" width=\"960\" height=\"640\" \/><\/p>\n<p>I&#8217;ve recently purchased several <a href=\"http:\/\/www.ebay.com\/itm\/152147180815\">sonoff wifi<\/a> modules. Here&#8217;s <a href=\"http:\/\/www.ebay.com\/itm\/182430059458\">another one<\/a>, higher amperage. They usually go with an app that you can use to turn them on \/ off. However, that&#8217;s pretty boring \ud83d\ude42 I decided to make them run via http interface and also control them from Domoticz via virtual switch. Yes, it&#8217;s possible and not that hard \ud83d\ude42<\/p>\n<p>I decided to install <a href=\"https:\/\/www.letscontrolit.com\/wiki\/index.php\/ESPEasy\">ESPEasy<\/a>, because it&#8217;s universal one and you can configure it pretty easy via http interface and dedicated access point. Download\u00a0R147_RC8 from their site. You will also need Arduino IDE configured and running, and also install <a href=\"https:\/\/github.com\/esp8266\/Arduino\">ESP8266 module<\/a>. In the readme you can see how it&#8217;s done, I will not repeat it here.<\/p>\n<p>After you install the ESP8266 module, copy all libraries from the downloaded ESPEasy firmware from Source\/Libraries to your Arduino&#8217;s libraries folder. Depending on your OS of choice, this might be different location. Mine was in the package contents of Arduino app in Applications ( Mac OS X ).<\/p>\n<p>After you do it, open Source\/ESPEasy.ino in Arduino IDE. This you will be flashing. Configure your IDE settings. Mine are like this:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-90\" src=\"https:\/\/dev.krasi.net\/wp-content\/uploads\/2017\/04\/arduino_esp8266-300x177.png\" alt=\"\" width=\"300\" height=\"177\" \/><\/p>\n<p>Now, press &#8220;Verify&#8221; button. It should compile successfully. If not &#8211; check if you did the things above properly. For flashing, I use cheap PL2303 module. You can see how to configure and connect one of these <a href=\"\/2017\/03\/01\/playing-with-hc-12-wireless-transmitter-receiver\/\">in another post<\/a>.<\/p>\n<p>Okay, so now we need a bit of soldering \ud83d\ude42 There are some holes, which are actually\u00a0serial pins, power and GPIOs \ud83d\ude42 Sorry for the ugly overlay \ud83d\ude42<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-97 size-large\" src=\"https:\/\/dev.krasi.net\/wp-content\/uploads\/2017\/04\/DSCF4426-1-1024x683.jpg\" alt=\"\" width=\"960\" height=\"640\" \/><\/p>\n<p>So, we will be connecting the four ports, but I soldered a connector to make it easy for attaching the USB to Serial convertor:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-94 size-large\" src=\"https:\/\/dev.krasi.net\/wp-content\/uploads\/2017\/04\/DSCF4435-1024x683.jpg\" alt=\"\" width=\"960\" height=\"640\" \/><\/p>\n<p>The finished connection looks like this:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-95 size-large\" src=\"https:\/\/dev.krasi.net\/wp-content\/uploads\/2017\/04\/DSCF4437-1024x683.jpg\" alt=\"\" width=\"960\" height=\"640\" \/><\/p>\n<p>Now, in order to boot in program mode, you need to press the button on Sonoff while it&#8217;s not powered on, then plug it in the USB port of the computer and then release the button.<\/p>\n<p><strong>NOTE<\/strong>: Make sure you have no power on the input, otherwise you\u00a0<strong>will<\/strong>\u00a0fry it!<\/p>\n<p>So, configure your port in the Arduino IDE and you&#8217;re good to go. When you press &#8220;Upload&#8221; button, it should start uploading. If it fails with errors about<\/p>\n<pre>espcomm_sync failed<\/pre>\n<p>this means the device is not in program mode. Retry it again until it works.<\/p>\n<p>After it&#8217;s finished, disconnect everything, remove the serial to usb and connect power. Soon you should see a hotspot like ESP_0 or ESP_1. Connect to it. You will be presented with a screen to configure wifi connection. Connect it to your wifi network and restart it. Note the IP address. Mine was 192.168.88.20. Connect to it:\u00a0http:\/\/192.168.88.20\/ (use yours, of course) . You should see something like this:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-98\" src=\"https:\/\/dev.krasi.net\/wp-content\/uploads\/2017\/04\/esp_intf1-215x300.png\" alt=\"\" width=\"215\" height=\"300\" \/><\/p>\n<p>I would suggest to go to &#8220;Config&#8221; section and configure static IP address, so you always access it on the same address:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-99\" src=\"https:\/\/dev.krasi.net\/wp-content\/uploads\/2017\/04\/esp_intf2-188x300.png\" alt=\"\" width=\"188\" height=\"300\" \/><\/p>\n<p>Make sure &#8220;Unit nr&#8221; is not &#8220;0&#8221;. You don&#8217;t need to configure Domoticz here, as we&#8217;ll be using very simple interface to control it.<\/p>\n<p>Okay, so the relay is on GPIO12, and the LED is on GPIO13. Go to &#8220;Tools&#8221; -&gt; &#8220;Advanced&#8221; and tick &#8220;Rules&#8221; check box and then press the &#8220;Submit&#8221; button. A new main screen should appear &#8211; &#8220;Rules&#8221;:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-100\" src=\"https:\/\/dev.krasi.net\/wp-content\/uploads\/2017\/04\/esp_intf3-300x221.png\" alt=\"\" width=\"300\" height=\"221\" \/><\/p>\n<p>Here&#8217;s the script I used ( Note that to switch LED on, you need to send low signal to GPIO13 ):<\/p>\n<pre>on turnOn do\n gpio,12,1 \/\/ close\n gpio,13,0 \/\/ led on\nendon\n\non turnOff do\n gpio,12,0 \/\/ open\n gpio,13,1 \/\/ led off\nendon<\/pre>\n<p>Press &#8220;Submit&#8221; to save it.<\/p>\n<p>So, that&#8217;s it \ud83d\ude42 now you can turn it on\u00a0when you call URL like:<\/p>\n<pre>http:\/\/192.168.88.20\/control?cmd=event,TurnOn<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-103 size-large\" src=\"https:\/\/dev.krasi.net\/wp-content\/uploads\/2017\/04\/DSCF4501-1024x683.jpg\" alt=\"\" width=\"960\" height=\"640\" \/><\/p>\n<p>and turn it off\u00a0with:<\/p>\n<pre>http:\/\/192.168.88.20\/control?cmd=event,TurnOff<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-102 size-large\" src=\"https:\/\/dev.krasi.net\/wp-content\/uploads\/2017\/04\/DSCF4500-1024x683.jpg\" alt=\"\" width=\"960\" height=\"640\" \/><\/p>\n<p>Easy, right? If you create a virtual switch in Domoticz and add the on and off scripts there, <a href=\"\/2017\/05\/10\/controlling-sonoff-devices-from-domoticz\/\">you can control it directly<\/a>.<\/p>\n<p>That&#8217;s it \ud83d\ude42 Probably there is much easier way, I&#8217;ve seen several very simple sketches, that do exactly the same, but they are not easy to reconfigure as ESPEasy is. You have to add your credentials in the sketch, and that&#8217;s what I didn&#8217;t liked.<\/p>\n<p>EDIT: If you want to turn on \/ off the switch via the button, use the following rules:<\/p>\n<pre>on turnOn do\n gpio,12,1 \/\/ close\n gpio,13,0 \/\/ led on\nendon\n\non turnOff do\n gpio,12,0 \/\/ open\n gpio,13,1 \/\/ led off\nendon\n\non Button#Value do\n if [Button#Value] = 1\n gpio,12,1 \/\/ close\n gpio,13,0 \/\/ led on\n else \n gpio,12,0 \/\/ open\n gpio,13,1 \/\/ led off\n endif\nendon<\/pre>\n<p>and add a device in &#8220;Devices&#8221; tab:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-108 size-large\" src=\"https:\/\/dev.krasi.net\/wp-content\/uploads\/2017\/04\/esp_intf4-1024x354.png\" alt=\"\" width=\"960\" height=\"332\" \/><\/p>\n<p>With these settings:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-109 size-large\" src=\"https:\/\/dev.krasi.net\/wp-content\/uploads\/2017\/04\/esp_intf5-1000x1024.png\" alt=\"\" width=\"960\" height=\"983\" \/><\/p>\n<p>Now, the next time you press the button, it will toggle between On\/Off the relay.<\/p>\n<p>Enjoy \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve recently purchased several sonoff wifi modules. Here&#8217;s another one, higher amperage. They usually go with an app that you can use to turn them on \/ off. However, that&#8217;s pretty boring \ud83d\ude42 I decided to make them run via http interface and also control them from Domoticz via virtual switch. Yes, it&#8217;s possible and not that hard \ud83d\ude42 I decided to install ESPEasy, because it&#8217;s universal one and you can configure it pretty easy via http interface and dedicated access point. Download\u00a0R147_RC8 from their site. You will also need Arduino IDE configured and running, and also install ESP8266 module. In the readme you can see how it&#8217;s done, I will not repeat it here. After you install the ESP8266 module, copy all libraries from the downloaded ESPEasy firmware from Source\/Libraries to your Arduino&#8217;s libraries folder. Depending on your OS of choice, this might be different location. Mine was in the package contents of Arduino app in Applications ( Mac OS X ). After you do it, open Source\/ESPEasy.ino in Arduino IDE. This you will be flashing. Configure your IDE settings. Mine are like this: Now, press &#8220;Verify&#8221; button. It should compile successfully. If not &#8211; check if you did [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":92,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,3,5],"tags":[],"class_list":["post-89","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-general","category-hardware","category-software"],"_links":{"self":[{"href":"https:\/\/dev.krasi.net\/index.php?rest_route=\/wp\/v2\/posts\/89","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.krasi.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.krasi.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.krasi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.krasi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=89"}],"version-history":[{"count":0,"href":"https:\/\/dev.krasi.net\/index.php?rest_route=\/wp\/v2\/posts\/89\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dev.krasi.net\/index.php?rest_route=\/"}],"wp:attachment":[{"href":"https:\/\/dev.krasi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=89"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.krasi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=89"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.krasi.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=89"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}