Thethingsnetwork und MQTT – Abfrage der Sensordaten mit Node-Red

Im Netz gibt es immer wieder zu lesen, dass das TheThingsNetwork nicht offen sei und schlecht dokumentiert. An einem Beispiel möchte ich zeigen, dass die Dokumentation sehr wohl vorhanden ist und es möglich ist mit entsprechenden Parametern via „mosquitto_sub“, einem Linux Command, die Daten direkt auszulesen. Weiterhin zeige ich wie man diese Daten in Node-Red überführen kann um dann eine weitere Verarbeitung, wie z.B. Werte in einem Dashboard visuell anzuzeigen.

$ mosquitto_sub -h eu.thethings.network -t '+/devices/+/up' -u 'joachimlora' -P 'ttn-account-v2.XXXXXXXXXXXXXXXXX' -v
TTN Console
TTN Console
 Hier sind die Zugangsdaten für die Anmeldung von Benutzername und Passwort zu finden. Diese einfach in den mosquitto_sub Befehl übernehmen.
TTN - MQTT Abfrage
TTN – MQTT Abfrage
Bei erfolgreicher Anmeldung, sind wie oben in der SSH-Konsole angezeigt, folgende Daten eines Lora-Node Sensor zu sehen. Diese können nun an -> Node-Red übertragen werden.
[{"id":"cb7bd77f.ea40c","type":"json","z":"700a4b4c.aa1074","name":"","property":"payload","action":"","pretty":false,"x":470,"y":600,"wires":[["2495d469.badc3c"]]},{"id":"53343cad.c5eb5c","type":"mqtt in","z":"700a4b4c.aa1074","name":"","topic":"+/devices/+/up","qos":"2","broker":"b0bf9659.e02f3","x":290,"y":600,"wires":[["6dd58fc6.57cd48","cb7bd77f.ea40c"]]},{"id":"2495d469.badc3c","type":"debug","z":"700a4b4c.aa1074","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":710,"y":600,"wires":[]},{"id":"6dd58fc6.57cd48","type":"json","z":"700a4b4c.aa1074","name":"","property":"payload","action":"","pretty":false,"x":470,"y":680,"wires":[["3c5fb1a3.96a64e","3931265c.c18c2a","d437e2ab.43d798","ea757df4.7c7fb","3933930a.839d14"]]},{"id":"3c5fb1a3.96a64e","type":"function","z":"700a4b4c.aa1074","name":"Temperature","func":"// outputs: 2\nmsg.payload = msg.payload.payload_fields.temperature_1\nreturn msg;","outputs":1,"noerr":0,"x":650,"y":680,"wires":[["d7dec79b.306ee"]]},{"id":"3931265c.c18c2a","type":"function","z":"700a4b4c.aa1074","name":"Pressure","func":"// outputs: 2\nmsg.payload = msg.payload.payload_fields.barometric_pressure_3\nreturn msg;","outputs":1,"noerr":0,"x":640,"y":720,"wires":[["d7dec79b.306ee"]]},{"id":"d437e2ab.43d798","type":"function","z":"700a4b4c.aa1074","name":"Device ID","func":"// outputs: 2\nmsg.payload = msg.payload.dev_id\nreturn msg;","outputs":1,"noerr":0,"x":640,"y":640,"wires":[["d7dec79b.306ee"]]},{"id":"ea757df4.7c7fb","type":"function","z":"700a4b4c.aa1074","name":"RSSI","func":"// outputs: 2\nmsg.payload = msg.payload.metadata.gateways[0].rssi\nreturn msg;","outputs":1,"noerr":0,"x":630,"y":760,"wires":[["d7dec79b.306ee"]]},{"id":"3933930a.839d14","type":"function","z":"700a4b4c.aa1074","name":"SNR","func":"// outputs: 2\nmsg.payload = msg.payload.metadata.gateways[0].snr\nreturn msg;","outputs":1,"noerr":0,"x":630,"y":800,"wires":[["d7dec79b.306ee"]]},{"id":"d7dec79b.306ee","type":"debug","z":"700a4b4c.aa1074","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":830,"y":680,"wires":[]},{"id":"b0bf9659.e02f3","type":"mqtt-broker","z":"","name":"TTN","broker":"eu.thethings.network","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""}]

Mit diesem Flow , legen wir die Anbindung an das TheThingsNetwork per MQTT zu Node-Red fest und bei erfolgreichem Copy-Paste, sollte der Flow so aussehen wie unten angezeigt.

TTN – MQTT Nodered
 Mit diesem Flow, bauen uns wir ein Dashboard auf, welches die Werte visuell anzeigen lässt.
[{"id":"ecbaa805.de6f88","type":"mqtt in","z":"eb731a6c.fd4f2","name":"Temperature","topic":"ttn/temperature","qos":"2","broker":"c979da80.ef2bb","x":208,"y":185,"wires":[["45e7d0dc.ede19"]]},{"id":"45e7d0dc.ede19","type":"ui_gauge","z":"eb731a6c.fd4f2","name":"","group":"c6fe4cc5.6c0b68","order":2,"width":0,"height":0,"gtype":"gage","title":"Temperature","label":"C","format":"{{value}}","min":"-25","max":"45","colors":["#00b500","#e6e600","#ca3838"],"seg1":"28","seg2":"30","x":428,"y":185,"wires":[]},{"id":"c0980dc4.191188","type":"mqtt in","z":"eb731a6c.fd4f2","name":"Pressure","topic":"ttn/pressure","qos":"2","broker":"c979da80.ef2bb","x":200,"y":240,"wires":[["2cd82899.a8ec2"]]},{"id":"2cd82899.a8ec2","type":"ui_gauge","z":"eb731a6c.fd4f2","name":"","group":"c6fe4cc5.6c0b68","order":3,"width":0,"height":0,"gtype":"gage","title":"Pressure","label":"hPa","format":"{{value}}","min":"900","max":"1000","colors":["#00b500","#e6e600","#ca3838"],"seg1":"970","seg2":"971","x":430,"y":240,"wires":[]},{"id":"31a98b5d.275664","type":"ui_text","z":"eb731a6c.fd4f2","group":"c6fe4cc5.6c0b68","order":1,"width":0,"height":0,"name":"","label":"TTN Lora Sensor Munich 1","format":"{{msg.payload}}","layout":"row-center","x":200,"y":120,"wires":[]},{"id":"b0660f9a.a12258","type":"ui_text","z":"eb731a6c.fd4f2","group":"7b216e54.fde1e","order":1,"width":0,"height":0,"name":"","label":"TTN Lora Sensor Munich 1","format":"{{msg.payload}}","layout":"col-center","x":200,"y":80,"wires":[]},{"id":"5001ac6c.22841c","type":"mqtt in","z":"eb731a6c.fd4f2","name":"RSSI","topic":"ttn/rssi","qos":"2","broker":"c979da80.ef2bb","x":184,"y":305,"wires":[["584c873d.5919b8","9aee160a.685c9"]]},{"id":"584c873d.5919b8","type":"ui_gauge","z":"eb731a6c.fd4f2","name":"","group":"7b216e54.fde1e","order":2,"width":0,"height":0,"gtype":"gage","title":"RSSI","label":"db","format":"{{value}}","min":"-120","max":10,"colors":["#ff0000","#e6e600","#408000"],"seg1":"-61","seg2":"-60","x":414,"y":305,"wires":[]},{"id":"9aee160a.685c9","type":"ui_chart","z":"eb731a6c.fd4f2","name":"","group":"ad1be2d3.97b188","order":3,"width":0,"height":0,"label":"RSSI / db","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"x":420,"y":340,"wires":[[],[]]},{"id":"2560068f.090982","type":"mqtt in","z":"eb731a6c.fd4f2","name":"SNR","topic":"ttn/snr","qos":"2","broker":"c979da80.ef2bb","x":190,"y":400,"wires":[["a7d9b69c.06b66","11b564a1.e634c3"]]},{"id":"a7d9b69c.06b66","type":"ui_gauge","z":"eb731a6c.fd4f2","name":"","group":"7b216e54.fde1e","order":3,"width":0,"height":0,"gtype":"gage","title":"SNR","label":"db","format":"{{value}}","min":"0","max":"20","colors":["#00b500","#e6e600","#ca3838"],"seg1":"15","seg2":"16","x":410,"y":400,"wires":[]},{"id":"11b564a1.e634c3","type":"ui_chart","z":"eb731a6c.fd4f2","name":"","group":"ad1be2d3.97b188","order":2,"width":0,"height":0,"label":"SNR / db","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"x":420,"y":440,"wires":[[],[]]},{"id":"c979da80.ef2bb","type":"mqtt-broker","z":"","name":"unixweb","broker":"mqtt.unixweb.de","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""},{"id":"c6fe4cc5.6c0b68","type":"ui_group","z":"","name":"Values","tab":"7ca694fc.adacf4","disp":true,"width":"6","collapse":false},{"id":"7b216e54.fde1e","type":"ui_group","z":"","name":"Signal","tab":"7ca694fc.adacf4","order":2,"disp":true,"width":"6","collapse":false},{"id":"ad1be2d3.97b188","type":"ui_group","z":"","name":"Charts","tab":"7ca694fc.adacf4","order":3,"disp":true,"width":"6","collapse":false},{"id":"7ca694fc.adacf4","type":"ui_tab","z":"","name":"Home","icon":"dashboard"}]

So sieht dann bei erfolgreichem Copy-Paste in Node-Red der Flow aus:

TTN MQTT Nodered Dashboard
TTN MQTT Nodered Dashboard

Wenn alles richtig eingestellt und konfiguriert ist, sollte das Dashboard so aussehen. Übrigens das Dashboard erreicht man am Raspberry Pi (sofern node-red-dashboard installiert) über http:://<Raspberry-IP-Adresse>:/ui

Falls es Fragen gibt, einfach hier einen Kommentar hinterlassen.

Hier gibt es eine Livedemo : https://node.unixweb.eu/nodered/ui/#/0

Viel Spaß beim basteln und programmieren, wünscht Joachim

 

 

4 Kommentare

    1. Hallo Thomas

      Danke für Deinen Kommentar und den Link. Ist ja krass , wusste nicht dass das mit den Reifensensoren so geht.

      Grüße , Joachim

  1. Hallo Joachim, bin von Guido Burger sowie über Twitter über Deine tollen Arbeiten informiert worden. Respekt was du da so alles aus der Tüte zauberst. Aktuell beschäftige ich mich nun auch mit TTN und habe auch bereits ein Single Channel Gateway plus 3 Nodes zum Laufen gebracht. Habe auch die MQTT Anbindung realisiert die du beschrieben hast. Im Moment betreibe ich noch ein nodejs script welches mir die Daten in meine SmartHome Applikation schaufelt. Was ich nun auch gesehen habe – und Link bereits im Handy abgespeichert – ist Deine Node-Red Web App online. Hast du Node-Red auf Deinem Webserver laufen oder wird das nur gespiegelt ? Kannst du mir da einen Tip geben – habe im Web etwas recherchiert – aber nichts gefunden was für mich passt bzw. mich rockt.

    1. Hallo Achim

      Danke für Deine nette Nachricht. Der Node-Red Server ist kein Spiegel sondern Echtzeit. Wenn die Daten mal in MQTT drin sind, kannst Du Dashboard soviel Du willst anbinden via Node-Red. Die Daten laufen in meine Smarthome Lösung hinein und ebenfalls in diverse Demo Dashboard und andere Raspberry / Beaglebone Nodes.

      Zum installieren von Node-Red kannst diese Anleitung nehmen : https://www.digitalocean.com/community/tutorials/how-to-connect-your-internet-of-things-with-node-red-on-ubuntu-16-04
      Die klappt dann auch sehr gut und was Du noch brauchst, ist einen Webserver vorne dran. Eine Anleitung gibt es hier: https://openenergymonitor.org/forum-archive/node/12489.html
      So ist mein Node-Red Server nicht direkt über Port 1880 erreichbar. Du musst dann auch die settings.js anpassen und Deinen ganzen Node mit einem Benutzername und Passwort absichern, wie auch in meinem Fall.

      Falls es noch weitere Fragen gibt, einfach hier kommentieren, ich helfe gerne.

      Beste Grüße , Joachim

Kommentar hinterlassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*