Skip to content
Heinz Peter Hippenstiel edited this page Apr 8, 2017 · 11 revisions

ESPEasy ITEAD Sonoff S20 Timer-Clock

The Sonoff S20 has GPIO-12 attached to the relay, GPIO-0 attached to the button and GPIO-13 is attached to a green LED which is used as WLAN indicator. It has another blue LED which shows the state of the relay. Keep this in mind and adjust to your needs.

Steps for the setup:

Flash ESPEasy on your device

Go to the ESPEasy Wiki and follow the instructions how to download and flash it to your device. There are also a number of Youtube videos explaining it in detail. If you are brave you could try ESPEasyMega which is in beta.

Configure ESPEasy basics

After flashing the device you should connect it to normal power. After a few seconds the Wifi network ESP_0 appears - password is configesp. Connect to it and then launch your browser to http://192.168.4.1/ - select your home network from the list and enter your Wifi password. After 20 seconds the device should have joined your home network. At least with ESPEasyMega it tells you the IP address it has on your network. In case the IP is not displayed you may have to check your wifi router/access point for a device named newdevice. Others used the Smartphone app Fing to locate it. Rejoin your home network and use our browser to go to the IP of the device. Using a Fritzbox you may use http://newdevice.fritz.box/ or just try http://newdevice/.

Select the Config link and adjust the name to your needs. Best practice: Just use letters (lower case), numbers and dashes - it should adhere RFC 952. Adjust the Unit number if you need that and any of the other settings too. A static IP is useful to not hunt changing IPs.

Select the Tools link and then select Advanced. Setup NTP - some routers can be used as NTP server, for example a recent Fritzbox has this feature. If you don't have NTP in your local network, find a server that is close to you in the NTP.org Server List. The time is UTC so you need to adjust the offset and potentially select DST (Daylight Saving Time). You must also select Rules. Adjust the rest to your needs.

Configure devices

Select the Devices link and add two devices:

  • Device: Switch Input
    Name: Relay
    1st GPIO: GPIO-12 (D6)
    Pull UP: (not selected)
    Enabled: (selected)
    Value Name 1: State
  • Device: Switch Input
    Name: Button
    1st GPIO: GPIO-0 (D3)
    Pull UP: (selected)
    Switch Button Type: Push Button Active Low
    Enabled: (selected)
    Value Name 1: State

For everything not listed use the default. The Enabled flag may be missing in ESPEasy.

Select the Hardware Link and change:

  • Wifi Status Led: GPIO-13 (D7)
  • Pin mode 0 (D3): Input

You could also set a default state for GPIO-13 but using a rule has the advantage that it's set close to the moment when WLAN is really active and not right from the start.

Configure rules

For the rules part a few things have to be considered:

  • Do some setup after boot
  • Switch the relay at defined times on or off
  • Act on a button press

After a device is powered on it must determine if the relay should be on or off depending on the time. Just like a classic timer-clock but with the difference that the ESP based device will have (hopefully) the current time unlike mechanical timer-clocks. This is done after a few seconds so that NTP was able to get the current time. Another thing (at least for the S20) is to set the WLAN LED active.

To switch the relay on and off at specific times is pretty straight forward.

Another part is to act on a button press. All that needs to be done is to check the current relay state and toggle it. On the S20 a blue LED indicating the relay state is automaticallly triggered too. Some people use the WLAN LED to display the state of the relay. In that case you need to adjust the rule.

See rules1.txt for details. See rules1-switch.txt for an example rules file for the Smart Switch that has only one LED.

(Optional) Setup MQTT

Using MQTT to switch the relay on and off remotely requires only little more configuration. Note: Using Domoticz you need to set an additional IDX / Var for controller 1 value for the relay and button.

Depending if you are using ESPEasy or ESPEasyMega you'll find the Controller configuration on the Config link or as separated Controller link. I am using Mosquitto MQTT together with HA-Bridge. Non-integrated MQTT servers should be configured as OpenHAB MQTT which sounds a little misleading but OpenHAB has no integrated MQTT and uses some external one. You need to provide IP or name and user and password. That's it. Depending on your needs you may adjust the pub/sub patterns but the default should be sufficient.

Depending on the MQTT you use you can already send messages to trigger the relay. For example if your device is named sonoff-s20 and you use a Mosquitto MQTT server a mosquitto_pub -h mosquitto-server-name-or-ip -t /sonoff-s20/gpio/12 -m 1 will switch the relay on.

At least with ESPEasyMega you can enable/disable the publishing of device states. I suggest to only publish the relay state.

Independently of MQTT you can always switch the relay using HTTP like so http://sonoff-s20/control?cmd=GPIO,12,1

Clone this wiki locally