{"id":80644,"date":"2019-02-06T12:12:00","date_gmt":"2019-02-06T12:12:00","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=80644"},"modified":"2020-07-30T14:16:30","modified_gmt":"2020-07-30T14:16:30","slug":"hack-pir-motion-sensor-esp8266-hlk-pm03","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/hack-pir-motion-sensor-esp8266-hlk-pm03\/","title":{"rendered":"Hack a PIR Motion Sensor with an ESP8266"},"content":{"rendered":"\n<p>In this project we\u2019ll modify a commercial motion sensor (powered with mains voltage) with an ESP8266 to log data whenever motion is detected. The data will be sent to Node-RED using MQTT communication protocol. The ESP8266 will be powered through the motion sensor phase out wire using the HLK-PM03 AC\/DC converter. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"800\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/HLK-PM03-ESP8266.jpg?resize=1200%2C800&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Hack a PIR Motion Sensor with an ESP8266\" class=\"wp-image-80733\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/HLK-PM03-ESP8266.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/HLK-PM03-ESP8266.jpg?resize=300%2C200&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/HLK-PM03-ESP8266.jpg?resize=768%2C512&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/HLK-PM03-ESP8266.jpg?resize=1024%2C682&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>Before continuing with this project, we recommend taking a look at the following resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-with-esp8266-wifi-transceiver-review\/\">Getting Started with ESP8266 WiFi Transceiver (Review)<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-hi-link-hlk-pm03\/\">Power ESP8266 with Mains Voltage using Hi-Link HLK-PM03<\/a> <\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/what-is-mqtt-and-how-it-works\/\">What is MQTT and How It Works<\/a><\/li><li>You need to have installed in your Raspberry Pi: <a href=\"https:\/\/randomnerdtutorials.com\/getting-started-with-node-red-on-raspberry-pi\/\">Node-RED<\/a>, <a href=\"https:\/\/randomnerdtutorials.com\/getting-started-with-node-red-dashboard\/\">Node-RED Dashboard<\/a>, and <a href=\"https:\/\/randomnerdtutorials.com\/how-to-install-mosquitto-broker-on-raspberry-pi\/\">Mosquitto MQTT broker<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-pinout-reference-gpios\/\">ESP8266 GPIOs and Pinout Reference<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Parts Required:<\/h2>\n\n\n\n<p>Here\u2019s a list of the parts needed for this project:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noreferrer noopener nofollow\" aria-label=\"Motion Sensor 220V (opens in a new tab)\" href=\"https:\/\/www.banggood.com\/220V-Adjustable-PIR-Infrared-Human-Body-Motion-Sensor-Intelligent-Light-Switch-p-1090835.html?p=MA240439985285201910&amp;cur_warehouse=CN\" target=\"_blank\">PIR Motion Sensor 220V<\/a> (or <a rel=\"noreferrer noopener nofollow\" aria-label=\"110V alternative (opens in a new tab)\" href=\"https:\/\/www.banggood.com\/LED-12V-Infrared-PIR-Motion-Sensor-Detector-Wall-Light-Switch-140Degree-12M-p-980681.html?p=MA240439985285201910&amp;cur_warehouse=CN\" target=\"_blank\">110V PIR&nbsp;Motion&nbsp;Sensor<\/a>)<\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/esp-01-wi-fi-board\/\" target=\"_blank\">ESP8266-01<\/a> &#8211; read <a rel=\"noreferrer noopener\" aria-label=\"Best ESP8266 Wi-Fi Development Boards (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/best-esp8266-wi-fi-development-board\/\" target=\"_blank\">Best ESP8266 Wi-Fi Development Boards<\/a><\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/esp8266-01-serial-adapter\/\" target=\"_blank\">ESP8266-01 Serial Adapter<\/a> (to upload code to the ESP8266)<\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/hi-link-hlk-pm03\/\" target=\"_blank\">Hi-Link HLK-PM03<\/a> (to convert mains voltage to DC 3.3V)<\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/prototyping-circuit-board-stripboard\/\" target=\"_blank\">Small protoboard<\/a><\/li><li>Fuse Slow Blow (200mA)<\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/electrolytic-capacitors-kit\/\" target=\"_blank\">47 uF electrolytic capacitor<\/a><\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/raspberry-pi-board\/\" target=\"_blank\">Raspberry Pi<\/a> (to host Node-RED and MQTT broker) &#8211; read <a rel=\"noreferrer noopener\" aria-label=\"Best Raspberry Pi Starter Kits (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/best-raspberry-pi-3-starter-kits\/\" target=\"_blank\">Best Raspberry Pi Kits<\/a><\/li><\/ul>\n\n\n\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\n\n\n<h2 class=\"wp-block-heading\">Project Overview<\/h2>\n\n\n\n<p>This project is divided into three parts:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Building the circuit<\/li><li>Writing and uploading the ESP8266 code<\/li><li>Creating the Node-RED flow<\/li><\/ol>\n\n\n\n<p>The following image shows a high-level overview of the project we\u2019ll build.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"423\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/project-overview-pir-esp8266-hlk-pm03.png?resize=750%2C423&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 PIR Motion Sensor HLK-PM03\" class=\"wp-image-80649\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/project-overview-pir-esp8266-hlk-pm03.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/project-overview-pir-esp8266-hlk-pm03.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Motion Sensor<\/h3>\n\n\n\n<p>We\u2019ll hack a commercial motion sensor that has enough space to put an ESP-01 and the HLK-PM03 AC\/DC converter module inside.  We bought our motion sensor for $5 in a local store. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"500\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/DSC04162.jpgpir-motion-sensor.jpg?resize=750%2C500&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-80736\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/DSC04162.jpgpir-motion-sensor.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/DSC04162.jpgpir-motion-sensor.jpg?resize=300%2C200&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>When the PIR sensor detects motion, there&#8217;s power coming out from the red hot wire that can turn on a lamp or a device.  Your motion sensor should have a similar wiring diagram on the lid or printed in the instructions manual. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"500\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/pir-motion-sensor-wiring.jpg?resize=750%2C500&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"pir-motion-sensor-wiring\" class=\"wp-image-80737\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/pir-motion-sensor-wiring.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/pir-motion-sensor-wiring.jpg?resize=300%2C200&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>In our project, the motion sensor output load is the <a rel=\"noreferrer noopener\" aria-label=\"HLK-PM03 AC\/DC (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/hi-link-hlk-pm03\/\" target=\"_blank\">HLK-PM03 AC\/DC<\/a> converter module that will power the ESP8266.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"421\" 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=\"\" class=\"wp-image-78646\" 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\" \/><figcaption>Hi-Link HLK-PM03<\/figcaption><\/figure><\/div>\n\n\n\n<p><strong>Related: <\/strong><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-hi-link-hlk-pm03\/\">Power ESP8266 with AC voltage using HLK-PM03 Converter<\/a><\/p>\n\n\n\n<p>The HLK-PM03 module AC\/DC converter supplies 3.3V from either 110VAC or 220VAC. This makes it perfect to power the ESP8266 from mains voltage.<\/p>\n\n\n\n<p class=\"rntbox rntclgray\">In summary, when motion is detected there is power reaching the ESP8266. The ESP8266 can execute tasks for as long as the motion sensor is triggered.<\/p>\n\n\n\n<p>You may need to adjust the time duration that the sensor stays on, so your ESP8266 has enough time to execute its tasks. The sensor should have a knob to adjust the time (and another to adjust luminosity).<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"500\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/motion-sensor-lux-timer-trigger.jpg?resize=750%2C500&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-80738\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/motion-sensor-lux-timer-trigger.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/motion-sensor-lux-timer-trigger.jpg?resize=300%2C200&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>In our example, whenever the ESP8266 powers up, it runs a sketch that sends information to Node-RED via MQTT to log the date and time that motion was detected.<br><\/p>\n\n\n\n<p>Instead of sending information to Node-RED, you can execute other tasks, such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-publish-sensor-readings-to-google-sheets\/\">Log data to a Google Spreadsheet<\/a>;<\/li><li>Send an email warning that motion was detected;<\/li><li>Send notifications to your smartphone.<\/li><\/ul>\n\n\n\n<p>These tasks can be easily done using IFTTT.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Building the Circuit<\/h2>\n\n\n\n<p>The following schematic diagram shows the circuit for this project.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/esp8266-pir-arduino-ide-mqtt-hlk-pm03-circuit.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"548\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/esp8266-pir-arduino-ide-mqtt-hlk-pm03-circuit.png?resize=1024%2C548&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-80657\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/esp8266-pir-arduino-ide-mqtt-hlk-pm03-circuit.png?resize=1024%2C548&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/esp8266-pir-arduino-ide-mqtt-hlk-pm03-circuit.png?resize=300%2C161&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/esp8266-pir-arduino-ide-mqtt-hlk-pm03-circuit.png?resize=768%2C411&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/esp8266-pir-arduino-ide-mqtt-hlk-pm03-circuit.png?resize=280%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/esp8266-pir-arduino-ide-mqtt-hlk-pm03-circuit.png?w=1412&amp;quality=100&amp;strip=all&amp;ssl=1 1412w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div>\n\n\n\n<p>Remove your PIR motion sensor lid. Inside, it should have three wires: <strong>phase in<\/strong>, <strong>neutral<\/strong>, and <strong>phase out<\/strong>. Follow these steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Wire <strong>phase in<\/strong> (brown) and <strong>neutral <\/strong>(blue) to the motion sensor<\/li><li>Wire <strong>neutral <\/strong>(blue) and <strong>phase out<\/strong> (red) to the HLK-PM03 input<\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"500\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/motion-sensor-wiring.jpg?resize=750%2C500&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-80805\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/motion-sensor-wiring.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/motion-sensor-wiring.jpg?resize=300%2C200&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>It&#8217;s recommended to add a slow blow fuse right before the HKL-PM03 converter and a capacitor to the output. <\/p>\n\n\n\n<p class=\"rntbox rntclgreen\"><strong>Note: <\/strong> if you&#8217;re using an ESP8266 that is always powered on with the HLK-PM03, we recommend using <a href=\"https:\/\/randomnerdtutorials.com\/esp8266-hi-link-hlk-pm03\/\">this protection circuit<\/a>.<\/p>\n\n\n\n<p>The HLK-PM03 outputs 3.3V and GND. These are connected to the ESP8266 VCC and GND pin to power it.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"456\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/HLK-PM03-ESP8266-circuit.jpg?resize=750%2C456&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-80739\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/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\/2019\/02\/HLK-PM03-ESP8266-circuit.jpg?resize=300%2C182&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>We&#8217;ve built the HLK-PM03 and ESP8266 circuit on a small protoboard to save space. We&#8217;ve also added some header pins to place the ESP8266-01. This way you can plug and unplug the board every time you need to upload new code.<\/p>\n\n\n\n<p><strong>Recommended reading:<\/strong> <a href=\"https:\/\/randomnerdtutorials.com\/esp8266-pinout-reference-gpios\/\">ESP8266 GPIOs and Pinout refence guide<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Writing and Uploading the ESP8266 Code<\/h2>\n\n\n\n<p>We\u2019ll program the ESP8266 using the Arduino IDE. In order to upload code to your ESP8266, you need to install the ESP8266 add-on first, if you haven&#8217;t already (<a href=\"https:\/\/randomnerdtutorials.com\/how-to-install-esp8266-board-arduino-ide\/\">Install the ESP8266 Board in Arduino IDE<\/a>).<\/p>\n\n\n\n<p>You\u2019ll also need to install the <a rel=\"noreferrer noopener\" aria-label=\"PubSubClient  (opens in a new tab)\" href=\"https:\/\/github.com\/knolleary\/pubsubclient\" target=\"_blank\">PubSubClient<\/a> library to create an MQTT client with the ESP8266. The PubSubClient library provides a client for doing simple publish\/subscribe messaging with a server that supports MQTT (basically allows your ESP8266 to talk with Node-RED).<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" aria-label=\"Click here to download the PubSubClient library (opens in a new tab)\" href=\"https:\/\/github.com\/knolleary\/pubsubclient\/archive\/master.zip\" target=\"_blank\">Click here to download the PubSubClient library<\/a>. You should have a <em>.zip<\/em> folder in your Downloads folder<\/li><li>Unzip the<em> .zip<\/em> folder and you should get <strong>pubsubclient-master<\/strong> folder<\/li><li>Rename your folder from <strong><del>pubsubclient-master<\/del><\/strong> to <strong>pubsubclient<\/strong><\/li><li>Move the <strong>pubsubclient <\/strong>folder to your Arduino IDE installation <strong>libraries <\/strong>folder <\/li><\/ul>\n\n\n\n<p>Then, copy the following code to your Arduino IDE, but don&#8217;t upload it yet. You need to make a few modifications to make it work for you. <\/p>\n\n\n\n<p><strong>You need to edit the code with your own SSID, password and MQTT Broker IP Address (Raspberry Pi IP Address).<\/strong><\/p>\n\n\n\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#include &lt;ESP8266WiFi.h&gt;\n#include &lt;PubSubClient.h&gt;\n\n\/\/ Replace with your SSID, password and MQTT broker IP address\nconst char* ssid = &quot;REPLACE_WITH_YOUR_SSID&quot;;\nconst char* password = &quot;REPLACE_WITH_YOUR_PASSWORD&quot;;\nconst char* mqtt_server = &quot;REPLACE_WITH_YOUR_MQTT_BROKER_IP&quot;;\n\/\/For example\n\/\/const char* mqtt_server = &quot;192.168.1.144&quot;;\n\nWiFiClient espClient;\nPubSubClient client(espClient);\n\nvoid setup_wifi() {\n\n  delay(10);\n  \/\/ We start by connecting to a WiFi network\n  Serial.println();\n  Serial.print(&quot;Connecting to &quot;);\n  Serial.println(ssid);\n\n  WiFi.begin(ssid, password);\n\n  while (WiFi.status() != WL_CONNECTED) {\n    delay(500);\n    Serial.print(&quot;.&quot;);\n  }\n\n  randomSeed(micros());\n\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}\n\nvoid reconnect() {\n  \/\/ Loop until we're reconnected\n  while (!client.connected()) {\n    Serial.print(&quot;Attempting MQTT connection...&quot;);\n    \/\/ Create a random client ID\n    String clientId = &quot;ESP8266Client-&quot;;\n    clientId += String(random(0xffff), HEX);\n    \/\/ Attempt to connect\n    if (client.connect(clientId.c_str())) {\n      Serial.println(&quot;connected&quot;);\n    } else {\n      Serial.print(&quot;failed, rc=&quot;);\n      Serial.print(client.state());\n      Serial.println(&quot; try again in 5 seconds&quot;);\n      \/\/ Wait 5 seconds before retrying\n      delay(5000);\n    }\n  }\n}\n\nvoid setup() {\n  pinMode(BUILTIN_LED, OUTPUT);     \/\/ Initialize the BUILTIN_LED pin as an output\n  digitalWrite(BUILTIN_LED, HIGH);\n  Serial.begin(115200);\n  setup_wifi();\n  client.setServer(mqtt_server, 1883);\n  while (!client.connected()) {\n    reconnect();\n  }\n  Serial.print(&quot;Motion Detected&quot;);\n  \/\/ Publish MQTT message\n  String mqttMessage = &quot;Motion Detected&quot;;  \n  client.publish(&quot;esp\/pir&quot;, mqttMessage.c_str());\n}\n\nvoid loop() {\n  client.loop();\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\/ESP8266_Publish_MQTT_Message.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Include your network credentials<\/h3>\n\n\n\n<p>You need to include your network credentials in the following lines.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const char* ssid = \"REPLACE_WITH_YOUR_SSID\";\nconst char* password = \"REPLACE_WITH_YOUR_PASSWORD\";<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Include your MQTT broker IP address<\/h3>\n\n\n\n<p>You also need to include your MQTT broker IP address. <\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const char* mqtt_server = \"REPLACE_WITH_YOUR_MQTT_BROKER_IP\"; <\/code><\/pre>\n\n\n\n<p>To find your MQTT broker IP address, it should be configured first. We&#8217;re using Mosquitto broker hosted on a Raspberry Pi. Follow the next resources if you haven&#8217;t done that yet:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/how-to-install-mosquitto-broker-on-raspberry-pi\/\">How to Install Mosquitto Broker on Raspberry Pi<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/testing-mosquitto-broker-and-client-on-raspbbery-pi\/\">Testing Mosquitto Broker and Client on Raspbbery Pi<\/a><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">How the code works<\/h3>\n\n\n\n<p>The code is very simple. It simply publishes a message on a topic. For testing purposes, we&#8217;re also turning the on-board LED on every time the ESP8266 is powered. This is done on the <span class=\"rnthl rntliteral\">setup()<\/span> because it will only be executed once (when motion is detected and while the ESP8266 is powered).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void setup() {\n   pinMode(BUILTIN_LED, OUTPUT); \/\/ BUILTIN_LED pin as an output\n   digitalWrite(BUILTIN_LED, HIGH);\n   Serial.begin(115200);\n   setup_wifi();\n   client.setServer(mqtt_server, 1883);\n   while (!client.connected()) {\n     reconnect();\n   }\n   Serial.print(\"Motion Detected\");\n   \/\/ Publish MQTT message\n   String mqttMessage = \"Motion Detected\";  \n   client.publish(\"esp\/pir\", mqttMessage.c_str());\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Uploading the code<\/h3>\n\n\n\n<p>After modifying the code with your network credentials, you can upload it to your board. To upload the code to the ESP8266-01 you need a <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/esp8266-01-serial-adapter\/\" target=\"_blank\">serial adapter<\/a> or an <a href=\"https:\/\/makeradvisor.com\/tools\/ftdi-programmer-board\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">FTDI programmer<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Creating the Node-RED Flow<\/h2>\n\n\n\n<p>Before creating the flow, you need to have installed in your Raspberry Pi:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-with-node-red-on-raspberry-pi\/\">Node-RED<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-with-node-red-dashboard\/\">Node-RED Dashboard<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/how-to-install-mosquitto-broker-on-raspberry-pi\/\">Mosquitto Broker<\/a><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Importing the Node-RED flow<\/h3>\n\n\n\n<p>To import the provided Node-RED flow, go to the&nbsp;<a href=\"https:\/\/raw.githubusercontent.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/master\/Projects\/ESP8266-HLK\/NodeRED_Motion_Datalogger.txt\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">GitHub repository<\/a>&nbsp;or click the figure below to see the raw file, and copy the code provided.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/raw.githubusercontent.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/master\/Projects\/ESP8266-HLK\/NodeRED_Motion_Datalogger.txt\" target=\"_blank\" rel=\"noreferrer noopener\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"64\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/node-red-flow-github.png?resize=750%2C64&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-80745\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/node-red-flow-github.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/node-red-flow-github.png?resize=300%2C26&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/a><\/figure><\/div>\n\n\n\n<p>Next, in the Node-RED window, at the top right corner, select the menu, and go to&nbsp;<strong>Import&nbsp;<\/strong>&nbsp;&gt;&nbsp;<strong>Clipboard<\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"523\" height=\"291\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/11\/import-nodes.png?resize=523%2C291&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-45987\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/11\/import-nodes.png?w=523&amp;quality=100&amp;strip=all&amp;ssl=1 523w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/11\/import-nodes.png?resize=300%2C167&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 523px) 100vw, 523px\" \/><\/figure><\/div>\n\n\n\n<p>Then, paste the code provided and click&nbsp;<strong>Import<\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"501\" height=\"336\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/node-red-import-flow.png?resize=501%2C336&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-73111\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/node-red-import-flow.png?w=501&amp;quality=100&amp;strip=all&amp;ssl=1 501w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/09\/node-red-import-flow.png?resize=300%2C201&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 501px) 100vw, 501px\" \/><\/figure><\/div>\n\n\n\n<p>Here\u2019s the imported flow. It receives the ESP8266 MQTT messages and logs the time whenever motion is detected. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"722\" height=\"323\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/Node-RED-flow-ESP8266-HLK-PIR-f-1.png?resize=722%2C323&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-80748\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/Node-RED-flow-ESP8266-HLK-PIR-f-1.png?w=722&amp;quality=100&amp;strip=all&amp;ssl=1 722w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/Node-RED-flow-ESP8266-HLK-PIR-f-1.png?resize=300%2C134&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 722px) 100vw, 722px\" \/><\/figure><\/div>\n\n\n\n<p>We&#8217;ve also added two buttons to clear and refresh the log. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Node-RED Dashboard<\/h2>\n\n\n\n<p>After making all the necessary changes, click the&nbsp;<strong>Deploy<\/strong>&nbsp;button to save all the changes.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"138\" height=\"40\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/11_deploy_button.png?resize=138%2C40&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-40013\"\/><\/figure><\/div>\n\n\n\n<p>Now, your Node-RED application is ready. To access Node-RED Dashboard and see how your application looks, access any browser in your local network and type:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">http:\/\/<strong>Your_RPi_IP_address:1880<\/strong>\/ui<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>Now, you can test your project. Whenever motion is detected, the ESP8266 powers up and sends a message via MQTT to Node-RED.<\/p>\n\n\n\n<p>If you go to your Node-RED Dashboard you can see all the logs of when motion was detected. Your application should look as in the following figure. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"655\" height=\"607\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/node-red-log_f.png?resize=655%2C607&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-80803\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/node-red-log_f.png?w=655&amp;quality=100&amp;strip=all&amp;ssl=1 655w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/node-red-log_f.png?resize=300%2C278&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 655px) 100vw, 655px\" \/><\/figure><\/div>\n\n\n\n<p>The dashboard shows the last time motion was detected and all previous logs. You also have the option to refresh the log, and clear the log. Please note that clearing the log is irreversible. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>This project showed how you can hack a commercial motion sensor with an ESP8266 to make it smarter. You can easily power the ESP8266 with the HLK-PM03 from mains voltage and make a compact circuit that can fit in the motion sensor. <\/p>\n\n\n\n<p>Whenever motion is detected the ESP8266 is powered and executes a task. In this case, it publishes an MQTT message to log the time motion was detected, but you can easily write code to execute any other task.<\/p>\n\n\n\n<p>We hope you&#8217;ve found this project useful. If you liked this project, you may also be interested in one the following resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-hi-link-hlk-pm03\/\">Power ESP8266 with Mains Voltage using Hi-Link HLK-PM03 Converter<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-mqtt-esp32-esp8266\/\">MicroPython \u2013 Getting Started with MQTT on ESP32\/ESP8266<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-multisensor-shield-with-node-red\/\">ESP8266 Multisensor Shield with Node-RED<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/home-automation-using-esp8266\/\"><strong>[eBook] Home Automation using ESP8266<\/strong><\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In this project we\u2019ll modify a commercial motion sensor (powered with mains voltage) with an ESP8266 to log data whenever motion is detected. The data will be sent to Node-RED using MQTT communication protocol. <\/p>\n<p class=\"read-more-container\"><a href=\"https:\/\/randomnerdtutorials.com\/hack-pir-motion-sensor-esp8266-hlk-pm03\/\" class=\"read-more button\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":80733,"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,300,240,264,268],"tags":[],"class_list":["post-80644","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp8266","category-esp8266-project","category-0-esp8266","category-esp8266-projects","category-project","category-raspberry-pi-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/HLK-PM03-ESP8266.jpg?fit=1280%2C853&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/80644","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=80644"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/80644\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/80733"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=80644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=80644"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=80644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}