This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
app-notes:ein-mqtt-server-fuer-iot-telemetry-sensoren [2023/11/09 14:46] – [1. Concept] gray | app-notes:ein-mqtt-server-fuer-iot-telemetry-sensoren [2024/06/14 09:28] (current) – [Prerequisites] gray | ||
---|---|---|---|
Line 9: | Line 9: | ||
* NetModule Wireless Router with wireless connection | * NetModule Wireless Router with wireless connection | ||
- | * NRSW NRSW [[https:// | + | * NetModule SW [[https:// |
* A license is required for the LXC container | * A license is required for the LXC container | ||
* Not available for legacy products NB1600, NB2700, NB3700, NB3710 | * Not available for legacy products NB1600, NB2700, NB3700, NB3710 | ||
Line 66: | Line 66: | ||
| | ||
/* publish the message to mqtt broker Temperature */ | /* publish the message to mqtt broker Temperature */ | ||
- | ret = nb_mqtt_publish(HOST, | + | ret = nb_mqtt_publish(HOST, |
if (ret<0){ | if (ret<0){ | ||
| | ||
Line 90: | Line 90: | ||
| | ||
/* publish the message to mqtt broker Sys */ | /* publish the message to mqtt broker Sys */ | ||
- | ret = nb_mqtt_publish(HOST, | + | ret = nb_mqtt_publish(HOST, |
if (ret<0){ | if (ret<0){ | ||
| | ||
Line 356: | Line 356: | ||
===== - Bring data from the MQTT broker structured into a database ===== | ===== - Bring data from the MQTT broker structured into a database ===== | ||
- | Create an influx | + | An Influx |
- | + | ||
- | The structure in the database is then created with a Python script, using a Json format. | + | |
- | + | ||
- | <code c > | + | |
- | | + | |
- | { | + | |
- | ' | + | |
- | ' | + | |
- | ' | + | |
- | }, | + | |
- | ' | + | |
- | ' | + | |
- | } | + | |
- | } | + | |
- | ] | + | |
- | influxdb_client.write_points(json_body) | + | |
- | </ | + | |
- | + | ||
---- | ---- | ||
- | |||
- | |||
- | With the help of the Python script, you can specifically query data from the broker and store it in the database in a structured way. | ||
<code c shellyplusht subscribe_datenbank.py> | <code c shellyplusht subscribe_datenbank.py> | ||
Line 516: | Line 495: | ||
---- | ---- | ||
- | The Python | + | A cronjob starts the Python |
- | + | ||
- | < | + | |
- | + | ||
- | # Das Skript wird alle 1 Minuten plus 20 sekunden gestartet | + | |
- | * * * * * sleep 20; / | + | |
- | + | ||
- | </ | + | |
- | + | ||
- | The script checks if the process is still running. If it is, it does nothing. If the process was terminated it is started again. The Python scripts run endlessly. The cronjob only checks if they are still running. | + | |
< | < | ||
Line 560: | Line 530: | ||
From now on you have access to the data that is already in the database and that will be added over time. | From now on you have access to the data that is already in the database and that will be added over time. | ||
- | Which measurements have been deposited in the database: | ||
- | |||
- | < | ||
- | :~ $ influx | ||
- | Connected to http:// | ||
- | InfluxDB shell version: 1.8.10 | ||
- | :> use Shelly | ||
- | Using database Shelly | ||
- | :> show series | ||
- | key | ||
- | --- | ||
- | power, | ||
- | rh, | ||
- | tC, | ||
- | tC, | ||
- | tC, | ||
- | temperatur, | ||
- | temperatur, | ||
- | temperatur, | ||
- | temperatur, | ||
- | value, | ||
- | value, | ||
- | voltage, | ||
- | voltage, | ||
- | </ | ||
==== - The first visualization under Grafana ==== | ==== - The first visualization under Grafana ==== | ||
Line 608: | Line 553: | ||
With this function Grafana Discover I would try more function under the programs. One comes here quickly on solutions that are helpful in the implementation in the dashboard. | With this function Grafana Discover I would try more function under the programs. One comes here quickly on solutions that are helpful in the implementation in the dashboard. | ||
- | |||
- | If one is then with the conversion for a dashboard, one can fall back on information here. | ||
- | |||
- | {{: | ||
- | |||
- | ---- | ||
- | |||
- | ===== - Pass on information from the MQTT network to a free messenger. ===== | ||
- | |||
- | An XMPP server is used as the messenger. Because a standardized open protocol is used. There is an XMPP client for almost everything. | ||
- | |||
- | On the LXC container a XMPP Cleint is used under the command line. With it you can send messages over the XMPP protocol. | ||
- | |||
- | [[https:// | ||
- | |||
- | |||
- | If the command line program is installed on the container you can test it with these commands. It is important that the user of '' | ||
- | |||
- | < | ||
- | echo 'Hello World' | sendxmpp -n -t -u " | ||
- | -p Passwort_vom_Benutzer_Sender Benutzer_Empfänger@xmpp_Server_Adresse | ||
- | </ | ||
- | |||
- | The LXC container runs a TCP server under a Python script. | ||
- | |||
- | <code py tcp_server tcp-server.py> | ||
- | # a simple tcp server | ||
- | import socket,os | ||
- | sock = socket.socket(socket.AF_INET, | ||
- | sock.bind((' | ||
- | sock.listen(5) | ||
- | while True: | ||
- | connection, | ||
- | buf = connection.recv(1024) | ||
- | if (str(buf)) != " | ||
- | # print(" | ||
- | file = open(' | ||
- | temp = str(buf, ' | ||
- | file.write(str(temp)) | ||
- | # print(file.write(str(buf))) | ||
- | file.close() | ||
- | # else: | ||
- | # | ||
- | connection.send(buf) | ||
- | connection.close() | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | This waits for messages generated by SDK script on the router. New messages are then stored in a file " | ||
- | |||
- | About a cronjob every 10 seconds a script is started. This checks whether a new message is available. If so, this message is sent via xmpp. | ||
- | |||
- | < | ||
- | :~# crontab -e | ||
- | |||
- | # Das Skript wird alle 10 Sekunden gestartet | ||
- | * * * * * / | ||
- | * * * * * sleep 10; / | ||
- | * * * * * sleep 20; / | ||
- | * * * * * sleep 30; / | ||
- | * * * * * sleep 40; / | ||
- | * * * * * sleep 50; / | ||
- | </ | ||
- | |||
- | The cronjob script: | ||
- | |||
- | <code sh xmpp-nachricht xmpp_skript.sh> | ||
- | #!/bin/bash | ||
- | if [ -r / | ||
- | then | ||
- | sendxmpp -m / | ||
- | sleep 1s | ||
- | rm / | ||
- | fi | ||
- | </ | ||
---- | ---- | ||