This is firmware for the Sonoff S20 IoT wall switch, to be controlled by CatBus.
Inside the Sonoff S20 is an ESP8266 connected to a button and a relay, and the serial pins are hooked up. It is thus fairly easy to take apart and reflash with something else, in this case MicroPython.
The firmware:
- Uses the front button to toggle the switch on and off.
- Listens to an MQTT topic to turn the switch on and off.
- Publishes the state of the button to an MQTT topic when the button is pushed.
-
Install the latest version of
esptool
from PIP. Don't use the version from APT, it's too out-of-date. -
Erase the existing flash with
$ esptool.py --port /dev/ttyAMA0 erase_flash
, and hard reset the device. -
Write the MicroPython firmware with
$ esptool.py --port /dev/ttyAMA0 --baud 406900 write_flash 0 micropython.bin
, and hard reset the device. -
Connect to it with
$ screen /dev/ttyAMA0 115200
. -
Press
C-b
to bring up the MicroPython prompt, and runimport webrepl_setup
, enabling it at boot. This allows you to update the firmware over WiFi. -
Disable the WiFi Access Point (AP):
import network ap_if = network.WLAN(network.AP_IF) ap_if.active(False)
-
Enable the WiFi client:
import network sta_if = network.WLAN(network.STA_IF) sta_if.active(True) sta_if.scan() sta_if.connect('<essid>', '<password>') sta_if.isconnected() sta_if.ifconfig()
-
Copy
main.py
andcatbus.py
using webrepl_cli:$ python3 webrepl/webrepl_cli.py -p <password> main.py 192.168.16.42:/main.py $ python3 webrepl/webrepl_cli.py -p <password> catbus_config.py 192.168.16.42:/catbus_config.py
-
Run the
catbus_config
setup utility:import catbus_config catbus_config.edit()
-
Reboot the switch by unplugging it, waiting for around 10 seconds for the capacitors to drain, and plugging it back in.
- Tasmota is an all-in-one firmware for such devices.
- MicroPython docs for ESP8266.
To get the device's MAC address, run:
import network
import ubinascii
ubinascii.hexlify(network.WLAN().config('mac'), ':').decode()