{"id":78606,"date":"2018-12-13T14:29:45","date_gmt":"2018-12-13T14:29:45","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=78606"},"modified":"2019-10-31T10:39:12","modified_gmt":"2019-10-31T10:39:12","slug":"esp8266-hi-link-hlk-pm03","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp8266-hi-link-hlk-pm03\/","title":{"rendered":"Power ESP8266 with Mains Voltage using Hi-Link HLK-PM03 Converter"},"content":{"rendered":"<p>In this post you&#8217;ll learn how to power the ESP8266 (or ESP32) with mains voltage using the Hi-Link HLK-PM03 converter. As an example, we&#8217;ll use the ESP8266-01 to control a relay with a web server.<!--more--><\/p>\n<p><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-78648\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/hlk-pm03-esp8266-01.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"1200\" height=\"675\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/hlk-pm03-esp8266-01.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/hlk-pm03-esp8266-01.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/hlk-pm03-esp8266-01.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/hlk-pm03-esp8266-01.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/p>\n<p>The ESP32 and ESP8266 are cheap Wi-Fi modules perfectly suited for DIY projects in the Internet of Things (IoT) and Home Automation fields. Using an ESP32 or ESP8266 with a relay allows you to control any AC electronics appliances over Wi-Fi (using a web-server, for example).<\/p>\n<p>One of the biggest issues with these projects is to find a suitable power supply for the ESP32\/ESP8266 with a small form factor at the same time (in a final application you don\u2019t want to power the relay and the ESP32\/ESP8266 using two different power supplies). One solution is to power the ESP8266 or ESP32 from mains voltage using the AC\/DC converter Hi-Link HLK-PM03 (or HLK-PM01 model).<\/p>\n<h2>Introducing the Hi-Link HLK-PM03\/01 Converter Modules<\/h2>\n<p>The Hi-Link <a href=\"https:\/\/makeradvisor.com\/hi-link-hlk-pm03\/\" target=\"_blank\" rel=\"noopener noreferrer\">HLK-PM03<\/a>\u00a0 is a small AC\/DC converter\u00a0module as shown in the figure below.<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter size-full wp-image-78646\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/hlk-pm03.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"750\" height=\"421\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/hlk-pm03.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/hlk-pm03.jpg?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/p>\n<p>The HLK-PM03 AC\/DC converter can supply 3.3V from either 110VAC or 220VAC. This makes it perfect for small projects that need a 3.3V supply from mains voltage. You can also get 5V output using the HLK-PM01 instead.<\/p>\n<p>You can read more information about these modules&#8217; specifications:\u00a0<a href=\"http:\/\/www.hlktech.net\/product_detail.php?ProId=59\">HLK-PM03<\/a>\u00a0and\u00a0<a href=\"http:\/\/www.hlktech.net\/product_detail.php?ProId=54\" target=\"_blank\" rel=\"noopener noreferrer\">HLK-PM01<\/a>. There\u2019s also a really good article about the <a href=\"http:\/\/lygte-info.dk\/review\/Power%20Mains%20to%205V%200.6A%20Hi-Link%20HLK-PM01%20UK.html\" target=\"_blank\" rel=\"noopener noreferrer\">performance test<\/a> of the HLK-PM01.<\/p>\n<p>To power the ESP8266-01 from mains voltage, we&#8217;ll be using the HLK-PM03 to provide 3.3V to the VCC pin. If you need 5V to power other ESP8266 models or an ESP32 through the VIN pin, you can use the <a href=\"https:\/\/makeradvisor.com\/hi-link-hlk-pm01\/\" target=\"_blank\" rel=\"noopener noreferrer\">HLK-PM01 model<\/a> that provides 5V output and works similarly.<\/p>\n<p><strong>Where to Buy?<\/strong><\/p>\n<p>You can check the HLK-PM03 or HLK-PM01 modules on <a href=\"https:\/\/makeradvisor.com\/tools\/\">Maker Advisor<\/a> and find the best price.<\/p>\n<ul>\n<li><a href=\"https:\/\/makeradvisor.com\/hi-link-hlk-pm03\/\" target=\"_blank\" rel=\"noopener noreferrer\">HLK-PM03<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/hi-link-hlk-pm01\/\" target=\"_blank\" rel=\"noopener noreferrer\">HLK-PM01<\/a><\/li>\n<\/ul>\n<h2>Safety Warning<\/h2>\n<p>This project deals with mains voltage. Make sure you understand what you are doing. Please read the safety warning below carefully.<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter size-full wp-image-29199\" 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=\"\" width=\"700\" height=\"200\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/warning-m-1.png?w=700&amp;quality=100&amp;strip=all&amp;ssl=1 700w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/warning-m-1.png?resize=150%2C43&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/warning-m-1.png?resize=300%2C86&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/warning-m-1.png?resize=80%2C23&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/warning-m-1.png?resize=220%2C63&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/warning-m-1.png?resize=250%2C71&amp;quality=100&amp;strip=all&amp;ssl=1 250w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/warning-m-1.png?resize=280%2C80&amp;quality=100&amp;strip=all&amp;ssl=1 280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/warning-m-1.png?resize=510%2C146&amp;quality=100&amp;strip=all&amp;ssl=1 510w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<h2>Powering the ESP8266 with AC using the Hi-Link HLK-PM03 module<\/h2>\n<p>You can use the HLK-PM03 without any circuitry and attach it directly to the ESP8266 VCC pin. However, I don\u2019t recommend doing that. It is advisable to add a protection circuit with a thermal fuse and quick-blow fuses.<\/p>\n<p>Adding capacitors to the HLK-PM03 output is also a good idea to smooth voltage peaks and prevent unexpected resets or unstable behavior powering the ESP8266. We&#8217;ve also added a varistor across the mains input to protect the circuit from voltage peaks.<\/p>\n<h3>Parts Required<\/h3>\n<p>Here\u2019s a list of all the parts required to build the circuit for the project we&#8217;ll build:<\/p>\n<ul>\n<li><a href=\"https:\/\/makeradvisor.com\/hi-link-hlk-pm03\/\" target=\"_blank\" rel=\"noopener noreferrer\">HLK-PM03<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/esp-01-wi-fi-board\/\" target=\"_blank\" rel=\"noopener noreferrer\">ESP8266-01<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/esp8266-01-serial-adapter\/\" target=\"_blank\" rel=\"noopener noreferrer\">ESP8266-01 Serial Adapter<\/a> or <a href=\"https:\/\/makeradvisor.com\/tools\/ftdi-programmer-board\/\" target=\"_blank\" rel=\"noopener noreferrer\">FTDI Programmer<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/1-channel-3v-relay-module\/\" target=\"_blank\" rel=\"noopener noreferrer\">Relay Module (3.3V)<\/a><\/li>\n<li><a href=\"https:\/\/www.banggood.com\/20pcs-2-Pin-Plug-In-Screw-Terminal-Block-Connector-5_08mm-Pitch-p-993197.html?p=MA240439985285201910\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">Terminal blocks with 2 connectors<\/a><\/li>\n<li><a href=\"https:\/\/www.banggood.com\/10pcs-5mm-KF-301-3-Pin-PCB-Mount-Block-Screw-Terminal-NF-Connector-p-1136709.html?p=MA240439985285201910\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">Terminal blocks with 3 connectors<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/electrolytic-capacitors-kit\/\" target=\"_blank\" rel=\"noopener noreferrer\">Electrolytic capacitor 10uF<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/electrolytic-capacitors-kit\/\" target=\"_blank\" rel=\"noopener noreferrer\">Electrolytic capacitor 22uF<\/a><\/li>\n<li>Fuse Slow Blow (200mA)<\/li>\n<li>Thermal Fuse (72\u00baC)<\/li>\n<li>Fuse Quick Blow (630mA)<\/li>\n<li>Varistor<\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/prototyping-circuit-board-stripboard\/\" target=\"_blank\" rel=\"noopener noreferrer\">Stripboard (Prototyping Circuit Board )<\/a><\/li>\n<li>Wires<\/li>\n<\/ul>\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n<h3>Circuit Diagram<\/h3>\n<p>The following figure shows the circuit diagram.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/HLK-HiLink-with-ESP8266-circuit-1.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-78691 size-full\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/HLK-HiLink-with-ESP8266-circuit-1.png?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"1200\" height=\"675\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/HLK-HiLink-with-ESP8266-circuit-1.png?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/HLK-HiLink-with-ESP8266-circuit-1.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/HLK-HiLink-with-ESP8266-circuit-1.png?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/HLK-HiLink-with-ESP8266-circuit-1.png?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/a><\/p>\n<p>The J1 terminal block is where you should connect mains voltage.<\/p>\n<p>The 3.3V and GND after the capacitors will power the ESP8266-01.<\/p>\n<p>We&#8217;ve also added a three terminal block connected to the ESP8266 to get access to 3.3V, GND and GPIO 2 to control an output (relay module). Because we&#8217;re dealing with 3.3V you should use a <a href=\"https:\/\/makeradvisor.com\/tools\/1-channel-3v-relay-module\/\" target=\"_blank\" rel=\"noopener noreferrer\">3.3V relay module like this<\/a>, for example.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/HLK-HiLink-with-ESP8266-circuit-2-1.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-78693 size-full\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/HLK-HiLink-with-ESP8266-circuit-2-1.png?resize=1200%2C308&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"1200\" height=\"308\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/HLK-HiLink-with-ESP8266-circuit-2-1.png?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/HLK-HiLink-with-ESP8266-circuit-2-1.png?resize=300%2C77&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/HLK-HiLink-with-ESP8266-circuit-2-1.png?resize=768%2C197&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/HLK-HiLink-with-ESP8266-circuit-2-1.png?resize=1024%2C262&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/a><\/p>\n<p>For testing purposes, we\u2019ve soldered the circuit on a prototype board. We plant to build a PCB with this circuit in a future project.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/hlk-pm03-esp8266-circuit.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"750\" height=\"421\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/hlk-pm03-esp8266-circuit.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/hlk-pm03-esp8266-circuit.jpg?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/p>\n<p><strong>Soldering Thermal Fuses<\/strong><\/p>\n<p>The thermal fuse used in the circuit is a 73\u00baC fuse. This means you need to be very careful when soldering it as the heat from the soldering iron can cause it to blow. To learn more about how to solder thermal fuses, we recommend taking a look at the following resources:<\/p>\n<ul>\n<li><a href=\"http:\/\/axotron.se\/blog\/careful-when-you-solder-thermal-fuses\/\" target=\"_blank\" rel=\"noopener noreferrer\">Careful when you solder thermal fuses<\/a><\/li>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=qDc9i3p_1Xo\" target=\"_blank\" rel=\"noopener noreferrer\">Youtube video &#8211; How to solder thermal fuses<\/a><\/li>\n<\/ul>\n<h2>Code<\/h2>\n<p>In this example we&#8217;ve programmed the ESP8266 with Arduino IDE. To program the ESP8266 using Arduino IDE, you need to have the ESP8266 add-on installed. Follow the next tutorial to install the ESP8266 add-on, if you haven&#8217;t already.<\/p>\n<ul>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/how-to-install-esp8266-board-arduino-ide\/\">How to Install the ESP8266 Board in Arduino IDE<\/a><\/li>\n<\/ul>\n<p>The following code creates a web server that you can access to control the relay connected to the ESP8266 (GPIO 2) via Wi-Fi in your local network.<\/p>\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos\n  Complete project details at https:\/\/randomnerdtutorials.com  \n*********\/\n\n\/\/ Load Wi-Fi library\n#include &lt;ESP8266WiFi.h&gt;\n\n\/\/ Replace with your network credentials\nconst char* ssid     = &quot;REPLACE_WITH_YOUR_SSID&quot;;\nconst char* password = &quot;REPLACE_WITH_YOUR_PASSWORD&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\/\/ Auxiliar variables to store the current output state\nString output2State = &quot;off&quot;;\n\n\/\/ Assign output variables to GPIO pins\nconst int output2 = 2;\n\n\/\/ Current time\nunsigned long currentTime = millis();\n\/\/ Previous time\nunsigned long previousTime = 0; \n\/\/ Define timeout time in milliseconds (example: 2000ms = 2s)\nconst long timeoutTime = 2000;\n\nvoid setup() {\n  Serial.begin(115200);\n  \/\/ Initialize the output variables as outputs\n  pinMode(output2, OUTPUT);\n  \/\/ Set outputs to LOW\n  digitalWrite(output2, LOW);\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}\n\nvoid loop(){\n  WiFiClient client = server.available();   \/\/ Listen for incoming clients\n\n  if (client) {                             \/\/ If a new client connects,\n    currentTime = millis();\n    previousTime = currentTime;\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() &amp;&amp; currentTime - previousTime &lt;= timeoutTime) { \/\/ loop while the client's connected\n      currentTime = millis();\n      if (client.available()) {             \/\/ if there'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 == '\\n') {                    \/\/ if the byte is a newline character\n          \/\/ if the current line is blank, you got two newline characters in a row.\n          \/\/ that'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's coming, then a blank line:\n            client.println(&quot;HTTP\/1.1 200 OK&quot;);\n            client.println(&quot;Content-type:text\/html&quot;);\n            client.println(&quot;Connection: close&quot;);\n            client.println();\n            \n            \/\/ turns the GPIOs on and off\n            if (header.indexOf(&quot;GET \/2\/on&quot;) &gt;= 0) {\n              Serial.println(&quot;GPIO 2 on&quot;);\n              output2State = &quot;on&quot;;\n              digitalWrite(output2, HIGH);\n            } else if (header.indexOf(&quot;GET \/2\/off&quot;) &gt;= 0) {\n              Serial.println(&quot;GPIO 2 off&quot;);\n              output2State = &quot;off&quot;;\n              digitalWrite(output2, LOW);\n            }\n            \n            \/\/ Display the HTML web page\n            client.println(&quot;&lt;!DOCTYPE html&gt;&lt;html&gt;&quot;);\n            client.println(&quot;&lt;head&gt;&lt;meta name=\\&quot;viewport\\&quot; content=\\&quot;width=device-width, initial-scale=1\\&quot;&gt;&quot;);\n            client.println(&quot;&lt;link rel=\\&quot;icon\\&quot; href=\\&quot;data:,\\&quot;&gt;&quot;);\n            \/\/ CSS to style the on\/off buttons \n            \/\/ Feel free to change the background-color and font-size attributes to fit your preferences\n            client.println(&quot;&lt;style&gt;html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}&quot;);\n            client.println(&quot;.button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;&quot;);\n            client.println(&quot;text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}&quot;);\n            client.println(&quot;.button2 {background-color: #77878A;}&lt;\/style&gt;&lt;\/head&gt;&quot;);\n            \n            \/\/ Web Page Heading\n            client.println(&quot;&lt;body&gt;&lt;h1&gt;ESP8266 Web Server&lt;\/h1&gt;&quot;);\n            \n            \/\/ Display current state, and ON\/OFF buttons for GPIO 5  \n            client.println(&quot;&lt;p&gt;GPIO 2 - State &quot; + output2State + &quot;&lt;\/p&gt;&quot;);\n            \/\/ If the output5State is off, it displays the ON button       \n            if (output2State==&quot;off&quot;) {\n              client.println(&quot;&lt;p&gt;&lt;a href=\\&quot;\/2\/on\\&quot;&gt;&lt;button class=\\&quot;button\\&quot;&gt;ON&lt;\/button&gt;&lt;\/a&gt;&lt;\/p&gt;&quot;);\n            } else {\n              client.println(&quot;&lt;p&gt;&lt;a href=\\&quot;\/2\/off\\&quot;&gt;&lt;button class=\\&quot;button button2\\&quot;&gt;OFF&lt;\/button&gt;&lt;\/a&gt;&lt;\/p&gt;&quot;);\n            } \n               \n            client.println(&quot;&lt;\/body&gt;&lt;\/html&gt;&quot;);\n            \n            \/\/ The HTTP response ends with another blank line\n            client.println();\n            \/\/ Break out of the while loop\n            break;\n          } else { \/\/ if you got a newline, then clear currentLine\n            currentLine = &quot;&quot;;\n          }\n        } else if (c != '\\r') {  \/\/ 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    client.stop();\n    Serial.println(&quot;Client disconnected.&quot;);\n    Serial.println(&quot;&quot;);\n  }\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\/ESP8266\/HLK_PM03_ESP8266.ino\" target=\"_blank\">View raw code<\/a><\/p>\n<h3>Setting Your Network Credentials<\/h3>\n<p>You need to modify the following\u00a0lines with your network credentials: SSID and password before uploading the code. The code is well commented on where you should make the changes.<\/p>\n<pre>\/\/ Replace with your network credentials\nconst char* ssid \u00a0\u00a0\u00a0\u00a0= \"<strong>REPLACE_WITH_YOUR_SSID<\/strong>\";\nconst char* password = \"<strong>REPLACE_WITH_YOUR_PASSWORD<\/strong>\";<\/pre>\n<p>For an explanation of the code, you can read the following tutorial: <a href=\"https:\/\/randomnerdtutorials.com\/esp8266-web-server-with-arduino-ide\/\">ESP8266 Web Server with Arduino IDE<\/a>.<\/p>\n<p><strong>Note:<\/strong> if you\u2019re using an ESP32 with the HLK-PM01 module, you can use the code in\u00a0<a href=\"https:\/\/randomnerdtutorials.com\/esp32-web-server-arduino-ide\/\">this tutorial<\/a>.<\/p>\n<h3>Uploading the Code<\/h3>\n<p>Upload the code provided to the ESP8266-01 board using an FTDI programmer or a ESP8266-01 serial adapter.<\/p>\n<p>If you&#8217;re using an <a href=\"https:\/\/makeradvisor.com\/tools\/ftdi-programmer-board\/\" target=\"_blank\" rel=\"noopener noreferrer\">FTDI programmer like this<\/a>, you need to make a connection as shown in the following schematic diagram:<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-5131 size-full\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/02\/ESP8266-Flasher-e1544623897676.png?resize=759%2C216&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"759\" height=\"216\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/02\/ESP8266-Flasher-e1544623897676.png?w=759&amp;quality=100&amp;strip=all&amp;ssl=1 759w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2015\/02\/ESP8266-Flasher-e1544623897676.png?resize=300%2C85&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 759px) 100vw, 759px\" \/><\/p>\n<p>After uploading the code, open the Serial Monitor to get the ESP8266-01 IP address. You&#8217;ll need it to access the web server.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78694\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/arduino-ide-serial-monitor-esp8266-ip-address.png?resize=599%2C269&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"599\" height=\"269\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/arduino-ide-serial-monitor-esp8266-ip-address.png?w=599&amp;quality=100&amp;strip=all&amp;ssl=1 599w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/arduino-ide-serial-monitor-esp8266-ip-address.png?resize=300%2C135&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 599px) 100vw, 599px\" \/><\/p>\n<h2>Demonstration<\/h2>\n<p>After uploading the code to your ESP8266 and getting the IP address, place it in the circuit and apply power to the circuit.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Warning:<\/strong><\/span> <span style=\"color: #ff0000;\">don&#8217;t touch the circuit while it is connected to mains voltage!<\/span><\/p>\n<p>Open your browser, type the ESP IP address and the following page should load.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78685\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/esp8266-web-server-relay.jpg?resize=559%2C373&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"559\" height=\"373\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/esp8266-web-server-relay.jpg?w=559&amp;quality=100&amp;strip=all&amp;ssl=1 559w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/esp8266-web-server-relay.jpg?resize=300%2C200&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 559px) 100vw, 559px\" \/><\/p>\n<p>You should be able to control the relay remotely on your local network.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78688\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/hlk-pm03-esp8266-relay.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"750\" height=\"421\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/hlk-pm03-esp8266-relay.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/hlk-pm03-esp8266-relay.jpg?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/p>\n<p><strong>Note:<\/strong> we&#8217;re using the circuit on a prototype board for demonstration purposes. If you want to use the circuit in a final application, we recommend building a PCB and <strong><span style=\"color: #ff0000;\">place it inside a proper <a style=\"color: #ff0000;\" href=\"https:\/\/makeradvisor.com\/tools\/project-box-enclosure\/\" target=\"_blank\" rel=\"noopener noreferrer\">project box enclosure<\/a><\/span><\/strong>.<\/p>\n<p><a href=\"https:\/\/makeradvisor.com\/tools\/project-box-enclosure\/\" target=\"_blank\" rel=\"noopener noreferrer\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-23713\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/06\/box-1.jpg?resize=433%2C286&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"433\" height=\"286\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/06\/box-1.jpg?w=581&amp;quality=100&amp;strip=all&amp;ssl=1 581w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/06\/box-1.jpg?resize=150%2C99&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/06\/box-1.jpg?resize=300%2C198&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/06\/box-1.jpg?resize=80%2C53&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/06\/box-1.jpg?resize=220%2C145&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/06\/box-1.jpg?resize=151%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 151w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/06\/box-1.jpg?resize=227%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 227w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/06\/box-1.jpg?resize=360%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 360w\" sizes=\"(max-width: 433px) 100vw, 433px\" \/><\/a><\/p>\n<p>We plan to build this circuit on a PCB in the upcoming weeks if there&#8217;s enough interest, and we&#8217;ll share all the files and resources you need to build your own PCB with the HLK-PM03 or HLK-PM01 modules.<\/p>\n<h2>Wrapping Up<\/h2>\n<p>The HLK-PM01 and HLK-PM03 are converter modules that provide 5V and 3.3V respectively, from mains voltage. This provides an easy way to power your ESP8266 or ESP32 in your IoT and Home Automation projects.<\/p>\n<p>We hope you&#8217;ve found this project helpful! If you like these subjects, you may also like:<\/p>\n<ul>\n<li><strong><a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">[Course] Learn ESP32 with Arduino IDE<\/a><\/strong><\/li>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/home-automation-using-esp8266\/\">[Course] Home Automation using ESP8266<\/a><\/li>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/latching-power-switch-circuit-auto-power-off-circuit-esp32-esp8266-arduino\/\" data-cke-saved-href=\"https:\/\/randomnerdtutorials.com\/latching-power-switch-circuit-auto-power-off-circuit-esp32-esp8266-arduino\/\">Latching Power Switch Circuit (Auto Power Off)<\/a><\/li>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-multisensor-shield\/\" data-cke-saved-href=\"https:\/\/randomnerdtutorials.com\/esp8266-multisensor-shield\/\">Build a Multisensor Shield for ESP8266<\/a><\/li>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/build-an-all-in-one-esp32-weather-station-shield\/\" data-cke-saved-href=\"https:\/\/randomnerdtutorials.com\/build-an-all-in-one-esp32-weather-station-shield\/\">All-in-One ESP32 Weather Station Shield<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this post you&#8217;ll learn how to power the ESP8266 (or ESP32) with mains voltage using the Hi-Link HLK-PM03 converter. As an example, we&#8217;ll use the ESP8266-01 to control a &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Power ESP8266 with Mains Voltage using Hi-Link HLK-PM03 Converter\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp8266-hi-link-hlk-pm03\/#more-78606\" aria-label=\"Read more about Power ESP8266 with Mains Voltage using Hi-Link HLK-PM03 Converter\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":78648,"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":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[214,265,246,240,300,264],"tags":[],"class_list":["post-78606","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp8266","category-esp8266-project","category-esp8266-arduino-ide","category-esp8266-projects","category-0-esp8266","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/12\/hlk-pm03-esp8266-01.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\/78606","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\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=78606"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/78606\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/78648"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=78606"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=78606"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=78606"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}