Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. Help getting started with MQTT gateway

Help getting started with MQTT gateway

Scheduled Pinned Locked Moved Development
22 Posts 2 Posters 7.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • hekH hek

    (this post currently applies to the development branch)

    You can run the MQTT gateway on an Arduino+W5100 ethernet module or the ESP8266. Connect radio and ethernet module exactly like for the normal gateway.

    Download and install the MySensors development branch

    The use one of the following sketches depending on the hardware you use:
    https://github.com/mysensors/Arduino/blob/development/libraries/MySensors/examples/GatewayW5100MQTTClient/GatewayW5100MQTTClient.ino
    or
    https://github.com/mysensors/Arduino/blob/development/libraries/MySensors/examples/GatewayESP8266MQTTClient/GatewayESP8266MQTTClient.ino

    The topic resembles the serial protocol. You must define you preferred subscribe and publish prefix in the sketch. The topic is build like this:
    MY_MQTT_PUBLISH_TOPIC_PREFIX/FROM-NODE-ID/SENSOR-ID/CMD-TYPE/ACK-FLAG/SUB-TYPE

    Read more about the serial protocol here. An example topic for data received from your gateway could look like this:

    mygateway1-out/2/1/1/0/49

    If you want to send data to your sensors use MY_MQTT_SUBSCRIBE_TOPIC_PREFIX defined in your sketch. Should be looking like this (using default sketch):

    mygateway1-in/2/1/1/0/49

    You can test your gateway using mosquitto as broker.

    Make sure to set MY_IP_GATEWAY_ADDRESS in gateway pointing to you computers ip-number (on the same lan).

    Start the broker (leave this running):
    > mosquitto

    Now start your gateway and you should see something like this in the gateway log. Note that I have a GPS sensor running in my radio network which send data to the gateway all the time:

    0;0;3;0;9;gateway started, id=0, parent=0, distance=0
    0;0;3;0;9;Attempting MQTT connection...
    0;0;3;0;9;MQTT connected
    0;0;3;0;9;read: 2-2-0 s=1,c=1,t=49,pt=0,l=22,sg=0:55.722519;13.018120
    0;0;3;0;9;Sending message on topic: mygateway1-out/2/1/1/0/49
    0;0;3;0;9;read: 2-2-0 s=1,c=1,t=49,pt=0,l=22,sg=0:55.722519;13.018121
    0;0;3;0;9;Sending message on topic: mygateway1-out/2/1/1/0/49
    

    Subscribe to messages (in another shell on your computer)
    > mosquitto_sub -v -t 'mygateway1-out/#'

    You should see the messages like you saw in the gateway-log showing up like this:

    mygateway1-out/2/1/1/0/49 55.722519;13.018121;13
    mygateway1-out/2/1/1/0/49 55.722519;13.018114;12
    mygateway1-out/2/1/1/0/49 55.722527;13.018120;11
    mygateway1-out/2/1/1/0/49 55.722534;13.018122;10
    

    Sending (publishing) messages to your sensor network:
    > mosquitto_pub -t 'mygateway1-in/2/1/1/0/49' -m '0,29'

    S Offline
    S Offline
    Samuel235
    Hardware Contributor
    wrote on last edited by Samuel235
    #2

    @hek I've changed the port number and MY_IP_GATEWAY_ADDRESS to the address of the RaspberryPi running Mosquitto. I've set my static IP, router gateway and subnet as follows in the sketch, uploaded it to the arduino and its not showing up in the subscribed topic of mosquitto_sub -v -t '#' when i restart the gateway.

    // Enable MY_IP_ADDRESS here if you want a static ip address (no DHCP)
    #define MY_IP_ADDRESS 192,168,0,85
    
    // 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      
    

    I am able to ping the address of the arduino, so i know the connection of the arduino is, somewhat, correct.

    Is there anything silly that i'm doing wrong here? I've tried changing:

    // W5100 Ethernet module SPI enable (optional if using a shield/module that manages SPI_EN signal)
    //#define MY_W5100_SPI_EN 4
    

    but that didn't work either. I'm struggling to see anything else that would be needed apart from the address for the broker itself.

    Edit: Would it just be better for me to run the Gateway as a Broker itself rather than as a client connecting to the broker on the raspberryPi?

    1 Reply Last reply
    0
    • hekH hek

      @samuel235

      I didn't bother trying. The ENC-library is gigantic and the MQTT implementation used does not support the ENC-module.

      S Offline
      S Offline
      Samuel235
      Hardware Contributor
      wrote on last edited by
      #3

      I have my controller running openHAB and mosquitto perfect now, my MQTTGateway is working too. I've made a humidity sensor node with the sketch in the library folder, its passing (null) on the specified topic.

      I'm guessing that the (null) means that there is no message in the communication. Am i correct in assuming this?

      hekH 1 Reply Last reply
      0
      • S Samuel235

        I have my controller running openHAB and mosquitto perfect now, my MQTTGateway is working too. I've made a humidity sensor node with the sketch in the library folder, its passing (null) on the specified topic.

        I'm guessing that the (null) means that there is no message in the communication. Am i correct in assuming this?

        hekH Offline
        hekH Offline
        hek
        Admin
        wrote on last edited by
        #4

        @samuel235 said:

        I'm guessing that the (null) means that there is no message in the communication. Am i correct in assuming this?

        Some messages has no payload. But humidity/set should have one.

        S 1 Reply Last reply
        0
        • hekH hek

          @samuel235 said:

          I'm guessing that the (null) means that there is no message in the communication. Am i correct in assuming this?

          Some messages has no payload. But humidity/set should have one.

          S Offline
          S Offline
          Samuel235
          Hardware Contributor
          wrote on last edited by Samuel235
          #5
          send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,sg=0,st=ok:
          H: 41.90
          req id
          send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,sg=0,st=ok:
          T: 24.70
          req id
          send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,sg=0,st=ok:
          H: 42.00
          req id
          send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,sg=0,st=ok:
          H: 41.80
          req id
          send: 255-255-0-0 s=255,c=3,t=3,pt=0,l=0,sg=0,st=ok:
          T: 24.80
          req id
          

          This is just a snippet from my Humidity sensor, and there is nothing sitting at the end of the send line, so i'm guessing the sensor node is not releasing a value for humidity.

          1 Reply Last reply
          0
          • hekH Offline
            hekH Offline
            hek
            Admin
            wrote on last edited by hek
            #6

            The node is requesting an id from the controller. "req id"...

            So you either have to answer this request (through mqtt) or set a static id on the node.

            #define MY_NODE_ID 42

            S 1 Reply Last reply
            0
            • hekH hek

              The node is requesting an id from the controller. "req id"...

              So you either have to answer this request (through mqtt) or set a static id on the node.

              #define MY_NODE_ID 42

              S Offline
              S Offline
              Samuel235
              Hardware Contributor
              wrote on last edited by
              #7

              @hek I wondered why i was getting 255-255 everywhere. Does this have anything to do with that?

              By the way, i assumed that all the configs were setup to handout random ID's, i'm sure i kept reading "automatic ID assign" when i was editing configs for the radios....

              1 Reply Last reply
              0
              • hekH Offline
                hekH Offline
                hek
                Admin
                wrote on last edited by
                #8

                The node uses id 255 temporarily while asking for an id.

                It's automatic if you're controller supports it. But as this mqtt topic layout is quite new, I doubt many controllers support it.

                The previous (1.5) MQTT gateway did some handouting on its own which wasn't optimal (stored last used id in EEPROM resulting in no control/reuse of ids).

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Samuel235
                  Hardware Contributor
                  wrote on last edited by
                  #9

                  Ahhh right okay, well this is what i kept reading in 'MyConfig.h':

                  // Node id defaults to AUTO (tries to fetch id from controller)
                  #ifndef MY_NODE_ID
                  #define MY_NODE_ID AUTO
                  #endif
                  

                  However, i shall manually set the ID on my nodes now.

                  I have put '#define MY_NODE_ID 42' into the humidity sketch, that isn't working, is there a specific place it needs to be? I'm just looking through the config files to see where about in the sendMessage code it calls for it and how it calls for it. I've tried to comment those lines out so it doesn't even attempt to get a ID from the controller, that isn't working at the moment.

                  1 Reply Last reply
                  0
                  • hekH Offline
                    hekH Offline
                    hek
                    Admin
                    wrote on last edited by
                    #10

                    @samuel235 said:

                    is there a specific place it needs to be?

                    Add it before the inclusion of MySensor.h

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      Samuel235
                      Hardware Contributor
                      wrote on last edited by
                      #11

                      I tried what you advised but it wasn't asigning a ID to the node still. So after a little looking around i found that you can specify the node id within gw.begin().

                      So i have got it as gw.begin(NULL, 4, false).

                      While i understand that the second piece of data is the node id, and the third is whether or not it goes into sleep mode. What is the first? Also, with me calling false for the sleep mode and then at the end of the sketch it tells the node to sleep for 30 seconds, am i over riding that sleep mode that i call at the end?

                      1 Reply Last reply
                      0
                      • hekH Offline
                        hekH Offline
                        hek
                        Admin
                        wrote on last edited by
                        #12

                        Ah.. you're running mixed library versions on node/gateway.. Yes, then you'd need to call gw.begin() on the node.

                        S 1 Reply Last reply
                        0
                        • hekH hek

                          Ah.. you're running mixed library versions on node/gateway.. Yes, then you'd need to call gw.begin() on the node.

                          S Offline
                          S Offline
                          Samuel235
                          Hardware Contributor
                          wrote on last edited by
                          #13

                          But will this overwrite any config/settings like sleep or anything? What does the NULL mean at the start of gw.begin?

                          1 Reply Last reply
                          0
                          • hekH Offline
                            hekH Offline
                            hek
                            Admin
                            wrote on last edited by
                            #14

                            I'm splitting this discussion to a new topic.

                            1 Reply Last reply
                            0
                            • hekH Offline
                              hekH Offline
                              hek
                              Admin
                              wrote on last edited by
                              #15

                              The 1.5 API/begin() is documented here:
                              http://www.mysensors.org/download/sensor_api_15#the-full-api

                              First argument is callback-function used if you're interested in incoming data. NULL won't overwrite any configuration.

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                Samuel235
                                Hardware Contributor
                                wrote on last edited by
                                #16

                                I'm sorry, i got confused about which version i was running. I thought it was the development humidity sketch and therefor i didn't look for the API of it. Makes sense now. Thank you.

                                1 Reply Last reply
                                0
                                • S Offline
                                  S Offline
                                  Samuel235
                                  Hardware Contributor
                                  wrote on last edited by Samuel235
                                  #17

                                  I now have my controller, broker and gateway setup working fine. I then created a temp and humidity sensor, works fine. I then made a motion node, works fine. Then a light switch node that all work perfectly fine together. Now, i come to configure my relay module (which im struggling with getting it to receive the messages) so i open my serial monitor for the gateway and see that even though its got a solid connection, it keeps trying to make another MQTT connection. Over and over again....

                                  0;0;3;0;9;Attempting MQTT connection...
                                  0;0;3;0;9;MQTT connected
                                  0;0;3;0;9;Attempting MQTT connection...
                                  0;0;3;0;9;MQTT connected
                                  0;0;3;0;9;Attempting MQTT connection...
                                  0;0;3;0;9;MQTT connected
                                  0;0;3;0;9;Attempting MQTT connection...
                                  0;0;3;0;9;MQTT connected
                                  0;0;3;0;9;Attempting MQTT connection...
                                  0;0;3;0;9;MQTT connected
                                  

                                  There is nothing in the loop section of the sketch, so all i can assume is that it keeps dropping its connection to mosquitto broker thats wired over ethernet?

                                  1 Reply Last reply
                                  0
                                  • hekH Offline
                                    hekH Offline
                                    hek
                                    Admin
                                    wrote on last edited by
                                    #18

                                    Strange.. And the log on the relay node?

                                    S 1 Reply Last reply
                                    0
                                    • hekH hek

                                      Strange.. And the log on the relay node?

                                      S Offline
                                      S Offline
                                      Samuel235
                                      Hardware Contributor
                                      wrote on last edited by
                                      #19
                                      This post is deleted!
                                      1 Reply Last reply
                                      0
                                      • S Offline
                                        S Offline
                                        Samuel235
                                        Hardware Contributor
                                        wrote on last edited by Samuel235
                                        #20
                                        send: 4-4-0-0 s=255,c=0,t=18,pt=0,l=5,sg=0,st=fail:1.5.1
                                        send: 4-4-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=fail:0
                                        repeater started, id=4, parent=0, distance=1
                                        send: 4-4-0-0 s=255,c=3,t=11,pt=0,l=5,sg=0,st=fail:Relay
                                        send: 4-4-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,st=fail:1.0
                                        send: 4-4-0-0 s=1,c=0,t=3,pt=0,l=0,sg=0,st=fail:
                                        
                                        1 Reply Last reply
                                        0
                                        • S Offline
                                          S Offline
                                          Samuel235
                                          Hardware Contributor
                                          wrote on last edited by Samuel235
                                          #21

                                          Just to let you know (Not sure if it tells you that i have edited a post), i have removed what i said about the gateway not working, that is back online now, however its back to doing exactly what it was doing earlier and keep trying to connect. But literally its spamming at a rate of like 10/20 per second...

                                          0;0;3;0;9;Attempting MQTT connection...
                                          0;0;3;0;9;MQTT connected
                                          0;0;3;0;9;Attempting MQTT connection...
                                          0;0;3;0;9;MQTT connected
                                          

                                          I have a weird feeling that there may be a power issue or a connection issue somewhere on this gateway. (Its in a cupboard since i got it working this morning so its a little hard for me to believe it).

                                          I have tried a restart on the MQTT broker btw, still nothing.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          13

                                          Online

                                          11.7k

                                          Users

                                          11.2k

                                          Topics

                                          113.1k

                                          Posts


                                          Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • MySensors
                                          • OpenHardware.io
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular