Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
sonoff [25.01.2018 05:39] – [Rules Engine - node-red] Pascal Suter | sonoff [07.09.2021 05:52] (current) – [Making the installation permanent] Pascal Suter | ||
---|---|---|---|
Line 11: | Line 11: | ||
===== alternative firmware ===== | ===== alternative firmware ===== | ||
* [[https:// | * [[https:// | ||
- | * works also on some other ESP based devices | + | * works also on lots of ESP based devices |
* 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 39: | 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, | + | <del>I strongly recommend to edit the settings before compiling and adding your wifi credentials, |
Here are the steps it took to flash the stock sonoff (current and more detailed instructions can be found in the [[https:// | Here are the steps it took to flash the stock sonoff (current and more detailed instructions can be found in the [[https:// | ||
- | - download | + | |
- | - create a subdirectory called '' | + | **Update**: again, |
- | | + | |
- | | + | here are the steps using Tasmotizer: |
+ | | ||
+ | pip3 install | ||
+ | pip3 install tasmotizer | ||
+ | tasmotizer.py</ | ||
+ | | ||
+ | * 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 devices. VCC is 3.3V NOT 5V! {{: | ||
+ | | ||
+ | * hold down the push button and plug-in the usb adapter. | ||
+ | * let go of the push button and click " | ||
+ | * click " | ||
+ | * 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, | ||
+ | |||
+ | - [[setup arduino IDE for ESP8266]] | ||
- download the sourcecode tar.gz or zip package from the [[https:// | - download the sourcecode tar.gz or zip package from the [[https:// | ||
- extract the contents of the '' | - extract the contents of the '' | ||
- | - verify your settings under Tools are like these: < | ||
- | Board: " | ||
- | Flash Mode: " | ||
- | Flash Size: "1M (no SPIFFS)" | ||
- | Debug Port: " | ||
- | Debug Level: " | ||
- | LwIP Variant: "v2 Prebuilt (MSS=536)" | ||
- | Reset Method: " | ||
- | Crystal Frequency: "26 MHz" | ||
- | Flash Frequency: " | ||
- | Upload Using: " | ||
- | CPU Frequency: "80 MHz" | ||
- | Upload Speed: " | ||
- | Port: Your COM port connected to sonoff | ||
- | </ | ||
- edit '' | - edit '' | ||
- **make sure your sonoff is disconnected from the AC power lines*** | - **make sure your sonoff is disconnected from the AC power lines*** | ||
Line 148: | Line 152: | ||
In order to turn the lights on in a room, you can publish to the topic '' | In order to turn the lights on in a room, you can publish to the topic '' | ||
mosquitto_pub -h mqtt.psuter.ch -u sonoffs -P sonoff -t set/ | mosquitto_pub -h mqtt.psuter.ch -u sonoffs -P sonoff -t set/ | ||
+ | |||
===== Rules Engine - node-red ===== | ===== Rules Engine - node-red ===== | ||
Line 177: | Line 182: | ||
and here is what it looks like: | and here is what it looks like: | ||
{{ : | {{ : | ||
+ | ==== 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: | ||
+ | |||
===== 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 197: | Line 205: | ||
copy the '' | copy the '' | ||
+ | |||
+ | 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 ''/ | ||
+ | <code bash / | ||
+ | #!/bin/bash | ||
+ | |||
+ | # start the bridge in the background | ||
+ | / | ||
+ | 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/ | ||
+ | sed -E -e ' | ||
+ | )" +%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 | ||
+ | </ | ||
create the startup service file for '' | create the startup service file for '' | ||
Line 203: | 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=After=mosquitto.service | ||
[Service] | [Service] | ||
- | ExecStart=/usr/bin/node /usr/local/lib/ | + | # |
+ | ExecStart=/usr/local/bin/ | ||
# Required on some systems | # Required on some systems | ||
# | # | ||
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 229: | Line 277: | ||
[Unit] | [Unit] | ||
Description=Node.js Node-RED | Description=Node.js Node-RED | ||
- | Requires=After=node-red.service | + | Requires=node-red.service |
+ | After=node-red.service | ||
[Service] | [Service] | ||
Line 281: | Line 330: | ||
===== Future projects ===== | ===== Future projects ===== | ||
eventually I'd like to write a script that uses the [[https:// | eventually I'd like to write a script that uses the [[https:// | ||
+ | |||
+ | see my progress on that at [[MQTT for digitalSTROM]] |