So I have been looking at making some sensors when i stumbled upon the these PIR sensors.
After a quick check I realised that they would fit nicely where the button goes on the S26 and as i had a faulty one laying around doing nothing I thought that I might try it.
!!DISCLAIMER!!
You do this project at your own risk!
I am not a qualified electrician, but I am an idiot. This project involves working with the mains power supply so DO NOT be an idiot like me and seek help from a qualified professional when it comes working with high voltages.
Here are the list of products I used:
Sonoff S26
AC-DC Converter 110V 220V 230V to 5V
IR Pyroelectric Infrared PIR Motion Sensor
Wemos D1 Mini
Here is how i did it:
- Removed the Sonoff S26’s circuitry leaving just the live and neutral wire.
- Soldered the live and neutral wires to the buck converter.
- Soldered the +/- wires for the 5V output.
- Soldered the 5V output wires to a micro USB plug.
- Soldered the wires to the 5V, Ground & D1 GPIO pins.
- Used the Arduino IDE to upload the script below.
- Carefully put all parts into the S26 and screw it back together.
Arduino Settings
- Board Manager: http://arduino.esp8266.com/stable/package_esp8266com_index.json
- Upload Settings
Arduino Script
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Replace with your SSID, password and MQTT broker IP address
const char* ssid = "WIFI_NETWORK";
const char* password = "WIFI_PASSWORD";
const char* mqtt_user = "MQTT_USERNAME";
const char* mqtt_password = "MQTT_PASSWORD";
const char* mqtt_server = "MQTT_IP_ADDRESS";
const char* clientID = "sonfoff/pir";
const char* topic = "sonfoff/pir";
const int inputPin = D3;//input pin is D3 in here but labelled D1 on the D1 mini
int motionState = 0;
int i = 0;
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.mode(WIFI_STA);
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...");
// Attempt to connect
if (client.connect(clientID, mqtt_user, mqtt_password)) {
Serial.println("connected");
client.publish(topic, "test", true);
} 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();
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
motionState = digitalRead(inputPin);
if ( motionState == HIGH ) {
Serial.println("Motion Detected");
client.publish(topic, "on", true);
i = 0;
}
while(motionState == LOW && i ==0){
client.publish(topic, "off", true);
Serial.println("Motion Stopped");
i++;
}
}