sonoff

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
sonoff [23.01.2018 21:50] – [Making the installation permanent] Pascal Sutersonoff [07.09.2021 05:52] (current) – [Making the installation permanent] Pascal Suter
Line 11: Line 11:
 ===== alternative firmware ===== ===== alternative firmware =====
   * [[https://github.com/arendst/Sonoff-Tasmota|Tasmota]]   * [[https://github.com/arendst/Sonoff-Tasmota|Tasmota]]
-    * works also on some other ESP based devices+    * works also on lots of ESP based devices and can be easily adopted to support even more. for example [[MagicHome|MagicHome RGB LED Controllers]]
     * provides control via serial / web / MQTT     * provides control via serial / web / MQTT
 ===== What I intend to do with it ===== ===== What I intend to do with it =====
Line 22: Line 22:
 at first i will use the existing MQTT bridge for DS which uses the Web-API. In the future i would like to write a little module that uses the Virtual Device API of digitalSTROM which would allow me to emualte a digitalSTROM device for my sonoff. this would make the integration even smoother and there would be no need for an external rules engine anymore. However, i'll get started with the web-api and rules engine first, as I'd like to try out some MQTT stuff first :)  at first i will use the existing MQTT bridge for DS which uses the Web-API. In the future i would like to write a little module that uses the Virtual Device API of digitalSTROM which would allow me to emualte a digitalSTROM device for my sonoff. this would make the integration even smoother and there would be no need for an external rules engine anymore. However, i'll get started with the web-api and rules engine first, as I'd like to try out some MQTT stuff first :) 
  
 +===== The simple solution =====
 +Isn't there a simple solution to that? Shure there is, but what fun would it be? :) 
 +
 +the digitalSTROM server comes with some "apps" that can be installed on it via a repository. one of the standard apps that is even pre-installed is called ''Event-Responder'' and does all we need in order to get our sonoff to turn on and off depending on scenes selected in rooms. Because it is so easy to do, I will quickly hightlight how to do all that before I explain the more complex solution: 
 +
 +You can either flash the Tasmona firmware as described below or hack your sonoff in any other way with any other firmware or even with the original firmware in place. All you need to achieve is, that you can send a http-get request to the sonoff to turn the light on and off. Tasmona does that, and since I am going to use it anyway for my project, I will assume you also flashed that. Also this solution requires the sonoff to have a static IP .. either do this with a reservation in your DHCP server or enter a fixed IP in the web-interface of Tasmona. 
 +
 +In the dss web menu go to apps and open the ''Event-Responder'' app. create a new event and select the room and scene you want to use as trigger for your sonoff to turn on. as initiated activity select "Call URL" and enter ''%%http://<sonoff-ip>/cm?cmnd=Power%20On%%''. then give it some useful name and save.
 +
 +Create a second Responder, but this time select all other scenes in the same room as trigger (you can do that by holding CTRL on your keyboard while clicking on all the scenes you do NOT want your light to be on). then as action use again Call URL but this time enter ''%%http://<sonoff-ip>/cm?cmnd=Power%20Off%%''
 +
 +that's it, you're all set. Congratulations you just saved yourself a day worth of fiddling with tools and scripts :) So why am I still going down the complicated route.. well, mainly **because I can** :) and also because i want to use more devices including some input devices in the future to do more complicated stuff which exceeds the capabilities of my DSS. Also, the Event-Responder is quite slow, it is even slower than my first solution with the DSS-API based mqtt-connector. So expect to wait 2-3 seconds for your light to go on. Unless you have a newer DSS than me. The newer models (DSS20 and 22) have faster cpu's and should be able to process these responders alot quicker. 
 ===== Falshing Tasmota Firmware ===== ===== Falshing Tasmota Firmware =====
 this is straight forward, just follow the guides on the [[https://github.com/arendst/Sonoff-Tasmota|Tasmota github]] site. there is a wiki with loads of information.  this is straight forward, just follow the guides on the [[https://github.com/arendst/Sonoff-Tasmota|Tasmota github]] site. there is a wiki with loads of information. 
Line 27: Line 39:
 I personally went for flashing over a USB TTL adapter using the Arduino IDE. It is important to notice, that the TTL adapter needs to provide 3.3V, not 5V! Also i recommend using a PL2303 based USB TTL adapter, as others did not work for me (spent hours trying until I finally gave up and tried with a PL2303 based one I had laying around). Another important Point was, how to enter the programming mode: Hold down the pushbutton while connecting only the power pins. Leave at least one of the two data pins disconnected. Then let go of the button and connect the RX and TX (crossed). Flasing did not work for me when i connected all wires at the same time while holding down the button.  I personally went for flashing over a USB TTL adapter using the Arduino IDE. It is important to notice, that the TTL adapter needs to provide 3.3V, not 5V! Also i recommend using a PL2303 based USB TTL adapter, as others did not work for me (spent hours trying until I finally gave up and tried with a PL2303 based one I had laying around). Another important Point was, how to enter the programming mode: Hold down the pushbutton while connecting only the power pins. Leave at least one of the two data pins disconnected. Then let go of the button and connect the RX and TX (crossed). Flasing did not work for me when i connected all wires at the same time while holding down the button. 
  
-I strongly recommend to edit the settings before compiling and adding your wifi credentials, this will save some time and hassle when configuring the device later on. It will simply connect to your wifi and you can access its webpage to do the rest of the configuration. +<del>I strongly recommend to edit the settings before compiling and adding your wifi credentials, this will save some time and hassle when configuring the device later on. It will simply connect to your wifi and you can access its webpage to do the rest of the configuration. </del> **Update**: don't do this! whenever possible, flash the stock ''tasmota.bin'' file, it is alot easier to do so and your sonoff will fall back to a setup mode if the wifi password has changed and it can no longer login.. also with new tools such as Tasmotizer, the credentials can be configured after flashing over the serial connection which makes it much easier than hardcoding and compiling your settings.  
  
 Here are the steps it took to flash the stock sonoff (current and more detailed instructions can be found in the [[https://github.com/arendst/Sonoff-Tasmota/wiki/Arduino-IDE|Tasmota wiki]] Here are the steps it took to flash the stock sonoff (current and more detailed instructions can be found in the [[https://github.com/arendst/Sonoff-Tasmota/wiki/Arduino-IDE|Tasmota wiki]]
-  - download the latest [[https://www.arduino.cc/en/Main/Software|Arduino IDE]] and unpack it.  + 
-  - create a subdirectory called ''portable'' +**Update**: again, the instructions below are obsolete and may only be valid if you want to re-compile tasmota.. but you rarely will have to do that.. Instead i recommend using [[https://github.com/tasmota/tasmotizer|Tasmotizer]], a very simple python gui which can be installed using pip3 on Windows, linux and mac (for widows there is even an exe installer). besides flashing your tasmota compatible device it can also set your wifi settings and subsequently show the ip of the device as long as it is still attached to the serial interface 
-  - start your arduino IDE and go to ''File->preferences'' and add this to //Additional Boards Manger URLs//:  ''http://arduino.esp8266.com/stable/package_esp8266com_index.json'' + 
-  - select OK and go to ''Tools-Boards...-Boards Manager...'' and enter ''ESP'' into the search. now install the ''esp8266'' module and close that window+here are the steps using Tasmotizer:  
 +  * install tasmotizer: <code> 
 +pip3 install --upgrade pip wheel 
 +pip3 install tasmotizer 
 +tasmotizer.py</code> 
 +  * under "Select Image" choose "release" and then select whatever image you want to flash, in most cases this will be ''tasmota.bin''  
 +  * connect your sonoff to the serial2usb adapter WITH MAINS DISCONNECTED!! here is the necessary wiring.. note that rx and tx are crossed over between the two devicesVCC is 3.3V NOT 5V! {{:sonoff:pasted_20201130-140133.png}} 
 +  * here's the pinout for a shelly1 .. with the shelly1 you need to connect the gpio1 pin to ground whil you power it on in order to get it into flash mode.. {{:sonoff:pasted_20201204-194342.png}} 
 +  * hold down the push button and plug-in the usb adapter 
 +  * let go of the push button and click "refresh" next to the "Select Port" drop downmost likely this will see and select your usb adapter  
 +  * click "Tasmotize" and wait for it to erase and reflash your device.  
 +  * once that is done, unplug your usb adapter and plug it back in, to reset the sonof  
 +  * now click on send config and enter your desired config, then upload it 
 +  * a few seconds later, your sonoff sould be connected to your network and with Get IP you can now see the ip it got from your DHCP.  
 + 
 + 
 +If you really want to compile tasmota yourself, you should read the official documentation, as what follows now is already several years old and probably no longer exactly truehowever, I wrote it, so here it is anyway :) ..  
 + 
 +  - [[setup arduino IDE for ESP8266]]
   - download the sourcecode tar.gz or zip package from the [[https://github.com/arendst/Sonoff-Tasmota/releases|Tasmota releases page]]   - download the sourcecode tar.gz or zip package from the [[https://github.com/arendst/Sonoff-Tasmota/releases|Tasmota releases page]]
   - extract the contents of the ''lib'' directory into your arduino ide's ''portable/sketchbook/libraries'' directory and extract the entire ''sonoff'' directory into your arduin IDE's ''portable/sketchbook/'' directory   - extract the contents of the ''lib'' directory into your arduino ide's ''portable/sketchbook/libraries'' directory and extract the entire ''sonoff'' directory into your arduin IDE's ''portable/sketchbook/'' directory
-  - verify your settings under Tools are like these: <code> 
-Board: "Generic ESP8266 Module"  <<<<!!!! 
-Flash Mode: "DOUT"  <<<<!!!! 
-Flash Size: "1M (no SPIFFS)"  <<<<!!!! 
-Debug Port: "Disabled" 
-Debug Level: "None" 
-LwIP Variant: "v2 Prebuilt (MSS=536)" 
-Reset Method: "ck" 
-Crystal Frequency: "26 MHz" 
-Flash Frequency: "40MHz" 
-Upload Using: "Serial" 
-CPU Frequency: "80 MHz" 
-Upload Speed: "115200" 
-Port: Your COM port connected to sonoff 
-</code> 
   - edit ''user_config.h'' and enter your wifi name and password. all other settings can be changed later once the device is booted, but you can of course set them here if you want. this will make it much faster if you flash alot of these devices and want the same settings on all of them.    - edit ''user_config.h'' and enter your wifi name and password. all other settings can be changed later once the device is booted, but you can of course set them here if you want. this will make it much faster if you flash alot of these devices and want the same settings on all of them. 
   - **make sure your sonoff is disconnected from the AC power lines***   - **make sure your sonoff is disconnected from the AC power lines***
Line 136: Line 152:
 In order to turn the lights on in a room, you can publish to the topic ''set/dss/apartment/zones/Garden/groups/1/lastCalledScene'' and set the value to ''5'' or ''0''. here is an example using ''mosquitto_pub'':  In order to turn the lights on in a room, you can publish to the topic ''set/dss/apartment/zones/Garden/groups/1/lastCalledScene'' and set the value to ''5'' or ''0''. here is an example using ''mosquitto_pub'': 
   mosquitto_pub -h mqtt.psuter.ch -u sonoffs -P sonoff -t set/dss/apartment/zones/Garden/groups/1/lastCalledScene -m 5   mosquitto_pub -h mqtt.psuter.ch -u sonoffs -P sonoff -t set/dss/apartment/zones/Garden/groups/1/lastCalledScene -m 5
 +
  
 ===== Rules Engine - node-red ===== ===== Rules Engine - node-red =====
 Now that we got both our devices connected to the MQTT Broaker it is time to set up a Rules engine, to conenct the two together :)  Now that we got both our devices connected to the MQTT Broaker it is time to set up a Rules engine, to conenct the two together :) 
  
-[[https://nodered.org|Node-Red]] looks like a very capable and interesting Rules engine. I have also spotted another interesting open source project called [[https://thingsboard.io/]] which seems to be interesting for monitoring stuff. not in my focus right now, but worth a mention in order to investigate it later some day :) +[[node-red]] looks like a very capable and interesting Rules engine. I have also spotted another interesting open source project called [[https://thingsboard.io/]] which seems to be interesting for monitoring stuff. not in my focus right now, but worth a mention in order to investigate it later some day :) 
  
 installation is again very easy through npm:  installation is again very easy through npm: 
Line 165: Line 182:
 and here is what it looks like:  and here is what it looks like: 
 {{ :node-red:flow.png |}} {{ :node-red:flow.png |}}
 +==== a word about retention ====
 +it seems node-red sets the retention flag of mqtt by default. I learned this when i used it together with the mqtt-dss-connector: i created a rule that turned the light in the bathroom on. if i turned it off later (not via node-red) and then restarted the mqtt-dss-bridge the light in my bathroom went on again "for no reason" .. this was because that on command was published with the retention flag set to on (or actually not set to anything in node-red). so i set it to "false" and the problem was resolved. 
 +
 ===== Making the installation permanent ===== ===== Making the installation permanent =====
 this might be different in your setup, but I would like to install the npm modules globally and then run them as non-root users through a systemd script to make sure they are auto-startet as soon as my system boots. this might be different in your setup, but I would like to install the npm modules globally and then run them as non-root users through a systemd script to make sure they are auto-startet as soon as my system boots.
Line 185: Line 205:
  
 copy the ''mqtt-dss-bridge'' ''config.js'' file to ''/usr/local/lib/node_modules/mqtt-dss-bridge/config.js''  copy the ''mqtt-dss-bridge'' ''config.js'' file to ''/usr/local/lib/node_modules/mqtt-dss-bridge/config.js'' 
 +
 +It happened a couple of times, that the  mqtt-dss-bridge process was running without actually updating the state of the dss target. this happened after some network issues for example. since the mqtt-dss-bridge process did not exit but rather continued to run and just do nothing, systemd did not notice any problem and did not restart the service. for that reason I created a little wrapper script which checks the age of the last status update and exits with exit code 1 if that state is older than 10 seconds for longer than 10 seconds (it usually takes a couple of seconds upon starting the gateway to catch up, hence we wait 10 addiitonal seconds before killing mqtt-dss-bridge and exiting with an error state) 
 +
 +This wrapper also mutes the output of mqtt-dss-bridge which otherwise floods your syslog. 
 +
 +save this script to a convenient location such as ''/usr/local/bin/mqtt-dss-bridge.sh'' 
 +<code bash /usr/local/bin/mqtt-dss-bridge.sh>
 +#!/bin/bash
 +
 +# start the bridge in the background 
 +/usr/bin/node /usr/local/lib/node_modules/mqtt-dss-bridge/index.js > /dev/null 2>&1 &
 +PID=$!
 +echo "DSS Bridge started with PID $PID"
 +# start a loop to monitor the status every second
 +errcntr=0
 +lastUpdate=999
 +while true; do 
 +    lastUpdate=$(( \
 +            $(date +%s)\
 +            -\
 +            $(date -d "$(\
 +                mosquitto_sub -h localhost -u sonoffs -P sonoff -t dss/state/lastDiscovered -C 1 | \
 +                sed -E -e 's/"([0-9]{4}-[0-9]{2}-[0-9]{2})T([0-9]{2}:[0-9]{2}:[0-9]{2}).*$/\1 \2Z/'\
 +            )" +%s)\
 +        ))
 +    if [ 10 -lt $lastUpdate ]; then 
 +            if [ $errcntr -lt 10 ]; then
 +                let errcntr++
 +                echo "DSS Bridge state is too old (last updated $lastUpdate seconds ago, max 10 allowed). This was failure $errcntr out of 10 accepted failures" 
 +            else 
 +                echo "DSS bridge state is still too old ($lastUpdate seconds) after 10 retries. killing mqtt-dss-bridge (pid $PID) and exit with error status 1"
 +                kill $PID
 +                exit 1
 +            fi
 + fi
 +    sleep 1
 +done
 +</code>
  
 create the startup service file for ''mqtt-dss-bridge'':  create the startup service file for ''mqtt-dss-bridge'': 
Line 191: Line 249:
 [Unit] [Unit]
 Description=Node.js MQTT DSS Bridge Description=Node.js MQTT DSS Bridge
-Requires=After=mosquitto.service       # Requires the mosquitto service to run first+# Requires the mosquitto service to run first 
 +Requires=After=mosquitto.service
  
 [Service] [Service]
-ExecStart=/usr/bin/node /usr/local/lib/node_modules/mqtt-dss-bridge/index.js+#ExecStartPre=/bin/sleep 60 
 +ExecStart=/usr/local/bin/mqtt-dss-bridge.sh
 # Required on some systems # Required on some systems
 #WorkingDirectory=/usr/local/lib/node_modules/mqtt-dss-bridge/ #WorkingDirectory=/usr/local/lib/node_modules/mqtt-dss-bridge/
 Restart=always Restart=always
- # Restart service after 10 seconds if node service crashes+ # Restart service after 10 seconds if wrapper exits with code > 0
 RestartSec=10 RestartSec=10
  # Output to syslog  # Output to syslog
Line 217: Line 277:
 [Unit] [Unit]
 Description=Node.js Node-RED Description=Node.js Node-RED
-Requires=After=node-red.service       # Requires the mosquitto service to run first+Requires=node-red.service       # starts node-red first when this service is started manually 
 +After=node-red.service       # starts this after node-red on boot
  
 [Service] [Service]
Line 264: Line 325:
  
 now restore your node-red settings now restore your node-red settings
 +
 +===== Next Steps =====
 +try to get ''mqtt-dss-bridge'' to use some sort of authentication. for encryption is not that important as the sonoff's probably won't have enough power to use encryption, and that's where it's needed most. (see [[https://github.com/arendst/Sonoff-Tasmota/issues/146|#146]])
 ===== Future projects ===== ===== Future projects =====
 eventually I'd like to write a script that uses the [[https://github.com/plan44/vdcd|plan44 vdcd]] external device API to integrate a sonoff device into a digitalSTROM setup as an emulated digitalstorm device. this will overcome the lag that is generated by querying the Web-API. we could then also create a dummy light bulb which will just publish its state to MQTT. by assigning it a different value for all of the scenes it is in, we can pass the room-state to MQTT with almost no lag. This will enable other devices like the sonoff to react immediately and not just during the next poll-interval.  eventually I'd like to write a script that uses the [[https://github.com/plan44/vdcd|plan44 vdcd]] external device API to integrate a sonoff device into a digitalSTROM setup as an emulated digitalstorm device. this will overcome the lag that is generated by querying the Web-API. we could then also create a dummy light bulb which will just publish its state to MQTT. by assigning it a different value for all of the scenes it is in, we can pass the room-state to MQTT with almost no lag. This will enable other devices like the sonoff to react immediately and not just during the next poll-interval. 
 +
 +see my progress on that at [[MQTT for digitalSTROM]]
  • sonoff.1516740608.txt.gz
  • Last modified: 23.01.2018 21:50
  • by Pascal Suter