B4J Tutorial Controlling Sonoff S20 smart power plugs [B4X] [Home Automation]

Hi gang,

Here's how I got the Sonoff S20 to work with B4X. As the standard firmware doesn't come with an API and I wasn't successful trying to translate pySONOFF to B4X, I went down another path which was more than worth the effort of looking things up and ordering a bit of additional low-cost hardware, and flashed the well-known open source Tasmota firmware onto the plugs. Everything was done on Windows 10.

Additional required hardware
1. A USB-to-serial module. I had ordered several of those as the first one didn't do the job; this one worked flawlessly: www.amazon.de/-/en/gp/product/B00GNPJ1CE/ref=ppx_yo_dt_b_asin_title_o03_s00
2. Breakaway board pin connectors, which make it a lot easier to ensure a steady connection between that module and the S20's board, as the board only contains tiny soldering holes as opposed to pins. I purchased this pack (which admittedly is overkill as you only need the ones that are male on both sides, and you really only need a 4-pin piece): www.amazon.de/-/en/gp/product/B078SQ1CZF/ref=ppx_yo_dt_b_asin_title_o04_s00

Useful reads
1. You may want to search YouTube for 'Sonoff S20 Tasmota' to get an idea of what's about to follow.
2. https://tasmota.github.io/docs/devices/Sonoff-S20/
3. https://tasmota.github.io/docs/Getting-Started/#hardware-preparation

Software to download
1. The FTDI driver in order to use the USB-to-serial module with Windows 10. You'll find it at https://www.ftdichip.com/Drivers/VCP.htm ; run the installer with administrator privilege.
2. The Tasmota firmware, i.e. 'tasmota.bin' from http://ota.tasmota.com/tasmota/release.
3. Tasmotizer, which is the program used for flashing, from https://github.com/tasmota/tasmotizer.

1. Make sure you've installed the FTDI driver and downloaded the other software.
2. When connecting the hardware (see the aforementioned 'Useful reads'), make sure that the USB-to-serial module's jumper is set to 3.3V as 5V would fry your Sonoff S20, and that you connect the module's TX to the Sonoff's RX and vice versa.
3. As mentioned several times in the aforementioned documents, do NOT plug the Sonoff into the mains unless you don't mind electrocuting yourself.
4. Connect PC -> USB-to-serial module -> Sonoff S20 while pushing the little red button on the Sonoff's board so that it boots into programming mode.
5. Run Tazmotizer and select the Tasmota firmware you downloaded; make a note of the Sonoff's MAC address if you use MAC filtering on your router. Flashing just takes 1 or 2 minutes.

Initial configuration
1. Connect to the Sonoff via wifi; it will show up with an SSID like 'tasmota_XXXXXX-XXXX'; your browser will open with the configuration page.
2. Enter your SSID (case-sensitive!) for 'AP1 SSId' and its password for 'AP1 Password'.
3. Click 'Save'; the Sonoff will now be restarted, after which you can access it from your LAN.

1. If your router has MAC filtering enabled, add the Sonoff's MAC address to the whitelist.
2. Consider assigning a static IP address (as opposed to letting DHCP assign a dynamic one) to the Sonoff on your router.
3. Optionally, to further configure (or just see which options exist in) Tasmota on the Sonoff, e.g. for MQTT and more, just navigate to its IP address with your browser.
4. Now you can control the Sonoff using these straightforward URLs (replace '' with your IP address); MQTT is possible as well but I didn't play with that:
- Get current state:
- Switch on: On
- Switch off: Off
- Toggle the state: Toggle

All of these URLs return the Sonoff's state as JSON; check the attached example projects to see how to interpret it.

Enjoy !


  • SonoffS20demo_B4A.zip
    10.9 KB · Views: 455
  • SonoffS20demo_B4J.zip
    3.7 KB · Views: 422
  • SonoffS20demoNonUI_B4J.zip
    1.2 KB · Views: 402
Last edited: