Micropython installation on Sonoff Wifi Smart Switch
So I finally got my Sonoff wifi enabled smart switches from China this week and I'm ready to start building my home automation system. As I'm also learning Python at the moment I thought it would be a great idea to run Micropython on the Sonoff's to integrate in to the system. As the Sonoff's are basically an ESP8266 with a relay they can easily be updated to use the official Micropython firmware available from the Micropython website.
- Sonoff Wifi Enabled Switch with at least 1MB Flash
- USB to serial adapter (make sure it 3.3V )
- Micropython Firmware (stable 1MB version) available from HERE
- Pyhton/installed installed your Windows/Linux device
Open the Sonoff and identify the 5 pin header located on the main board (see below). Pin 1 is at the bottom as per this image nearest the button.
- 1 - VCC
- 2 - RX
- 3 - TX
- 4 - GND
- 5 - NC
Rather than solder header pins to the board I just used some header pins connected to the USB to serial and held them in place with some bluetack while I updated the firmware.
On your desktop install the ESPtool Python script to update the firmware.
pip install esptool
Connect your USB to serial converter to the header port and before you plug in make sure you hold down the Button on the Sonoff to put the unit into update mode.
Next we need to erase the existing flash on the Sonoff, change the COM port to whatever your local one is.
esptool.py --port COM5 erase_flash
Finally go to the folder where you downloaded the Micropython firmware are run the following for flash the device.
esptool.py --port COM5 --baud 460800 write_flash --flash_size=detect 0 esp8266-20170108-v1.8.7.bin
Make sure the COM port is changed to match your device and the filename matches you firmware download. If you have issue flashing try changing the baud rate down to 115200.
Testing the install.
Once the unit is flashed reboot it. You should be able to use PUTTY (or similar) to connect to the COM port defined and you will get the Micropython prompt.
To test everything is working we'll try and blink the on-board LED, which is connected to GPIO 13.
pin = machine.Pin(13) pin = machine.Pin(13, machine.Pin.OUT)
pin.toggle() to toggle the LED on and off.