Personalize the MQTT topics
-
Hi there all.
I finally finish to make work my first node and I'm very happy that it's work greate.I just want to ask if it's possible to personalize the MQTT topics.
I mean, if it's possible in the GATEWAY code to intercept the NodeID, SensorID and value
and make a personalized topics in order to send it to MQTT broker.I ask this because I make just a unique node and in my MQTT
it's already a lot of confusion...
There is a lot of number that I must translate every time I take a look.What I would like is... use the MQTT code in order to make the broker connection,
stop the default update of topics, intercept the NodeID/SensorID/Value and give e personalized name.Thanks all in advance
Denis
-
@DenisJ that should be possible, although mysensors does not implement it.
What you could do is use the MQTT MySensors gateway as-is, and use a service on top, running somewhere else, that subscribes to the mysensors MQTT topics, 'decodes' them and republishes the data under different, understandable topics to the MQTT broker.
This way you are not deviating from the MySensors gateway implementation (e.g. when updates are released) and you do not have to modify & reflash your gateway each time a node is added.
I personally use node-red for the decoding, but any other 'programming' tool with MQTT access should work.
-
Thanks a lot Yveaux... it's just what I work to from about 2 hours
I'll split all sensors in Node-Red with a new topic name.
I use something like:/* id SensGarag: 11 id SensCame1: id SensCame2: id SensCame3: id SensCucin: id SensFuori: id SensSalon: id SensSerra: type Temp : 51 type Hum : 52 type Press: 53 */ // sensor id (topic:MySensors-out/xx/) var parts = msg.topic.split("/"); id = parts[1]; type = parts[2];
Thanks a lot again
Denis
-
@DenisJ nice!
Btw. @tbowmo created a nice set of node-red nodes to de- and encode MySensors messages. Maybe they can be of good use to you.
-
I have try to install it but node-red give me some error.
But I have make a function that have a tabel that personalize all topics.
So I'll make all my future sensors in this mode.Thanks again
Denis
-
I'm interested, can you share some more info on this feature ??
-
Practically I receive the MQTT topics from the MySensors Gateway.
Then in Node-Red I have make a function that translate the MySensors MQTT topics in a personalized MQTT topics,
in order to have clear names of the sensors value.In Node-Red you make a flow like this:
Then in the function node you make something like this... but personalized:
// add timestamp to every sensor reads var dt = new Date(); var hh = dt.getHours(); var mm = dt.getMinutes(); var ss = dt.getSeconds(); var yy = dt.getFullYear(); var mo = dt.getMonth() + 1; // want 1 more var dd = dt.getDate(); //var daynum = date.getDay(); timeST = (dd < 10 ? "0" : "") + dd + "-" + (mo < 10 ? "0" : "") + mo + "-" + yy + " " + (hh < 10 ? "0" : "") + hh + ":" + (mm < 10 ? "0" : "") + mm + ":" + (ss < 10 ? "0" : "") + ss; /* sensors ID id SensGaraj: 11 id SensCame1: id SensCame2: id SensCame3: id SensCucin: id SensSerra: 12 id SensFuori: id SensSalon: type Temp : 51 type Hum : 52 type Press : 53 type BatVol: 54 type BatLev: 255 */ ret = 0; // send the return or not // sensor id (topic:MySensors-out/xx/) var parts = msg.topic.split("/"); id = parts[1]; type = parts[2]; val = msg.payload; if (id == 11 ){sname = "SensGaraj";ret = 1;} if (id == 12 ){sname = "SensSerra";ret = 1;} // if etc... if (ret == 1){ if (type == 51 ){stype = "Temp"; } if (type == 52 ){stype = "Hum";} if (type == 53 ){stype = "Press";} if (type == 54 ){stype = "BattVol";} if (type == 255){stype = "BattLev";} msg = {}; //initialize msg object msg.measurement = "sensors"; // tabella/file > influxdb msg.topic = "MySensors/" + sname + "/" + stype; msg.payload = {value: val, timeST: timeST} node.status({fill:"green",shape:"ring",text:sname}); return msg; } //////////////////////////////////////////////////////////////
If you need more help just let me know
Denis
Suggested Topics
-
Arduino Celebrates 10 years (Malmö/Sweden)
Announcements • 29 Mar 2014, 17:08 • hek 29 Mar 2014, 17:08 -
MYSController with MQTT Gateway (via Node Red)
Development • 11 Aug 2024, 22:35 • slt1 11 Aug 2024, 22:35 -
MQTT GW on ESP8266 supporting SSL/TLS?
Development • 4 Jun 2024, 10:21 • bgunnarb 27 Jun 2024, 13:53 -
Radio waking up for no reason.
Development • 4 Jul 2020, 21:09 • Sasquatch 15 Jan 2025, 08:33 -
Gateways
Development • 6 Feb 2024, 01:16 • OldSurferDude 6 Feb 2024, 14:22 -
Saving last known good state, but not in EEPROM
Development • 30 Jan 2024, 18:46 • OldSurferDude 15 Jan 2025, 08:51