Sonoff Wall Switch

Introduction: Sonoff Wall Switch


small piect of 1/4" plywood

Yosoo Spring Pressure Test Probe Pogo Pin P50-B1 Dia 0.68mm Length 16mm 75g Pack of 100 Gold $8

5V 3.3V FT232RL FTDI USB to TTL Serial Adapter Module for Arduino Mini Port

simple on/off switch

Step 1: Starting the Project

OK, just another Sonoff project. There's another one where you replace a wall switch but the code wasn't shown and didn't work the way I wanted it to work - so, here's mine.

First I've created a small jig to program my Sonoff's. I've used spring probes rather than soldering in pins. ie: vacuum board. That way I simply set the board in place, press the button, flip switch from off to on, hold for a couple of seconds and then upload.

Once programmed I can web to http://[IP]/on or off or "curl http://192.../on"

Step 2:

I can't get the code to display less than and greater than to display - so it's "LT" and "GT"


#include LTESP8266WiFi.hGT
#include LTWiFiClient.hGT
#include LTESP8266WebServer.hGT

IPAddress ip( 192, 168, 0, 132 );
IPAddress gateway( 192, 168, 0, 1 );
IPAddress subnet( 255, 255, 255, 0 );

// Replace with your network credentials
const char* ssid = "Wood******";
const char* password = "43f********";

ESP8266WebServer server(80);
int relayState = 0;
int relay = 12;
int led = 13;
int button = 0;
int button_state = 0;

LTa href=onGTLTbutton style=background-color:green;color:white;width:100px; height:40px;GTLTbGTLTbigGTGTONLTLT/buttonGTLT/aGT LTa href=offGTLTbutton style=b ackground-color:red; color:white;width:100px; height:40px;GTLTbGTLTbigGTOFFLT/buttonGTL T/aGT";String webPageOn ="

String webPageOff =LTa href=onGTLTbutton style=background-color:green;color:white;width:100px; height:40px;GTLTbGTLTbigGTONLT/buttonGTLT/aGT LTa href=offGTLTbutton style=back ground-color:red; color:white;width:100px; height:40px;GTLTbGTLTbigGTGTOFFLTLT/buttonGT LT/aGT";

void setup(void){

// preparing GPIOs
pinMode(led, OUTPUT); digitalWrite(led, HIGH);
pinMode(relay, OUTPUT); digitalWrite(relay, HIGH);
pinMode(button, INPUT);

WiFi.config (ip, gateway, subnet);
WiFi.begin ( ssid, password );
// Wait for connection

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

server.on("/", [](){
if (relayState == 0)
server.send(200, "text/html", webPageOff);
server.send(200, "text/html", webPageOn);

server.on("/on", [](){
server.send(200, "text/html", webPageOn);
digitalWrite(relay, HIGH); // Relay On, Led Off
digitalWrite(led, HIGH);
relayState = 1;

server.on("/off", [](){
server.send(200, "text/html", webPageOff);
digitalWrite(relay, LOW); // Relay Off, Led On
digitalWrite(led, LOW);
relayState = 0;

void loop(void)

button_state = digitalRead(button);
Serial.print (button_state);
// Has the button been pressed - if so toggle
if ( button_state == 0 )
if (relayState == 0)
digitalWrite(relay, HIGH); // Relay On
digitalWrite(led, HIGH); // Led Off
relayState = 1; // save state

} else {
digitalWrite(relay, LOW); // Relay Off
digitalWrite(led, LOW); // Led On
relayState = 0; // save state
delay (300); // worry about bounce

server.handleClient(); // anything via the wifi
delay (300);

Be the First to Share


    • Hour of Code Speed Challenge

      Hour of Code Speed Challenge
    • Cookie Speed Challenge

      Cookie Speed Challenge
    • Reclaimed Materials Contest

      Reclaimed Materials Contest