MQTT - OpenHAB - Mysensor Library -> How to subscribe?
-
Hi guys,
I am trying to control my RGB LED Strip behind my TV using Open HAB! The Hardware part using an arduino is done and working, now I am trying to control the arduino using OpenHAB and MQTT but I am very new to MQTT...
So, I know I could subscribe to a topic if I would use a mosquitto Server (what I don´t have because I am using the MySensors MQTT Gateway) and it would look sth. like this in Openhab:{mqtt=">[localbroker:/openHAB/Nachtlicht/RED:state:*:default]"}so here I would subscribe to a special topic, but how could I achieve this with the MySensors library?
And one more thing: If I have an arduino then it would get an NodeID, but the attached sensors (Light Sensor, Humidity sensor...) would need a childID (one ID for each sensor)!?
Thanks for your help!!
-
I would like to post my code here, but it´s actually not my code but the code from this website:
link
I am trying to make it MySensors MQTT Gateway compatible and I am not making any progress right now and need you help. First of all I would like to post the original arduino code from the above mentioned website:#include <SPI.h> #include <Ethernet.h> #include <PubSubClient.h> // http://knolleary.net/arduino-client-for-mqtt/ // MAC Adresse des Ethernet Shields byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; // IP des MQTT Servers byte server[] = { 192, 168, 2, 9 }; // Ethernet Client zur Kommunikation des MQTT Clients EthernetClient ethClient; // MQTT Client zur Kommunikation mit dem Server // Server - Variable des Types byte mit Serveradresse // 1883 - Ist der Standard TCP Port // callback - Function wird aufgerufen wen MQTT Nachrichten eintreffen. Am ende des Sketches // ethClient - Angabe des Ethernet Clients PubSubClient mqttClient(server, 1883, callback, ethClient); // Timervariable für eine Verzögerung. Als alternative zu delay was die verarbeitung anhält. int timer = 0; int EndTimer = 200; // Pins des RGB LED Strip int LEDred=5; int LEDgreen=6; int LEDblue=3; // Übermittelte Farbwerte int LEDredValue = 0; int LEDgreenValue = 0; int LEDblueValue = 0; // Gerade gesetzte Farbe int CURredValue = 0; int CURgreenValue = 0; int CURblueValue = 0; void setup() { // Setzen der PINS als Ausgang pinMode(LEDblue, OUTPUT); pinMode(LEDred, OUTPUT); pinMode(LEDgreen, OUTPUT); // Bei start Farbe Blau setzen analogWrite(LEDred, 0); analogWrite(LEDgreen, 0); analogWrite(LEDblue, 100); // Initialisierung des Ethernets if (Ethernet.begin(mac) == 0) { // Wenn DHCP fehlschlägt dann rot setzen und aufhören analogWrite(LEDred, 100); analogWrite(LEDgreen, 0); analogWrite(LEDblue, 0); while (true); } else { // Wenn DHCP OK ist dann grün setzen LEDgreenValue = 100; } } void loop() { // Aufbau der Verbindung mit MQTT falls diese nicht offen ist. if (!mqttClient.connected()) { mqttClient.connect("arduinoClient"); // Abonieren von Nachrichten mit dem angegebenen Topics mqttClient.subscribe("/openHAB/Nachtlicht/RED/#"); mqttClient.subscribe("/openHAB/Nachtlicht/GREEN/#"); mqttClient.subscribe("/openHAB/Nachtlicht/BLUE/#"); // Alternative Abonierung aller Topics unter /openHAB/Nachtlicht // mqttClient.subscribe("/openHAB/Nachtlicht/#"); } if (timer <= EndTimer) timer++; else { timer = 0; if (LEDredValue < CURredValue) CURredValue--; else if (LEDredValue > CURredValue) CURredValue++; if (LEDgreenValue < CURgreenValue) CURgreenValue--; else if (LEDgreenValue > CURgreenValue) CURgreenValue++; if (LEDblueValue < CURblueValue) CURblueValue--; else if (LEDblueValue > CURblueValue) CURblueValue++; } analogWrite(LEDred, CURredValue); analogWrite(LEDgreen, CURgreenValue); analogWrite(LEDblue, CURblueValue); mqttClient.loop(); // Schleife für MQTT } // =========================================================== // Callback Funktion von MQTT. Die Funktion wird aufgerufen // wenn ein Wert empfangen wurde. // =========================================================== void callback(char* topic, byte* payload, unsigned int length) { // Zähler int i = 0; // Hilfsvariablen für die Convertierung der Nachricht in ein String char message_buff[100]; // Kopieren der Nachricht und erstellen eines Bytes mit abschließender \0 for(i=0; i<length; i++) { message_buff[i] = payload[i]; } message_buff[i] = '\0'; // Konvertierung der nachricht in ein String String msgString = String(message_buff); // Überprüfung des Topis und setzen der Farbe je nach übermittelten Topic if (String(topic) == "/openHAB/Nachtlicht/RED") LEDredValue = round(msgString.toInt() * 2.55); if (String(topic) == "/openHAB/Nachtlicht/GREEN") LEDgreenValue = round(msgString.toInt() * 2.55); if (String(topic) == "/openHAB/Nachtlicht/BLUE") LEDblueValue = round(msgString.toInt() * 2.55); }and here follows my "modified" part. I hope you guys can support me here!
#include <MySensor.h> #include <SPI.h> // Timervariable für eine Verzögerung. Als alternative zu delay was die verarbeitung anhält. int timer = 0; int EndTimer = 200; // Pins des RGB LED Strip int LEDred=3; int LEDgreen=4; int LEDblue=2; // Übermittelte Farbwerte int LEDredValue = 0; int LEDgreenValue = 0; int LEDblueValue = 0; // Gerade gesetzte Farbe int CURredValue = 0; int CURgreenValue = 0; int CURblueValue = 0; MySensor gw; MyMessage dimmerMsg(21, V_DIMMER); //21=Sensor ID 21, Dimmer; An/Aus MyMessage lightMsgR(22, V_LIGHT); //21=Sensor ID 21, Rot MyMessage lightMsgG(23, V_LIGHT); //21=Sensor ID 21, Grün MyMessage lightMsgB(24, V_LIGHT); //21=Sensor ID 21, Blau void setup() { //MySensors Library initialisieren gw.begin(); //Sensor ID und Typ übermitteln gw.present(21, S_DIMMER); gw.present(22, V_LIGHT); gw.present(23, V_LIGHT); gw.present(24, V_LIGHT); // Setzen der PINS als Ausgang pinMode(LEDblue, OUTPUT); pinMode(LEDred, OUTPUT); pinMode(LEDgreen, OUTPUT); // Bei start Farbe Blau setzen analogWrite(LEDred, 0); analogWrite(LEDgreen, 0); analogWrite(LEDblue, 100); } void loop() { // Abonieren von Nachrichten mit dem angegebenen Topics mqttClient.subscribe("/openHAB/Nachtlicht/RED/#"); mqttClient.subscribe("/openHAB/Nachtlicht/GREEN/#"); mqttClient.subscribe("/openHAB/Nachtlicht/BLUE/#"); } if (timer <= EndTimer) timer++; else { timer = 0; if (LEDredValue < CURredValue) CURredValue--; else if (LEDredValue > CURredValue) CURredValue++; if (LEDgreenValue < CURgreenValue) CURgreenValue--; else if (LEDgreenValue > CURgreenValue) CURgreenValue++; if (LEDblueValue < CURblueValue) CURblueValue--; else if (LEDblueValue > CURblueValue) CURblueValue++; } analogWrite(LEDred, CURredValue); analogWrite(LEDgreen, CURgreenValue); analogWrite(LEDblue, CURblueValue); mqttClient.loop(); // Schleife für MQTT } // =========================================================== // Callback Funktion von MQTT. Die Funktion wird aufgerufen // wenn ein Wert empfangen wurde. // =========================================================== void callback(char* topic, byte* payload, unsigned int length) { // Zähler int i = 0; // Hilfsvariablen für die Convertierung der Nachricht in ein String char message_buff[100]; // Kopieren der Nachricht und erstellen eines Bytes mit abschließender \0 for(i=0; i<length; i++) { message_buff[i] = payload[i]; } message_buff[i] = '\0'; // Konvertierung der nachricht in ein String String msgString = String(message_buff); // Überprüfung des Topis und setzen der Farbe je nach übermittelten Topic if (String(topic) == "/openHAB/Nachtlicht/RED") LEDredValue = round(msgString.toInt() * 2.55); if (String(topic) == "/openHAB/Nachtlicht/GREEN") LEDgreenValue = round(msgString.toInt() * 2.55); if (String(topic) == "/openHAB/Nachtlicht/BLUE") LEDblueValue = round(msgString.toInt() * 2.55); }I added LED Red, Green and Blue and set child ID´s to 22,23 and 24, 21 is for Dimmer. I have no idea if this done the right way
MyMessage dimmerMsg(21, V_DIMMER); //21=Sensor ID 21, Dimmer; An/Aus MyMessage lightMsgR(22, V_LIGHT); //21=Sensor ID 21, Rot MyMessage lightMsgG(23, V_LIGHT); //21=Sensor ID 21, Grün MyMessage lightMsgB(24, V_LIGHT); //21=Sensor ID 21, Blauespecially the following part is a big problem to me because I don´t know how to "subscribe" with the MySensors MQTT library:
void loop() { // Abonieren von Nachrichten mit dem angegebenen Topics mqttClient.subscribe("/openHAB/Nachtlicht/RED/#"); mqttClient.subscribe("/openHAB/Nachtlicht/GREEN/#"); mqttClient.subscribe("/openHAB/Nachtlicht/BLUE/#"); }how must I modify this??
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login