Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revisionBoth sides next revision | ||
sonoff [22.01.2018 23:24] – [Future projects] Pascal Suter | sonoff [08.12.2018 09:50] – [MQTT interface for digitalSTROM] 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 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 " | ||
+ | |||
+ | 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 '' | ||
+ | |||
+ | 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 '' | ||
+ | |||
+ | 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:// | this is straight forward, just follow the guides on the [[https:// | ||
Line 30: | Line 42: | ||
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 the latest | + | - [[setup arduino IDE for ESP8266]] |
- | - create a subdirectory called '' | + | |
- | - start your arduino IDE and go to '' | + | |
- | - select OK and go to '' | + | |
- 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 118: | Line 112: | ||
npm install --save mqtt-dss-bridge | npm install --save mqtt-dss-bridge | ||
this will create a sub-directory '' | this will create a sub-directory '' | ||
- | * '' | + | * '' |
* '' | * '' | ||
* '' | * '' | ||
Line 132: | Line 126: | ||
this helps in finding your way around the massive amount of inormation that is published by the dss bridge. | this helps in finding your way around the massive amount of inormation that is published by the dss bridge. | ||
- | Since my goal for now is to turn on my sonoff when a certain scene is selected in a room i am most interested in the topic '' | + | Since my goal for now is to turn on my sonoff when a certain scene is selected in a room i am most interested in the topic '' |
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/ | ||
+ | |||
+ | ==== retention and QoS ==== | ||
+ | the mqtt-dss-bridge is a bit of a special service regarding retention and QoS since the state of our DigitalStrom environment is not lost when the service is restarted.. so DigitalSTROM has basically its built-in retention. when the mqtt-dss-bridge service is started it publishes the sate for all properties of our DigitalSTROM environment to the mqtt server. this includes things that where published before. By Default the mqtt-dss-bridge uses a QoS of 1 which means, every message will be sent out at least once. In my case i have created a rule that turns all the lights in the entire hous off as soon as one room enters the deep off state.. this allows me to press and hold the light switch for 3 seconds in the bedroom and the entire house will turn dark. unfortunately though, due to this qos setting of 1 the message will be re-sent as soon as the mqtt-dss-service is restarted. so as soon as at least one room in my house is in the deep-off state all lights will go out when i restart the service. to avoid this, i set the QoS to 0, so the mqtt server will ignore the re-published message upon restart of the mqtt server. To set the qos you need to edit the file in ''/ | ||
+ | |||
+ | a similar issue araises if you use for example node-red to create the above mentioned rule and set the retention flag to true or leave it unset which is true by default in node-red. the lights-off command will be kept on the mqtt server and it will be sent to the digitalstrom bus each time the dss bridge is reloaded. this is of course correct but not wanted in our case. so remember to set that flag to false in node-red for your dss nodes if you don't want to redo the last command on that node each time the mqtt-dss-bridge is restarted. | ||
===== 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:// | + | [[node-red]] looks like a very capable and interesting Rules engine. I have also spotted another interesting open source project called [[https:// |
installation is again very easy through npm: | installation is again very easy through npm: | ||
Line 150: | Line 149: | ||
it tells you the server is running on '' | it tells you the server is running on '' | ||
+ | while programming without writing anything might be easy, it certainly makes it harder to describe what comes next: | ||
+ | - create a new flow and start with an mqtt input. first create a new mqtt server and enter your server address and credentials in there. then enter the input-topic (in MQTT terms, tell node-red what to subscribe to). in our case that is '' | ||
+ | - create a '' | ||
+ | - from the json node link to a '' | ||
+ | - now from the first condition link to a new '' | ||
+ | - from both these change nodes link to the same '' | ||
+ | - now hit " | ||
+ | your sonoff should now turn on when scene1 is selected in the " | ||
+ | here is the exported flow in case you want to import that into your Node-RED editor to make sure you have the same settings: < | ||
+ | [{" | ||
+ | </ | ||
+ | and here is what it looks like: | ||
+ | {{ : | ||
+ | ===== 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. | ||
+ | |||
+ | before you start, make sure you export your '' | ||
+ | |||
+ | the easy one first: Mosquitto. Since we used the ubuntu package, it already came with the necessary startup scripts, so a simple | ||
+ | systemctl enable mosquitto.service | ||
+ | is enough :) | ||
+ | |||
+ | now lets install our npm modules ('' | ||
+ | npm install --global --save mqtt-dss-bridge | ||
+ | npm install --global --save --unsafe-perm node-red | ||
+ | |||
+ | create the users and groups to run these tools: | ||
+ | addgroup --system mqtt-dss-bridge | ||
+ | addgroup --system node-red | ||
+ | adduser --system --no-create-home --ingroup mqtt-dss-bridge mqtt-dss-bridge | ||
+ | adduser --system --ingroup node-red node-red | ||
+ | |||
+ | copy the '' | ||
+ | |||
+ | create the startup service file for '' | ||
+ | < | ||
+ | cat > / | ||
+ | [Unit] | ||
+ | Description=Node.js MQTT DSS Bridge | ||
+ | Requires=mosquitto.service | ||
+ | After=mosquitto.service | ||
+ | |||
+ | [Service] | ||
+ | ExecStart=/ | ||
+ | # Required on some systems | ||
+ | # | ||
+ | Restart=always | ||
+ | # Restart service after 10 seconds if node service crashes | ||
+ | RestartSec=10 | ||
+ | # Output to syslog | ||
+ | StandardOutput=syslog | ||
+ | StandardError=syslog | ||
+ | SyslogIdentifier=mqtt-dss-bridge | ||
+ | User=mqtt-dss-bridge | ||
+ | Group=mqtt-dss-bridge | ||
+ | Environment=NODE_ENV=production | ||
+ | |||
+ | [Install] | ||
+ | WantedBy=multi-user.target | ||
+ | EOF | ||
+ | </ | ||
+ | |||
+ | create systemd service for '' | ||
+ | cat > / | ||
+ | [Unit] | ||
+ | Description=Node.js Node-RED | ||
+ | Requires=node-red.service | ||
+ | After=node-red.service | ||
+ | |||
+ | [Service] | ||
+ | ExecStart=/ | ||
+ | # Required on some systems | ||
+ | WorkingDirectory=/ | ||
+ | Restart=always | ||
+ | # Restart service after 10 seconds if node service crashes | ||
+ | RestartSec=10 | ||
+ | # Output to syslog | ||
+ | StandardOutput=syslog | ||
+ | StandardError=syslog | ||
+ | SyslogIdentifier=node-red | ||
+ | User=node-red | ||
+ | Group=node-red | ||
+ | Environment=NODE_ENV=production PORT=1880 | ||
+ | |||
+ | [Install] | ||
+ | WantedBy=multi-user.target | ||
+ | EOF | ||
+ | </ | ||
+ | |||
+ | create a rule for '' | ||
+ | < | ||
+ | cat > / | ||
+ | if \$programname == ' | ||
+ | if \$programname == ' | ||
+ | EOF | ||
+ | </ | ||
+ | create a rule for '' | ||
+ | < | ||
+ | cat > / | ||
+ | if \$programname == ' | ||
+ | if \$programname == ' | ||
+ | EOF | ||
+ | </ | ||
+ | |||
+ | restart '' | ||
+ | systemctl restart rsyslog.service | ||
+ | systemctl daemon-reload | ||
+ | systemctl start mqtt-dss-bridge.service | ||
+ | systemctl start node-red.service | ||
+ | check if the services are all running fine. if that is the case, enable them to autostart on boot: | ||
+ | systemctl enable mqtt-dss-bridge.service | ||
+ | systemctl enable node-red | ||
+ | |||
+ | now restore your node-red settings | ||
+ | |||
+ | ===== Next Steps ===== | ||
+ | try to get '' | ||
===== 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]] |