How to know what MQTT topic to send data on to reach a relay?

  • Hardware Contributor

    I have OpenHAB and mosquitto broker running on a raspberryPi with a MQTTClientGateway handling all the node communications. I'm perfectly fine with receiving messages from the sensors to the gateway, however i can't get a message from OpenHAB to the relay node to turn the relay on or off. It is a standard sketch with only 1 relay. I've read through all the API's for the message communication, and tried various different topics which i thought i had worked out, but i just can't seem to get it working.

    The message prefixes are defined in the gateway as follows:

    // Set this nodes subscripe and publish topic prefix
    #define MY_MQTT_PUBLISH_TOPIC_PREFIX "Gateway1-out"
    #define MY_MQTT_SUBSCRIBE_TOPIC_PREFIX "Gateway1-in"

    When the relay node connects to the Gateway i get this message:

    0;0;3;0;9;Sending message on topic: Gateway1-out/4/1/0/0/3

    The relay node has an ID of 4. From looking through the API's I worked it out to be the following topic:


    So, i tried to publish the following from the broker:

    mosquitto_pub -t Gateway1-in/4/0/1/0/2 -m '1'

    Just in case you are wondering what i have changed in the sketch, Ill put it here...

     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     * Created by Henrik Ekblad <>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list:
     * Documentation:
     * Support Forum:
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     * Version 1.0 - Henrik Ekblad
     * The W5100 MQTT gateway sends radio network (or locally attached sensors) data to your MQTT broker.
     * The node also listens to MY_MQTT_TOPIC_PREFIX and sends out those messages to the radio network
     * LED purposes:
     * - To use the feature, uncomment WITH_LEDS_BLINKING in MyConfig.h
     * - RX (green) - blink fast on radio message recieved. In inclusion mode will blink fast only on presentation recieved
     * - TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly
     * - ERR (red) - fast blink on error during transmission error or recieve crc error  
     * See for wiring instructions.
     * nRF24L01+  ESP8266
     * VCC        VCC
     * CE         GPIO4          
     * CSN/CS     GPIO15
     * SCK        GPIO14
     * MISO       GPIO12
     * MOSI       GPIO13
     * Not all ESP8266 modules have all pins available on their external interface.
     * This code has been tested on an ESP-12 module.
     * The ESP8266 requires a certain pin configuration to download code, and another one to run code:
     * - Connect REST (reset) via 10K pullup resistor to VCC, and via switch to GND ('reset switch')
     * - Connect GPIO15 via 10K pulldown resistor to GND
     * - Connect CH_PD via 10K resistor to VCC
     * - Connect GPIO2 via 10K resistor to VCC
     * - Connect GPIO0 via 10K resistor to VCC, and via switch to GND ('bootload switch')
      * Inclusion mode button:
     * - Connect GPIO5 via switch to GND ('inclusion switch')
     * Hardware SHA204 signing is currently not supported!
     * Make sure to fill in your ssid and WiFi password below for ssid & pass.
    #include <EEPROM.h>
    #include <SPI.h>
    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    // Enables and select radio type (if attached)
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    // Set this nodes subscripe and publish topic prefix
    #define MY_MQTT_PUBLISH_TOPIC_PREFIX "Gateway1-out"
    #define MY_MQTT_SUBSCRIBE_TOPIC_PREFIX "Gateway1-in"
    // Set MQTT client id
    #define MY_MQTT_CLIENT_ID "mysensor"
    // W5100 Ethernet module SPI enable (optional if using a shield/module that manages SPI_EN signal)
    //#define MY_W5100_SPI_EN 4  
    // Enable Soft SPI for NRF radio (note different radio wiring is required)
    // The W5100 ethernet module seems to have a hard time co-operate with 
    // radio on the same spi bus.
    #if !defined(MY_W5100_SPI_EN)
      #define MY_SOFTSPI
      #define MY_SOFT_SPI_SCK_PIN 14
      #define MY_SOFT_SPI_MISO_PIN 16
      #define MY_SOFT_SPI_MOSI_PIN 15
    // When W5100 is connected we have to move CE/CSN pins for NRF radio
    #define MY_RF24_CE_PIN 5
    #define MY_RF24_CS_PIN 6
    // Enable these if your MQTT broker requires usenrame/password
    //#define MY_MQTT_USER "username"
    //#define MY_MQTT_PASSWORD "password"
    // Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
    #define MY_IP_ADDRESS 192,168,0,22
    // If using static ip you need to define Gateway and Subnet address as well
    #define MY_IP_GATEWAY_ADDRESS 192,168,0,1
    #define MY_IP_SUBNET_ADDRESS 255,255,255,0
    // MQTT broker ip address.  
    #define MY_CONTROLLER_IP_ADDRESS 192, 168, 0, 21
    // The MQTT broker port to to open 
    #define MY_PORT 1883      
    // Flash leds on rx/tx/err
    // Set blinking period
    // Enable inclusion mode
    // Enable Inclusion mode button on gateway
    // Set inclusion mode duration (in seconds)
    // Digital pin used for inclusion mode button
    #define MY_DEFAULT_ERR_LED_PIN 16  // Error led pin
    #define MY_DEFAULT_RX_LED_PIN  16  // Receive led pin
    #define MY_DEFAULT_TX_LED_PIN  16  // the PCB, on board LED
    #include <Ethernet.h>
    #include <MySensor.h>
    void setup() { 
    void presentation() {
      // Present locally attached sensors here    
    void loop() {
      // Send locally attech sensors data here

  • Hardware Contributor


    If i publish a message from mosquitto out to the Gateway of:

    mosquitto_pub -t Gateway1-in/4/1/1/0/2 -m '0'

    I get this in the Gateway serial interface:

    0;0;3;0;9;Message arrived on topic: Gateway1-in/4/1/1/0/2

    Now, the controller isn't reporting that it sent out a message to node 4 (which is my relay node). Should i expect to see it serial print a message saying its sent that message to the node? I've had a little attempt to look through the .h files to see if it is supposed to be saying its sent a message out, i would have thought so considering that it shows when it sends out a message to the controller....

  • Admin

    If MY_DEBUG is enabled on the gateway it should print something when sending a message.

  • Hardware Contributor

    @hek Which it is enabled still. No sent message output. is the message i'm sending confirm to what you would expect in the form of, is it the correct API layout? I got to Gateway1-in/4/1/1/0/2 by using the API of: node-id;child-sensor-id;message-type;ack;sub-type. So based on that i chose the values based on the below information:

    Gateway1-in has been used because that is what i specify in my Gateway: #define MY_MQTT_SUBSCRIBE_TOPIC_PREFIX "Gateway1-in".

    4 has been used for the node-id because when i connect the relay node, the node prints out "Node Id is set to: 4" and the gateway reads:

    0;0;3;0;9;read: 4-4-0 s=255,c=0,t=18,pt=0,l=10,sg=0:1.6.0-beta
    0;0;3;0;9;Sending message on topic: Gateway1-out/4/255/0/0/18
    0;0;3;0;9;read: 4-4-0 s=255,c=3,t=6,pt=1,l=1,sg=0:0
    0;0;3;0;9;Sending message on topic: Gateway1-out/4/255/3/0/6
    0;0;3;0;9;read: 4-4-0 s=255,c=3,t=11,pt=0,l=5,sg=0:Relay
    0;0;3;0;9;Sending message on topic: Gateway1-out/4/255/3/0/11
    0;0;3;0;9;read: 4-4-0 s=255,c=3,t=12,pt=0,l=3,sg=0:1.0
    0;0;3;0;9;Sending message on topic: Gateway1-out/4/255/3/0/12
    0;0;3;0;9;read: 4-4-0 s=1,c=0,t=3,pt=0,l=0,sg=0:
    0;0;3;0;9;Sending message on topic: Gateway1-out/4/1/0/0/3

    Now the Child-id is where the issue may be at, I am assuming it names them either starting at 0 or 1. The only part i can see playing any relevance in this addressing is in the setup section of the node it has "int sensor=1", now i can only assume that Is assigning the relay a address of 1. However, it didn't work so i tried 0 too, no luck there. So i thought, what is i get it to print its sensorID out in the serial monitor, so i called "Serial.print('sensor');" and it started to return "Sensor ID is set to: 28530" which didn't think was right. So would you say i need to be specifying a childID like in the other sketches? Even the RelayWithButtonActuator.ino has a ChildID specified, i'm not sure why this hasn't?

    I used 1 for the message type so i could set a value to a sensor (is this incorrect because its a switch and not a sensor?).

    I then used 0 for the Ack because its a incoming normal message.

    Then i used 2 for the sub-type for V_LIGHT on the set message type.

    Would you say that the construction of my message looks okay and conforms with the API? The only part I'm unsure about is the Child-Sensor-ID.

  • Admin

    When did you last update the library code?

    Do you have this that was committed 12 days ago?

  • Hardware Contributor

    I got the development branch 2 days ago from your github.

    Would the child sensor id be the same on the publish topic as it is on the subscribe topic, surely yes, if so then the relay has a ID of 1. Which implies that i have my published message correct and its just not leaving the gateway. Which would then point towards what your suggesting of my gateway not being a repeater.

    I just checked what the change was on the MySensor.h file for that update you just linked and i can confirm i have it in my version of MySensor.h:

    #if defined(MY_GATEWAY_MQTT_CLIENT)
    	#if defined(MY_RADIO_FEATURE)
    		// We assume that a gateway having a radio also should act as repeater

  • Admin

    @samuel235 said:

    Would you say that the construction of my message looks okay and conforms with the API?

    Yes it looks ok. Very strange.

    Far fetched: Could it be something about case sensitivity on topic prefix naming? But you get "Message arrived on topic" message... Hmm..

  • Hardware Contributor

    @hek I've just been comparing the 1.5 RelayActuator sketch with the Development one.

    Is it supposed to be completely different? I'm assuming its referenced in the MySensor.h but the development sketch doesn't have any of the gw. options or anything. This is how its supposed to be? In an attempt to lower the memory needed on the node side?

    Just out of interested, could you briefly explain or point me to the page where i can decode the sending message layout "send: 4-4-0-0 s=1,c=0,t=3,pt=0,l=0,sg=0,st=ok:" Makes no sense to me at all. I only know that the s=1 is something to do with the sensor attached maybe? I keep looking everywhere to decode this and i can't get any sense out of it. It probably wont have any influence on this issue but i would just like to understand the methods, if possible.

  • Hardware Contributor

    I've just found this in the sketch

    void receive(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.isAck()) {
         Serial.println("This is an ack from gateway");

    So, i changed my published message to: mosquitto_pub -t Gateway1-in/4/1/1/1/2 -m '1'.

    The relay has not printed anything out, so i think its fair to assume that the issue is for some reason or another the gateway is not pushing that message to the relay node.

  • I´m new on this forum, and I'm really excited about the MySensors project. I've done a lot of research on the internet and haven't found anything close to what you guys are doing.Thanks Henrik and the rest of the team involved! Excellent work, really!

    I haven't tried yet the 1.6 Development Branch; but when looking into your MQTT Client issue I don't recognize the MQTT message format being used (I could be deeply wrong about something I haven't understood about your set-up). Normally the standard MQTT format is a/b/c/d + payload. In my GW_MQTT Client pre-version I'm using to send messages to a Relay sensor I use format: "a/b/c/d/set + payload. In your case it seems to be a/b/c/d/e/f + payload. That wouldn't work in my case. Or in 'MySensors' terms it would be: 'MyMQTT/Sensor_Node_id/Child_Id/Sub_Type/set' + Payload. I use for my relay sensor Sub_Type = V_LIGHT. You're using Sub_Type=2, I guess it is incorrect.

    With mosquitto publish client command it would be: mosquitto_pub -t Gateway1-in/4/0/V_LIGHT/set -m 1 or 0. This works ok for me. I think the command '/set' for publishing to the sensor isn't used in current MySensors V1.6 GW_MQTT_Client proposal. Not sure yet how this has been arranged in V1.6 development branch. I have to look into the code.

    I have an up and running test environment with a pre-relaease to V1.6 MySensors MQTT-GW-Client, a mosquitto-brooker, ZABBIX for monitoring and a bunch of sensors (so far: Temperatue / Humidity / RFID-reader / Energy-Consumption / ON-OFF-sensors (door-open, smoke alarm, motion detector etc.)/ Action-Relays / Battery back-up voltage measurement). It's working incredibly well. I'm impressed so far! To push messages to the relay over MQTT works fine too. My expectations are to reach hopefully hundreds of environmental sensors over MQTT and MySensors to monitor our data-nodes at work via ZABBIX. If anyone is interested in my set-up and preliminary hardware prototypes pls. let me know. (sorry if it's the wrong forum-channel).

    Right now I'm looking into the new RS485 concept added to V1.6. Hopefully it'll work with MQTT? I have been struggling too much with NRF24L01+ without being fully satisfied. Interested as well in the new RFM69W radio with better range with the LowPowerLabs Moteino board, but haven't got time to test so far.

  • Hardware Contributor

    @jpaulin I'm not completely sure in what your reply objective is. However, I'm not convinced that you understand the message API properly.... gateway-subscription-prefix/node-id/child-sensor-id/message-type/ack/sub-type/payload. I'm not sure what your meaning with the "/set" at the end of your message layout? For an example, my gateway subscription prefix is "Gateway-1". So for me to set a value of 1 on my relay node i would send the payload as '1' on the route of:

    "/Gateway-1/4/1/1/0/2/[Payload-Goes-Here]" Now, i personally use 2 for the Sub_Type rather than V_Light. Not sure if that will impose any issues further down the line, but it works perfectly for me right now.

    Just to let you know, I run my controller (OpenHAB) and my MQTT Broker (Mosquitto) on a RaspberryPi, then my Gateway is using the development branch of W5100GatewayMQTT which is pretty awesome, and then my sensors at the moment are running all on Arduino Nano Pros, but I'm in the process of converting those to Arduino Pro Minis for lower power consumption and to enable me to get close to installing them in socket boxes behind light switches.

  • ok, I think I need to look into the new MQTTW5100 code to understand better the new API structure. Anyhow, I had a similar case as you mentioned with my MQTT pre-version, where the mesage_type has to be written in ASCII (V_LIGHT), and internally in the .cpp code it's translated to 2. Could be your case, maybe? In the MQTT-GW-Client pre-version I'm using it expects '/set' after the a/b/c/d MQTT message to allow sending forward a message to the sensor node, to avoid message loops. I guess that's been changed in the V1.6 branch.

  • Hardware Contributor

    Yeah, the numbers of the type of message is set as variables in the .cpp or the .h file somewhere, i can't remember which one off the top of my head.

    The /set might still be in but all i'm saying is that i don't need to specify it in my sketches. It may be referenced somewhere. I'm sure Hek could advise on this, not that its an issue as we both have working examples. Just out of curiosity more than anything. The new dev branch for the MQTTGateway is super smooth and functional now. Give it a shot!

  • ok. think I'll give it a try on the weekend.

  • Here is a page on the message structure. It's the same as the Serial API. There is also a list of all the subtypes.

  • @samuel235 I guess you have the same problem which I faced. I managed to receive the topic on the gateway like you have did, but it wasn't sent to the sensor node.

    There was two fixes for these to work for me. First one which is defining #MY_REAPEATER_NODE (You have already did this ) and hek has verified this solution. The other issue is related to a ( IF CONDITION ) in the code but it was not verified by hek so it was reported as a bug as no one else complained except me.

    Please read this post that includes details explanation of the two fixes

    This is also the main thread where I had the same problem which you complain of now. You can read it if you want also

    Please update me. Hope that it solves your problem.

  • Hardware Contributor

    @ahmedadelhosni I have got my gateway working perfectly with my sensors now, have a look at my topic i created for this issue.

  • @samuel235 Yes. That's the same issue which I was pointing to but with a better solution. Glad that someone guided you (Y)