Introduction: ESP8266 Hack: Enhance Your PIR Motion Sensor

We will adapt a commercial motion sensor, which operates on mains voltage, for this project. The modification involves integrating an ESP8266 to record data upon detecting motion. This data will then be transmitted to Node-RED via the MQTT communication protocol. Power for the ESP8266 will be supplied through the motion sensor’s phase-out wire using the HLK-PM03 AC/DC converter.

Supplies

  • ESP8266 NodeMCU CP2102 Amazon
  • HC-SR501 PIR Infrared Sensor Amazon

Step 1: Project Overview

This project consists of three main parts:

  1. Building the circuit
  2. Writing and uploading the ESP8266 code
  3. Creating the Node-RED flow

The diagram below provides a high-level overview of the project we will construct.

Motion Sensor

We will modify a commercial motion sensor, ensuring it has sufficient space to accommodate an ESP-01 and the HLK-PM03 AC/DC converter module. Our motion sensor was purchased for $5 from a local store.

When the PIR sensor detects motion, power is supplied through the red hot wire, capable of activating a lamp or device. Your motion sensor should include a wiring diagram on the lid or in the instruction manual.

In our setup, the output load of the motion sensor is the HLK-PM03 AC/DC converter module, which powers the ESP8266.

The HLK-PM03 module’s AC/DC converter provides 3.3V from either 110VAC or 220VAC, making it ideal for powering the ESP8266 from mains voltage.

In summary, motion detection triggers power to reach the ESP8266, allowing it to perform tasks as long as the motion sensor remains active.

You may need to adjust the sensor’s duration of activity to ensure the ESP8266 has sufficient time to execute its tasks. The sensor should have knobs for adjusting both time and luminosity.

In our example, upon powering up, the ESP8266 runs a sketch that sends information to Node-RED via MQTT to log the date and time of motion detection.

Alternatively, instead of sending information to Node-RED, you can perform other tasks such as:

  • Logging data to a Google Spreadsheet
  • Sending an email alert for motion detection
  • Sending notifications to your smartphone

These tasks can be easily accomplished using IFTTT.

Step 2: Building the Circuit

The schematic diagram below illustrates the circuit for this project.

Start by removing the lid of your PIR motion sensor. Inside, you should find three wires: phase in, neutral, and phase out. Follow these steps:

  • Connect phase in (brown) and neutral (blue) wires to the motion sensor.
  • Connect neutral (blue) and phase out (red) wires to the input of the HLK-PM03.

It’s advisable to incorporate a slow-blow fuse just before the HKL-PM03 converter and a capacitor at the output.

Note: If you’re utilizing an ESP8266 that remains powered on with the HLK-PM03, we recommend employing this protection circuit.

The HLK-PM03 supplies 3.3V and GND, which are linked to the ESP8266’s VCC and GND pins to provide power.

We’ve assembled the HLK-PM03 and ESP8266 circuit on a compact protoboard to conserve space. Additionally, we’ve included header pins for the ESP8266-01 module, allowing for easy attachment and detachment of the board whenever new code needs to be uploaded.

Step 3: Writing and Uploading the ESP8266 Code

To program the ESP8266, we’ll utilize the Arduino IDE. Before uploading any code to your ESP8266, ensure you have installed the ESP8266 add-on (Install the ESP8266 Board in Arduino IDE) if you haven’t already done so.

Additionally, you’ll need to install the PubSubClient library to establish an MQTT client with the ESP8266. This library facilitates simple publish/subscribe messaging with a server supporting MQTT, essentially enabling communication between your ESP8266 and Node-RED.

Follow these steps:

  1. Download the PubSubClient library by clicking here. This will result in a .zip folder in your Downloads directory.
  2. Unzip the .zip folder to obtain the pubsubclient-master folder.
  3. Rename the folder from pubsubclient-master to pubsubclient.
  4. Move the pubsubclient folder to the libraries folder within your Arduino IDE installation directory.

Next, copy the provided code to your Arduino IDE, but refrain from uploading it immediately. You’ll need to make several modifications to tailor it to your specific requirements.

Ensure to edit the code with your own SSID, password, and MQTT Broker IP Address (Raspberry Pi IP Address).

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// Replace with your SSID, password and MQTT broker IP address
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";
const char* mqtt_server = "REPLACE_WITH_YOUR_MQTT_BROKER_IP";
//For example
//const char* mqtt_server = "192.168.1.144";

WiFiClient espClient;
PubSubClient client(espClient);

void setup_wifi() {

delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

randomSeed(micros());

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}

void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
digitalWrite(BUILTIN_LED, HIGH);
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
while (!client.connected()) {
reconnect();
}
Serial.print("Motion Detected");
// Publish MQTT message
String mqttMessage = "Motion Detected";
client.publish("esp/pir", mqttMessage.c_str());
}

void loop() {
client.loop();
}


Step 4: See Full Article Here