MQTT Broker gateway

  • Plugin Developer


    I'm currently busy implementing an mqtt client plugin, and i'm going to use mysensors for it's first predefined implementation so i have a couple of questions

    I took a look at the api page and saw this in the mqtt documentation:


    Does this means as example:


    OR this?

    • Are variable types defined by their index used or by name?
    • Also it states it keeps track of node id's how are these reported by the broker, should the controller still hand out free id's?

    I currently do not have the hardware present to at least set it up to test and look at what exactly is happening.

  • Code Contributor

    @John MyMQTT/[0-255]/[0-255]/[STRING]/[VALUE]

    MQTT.cpp :

    sprintf(&buffer[buffsize],"/%i/%i/V_%s", msg.sender, msg.sensor, getType(convBuf, &vType[msg.type]));

    note V_%s -> MyMQTT/[0-255]/[0-255]/V_[STRING]/[VALUE]
    also note there is no trailing / before payload, as
    I choose not to have trailing slash, but if needed its easy to make it configurable.

    and get type translates from this list

      char V_0[] PROGMEM = "TEMP";		//V_TEMP
      char V_1[] PROGMEM = "HUM";		//V_HUM
      char V_2[] PROGMEM = "LIGHT";		//V_LIGHT
      char V_3[] PROGMEM = "DIMMER";		//V_DIMMER
      char V_4[] PROGMEM = "PRESSURE";	//V_PRESSURE
      char V_5[] PROGMEM = "FORECAST";	//V_FORECAST
      char V_6[] PROGMEM = "RAIN";		//V_RAIN
      char V_7[] PROGMEM = "RAINRATE";	//V_RAINRATE
      char V_8[] PROGMEM = "WIND";		//V_WIND
      char V_9[] PROGMEM = "GUST";		//V_GUST
      char V_10[] PROGMEM = "DIRECTON";	//V_DIRECTON
      char V_11[] PROGMEM = "UV";		//V_UV
      char V_12[] PROGMEM = "WEIGHT";		//V_WEIGHT
      char V_13[] PROGMEM = "DISTANCE";	//V_DISTANCE
      char V_14[] PROGMEM = "IMPEDANCE";	//V_IMPEDANCE
      char V_15[] PROGMEM = "ARMED";		//V_ARMED
      char V_16[] PROGMEM = "TRIPPED";	//V_TRIPPED
      char V_17[] PROGMEM = "WATT";		//V_WATT
      char V_18[] PROGMEM = "KWH";		//V_KWH
      char V_19[] PROGMEM = "SCENE_ON";	//V_SCENE_ON
      char V_20[] PROGMEM = "SCENE_OFF";	//V_SCENE_OFF
      char V_21[] PROGMEM = "HEATER";		//V_HEATER
      char V_22[] PROGMEM = "HEATER_SW";	//V_HEATER_SW
      char V_23[] PROGMEM = "LIGHT_LEVEL";	//V_LIGHT_LEVEL
      char V_24[] PROGMEM = "VAR1";		//V_VAR1
      char V_25[] PROGMEM = "VAR2";		//V_VAR2
      char V_26[] PROGMEM = "VAR3";		//V_VAR3
      char V_27[] PROGMEM = "VAR4";		//V_VAR4
      char V_28[] PROGMEM = "VAR5";		//V_VAR5
      char V_29[] PROGMEM = "UP";		//V_UP
      char V_30[] PROGMEM = "DOWN";		//V_DOWN
      char V_31[] PROGMEM = "STOP";		//V_STOP
      char V_32[] PROGMEM = "IR_SEND";	//V_IR_SEND
      char V_33[] PROGMEM = "IR_RECEIVE";	//V_IR_RECEIVE
      char V_34[] PROGMEM = "FLOW";		//V_FLOW
      char V_35[] PROGMEM = "VOLUME";		//V_VOLUME
      char V_36[] PROGMEM = "LOCK_STATUS";	//V_LOCK_STATUS
      char V_37[] PROGMEM = "DUST_LEVEL";	//V_DUST_LEVEL
      char V_38[] PROGMEM = "VOLTAGE";	//V_VOLTAGE
      char V_39[] PROGMEM = "CURRENT";	//V_CURRENT
      char V_40[] PROGMEM = "";		//
      char V_41[] PROGMEM = "";		//
      char V_42[] PROGMEM = "";		//
      char V_43[] PROGMEM = "";		//
      char V_44[] PROGMEM = "";		//
      char V_45[] PROGMEM = "";		//
      char V_46[] PROGMEM = "";		//
      char V_47[] PROGMEM = "";		//
      char V_48[] PROGMEM = "";		//
      char V_49[] PROGMEM = "";		//
      char V_50[] PROGMEM = "";		//
      char V_51[] PROGMEM = "";		//
      char V_52[] PROGMEM = "";		//
      char V_53[] PROGMEM = "";		//
      char V_54[] PROGMEM = "";		//
      char V_55[] PROGMEM = "";		//
      char V_56[] PROGMEM = "";		//
      char V_57[] PROGMEM = "";		//
      char V_58[] PROGMEM = "";		//
      char V_59[] PROGMEM = "";		//
      char V_60[] PROGMEM = "Started!\n";	//Custom for MQTTGateway
      char V_61[] PROGMEM = "SKETCH_NAME";	//Custom for MQTTGateway
      char V_62[] PROGMEM = "SKETCH_VERSION"; //Custom for MQTTGateway
      char V_63[] PROGMEM = "UNKNOWN"; 	//Custom for MQTTGateway
      char V_64[] PROGMEM = "FW_CREQ"; 	//Custom for MQTTGateway
      char V_65[] PROGMEM = "FW_CRES"; 	//Custom for MQTTGateway
      char V_66[] PROGMEM = "FW_REQ"; 	//Custom for MQTTGateway
      char V_67[] PROGMEM = "FW_RES"; 	//Custom for MQTTGateway

    The MQTTgateway hands out ID's; configured in MQTT.h:

      #define MQTT_FIRST_SENSORID	20  		// If you want manually configured nodes below this value. 255 = Disable
      #define MQTT_LAST_SENSORID	254 		// 254 is max! 255 reserved.
      #define MQTT_BROKER_PREFIX	"MyMQTT"	// First prefix in MQTT tree, keep short!
      #define MQTT_SEND_SUBSCRIPTION 1		// Send empty payload (request) to node upon MQTT client subscribe request.
      // NOTE above : Beware to check if there is any length on payload in your incommingMessage code:
      // Example: if (msg.type==V_LIGHT && strlen(msg.getString())>0) otherwise the code might do strange things.

  • Plugin Developer


    And how about the internal messages? Are these somewhat compatible with the Serial API? Like this one now has a "I_GATEWAY_READY" internal var, this one complies then with the "Started!\n" message?

    Also about the presentation (if the node does it), are these also passed through?

    I know it's a bit question banging... But i just want to do it right.

  • Code Contributor

    @John No worries! ๐Ÿ™‚
    Presentation is On Todo.. Or Rather To think About list is presentation . I dont do anything with that as it is (Just ignores it). I havn't had any good idea for that yet.

    Internal is nothing more or less, it connects and set a variable that it is connected. ๐Ÿ™‚ But sure, there could be transport for internal messages also.
    I do have an experimental version there I can send internal messages to reboot node, reboot gateway and set clear read eeprom values. (over mqtt-protocol) but this s not finnished yet.. Ideas are welcome!

  • Code Contributor

    @John About Presentation ; My idea was that next version of the variable system I will either store some bytes in the MQTT gateway about what S_types to V_types, or hopefully the next protocol contains both those so the GW doesnt have to remember. And then add an extra directory to the MQTT path ( MyMQTT/[0-255]/[0-255]/S_[STRING]/V_[STRING]/[VALUE]

  • Plugin Developer

    I think maybe it depends on the next protocol implementation. Personally i wouldn't mind if the full path mapping was /[0-255]/[0-255]/etc.. to keep (prog)mem print smaller (use the V_ and S_ array index values). It could leave space for more specific things or some extra gateway specific functions.

    it would be nice if the mqtt gateway output was "cloned" to the serial output. Controllers should/could be able to map the S_ and V_ types internally. I'm not aware of the openhab config if that would allow this, or that such a setup would require a lot of coding at openhab side.

    It would be more difficult to read though if a path would only exist of numbers. But could also introduce easier maintainability in your library because you wouldn't have to keep track on type namings.

  • Code Contributor

    @John That was my first aproach, and I might set a define configuration to choose if the MQTT gateway should translate or not. As you say, the footprint would be much smaller.
    Might be next version of the MQTT, slectable address layout ๐Ÿ™‚

  • Plugin Developer

    That would be really cool ๐Ÿ™‚

    It would be nice though if there was a way to identify node id's with AUTO turned on. Maybe i'm missing something or do you have to know up front with auto enabled which node id is going to be assigned?

    I'm just asking this because of identifying new/existing nodes in the network. Is it the V_SKETCH_NAME i can refer to?

    And i hope some last questions protocol specific:

    • Version compatibility is 3.1?
    • All session are clean by default?

  • Code Contributor

    @John upon node startup it translates gw.sendSketchInfo("Battery Meter", "1.0"); into MyMQTT/20/255/V_SKETCH_NAME and version

    so you could have a list as in my first post and it would get filled up with all your sketch names

    @John ; Yes and I hope so ๐Ÿ™‚ (I might have missed some stuff, I do not take regard of QOS etc)
    if its not working,, I'll try to fix it ๐Ÿ™‚

  • Plugin Developer

    Thanks for all the info. It becomes more clear now, All nodes users add are persistent anyway.

  • Code Contributor

    @ToSa I'll update mqtt later with option to not translate V_TYPE into name string but to keep it as a byte, that would save a lot of memory and might solve problem you have. I'll keep you posted

  • Hero Member

    As I understand it:


    The packet on the left would be from the node, the packet on the right would be to the node. Besides swapping sender and destination, the difference is that in 1.4 the gateway does not have the information to binary encode the value, so it sends the value as a string (payload type 0).
    EDIT: added alternate MQTT respresentation of V_code as numeric

  • Code Contributor

    @Zeph but soon there will be a choise to translate v_ to text string or keep it as a byte value

  • Hero Member

    At first, I thought MQTT would be a great extension to the functionality, in particular because it might systematize the idea of routing messages to more than one destination - eg: to both a HA controller and to a cloud storage service.

    So I saw these possible advantages of MQTT:

    • Route to multiple destinations, dynamically updated
    • Subscription wildcards to filter for desired subsets
    • Possibly easier to build plug-ins for some HA controllers

    As I look deeper into it, I'm coming to wonder about that.

    • There are lighter weight ways to send to more than one destination
    • Does subscription wildcarding meet the filtering needs well (below)?
    • Is it easier or not?

    I'm wondering what subscription wildcards really get used - how valuable is the type of filtering functionality offered by MQTT for our purposes? The meaingful patterns I would imagine are:

    • everything
    • everything from one node
    • everything from one child of one node
    • everything of type V_RAIN from one child of one node (ie: one variable)
    • everything of type V_RAIN from anywhere

    Are any of these (or other options) in use except "everything"? Do you anticipate that they will be?

    Just as "route to multiple destinations" could be lighter, filtering could be done differently as well. For example, suppose you wanted to receive all report of inside temperature (7 sensors in your network) or humidity (5 sensors), but not outside temp or humidity and not other v codes. That might take 12 subscriptions - and they would have to be manually set up, or make use of manually entered metadata tables in the controller plug-in (eg: the plug-in might be configured to know which nodes had temp and/or humidity, and which ones were considered "inside", if it was written to handled that).

    I could imagine that even if the subscription filtering wasn't highly useful, MQTT might make it easier to create plug-ins for some HA controllers, than using semicolon separated values (serial text format). Is that the case?

    I'm very open to hearing other advantages of MQTT, I'm not against it, just trying to understand the benefits vs cost aspect better.

  • Hero Member

    The other extension that occurs to me, in the gateway-as-transparent-switch model, is that if we ever wanted to encode binary values OTA from the gateway to the node, the plug-in would have to tell the gateway which payload_type to use. In the Semicolon Separted Values format, that could be another parameter, eg:

    <node id>;<child id>;<command>;<ack>;<v_code>;<payload_type>;<value>

    Or it could be added to one of the other fields with a comma sub-delimiter, etc.

    How would a controller plug in tell the MQTT gateway to use payload type P_UNIT16 to send to node 7?

    I'm thinking that it would be done in the value string, rather than encoded into the topic. So maybe the value string goes from "321" to "4,321" or something like that.

  • Plugin Developer

    What happens if a node does not receive it's address when it requests one (because of failed send). It is not registered within the broker?

  • Code Contributor

    @Damme said:

    @ToSa I'll update mqtt later with option to not translate V_TYPE into name string but to keep it as a byte, that would save a lot of memory and might solve problem you have. I'll keep you posted

    I could not follow the discussions the last few days - will definitely have a look over the weekend and let you know if that worked. Thanks!

  • Plugin Developer

    When the MQTT gateway prints information about itself like "started", is this done with node id 0 or 255?

  • Code Contributor

    @John 'started' is only a internal log message, I've changed how that part works.

    I've also fixed a couple of bugs and will test the changes tomorrow before I publish them.
    New functionallity there user can choose of MQTT should translate ID to V_TYPE or not too.

  • Plugin Developer

    Ok, my plugin will then just try to reconnect.

    Ok, i will check that then probably the day after tomorrow, i will make my code take no translation as first and if not found try the naming (internally), because the server takes over the semantics anyway because it also is going to include a MQTT broker service.

    Currently the MQTT implementation is being tested with an end user. Maybe he will help test the new possibility without V_TYPE translation when your changes are implemented.

  • Plugin Developer

    When does exactly the messages like "MyMQTT/[NODEID]/255/V_SKETCH_NAME" and/or "MyMQTT/[NODEID]/255/V_SKETCH_VERSION" appear? is this only when an address is assigned or always when a node is started?

  • Code Contributor

    @John then node is started, There was a bug not processing that message which is fixed now.
    hasn't been pulled to official yet but can be found at

  • Plugin Developer

    Thnx, was struggling with this with a user. Will need to wait for the next release :).

  • @Damme when will the new release be available?

  • Dear colleagues,

    I have been using OpenHAB for a couple of days now, everything is going the right way, I've been using it with mosquitto broker for Windows, everything is solid, working well an fast. Decided to go a step further and use the MQTT Gateway presented here.

    I've made a MQTT Gateway but get the "Not connected to broker" message from OpenHAB. First of all, the Gateway is an arduino uno with a w5100 shield on top in which I inserted the RF module (NRF24L01). Configured my ip in the gateway sketch ( and uploaded to the arduino.

    The first test was to connect with a phone (using MQTT from Playstore - used it many times before) and with a PC (using WMQTT Utility - used it many times before) and things don't look that promising, the problem beeing that client somethimes disconnects, and I cannot receive the message published in a topic (topic that I am subscribed to on the phone, pc and tablet using MQTT clients). I somethimes get the message in the serial monitor of the arduino, somethimes not.

    Configured openhab.cfg to use the mqtt broker at (before I was using mosquitto on pc and working without problems) and everythime I change state of something in the dashboard of openhab I get this Broker Connection not Started even if after starting openhab I get serial output from arduino for some time, after this everything drops. Here is the serial output:serial_output.txt

    Last two entries are: I opened the MQTT client on my phone, connected to MQTT Gateway and subscribed to topic home/# and the last one is me publishing from the phone to topic home/test the message "test message". None of the messages come back to the clients on the phone, tablet, pc, all of them subscribed to home/#

    And here is the output from openhab: openhab_output.txt Ignore the samsung connect stuff, I have pull it off the network, i have managed to get it working well.

    Best regards,


  • Code Contributor

    @adrianmihai83 "home/openHAB/out/Temperature_GF_Corridor/state" is probobly long string, 47 chars. I have not investigated exacly how long it can be but keep it short. And the adress layout is also wrong, is should be something like MyMQTT/21/1/V_LIGHT . MQTTGateway is not compatible with the mosquitto broker you're using.

  • @Damme , your instructions were highly appreciated, got it working the first time. I will start creating the network now, I guess that everything will be ok. Will follow this forum for info and knowledge.

    Now I will migrate openHAB to Rasberry Pi and add all the sensors one by one, there are a few.

    A question though: I am using the humidity sketch and get MyMQTT/0/1/V_TEMP and MyMQTT/0/0/V_HUM reading fine but I cannot get sketch name... I don't know what I am doing wrong... Read above and I guess I have to wait for new version?

    Thank you.


    Edit: Read more about Rasbery Pi capabilities regarding openHAB and I think I will use a cubietruck... Does anybody use one, cubieboard 2 or cubietruck?

  • Code Contributor

    @adrianmihai83 Sketch-name was a bug that I fixed in development branch, thought @hek updated master branch also. I can post the fix later on in here.

  • Maybe a dumb question but I don't figure things, here is my problem:

    Uploaded the relay sketch to the same arduino that I used for humidity/temperature node (I don't know if this is relevant, the network has only one node, this one). Modified the pin that the relay is connected to: #define RELAY_1 4.

    I am using the MQTT Gateway openHAB and cannot activate the relay...

    Serial output of relay sketch is:
    repeater started, id 0
    send: 0-0-0-0 s=255,c=0,t=18,pt=0,l=3,st=ok:1.4
    send: 0-0-0-0 s=255,c=3,t=6,pt=1,l=1,st=ok:0
    send: 0-0-0-0 s=255,c=3,t=11,pt=0,l=5,st=ok:Relay
    send: 0-0-0-0 s=255,c=3,t=12,pt=0,l=3,st=ok:1.0
    send: 0-0-0-0 s=1,c=0,t=3,pt=0,l=3,st=ok:1.4

    Serial output of gateway is:
    0;0;3;0;9;read: 0-0-0 s=255,c=0,t=18,pt=0,l=3:1.4
    0;0;3;0;9;read: 0-0-0 s=255,c=3,t=6,pt=1,l=1:0
    0;0;3;0;9;read: 0-0-0 s=255,c=3,t=11,pt=0,l=5:Relay
    0;0;3;0;9;read: 0-0-0 s=255,c=3,t=12,pt=0,l=3:1.0
    0;0;3;0;9;read: 0-0-0 s=1,c=0,t=3,pt=0,l=3:1.4

    My openHAB configuration is:

    Switch node2_sw1 "sw2" (node2,all) {mqtt=">mysensor:MyMQTT/0/1/V_LIGHT:command:ON:1],>[mysensor:MyMQTT/0/1/V_LIGHT:command:OFF:0]"}

    What am I doing wrong?

    Thank you!

  • If I run the MQTTGateway my temperature shows in fahrenheit when I run the same nodes on a SerialGateway it is showing Celsius why will this be the case?

  • Downloaded from latest MQTT Gateway (the one that responds on serial with "Ok!" not "Started!", with the modifications to MySensors.cpp) but no difference, still no sketch name and with the above settings, openHAB cannot control the relay sensor...

    I used the same arduino that is working with Temp_Humidity sensor so the setup is good, the relay pins are specified in the sketch. I used the Clean_Eeprom_Config before uploading, I even used other Arduino Uno, no diffrerence, still cannot control the relay sensor from openHAB.

    Anyone has a clue, a hint or something? The serial output from both the sensor and the gateway, together with openHAB configuration are two posts up.

    Edit: 3 hours later and managed to make many of the sensors (Door, Distance, Dust, Gas, Humidity, Light, Motion) everyone working in conjunction with MQTT Gateway and openHAB except the Relay one...

    Any help is appreciated.


  • Callback for incomming messages does not occur so I guess that the message does not get to the relay sensor. Is there someone that is using this with MQTT Gateway and openHAB?

  • @adrianmihai83 I was testing this today and it was working with the MQTTGateway. Here is my config file same as above:

    Switch node2_sw2 "Study Lights" (node2,all) { mqtt=">[mysensor:MyMQTT/2/1/V_LIGHT:command:ON:1],>[mysensor:MyMQTT/2/1/V_LIGHT:command:OFF:0],<[mysensor:MyMQTT/2/1/V_LIGHT:command:MAP(]" }

    Here is the info from the serial port monitor.


  • Will test it tomorrow morning and come back with result, what i see is exactly the same configuration, the only difference is that your's is on the 3'rd node and mine on 1'st node. Anyway, will see.

    Thank you for your answer.

    I am still waiting for my other NRF24L01 ( I ordered 30 pcs), they will arive this week. Maybe there is a problem running the sketch on node one, id 0. Tomorrow I will go and buy 2 of the NRF24 from a store even if they are 4 times more expensive, this not working is killing me ๐Ÿ™‚

  • Why is the broker offering me the same id for all of the sensors? I start the first one, broker offers me id 0, without disconnecting it I connect the second sensor and the gateway offers me same id, id=0.

    I have cleared eeprom to both of them and see the requesting id first time i power them up, but still same id for both...

    Edit 1:

    Defined static ID and the relay works, didn't modify anything, just static ID 2 to the relay...

    Edit 2:

    Just tested with ID 0, 1 and 2 and I can confirm that the relay does NOT work with ID 0, it's almost a week now and I have tried everything and the answer was as simple as this: does not work with ID 0.

  • Hero Member

    By ID do you mean node ID? If so, the gateway has node ID 0, so I'm not surprised if it's not good for other nodes.

    But you may be talking about a child ID or something else.

  • Yes, I am talking about node ID, but what I can tell you is that until two days ago I didn't assigned node ID manual and I didn't have 3 NRF's to test with gateway + 2 nodes. Only when I got other RF's and made a couple of sensors saw that the gateway is assigning node ID 0 to all of the sensors, and all sensors worked, for example:

    1. temp humidity node had Node ID 0 and Child 0 and 1, and from openHAB I could use them at MYMQTT/0/0/V_HUM and MYMQTT/0/1/V_TEMP
    2. motion sensor at MYMQTT/0/2/V_MOTION
    3. and do on, other 2 sensors, all I had to take care is that all of them had distinct Child ID

    Only when I got other NRF's and could make sensors and use them in paralel saw that the gateway is offering NODE ID 0 to all of my sensors, I don't know why, checked MYMQTT.h and it was set to auto assign Node ID, even in the statemant gateway.begin() assumes that the gateway should auto assign Node ID, and gw.begin(incomingMessage, AUTO, true) states it clear that relay sensor should get Node ID from the gateway.

    All I did is manual assigned nodes for every one and workking like a charm. I simply like it this way, it let's more control, it's simply more "my way".

    Now I want to combine some of the sensors and use a Mega2560 for this, hoping for a nice outcome.

  • Plugin Developer

    This post is deleted!

  • Code Contributor

    @John Could you send me log from MQTTgateway also? (or tcpdump)

  • Plugin Developer

    This post is deleted!

  • I have a next problem:
    OpenHAB ( <-> Arduino GW mqtt (<-> Arduino TEMP (DHT)

    org.eclipse.paho.client.mqttv3.MqttException: โ•ั… ั”ั„ั€ั…ะ„ั‘ย  ััŽั„ัŠั‹โ– ัžัˆะ„โ„–ั‘ย  ัŠ ั‘ั…ะั‚ั…ะั”
    at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNet
    at org.eclipse.paho.client.mqttv3.internal.ClientComms$
    at Source)
    Caused by: Connection timed out: connect
    at Method)
    at Source)
    at Source)
    at Source)
    at Source)
    at Source)
    at Source)
    at Source)
    at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNet
    at org.eclipse.paho.client.mqttv3.internal.ClientComms$
    at Source)

    with ping everything is ok.

  • Plugin Developer

    This post is deleted!

  • I connected the serial to MQTT GW:
    If I have connected Wiznet, I've got "0;0;3;0;9;check wires",
    if i disconnected Wiznet then got "Started!"

  • Plugin Developer

    You can forget about my posts, after some quite intensive debugging i found out there was an ugly inconsistent race condition going on in my app. Issue is gone now.

  • Could anyone help me with running MQTTGateway with enc28j60 ethernet module? Should a gateway respond to ping? Openhab says 'no route to host'. Should i change anything else than Ethernet.h to UIPEthernet.h in source code ?

  • Code Contributor

    I just wanted to say hello again to everyone. I had to disappear for a while. Just had too much for my brain to handle. So all my automation project were on halt but will be continued again.

    I know there are lot of bugs in this GW and I will hopefullt start developing soon again. Just write your experience you had so far and I'll try to keep up ๐Ÿ™‚


  • Plugin Developer

    @Damme Good luck!

  • @Damme I tried today the mysensor 1.4.1 library, uploaded with Arduino 1.5.8 to UNO with Ethernet Shield (W5100) and an NRF24L01+pa+lna. Compilation and upload worked fine.
    I can ping the IP address defined in the MQTTGateway.ino sketch.
    I maintained on my RPI the openhab.cfg file with the url and port number from the gateway.
    When starting the openhab it shows:
    ... - MQTT Service initialization completed.
    ... - Starting MQTT broker connection 'mysensor'.

    Then a couple of message later:
    ...Mqtt Exception
    Caused by: connect timed out

    Any suggestion what I could look at.

    I also tried to work with the development library of Mysensors but did not get very far with that. My ultimate goal is to something like a moteino with the RFM69HW radio module in connection with either openhab or pidome.
    I have a quite a bit of hardware lying around here so I am happy to do any testing if somebody gives me some guidance.

  • This post is deleted!

  • Thanks for the mqtt broker. Just a tip to people who might fall in to the same pit as I:

    The mqtt broker gateway doesn't distribute node ids unless you're connected to it using a mqtt client. I.e. if you did what I did, installed the gateway and wanted to test that the sensors found the gateway and was distributing the code then it doesn't work until you've connected to it. I had to go through the source code to figure it out.

    If you don't have openhab or simply want to test, this is what I did on my raspberry (but should work similarly on others)

    // install mosquitto client
    sudo apt-get install mosquitto-clients

    // Start the client, replace the with your ip. The port will default to 1883

    mosquitto_sub -h -d -t hello/world -v

    You should be able to see messages:
    Received PUBLISH (d0, q0, r0, m0, 'MyMQTT/20/0/V_LIGHT_LEVEL', ... (2 bytes))

  • Hi All

    Just a simple question:

    Are there any limitation in how many MQTT connections/sessions the MQTT Gateway can handle?

    If so, can this be changed?

  • Hello. I am new to Mysensors, openhab and mosquitto.. I have a working knowledge of arduino and nrf24l01, and currently am running a wireless(nrf) twitter controlled heating scheduler, and am sending multiple temp (ds18b20) sensors to Xively feed.

    I am really hooked on the idea of mqtt and openhab, and i thought that mysensors seemed like a good solution for implementing that idea.

    My progress so far is to install the DallasTemperatureSensor by MySensors node to one nano, and the ethernet gateway sketc as a first step, and now i have installed the mqttgateway again from the build page, and i have a working install of mosquitto and openhab on a rasp pi b+.

    I am just having difficulty getting it all stuck together. I have created the sitemap items and transform files as above and can access the openhab sitemap for the above configuration. i just don't know if i need to configure other items? how can i test each step? and can i use mosquitto with the setup that i have outlined. There seems to be conflicting info about what can and cannot be done with mosquitto and mysensors.

    1 do i need to modify the sketch of the temp node? i have some output from the serial that suggests that it is communicating with the gateway.
    2 do i need to modify the gateway with info of the mosquitto server?
    3 what can i do to test each step of the transit from temp sensor to openhab?

    Thanks for any and all responses.

  • Hello!

    I've downloaded the 1.4.1 version of the library and tested it via OpenHab.
    I'm still not getting the sketch_name correctly via MQTT and Openhab. Is it still an open bug?



  • Hi Simon,
    I had the same problem
    I tried several time clean Eeprom and flash sketch again but with no results
    Only after cleaning of Eeprom with the following scratch my sensor is starting work correctly
    I recommend to try it:
    Clearing EEPROM


  • @mwhansen Hi, I am also struggling with this, seems it can only handle one connection now which makes it extremely hard to debug or check node ids

  • Code Contributor

    @Gambituk To connect to a external MQTT server (mosquitto) use this MQTT gateway version:

  • Thanks @celonunes I eventually found that version of gateway via somebody else. i have another post with some issues i found with that version. it's not really resolved, but i have some kind of workaround.

  • Code Contributor

    @mwhansen @alexeinz The MQTT gateway can handle up to four simultaneous socket connections, this is the limit of Arduino ethernet shield (Wiznet W5100).

    The problem is that openhab don't get along with the way the gateway handles multiple client packages. I will send a fix for this problem in the next few days.

  • @celonunes actually, i figured a way around it using mosquitto bridge
    that way its much more robust too, so I am good ๐Ÿ™‚
    can post my conf if anyone interested.

  • Code Contributor

    Those are the most recent changes to MQTT gateway:

    • Fixed node id distribution by the gateway (reported by @viking)
    • Fixed the problem with openhab and multiple MQTT clients
    • Fixed the appearance of garbage in messages sent from MQTT to a node

  • I can't change on-off manually for button with relay sketch, if i try, connection is lost, switching from screen never works again until restart everything.

    2015-03-05 15:51:29.200 [INFO ] [runtime.busevents ] - node2_sw2 received command OFF
    2015-03-05 15:51:32.624 [INFO ] [runtime.busevents ] - node2_sw2 received command ON

    Openhab 1.6.2 or 1.7.0 same result.

    **My main problem is losing connections with nodes. I also tried serial binding and same result.
    Mysensors+Openhab useless with mqtt and also serial binding

  • Code Contributor

    @C.r.a.z.y. Could you send the serial output from the gateway? And a more complete log from openhab showing errors?

  • Admin

    @C.r.a.z.y. said:

    I can't change on-off manually for button with relay sketch, if i try, connection is lost, switching from screen never works again until restart everything.

    Are you sure it isn't a powering issue of the "button-node" where radio shuts down when you press the button? Are you controlling a physical relay?

  • @celonunes You can find MQTT and serial logs in the zip files openhab 1.6.2 and 1.7.0

    @hek My nodes are working perfect when i only plug serial usb to any usb socket of a computer-printer-usb hub even a mobile phone charger ๐Ÿ™‚ they are good without server/controller

  • @hek My relay powered from nano icp headers maybe this makes trouble for wifi , i will change this and give update.

    My button is not normal button , its an ir distance sensor which trigers the relay.

  • I can't seem to get openhab to communicate with my MQTT gateway. I can connect to the gateway with the MyMQTT Android app and I receive messages from my light sensor node including the sketch name and light level. I can ping my gateway without issue as well.

    However, OpenHab never seems like it notices the gateway. No data in the sketch name or node result. I've used the sitemap, items, and config files as described above and I've also customized them to match my sensor node more specifically. Both without success. The only change I've made is my sensor node is defined as node ID 20 within it's sketch.

    When using start_Debug.bat what should I see in the command line window to indicate OpenHab even sees or connects to the gateway. I see no indication that it attempts to connect to the IP address of my gateway.


  • Maybe if you can share your items, sitemap, and rules if any.

  • can you also please paste a sample of what you see in myMQTT or screenshot maybe? or it can be easier to copy and paste from a terminal window? are you using raspberry pi or something else?

  • @hooraysimpsons unplug the power and restart your modem, check your dhcp menu if it has ip for Ardiuno.

    Check your mqtt from serial monitor, you must see "gateway started..."

  • mosquitto_sub -h localhost -v -t '#'
    This is what i use to see what is happening on mqtt broker, i am using a raspberry pi and mosquitto installed on the same pi. you can also swap localhost for ip address ie etc

  • @C.r.a.z.y. it sounds like he is getting as far as the mqtt broker, so the gateway must be working. ?

  • Crazy- I was thinking my network connection as a source of the problem. On my router homepage the gateway does not show up as being given an IP. However, the MyMQTT Android App sees the gateway without issue so the routing across my network from phone to gateway is successful. I have attached a screenshot of the MyMQTT andoird app output.

    2015-03-08 14.55.12.png

    I am trying to run OpenHab on a windows PC. I don't have mosquitto running at all. My understanding is that it is not required.

  • Group all
    Group node1 (all)
    Group node2 (all)
    Group sketch (all)

     Number node1_light  "Light Level"     (node1,all)  {mqtt="<[mysensor:MyMQTT/20/0/V_LIGHT_LEVEL:state:default]"}

    Try changing the first line of items like this above.. and in sitemap change node1_temp to node1_light

    Also see if activating any of the switches in openhab publishes to myMQTT ?

  • Restarting the modem and checking wires of arduino solved my problem before.
    I am sure mqtt have to be in dhcp list.

  • I have restarted my router with no effect. The gateway still does not show up as an active client (using DDWRT) but I still have a connection from phone to gateway.

    Added the above line. I've used one before like it but went back to what was given at the beggining of this thread to start anew. I also tried with the following edits
    "Light Level"
    "Light Level [%s]"
    "Light Level [%.1f]"

    None of which make a difference. The reason I think OpenHab doesn't even see the gateway is because nothing shows up in sketch name either which should work even if the above line was incorrect.

    I have done the testing with the Android App turned off as well since I know OpenHab had some issues earlier if other clients were accessing the gateway.

    I still see no reference to my gateway's IP in any of the OpenHab output/logs. So I'm not sure it is even trying.

    The only error message in my openhab log is:
    11:23:46.951 [ERROR] [sphere.cpr.AtmosphereFramework:2215 ] -

    I tried clearing the EEPROM in the gateway. I changed it's Mac address and removed the DHCP lease for it from my router. Still no evidence of the gateway on my router status page but also still able to connect to it with my phone.

    Another update:
    I installed Mosquitto and I see the light sensor level using mosquitto_sub -h -t MyMQTT

  • Code Contributor

    @hooraysimpsons Your gateway seems to be working fine. Do you see something like this on your openhab logs?

    2015-02-27 17:48:06.349 [INFO ] [.io.transport.mqtt.MqttService] - MQTT Service initialization completed.
    2015-02-27 17:48:06.350 [INFO ] [o.i.t.m.i.MqttBrokerConnection] - Starting MQTT broker connection 'mysensor'

  • @celonunes Nope. I figured it out. In my openhab config file there was space prior to my MQTT related lines

    " mqtt:mysensor.url=tcp://"
    " mqtt:mysensor.clientId=OpenhabMQTT"
    Instead of

    Removed the space and now I get my light sensor output value. I still don't get the sketch name coming through but I'll look into that later.

  • @celonunes @hek
    I installed this
    pi@raspberrypi ~ $ rpi-serial-console status
    Serial console on /dev/ttyAMA0 is disabled

    Now my nodes are working perfect when i use manual button or ui but my logs are not correct

    My config:

    add on : org.openhab.binding.serial_1.6.0.201411271703.jar
    String Arduino "Arduino" { serial="/dev/ttyUSB0" }

    As you see there are "fails" but nodes are working.

    015-03-09 12:37:51.725 [INFO ] [runtime.busevents ] - Arduino state updated to 0;0;3;0;9;send: 0-0-52-52 s=1,c=1,t=2,pt=0,l=1,st=fail:1

    2015-03-09 12:37:56.726 [INFO ] [runtime.busevents ] - Arduino state updated to 0;0;3;0;9;read: 52-52-0 s=255,c=0,t=18,pt=0,l=5:1.4.1
    0;0;3;0;9;read: 52-52-0 s=255,c=3,t=6,pt=1,l=1:0

    2015-03-09 12:37:58.768 [INFO ] [runtime.busevents ] - Arduino state updated to 0;0;3;0;9;read: 52-52-0 s=255,c=3,t=11,pt=0,l=14:Relay & Button
    52;255;3;0;11;Relay & Button
    0;0;3;0;9;read: 52-52-0 s=255,c=3,t=12,pt=0,l=3:1.0
    0;0;3;0;9;read: 52-52-0 s=1,c=0,t=3,pt=0,l=5:1.4.1
    0;0;3;0;9;read: 52-52-0 s=2,c=0,t=3,pt=0,l=5:1.4.1

    2015-03-09 12:37:59.700 [INFO ] [runtime.busevents ] - S2 received command ON
    2015-03-09 12:37:59.822 [INFO ] [runtime.busevents ] - Arduino received command 52;1;1;0;2;0

    2015-03-09 12:38:00.106 [INFO ] [runtime.busevents ] - Arduino state updated to 0;0;3;0;9;send: 0-0-52-52 s=1,c=1,t=2,pt=0,l=1,st=fail:0

    2015-03-09 12:38:00.546 [INFO ] [runtime.busevents ] - S2 received command OFF
    2015-03-09 12:38:00.661 [INFO ] [runtime.busevents ] - Arduino received command 52;1;1;0;2;1

    2015-03-09 12:38:00.967 [INFO ] [runtime.busevents ] - Arduino state updated to 0;0;3;0;9;send: 0-0-52-52 s=1,c=1,t=2,pt=0,l=1,st=fail:1

    2015-03-09 12:38:01.161 [INFO ] [runtime.busevents ] - S2 received command ON
    2015-03-09 12:38:01.339 [INFO ] [runtime.busevents ] - Arduino received command 52;1;1;0;2;0

    2015-03-09 12:38:01.649 [INFO ] [runtime.busevents ] - Arduino state updated to 0;0;3;0;9;send: 0-0-52-52 s=1,c=1,t=2,pt=0,l=1,st=fail:0

    2015-03-09 12:38:01.782 [INFO ] [runtime.busevents ] - S2 received command OFF
    2015-03-09 12:38:01.894 [INFO ] [runtime.busevents ] - Arduino received command 52;1;1;0;2;1

    2015-03-09 12:38:02.076 [INFO ] [runtime.busevents ] - Arduino state updated to 0;0;3;0;9;send: 0-0-52-52 s=1,c=1,t=2,pt=0,l=1,st=fail:1

  • Just want to make sure I'm understanding this correctly since I feel the MQTT terminology is new to me.

    The MQTT Broker is the MQTT server so if I wanted to use this with openhab on a pi I would just need the MQTT plugin and then point it to the IP of the gateway?

    I have the ethernet module ordered so I can't test myself yet.

  • Code Contributor

    @C.r.a.z.y. You are using the raspberry serial port to connect the openhab with the arduino and the arduino is running the SerialGateway sketch, is that right?

  • Code Contributor

    @Chaotic said:

    The MQTT Broker is the MQTT server so if I wanted to use this with openhab on a pi I would just need the MQTT plugin and then point it to the IP of the gateway?

    That's right.

  • I have ENC28J60-I/SO connected by ethernetcable to my Asus RT-n66u in LAN 4. But It will not show up in my router... On my ENC28J60 there is a red light on D1 and the ethernet port has a green light and a yellow flashing light.

    Serial monitor says "
    0;0;3;0;9;read: 20-20-0 s=255,c=0,t=17,pt=0,l=5:1.4.1
    0;0;3;0;9;read: 20-20-0 s=255,c=3,t=6,pt=1,l=1:0
    0;0;3;0;9;send: 0-0-20-20 s=255,c=3,t=6,pt=0,l=1,st=ok:M
    0;0;3;0;9;read: 20-20-0 s=255,c=3,t=11,pt=0,l=18:Temperature Sensor
    0;0;3;0;9;read: 20-20-0 s=255,c=3,t=12,pt=0,l=3:1.0

    . But nothing more. I have followed the MQTT gateway guide and set an static ip in the sketch.. DHCP is on in my router.

    The only thing I see in my router is : That the cable is connected to something and that its 10 Half duplex.

    I have also tried to connect it to other LAN ports but no difference

    What could be wrong?

  • Code Contributor

    @msebbe Did you try to ping the gateway static IP?

  • @celonunes

    Yes, both from windows cmd and from the router ui with this result:

    PING ( 56 data bytes

    --- ping statistics ---
    5 packets transmitted, 0 packets received, 100% packet loss

    Could be that I have not changed the MAC adress cause I tried to figure out how to do it but I dont understand this part from the MQTT gateway sketch:

    *1 -> NOTE: Keep first byte at x2, x6, xA or xE (replace x with any hex value) for using Local Ranges.

     #define TCP_PORT 1883						// Set your MQTT Broker Listening port.
     IPAddress TCP_IP ( 192, 168, 1, 197 );				// Configure your static ip-address here
     uint8_t TCP_MAC[] = { 0x02, 0xDE, 0xAD, 0x00, 0x00, 0x42 };	// Mac-address - You should change this! see note *2 above!

  • Code Contributor

    @msebbe Which library your are using for the ENC28J60? The UIPEthernet has some examples you could try to test if your module is working.

  • @celonunes
    Just wanted to say I was able to get this working.

    Also found a nice little MQTT utility that makes it easy to see the messages being sent from the broker/openhab.

    I'm sure I'll have more questions but most of my hardware is still on a boat from china so will be a little while till I discover them.

  • @celonunes In the Mqtt broker sketch, Ethernet.h was included. I suppose it should be UIPEthernet.h for me? So I tried that but nothing.

    I had better luck with the UIPEthernet examples, there my arduino shows up with an ip and mac adress.. Hmmm.


    So after using UIPEthernet "Advanced chat server" sketch, the MAC and IP showed up in my router. When I then tried with the MQTT sketch with UIPEthernet.h it would not work. So then I tried to copy the MAC and IP I used in the chat server sketch into the Mqtt sketch and bom! I can now ping it, so I guess it works now?

    edit2: Should it say anything more then "Started!" on serial monitor? Like "on ip" or something like that? Since Im now trying the MQTT in pidome as I know how to use pidome better. Pidome states;
    Connected: Yes, with at topic: MyMQTT/# (without subscribe promise)
    Last receive time: 00-00-0000 00:00
    Last send time: 00-00-0000 00:00

    This is what Serial monitor says

    But when I use device discovery it cannot find the temperature sensor that could be found using the serial gateway ๐Ÿ˜•

  • Hi everyone,

    Finally got this working but had a couple of quetions

    Does the MQTT Broker gateway not handle requests for time? I currently have a node that I'm using as an alarm clock which has a RTC module. I had hoped to be able to update the time via the gateway and polling the time from one of my computers which are connected to NTP servers but the message never seems to get put on a MQTT message so I can't have a computer respond to it.

  • Code Contributor

    @msebbe It doesn't print your ip, those numbers after "Started!" is related to mqtt packages showing that PiDome is connected.
    For the device discovery, there are probably a lot of other people who can help you with that because I don't know much about PiDome.

  • Code Contributor

    @Chaotic MQTT Broker gateway doesn't handle time requests.

  • @celonunes Ok, thanks! I got the discovery working by just rebuilding the same sensor from scratch!

    Sorry for so many questions.. But I have now changed to Openhab and got it working with my MQTT gateway. Now Another problem appear:[/IMG

    I would like to display the temperature in ยฐC like in your guide sketch (wich I am using) but instead it looks like in the picture. Same thing about humidity, why is it like this?

    Edit: For anyone wondering, I solved the issue with the degree symbol not showing up properly by replacing "ยฐC" with "& deg;C" without space between & deg

  • Hi Folks,

    New to mysensors and am impressed. I've been playing with OpenHAB and MQTT so this sounds like an interesting project. My question though, which might have been answered but I'm finding it hard to find the answer, is - is this bidirectional? As in if I had a 'sensor' on the Arduino network and that went over the radio network, hit the gateway and then got picked up by OpenHAB, could I then, if I had a 'actuator' or a 'do-er' node on the network, send a message to MQTT from OpenHAB to change the status of that actuator device? So for instance to start a motor controlling blinds for instance? Or is it a read only system?


  • @Brett-Jenkins

    It works both ways.

  • Thanks ๐Ÿ™‚

  • How can I implement MQTT for MQ-2 Gas sensor the correct way? In the mysensor sketch there is only one child ID and one V_VAR1 but 3 different outputs: LPG, CO and Smoke? I'm only receiving CO value and not all 3 in openhab.

  • @kunall There is V_VAR1-5 actually

    V_VAR1	24	Custom value
    V_VAR2	25	Custom value
    V_VAR3	26	Custom value
    V_VAR4	27	Custom value
    V_VAR5	28	Custom value

    Personally I would set up each value you want as a S_CUSTOM if you can't figure out another sensor type that fits.

    For smoke you can use S_SMOKE
    For Air Quality S_AIR_QUALITY
    Not sure what you'd want to use for CO2 through

  • @Chaotic Thanks! I will try this and get back with result.

  • @Damme
    how to run MySensors ( MQTT gateway) on the arduino due + Wiznet5100 + NRf24l01?
    Get the error:

    Arduino: 1.5.8 (Windows 8), Board: "Arduino Due (Programming Port)"
    In file included from Z:\arduino-1.5.8-new\libraries\DigitalIO/DigitalIO.h:31:0,
                     from EthernetGateway.ino:41:
    Z:\arduino-1.5.8-new\libraries\DigitalIO/DigitalPin.h:30:20: fatal error: avr/io.h: No such file or directory
     #include <avr/io.h>
    compilation terminated.
    ะžัˆะธะฑะบะฐ ะบะพะผะฟะธะปัั†ะธะธ.
      This report would have more information with
      "Show verbose output during compilation"
      enabled in File > Preferences.

  • @hooraysimpsons this solved my problem too! Everything else in my setup was a quick job but problem solving this issue took half a day. It should be more widely circulated..

Log in to reply

Suggested Topics

  • 6
  • 5
  • 27
  • 48
  • 4
  • 18
  • 75
  • 1