{"id":16883068,"url":"https://github.com/tsaarni/mqtt-micropython-smartsocket","last_synced_at":"2025-10-25T18:36:32.056Z","repository":{"id":91277198,"uuid":"127127412","full_name":"tsaarni/mqtt-micropython-smartsocket","owner":"tsaarni","description":"Alternative firmware for Itead S20 WiFi Smart Socket","archived":false,"fork":false,"pushed_at":"2018-03-30T17:22:18.000Z","size":12,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-10-05T21:47:18.929Z","etag":null,"topics":["itead","microcontroller","micropython","mqtt","python","sonoff"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tsaarni.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-03-28T11:01:43.000Z","updated_at":"2022-12-24T08:10:25.000Z","dependencies_parsed_at":null,"dependency_job_id":"22d80d18-cbac-4a20-b0e0-cc83c00ffebf","html_url":"https://github.com/tsaarni/mqtt-micropython-smartsocket","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/tsaarni/mqtt-micropython-smartsocket","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsaarni%2Fmqtt-micropython-smartsocket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsaarni%2Fmqtt-micropython-smartsocket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsaarni%2Fmqtt-micropython-smartsocket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsaarni%2Fmqtt-micropython-smartsocket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tsaarni","download_url":"https://codeload.github.com/tsaarni/mqtt-micropython-smartsocket/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tsaarni%2Fmqtt-micropython-smartsocket/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":281002773,"owners_count":26428008,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-25T02:00:06.499Z","response_time":81,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["itead","microcontroller","micropython","mqtt","python","sonoff"],"created_at":"2024-10-13T16:10:15.461Z","updated_at":"2025-10-25T18:36:32.027Z","avatar_url":"https://github.com/tsaarni.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Control Itead S20 WiFi Smart Socket with MicroPython and MQTT\n\nThis project is a simple MicroPython program for\n[Itead S20 Wifi Smart Socket](https://www.itead.cc/smart-socket.html).\nIt replaces the default firmware.  It allows remote control by using\nMQTT broker.  For example [Node-red](https://nodered.org/) with\nembedded MQTT broker\n[node-red-contrib-mqtt-broker](https://github.com/zuhito/node-red-contrib-mqtt-broker)\ncan be used as a controller to build automation flows for the device.\n\nThe program registers with MQTT broker, subscribes topic and waits for\nmessage with `on` or `off` payload.  When message is received, the state\nof relay switch is set to on or off.\n\nIf button is pressed on S20 Wifi Smart Socket, the program will publish\n`on` or `off` message to the broker.\n\n\n## Pre-requisites\n\nFirst, the default firmware on S20 Wifi Smart Socket needs to be\nreplaced with MicroPython firmware.  In order to flash MicroPython\nyou'll need USB UART with 3.3V option. Ebay sellers have various\noptions. Check that yours also provide 3.3V TTL level and not just\n3.3V output voltage.\n\nDownload MicroPython firmware image for ESP8266 from http://micropython.org/download#esp8266\n\nInstall esptool flasher into a Python virtualenv\n\n    virtualenv esptool-virtualenv\n    . esptool-virtualenv/bin/activate\n    pip install esptool\n\n\n## Locating the Pads for Programming the Microcontroller\n\nTo open S20 Wifi Smart Socket you'll find one screw under the red\nsticker at the back of the device.  After unscrewing it, pry the cover\nopen carefully from the sides.  There is only single clip holding the\ncover at the bottom, so it is easiest to start there.\n\nWhen the cover is open locate the pads marked in the figure as GND,\nTX, RX and 3.3V:\n\n![Imgur](http://i.imgur.com/TpKxdGK.jpg?1)\n\n\n\nTo program the microcontroller you can either solder pin header to the\nPCB or just solder wires directly.  Yet another alternative is to\nbuild a cable with pogo pins to create temporary connection:\n\n![Imgur](http://i.imgur.com/ckwMiwD.jpg?2)\n\n\n## Flashing MicroPython Firmware\n\nConnect USB UART to the 3.3V, GND, TX and RX pins:\n\n![Imgur](http://i.imgur.com/QdrVQXC.jpg)\n\n\nKeep reset button (the button in the middle of the PCB) pushed to\nenter programming mode while connecting USB UART to the device to\nenter the programming mode.  You can release the button after the\ndevice has been powered.  Then execute:\n\n    $ esptool.py --port /dev/ttyUSB0 erase_flash\n\n    esptool.py v2.1\n    Connecting....\n    Detecting chip type... ESP8266\n    Chip is ESP8266\n    Uploading stub...\n    Running stub...\n    Stub running...\n    Erasing flash (this may take a while)...\n    Chip erase completed successfully in 2.4s\n    Hard resetting...\n\n\nDisconnect the programmer and connect it again while again pushing the\nreset button to enter programming mode.  To flash micropython image run:\n\n    $ esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 ~/Downloads/esp8266-20170823-v1.9.2.bin\n\n    esptool.py v2.1\n    Connecting....\n    Detecting chip type... ESP8266\n    Chip is ESP8266\n    Uploading stub...\n    Running stub...\n    Stub running...\n    Changing baud rate to 460800\n    Changed.\n    Configuring flash size...\n    Auto-detected Flash size: 1MB\n    Flash params set to 0x0020\n    Compressed 601136 bytes to 392067...\n    Wrote 601136 bytes (392067 compressed) at 0x00000000 in 8.8 seconds (effective 546.4 kbit/s)...\n    Hash of data verified.\n\n    Leaving...\n    Hard resetting...\n\n\nDisconnect the programmer and connect it again but do NOT push the\nreset putton this time.  Use serial terminal emulator program such as\nminiterm which you should have now installed in esptool-virtualenv\n(part of pyserial which was installed as dependency of esptool).\nAfter launching terminal emulator press enter to get python prompt `\u003e\u003e\u003e`.\n\n    $ miniterm.py --raw /dev/ttyUSB0 115200\n\n    --- Miniterm on /dev/ttyUSB0  115200,8,N,1 ---\n    --- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---\n    #5 ets_task(4020edc0, 29, 3fff9150, 10)\n    OSError: [Errno 2] ENOENT\n\n    MicroPython v1.9.2-8-gbf8f45cf on 2017-08-23; ESP module with ESP8266\n    Type \"help()\" for more information.\n    \u003e\u003e\u003e\n\n\n## Configure Wifi Settings\n\nTo get S20 Wifi Smart Socket to join to your Wifi network at boot you\nmust connect wifi network at least once.  ESP8266 will store the connection\ninformation into EEPROM and use it each time when rebooting to reconnect.\n\nEnter paste mode by pressing CTRL+E in MicroPython prompt and copy \u0026\npaste following code:\n\n    import network\n    sta_if = network.WLAN(network.STA_IF)\n    sta_if.active(True)\n    sta_if.connect('MYSSID', 'MYPASSWORD')\n\n\nChange MYSSID to your wifi network name and MYPASSWORD to your wifi password.\nPrint the IP address your device got from your network by running:\n\n    sta_if.ifconfig()[0]\n\n\n## Set WebREPL Password\n\nWebREPL is a method to use the Python prompt remotely via a web\nsocket.  This is much more convenient way for trials and development\nthan using serial console, especially if you did not solder header\npins to the S20 Wifi Smart Socket to get permanent USB UART\nconnection.\n\nTo set WebREPL password, execute following in Python promopt while still\nbeing connected with serial console:\n\n    \u003e\u003e\u003e import webrepl_setup\n    WebREPL daemon auto-start status: disabled\n\n    Would you like to (E)nable or (D)isable it running on boot?\n    (Empty line to quit)\n    \u003e e\n    To enable WebREPL, you must set password for it\n    New password: NNNNNN\n    Confirm password: NNNNNN\n    Changes will be activated after reboot\n    Would you like to reboot now? (y/n) y\n\n\nThis password will be prompted from clients who connect WebREPL server\nvia network.\n\nTo connect your device over the network you can use WebREPL client in\nweb browser.  You can access it here http://micropython.org/webrepl/.\nIt requires the IP address and port of the WebREPL server running in\nS20 Wifi Smart Socket `ws://YOUR_IP:8266/` where YOUR_IP is the\naddress that the `if_config()` call returned.\n\nAlternative method for the browser based WebREPL client is\n[mpfshell](https://github.com/wendlers/mpfshell).  Install by running:\n\n    pip install mpfshell\n\n\nThen connect to the WebREPL server by running:\n\n    mpfshell ws:YOUR_IP,password\n\n\n## Test Switching the Relay with Python\n\nConnect to the webrepl and write following code to the Python prompt:\n\n    from machine import Pin\n    relay = Pin(12, Pin.OUT)\n    relay.on()  # turn power on\n    relay.off() # turn power off\n\n\nIf the device is only powered by USB UART you will not hear the relay\nclick, but you will see the LED lit.\n\n\n\n## Installing the Program\n\nFirst modify the configuration in `config.json` with your MQTT broker\naddress, username, password and topic.  Next upload the program and the\nconfig file:\n\n    mpfshell \"ws:YOUR_IP,password\"\n    put main.py\n    put config.json\n\n\nAfter rebooting the device, the program will establish connection to\nMQTT broker.  The connection between device and broker is not secured\nby TLS and the intention is to use the program with broker in trusted\nnetwork only.\n\n\n## References\n\n[1] S20 Wifi Smart Socket Schematic https://www.itead.cc/wiki/S20_Smart_Socket\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftsaarni%2Fmqtt-micropython-smartsocket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftsaarni%2Fmqtt-micropython-smartsocket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftsaarni%2Fmqtt-micropython-smartsocket/lists"}