Archiv für den Monat: November 2016

Use Sonoff S20 pins for Sensors with espeasy

The Sonoff S20 has only a 4 pin header (GND, TX, RX, VCC).

To use this pins (TX=GPIO1, RX=GPIO3) for sensors, the serial interface has to be disabled:
Tools – Advanced – Enable Serial port: uncheck

Now it is possible to use gpio1 and gpio3 for Devices:
espeasy-dht11

This is the first test on a breadboard with DHT11
sonoff-s20-dht11
I don’t think that a humidity sensor makes any sense inside the socket. Therefore, I think I will use DS12B20 soldered on a female pin header in the final version.

And this is the result:
espeasy-devices

The values are also published via mqtt:

# mosquitto_sub -h 192.168.234.100 -t "#" -v
/sonoff-s20/DHT11/Temperature 24.00
/sonoff-s20/DHT11/Humidity 33.00

Sonoff S20 and espeasy

Flash espeasy

Image can be found here.
To boot the sonoff into flash mode, press and hold the button while connecting it to power.

./esptool.py --port /dev/ttyUSB0 write_flash 0x000 ESPEasy_R120_1024.bin -fs 8m

Connect to wifi and configure

Connect to ESP_0 with password configesp and set up the wifi connection.

After connecting to wifi open the webinterface http://ip.of.sonoff/config and configure the name and unit number. The name of my sonoff is sonoff-s20.

Configure Hardware and Devices.

espeasy-hardware espeasy-button espeasy-relay

Enable rules: Tools – Advanced – Rules: Checked

Now there is a new menu entry named Rules

On Button#State=1 do
  if [Relay#State]=0
    gpio,12,1
  else
    gpio,12,0
  endif
endon

Now you can toggle the relay with the button.

MQTT usage

I use the protocol OpenHAB MQTT. So I can subscribe these events:

# mosquitto_sub -h my.mqtt.server -t "#" -v
/sonoff-s20/Relay/State 1
/sonoff-s20/Relay/State 0

To switch on and off you can publish these messages:

# mosquitto_pub -h 192.168.234.100 -t "/sonoff-s20/gpio/12" -m "1"
# mosquitto_pub -h 192.168.234.100 -t "/sonoff-s20/gpio/12" -m "0"

It is also possible to change „Relay“ to „gpio“ and „State“ to „12“. In this case, the subscribed and published strings are the same, but the rules also have to be modified.

openHAB configuration

After configuration of the mqtt-binding in openhab.cfg I created a new item-file
items/sonoff.items:

Switch sonoff01 "Sonoff Socket" {mqtt=">[mosquitto:/sonoff-s20/gpio/12:command:ON:1],>[mosquitto:/sonoff-s20/gpio/12:command:OFF:0],<[mosquitto:/sonoff-s20/Relay/State:state:MAP(mqtt.map)]", autoupdate="false"}

I also created the file transforms/mqtt.map

0=OFF
1=ON