in General



Got a few ITEAD Sonoff WiFi switches to automate my home. Why use the boring software, so I tried to hack the switches. This is a reference manual for me, but quite useful for anyone else if they want to use micropython, mqtt, web server and control the switches.

Required items

Flashing micropython

Make sure the FTDI cable is set to 3V which is very important. Then connect the cable as detailed in the table.


Programmer Sonoff (counting from the switch to bottom)
3V3 1
TX 2 (RX)
RX 3 (TX)

The easiest way to flash the firmware is to use the nodemcu-flasher available. Unfortunately available only for windows. The micropython-flash image esp8266-20170108-v1.8.7.bin can be downloaded from the website. On the config tab add the image. Sometimes it helps to first flash a blank image to the WiFi switch.


Installing MQTT

Install mosquitto, an open source MQTT broker. It is very easy to install, follow the installation instructions in the link or copy paste the following commands.

wget sudo
apt-key add mosquitto-repo.gpg.key
cd /etc/apt/sources.list.d/
sudo wget
sudo wget
sudo apt-get update
sudo apt-get install mosquitto

view raw

hosted with ❤ by GitHub

Programming MQTT

MQTT service is started and running on the machine. I will deal with the basic programming initially. May be later, MQTT will be tightened up with security like auth, base64 encoded message etc.

there are few things to know first hand. That would be, the pins for the physical objects on the switch. There is a relay, led and a button. I want to use the button in case I m not connected to internet to be able to toggle the switch. Led is concealed inside the housing, so doesn’t matter. Relay is very important which will be switching the on off the device that is connect. This pin configuration is specific to micro python firmware. In LUA it might be different.

PIN 12-----------------replay

PIN 13-----------------LED

PIN 0------------Button

from umqtt.simple import MQTTClient
from machine import Pin
import ubinascii
import machine
import micropython
global switch_state
switch_state = True
CLIENT_ID = ubinascii.hexlify(machine.unique_id())
SERVER = 'localhost'
TOPIC = b"home/tv"
#– Pin which the relay is connected to
relayPin = 12
p6 = Pin(relayPin, Pin.OUT) # create output pin on GPIO6
#– Connected to switch with internal pullup enabled
# buttonPin = 0
# buttonDebounce = 0
# p3 = Pin(buttonPin, Pin.OUT) # create output pin on GPIO3
#– MQTT led
p13 = Pin(mqttLed, Pin.OUT) # create output pin on GPIO7
def sub_cb(topic,msg):
global switch_state
print((topic, msg))
if msg == b"on":
switch_state = 1
elif msg == b"off":
switch_state = 0
def main(server=SERVER):
c = MQTTClient(CLIENT_ID, server)
# Subscribed messages will be delivered to this callback
print("Connected to %s, subscribed to %s topic" % (server, TOPIC))
while 1:
# micropython.mem_info()
if __name__ == '__main__':

Diving in to the code, there are only three parts that are done. One is the variable that is used around in all functions declared as global to keep the state known at all times. Then connect to mqtt and subscribe to a certain topic. Here I m controlling the switch for the TV system. The call back function will handle if the switch should be turned off or on. This means, just a if loop with toggling the pin 12 where the relay is connected and storing the current action in the global variable.

Securing MQTT:

coming soon



Write a Comment



This site uses Akismet to reduce spam. Learn how your comment data is processed.