{"id":29642,"date":"2018-03-12T12:18:02","date_gmt":"2018-03-12T12:18:02","guid":{"rendered":"http:\/\/randomnerdtutorials.com\/?p=29642"},"modified":"2020-04-13T21:31:01","modified_gmt":"2020-04-13T21:31:01","slug":"reprogram-sonoff-smart-switch-with-web-server","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/reprogram-sonoff-smart-switch-with-web-server\/","title":{"rendered":"Reprogram Sonoff Smart Switch with Web Server"},"content":{"rendered":"<p>In this post, you\u2019re going to learn how to flash custom firmware in the Sonoff device, so that you can control it with your own web server. I recommend that you read <a href=\"https:\/\/randomnerdtutorials.com\/sonoff-5-wifi-wireless-smart-switch-introduction\/\">my previous post<\/a> to get familiar with the Sonoff.&nbsp;We also have additional resources that describe how to <a href=\"https:\/\/randomnerdtutorials.com\/how-to-flash-a-custom-firmware-to-sonoff\/\">flash a custom firmware<\/a> to the Sonoff device using an FTDI programmer and the Arduino IDE.&nbsp;<!--more--><\/p>\n<p>If you don\u2019t have a&nbsp;<a href=\"https:\/\/makeradvisor.com\/tools\/sonoff-wi-fi-smart-switch\/\" target=\"_blank\" rel=\"noopener noreferrer\">Sonoff<\/a>&nbsp;yet, you can get one for approximately&nbsp;$5 \u2013 visit&nbsp;<a href=\"https:\/\/makeradvisor.com\/tools\/sonoff-wi-fi-smart-switch\/\" target=\"_blank\" rel=\"noopener noreferrer\">Maker Advisor<\/a> to find the best price.<\/p>\n<h2>First, watch the step by step video tutorial below<\/h2>\n<p style=\"text-align:center\"><iframe width=\"720\" height=\"405\" src=\"https:\/\/www.youtube.com\/embed\/ZztPgbAwOMk?rel=0\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<h2>Safety warning<\/h2>\n<p>Make sure you disconnect your Sonoff from mains voltage. Then, open the box enclosure.<\/p>\n<p><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/warning-m-1.png?resize=700%2C200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"warning-m\" width=\"700\" height=\"200\"><\/p>\n<h2>Sonoff pinout<\/h2>\n<p>The Sonoff is meant to be hacked, and you can see clearly that these connections were left out, so that you can solder some pins and upload a custom firmware. That\u2019s the pinout.<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/sonoff_gpio-r.jpg?resize=750%2C424&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"sonoff_gpio-r\" width=\"750\" height=\"424\"><\/p>\n<p>I&#8217;ve soldered 4 header pins, so that I can easily connect and disconnect wire cables to my Sonoff device.<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/pins-soldered.jpg?resize=622%2C369&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"pins-soldered\" width=\"622\" height=\"369\"><\/p>\n<h2>Preparing your 3.3V FTDI module<\/h2>\n<p>You need an FTDI module to upload a new firmware to your Sonoff. Use the schematics provided as a reference.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-54097\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/sonoffo-ftdi.jpg?resize=867%2C263&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"867\" height=\"263\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/sonoffo-ftdi.jpg?w=867&amp;quality=100&amp;strip=all&amp;ssl=1 867w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/sonoffo-ftdi.jpg?resize=300%2C91&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/sonoffo-ftdi.jpg?resize=768%2C233&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 867px) 100vw, 867px\" \/><\/p>\n<p><span style=\"font-weight: 400; color: #ff0000;\"><strong>Warning: <\/strong>uploading a custom firmware is irreversible and you\u2019ll no longer be able to use the app eWeLink.<\/span><\/p>\n<p>I\u2019ve added a toggle switch in the power line, so that I can easily turn the Sonoff on and off to flash a new firmware without having to unplug the FTDI module.<\/p>\n<p>I used hot glue to glue the ends of the wires together. This prevents you to make wrong connections between the FTDI and the Sonoff in the future.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/hot-glue-ftdi.jpg?resize=750%2C369&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"hot-glue-ftdi\" width=\"750\" height=\"369\"><\/p>\n<h2>Boot your Sonoff in Flashing Mode<\/h2>\n<p>To flash a new firmware to your Sonoff, you have to boot your Sonoff in flashing mode. Follow this 4 step process:<\/p>\n<p>1) Connect your 3.3V FTDI programmer to your computer<\/p>\n<p>2) Hold down the Sonoff button<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/hold-down-sonoff-button.jpg?resize=300%2C273&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"hold-down-sonoff-button\" width=\"300\" height=\"273\"><\/p>\n<p>3) Toggle the switch to apply power to the Sonoff circuit<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/apply-power-to-sonoff.png?resize=600%2C287&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"apply-power-to-sonoff\" width=\"600\" height=\"287\"><\/p>\n<p>4) Then, you can release the Sonoff button<\/p>\n<p>Now, your Sonoff should be in flashing mode and you can upload a new firmware.<\/p>\n<h2>Opening the Arduino IDE<\/h2>\n<p>You should have the ESP8266 add-on installed in the Arduino IDE &#8211; If you don\u2019t have the add-on installed, first follow this tutorial on <a href=\"https:\/\/randomnerdtutorials.com\/how-to-install-esp8266-board-arduino-ide\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Install the ESP8266 Board in Arduino IDE<\/a>.<\/p>\n<p><strong>You can upload the full sketch to your Sonoff (replace with your SSID&nbsp;and password):<\/strong><\/p>\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos\n  Complete project details at http:\/\/randomnerdtutorials.com  \n*********\/\n\n#include &lt;ESP8266WiFi.h&gt;\n#include &lt;WiFiClient.h&gt;\n#include &lt;ESP8266WebServer.h&gt;\n#include &lt;ESP8266mDNS.h&gt;\n\nMDNSResponder mdns;\n\n\/\/ Replace with your network credentials\nconst char* ssid = &quot;YOUR_SSID&quot;;\nconst char* password = &quot;YOUR_PASSWORD&quot;;\n\nESP8266WebServer server(80);\n\nString webPage = &quot;&quot;;\n\nint gpio13Led = 13;\nint gpio12Relay = 12;\n\nvoid setup(void){\n  webPage += &quot;&lt;h1&gt;SONOFF Web Server&lt;\/h1&gt;&lt;p&gt;&lt;a href=\\&quot;on\\&quot;&gt;&lt;button&gt;ON&lt;\/button&gt;&lt;\/a&gt;&amp;nbsp;&lt;a href=\\&quot;off\\&quot;&gt;&lt;button&gt;OFF&lt;\/button&gt;&lt;\/a&gt;&lt;\/p&gt;&quot;;  \n  \/\/ preparing GPIOs\n  pinMode(gpio13Led, OUTPUT);\n  digitalWrite(gpio13Led, HIGH);\n  \n  pinMode(gpio12Relay, OUTPUT);\n  digitalWrite(gpio12Relay, HIGH);\n \n  Serial.begin(115200); \n  delay(5000);\n  WiFi.begin(ssid, password);\n  Serial.println(&quot;&quot;);\n\n  \/\/ Wait for connection\n  while (WiFi.status() != WL_CONNECTED) {\n    delay(500);\n    Serial.print(&quot;.&quot;);\n  }\n  Serial.println(&quot;&quot;);\n  Serial.print(&quot;Connected to &quot;);\n  Serial.println(ssid);\n  Serial.print(&quot;IP address: &quot;);\n  Serial.println(WiFi.localIP());\n  \n  if (mdns.begin(&quot;esp8266&quot;, WiFi.localIP())) {\n    Serial.println(&quot;MDNS responder started&quot;);\n  }\n  \n  server.on(&quot;\/&quot;, [](){\n    server.send(200, &quot;text\/html&quot;, webPage);\n  });\n  server.on(&quot;\/on&quot;, [](){\n    server.send(200, &quot;text\/html&quot;, webPage);\n    digitalWrite(gpio13Led, LOW);\n    digitalWrite(gpio12Relay, HIGH);\n    delay(1000);\n  });\n  server.on(&quot;\/off&quot;, [](){\n    server.send(200, &quot;text\/html&quot;, webPage);\n    digitalWrite(gpio13Led, HIGH);\n    digitalWrite(gpio12Relay, LOW);\n    delay(1000); \n  });\n  server.begin();\n  Serial.println(&quot;HTTP server started&quot;);\n}\n \nvoid loop(void){\n  server.handleClient();\n} \n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Sonoff_local_web_server.ino\" target=\"_blank\">View raw code<\/a><\/p>\n<h2>Preparing your Arduino IDE<\/h2>\n<p>Having your Sonoff device still in flashing mode.<\/p>\n<ol>\n<li>Select your FTDI&nbsp;port number under the&nbsp;<strong>Tools<\/strong>&gt;&nbsp;<strong>Port<\/strong>&nbsp;&gt;&nbsp;<strong>COM14<\/strong> (in my case)<\/li>\n<li>Choose your ESP8266 board from&nbsp;<strong>Tools<\/strong>&gt;&nbsp;<strong>Board<\/strong>&nbsp;&gt;&nbsp;<strong>Generic ESP8266 Module<\/strong><\/li>\n<li>Select <strong>Flash Mode: &#8220;DOUT&#8221;<\/strong><\/li>\n<li>Press the Upload button<\/li>\n<\/ol>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-54099\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/arduino-ide-config-sonoff.png?resize=404%2C442&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"404\" height=\"442\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/arduino-ide-config-sonoff.png?w=404&amp;quality=100&amp;strip=all&amp;ssl=1 404w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/arduino-ide-config-sonoff.png?resize=274%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 274w\" sizes=\"(max-width: 404px) 100vw, 404px\" \/><\/p>\n<p>Wait a few seconds while the code is uploading. You should see a message saying \u201cDone Uploading\u201d.<\/p>\n<h2>Troubleshooting<\/h2>\n<p>If you try to upload the sketch and it prompts the following error message:<\/p>\n<pre>warning: espcomm_sync failed\nerror: espcomm_open failed<\/pre>\n<p>It means that your Sonoff is not in flashing mode. You&#8217;ll need to repeat the process described in section \u201cBoot your Sonoff in flashing mode\u201d described earlier in this guide.<\/p>\n<h2>Final circuit<\/h2>\n<p>After uploading the code, re-assemble your Sonoff. Be very careful with the mains voltage connections.<\/p>\n<p>It\u2019s the exact same procedure as shown in <a href=\"https:\/\/randomnerdtutorials.com\/sonoff-5-wifi-wireless-smart-switch-introduction\/\" target=\"_blank\" rel=\"noopener noreferrer\">the introductory guide<\/a>.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-29197\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/SONOFF_circuit.png?resize=1169%2C299&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"sonoff_circuit\" width=\"1169\" height=\"299\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/SONOFF_circuit.png?w=1169&amp;quality=100&amp;strip=all&amp;ssl=1 1169w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/SONOFF_circuit.png?resize=150%2C38&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/SONOFF_circuit.png?resize=300%2C77&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/SONOFF_circuit.png?resize=768%2C196&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/SONOFF_circuit.png?resize=1024%2C262&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/SONOFF_circuit.png?resize=80%2C20&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/SONOFF_circuit.png?resize=220%2C56&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/SONOFF_circuit.png?resize=250%2C64&amp;quality=100&amp;strip=all&amp;ssl=1 250w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/SONOFF_circuit.png?resize=280%2C72&amp;quality=100&amp;strip=all&amp;ssl=1 280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/SONOFF_circuit.png?resize=510%2C130&amp;quality=100&amp;strip=all&amp;ssl=1 510w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/SONOFF_circuit.png?resize=750%2C192&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/SONOFF_circuit.png?resize=975%2C249&amp;quality=100&amp;strip=all&amp;ssl=1 975w\" sizes=\"(max-width: 1169px) 100vw, 1169px\" \/><\/p>\n<h2>ESP8266 IP Address<\/h2>\n<p>Open the Arduino serial monitor at a baud rate of 115200. Connect GPIO 0 of your ESP8266 to VCC and reset your board.<\/p>\n<p>After a few seconds your IP address should appear. In my case it\u2019s <strong>192.168.1.70<\/strong>.<\/p>\n<h2><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/09\/esp-ip-address.png?resize=565%2C318&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"esp ip address\" width=\"565\" height=\"318\"><\/h2>\n<h2>Demonstration<\/h2>\n<p>For the final demonstration open any browser from a device that is connected to the same router that your Sonoff is. Then type&nbsp;the IP address and click Enter!<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/web-server.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"web-server\" width=\"750\" height=\"421\"><\/p>\n<p>Now when you press the buttons in your web server you can control&nbsp;the Sonoff switch and any device that is connected to it.<\/p>\n<p><strong>Sonoff tutorials list:<\/strong><\/p>\n<ul>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/sonoff-5-wifi-wireless-smart-switch-introduction\/\">Sonoff \u2013 $5 WiFi Wireless Smart Switch Introduction<\/a><\/li>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/how-to-flash-a-custom-firmware-to-sonoff\/\">How to Flash a Custom Firmware to Sonoff<\/a><\/li>\n<\/ul>\n<h2>Wrapping up<\/h2>\n<p>That\u2019s it for now, I hope you had fun learning about the Sonoff device. Make sure you <a href=\"https:\/\/randomnerdtutorials.com\/download\">subscribe to my blog<\/a>, because I\u2019ll be posting more tutorials about the Sonoff.<\/p>\n<p><strong>Do you have any questions?&nbsp;<\/strong><strong>Leave a comment down below!<\/strong><\/p>\n<p>Thanks for reading,<\/p>\n<p>Rui<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post, you\u2019re going to learn how to flash custom firmware in the Sonoff device, so that you can control it with your own web server. I recommend that &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Reprogram Sonoff Smart Switch with Web Server\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/reprogram-sonoff-smart-switch-with-web-server\/#more-29642\" aria-label=\"Read more about Reprogram Sonoff Smart Switch with Web Server\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":29649,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[265,214,246,300,269,225,264,275,218],"tags":[],"class_list":["post-29642","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp8266-project","category-esp8266","category-esp8266-arduino-ide","category-0-esp8266","category-guide-project","category-home-automation","category-project","category-sonoff","category-web-server"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/thumbnail-1.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/29642","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=29642"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/29642\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/29649"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=29642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=29642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=29642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}