Repurposed (Faulty) Sonoff S26 to a plug-in PIR sensor

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.


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

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() {

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

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {


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

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)) {
      client.publish(topic, "test", true);  
    } else {
      Serial.print("failed, rc=");
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  digitalWrite(BUILTIN_LED, HIGH);
  client.setServer(mqtt_server, 1883);
  while (!client.connected()) {

void loop() {
  if (!client.connected()) {

  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");


Was there not enough space to use the existing electronics in the switch?

It uses an esp8266 and has spare GPIO. It seems odd that you are replacing what you threw away with essentially the same thing.

1 Like

Not to mention that dodgy looking power supply. What fusing, over current and overheating protection do you have?

And that switch functionality is lost.

The switch functionality was already lost as it was a faulty S26 that never worked in the first place.

1 Like

According to the description for the converter it has

Over-voltage protection, over-current protection, over-temperature protection, over-power, short circuit protection.

Do you think this would be sufficient?

I don’t see that in the description you pointed to but if I missed it, that seems fine.

Edit: the description is hidden in a scrolling portion. Awful page design.

1 Like