How to send MQTT message from controller to Node?



  • Hi,

    I´ve set up a IR sender to control a RGB LED behind my TV. Now I want to communicate with this node using openhab and MQTT over NRF24. Unfortunately I have no idea how to send commands to a node, until now I was only sending informations from the Nodes/Sensors to the controller.

    So, I need to create functions for the Node like

    If command "ON/Off" received from openhab
    -send IR Command for ON/OFF
    If Command "Red" received from openhab
    -send IR Command for color red
    If Command "Fade" received from openhab
    -send IR Command for fading through colors

    I need to know how to send this from openhab or Mosquitto for example and especially how to catch these messages and process them in the Arduino code.

    I guess this is a beginners task but I have no idea where to start...

    Thanks for your help!



  • @siod Your mysensors gateway will handle this for you (assuming you have a MQTT gw, which I think you do since you can receive data from your mysensors nodes).

    Just send the correct MQTT message and the gateway will route it to the node. You then need to handle the message in the receive() function in your sketch.

    Have a look at https://forum.mysensors.org/topic/6765/rgb-led-strip, where I describe how I built my RGB lamp. It both sends and receives messages.



  • So, if I want to send HEX Values to the node, can I send them through openahb over MQTT like this (Node 2 / Sensor 2 / SET / NO ACK / V_IR_RECEIVE):

    Switch MQTTTestSwitch  "MQTT Test Switch"  (gBasement)  {mqtt=">[mysensor:mygateway1-in/2/2/1/0/33:command:ON:1E240],>[mysensor:mygateway1-in/2/2/1/0/33:command:OFF:1E249]"}  //????
    

    and pick it up on the node uisng the receive() function like this:

    void receive(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type==V_IR_RECEIVE) {
         // Send command to Turn LEDs On
         if (message.getstring()==1E240){   // ??? 
            Irsend.NEC(1E240)   //send IR command using IRemote Library over IR LED attached to PIN 4
        }
       } 
    

    ??? ??? ???

    Of course I could sned 1 or 0 to turn it On or Off, but when going to send color HEX Codes I thought I should start working whith HEX Codes from the beginning...



  • This the code I got so far, which does not work :(

    #define MY_RADIO_NRF24 //MySensor Library auf NRF24 Funkmodul einstellen, muss vor MySensor.h Initialisierung geschehen
    // Define Node ID
    #define MY_NODE_ID 5
    //#define MY_PARENT_NODE_ID 50 //Repeater Node 1!
    //#define MY_PARENT_NODE_IS_STATIC
    
    #include <MySensors.h>
    #include <SPI.h>
    #include <IRremote.h>
    
    #define CHILD_ID 1   // Id of the sensor child
    
    IRsend irsend;
    
    // Initialize motion message
    MyMessage msg(CHILD_ID, V_IR_SEND);
    
    void setup()
    {
      Serial.begin(9600);
    }
    
    void presentation()
    {
        // Send the sketch version information to the gateway and Controller
        sendSketchInfo("LED Licht TV Steuerung", "1.0");
    
        // Register all sensors to gw (they will be created as child devices)
        present(CHILD_ID, S_IR);
    }
    
    void loop() {
    
    
    }
    
    void receive(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type == V_IR_SEND) {
        // Send command to Turn LEDs On
        if (message.getString() == "FF02FD") { // ???
          for (int i = 0; i < 3; i++) {
            irsend.sendNEC(0xFF02FD, 32);
            Serial.println(message.getString());
            Serial.println("Message sent");
            delay(40);
          }
        }
      }
    }
    

    edit: from the serial monitor I see that the node receives the "FF02FD" String, but it does not enter this part

        if (message.getString() == "FF02FD") { // ???
          for (int i = 0; i < 3; i++) {
            irsend.sendNEC(0xFF02FD, 32);
            Serial.println(message.getString());
            Serial.println("Message sent");
            delay(40);
          }
        }
    

    edit2:

    This is what I am sending from openhab over MQTT:

    mygateway1-in/5/1/1/0/32
    

    edit3:

    Ok, now I see where it fails:

    message.getString()
    

    contains FF02FD

    but when I say

    if (message.getString() == "FF02FD")
    

    it does not recognise the FF02FD. But I don´t understand why

    edit4:

    Ok, I got it, finally...:

    message.getString("FF02FD");
    

    :astonished: :facepunch:


Log in to reply
 

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.