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. Announcements
  3. 💬 Building a MQTT Gateway

💬 Building a MQTT Gateway

Scheduled Pinned Locked Moved Announcements
81 Posts 30 Posters 13.9k Views 28 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.
  • L linkinpio

    Hello,
    I'm new here so sorry if this is not the right place for this question.
    But I am trying to build Arduino MQTT GW to act as a relay/light controller and report/interact with HomeAssistant.
    I think I have worked out most of my problems and got it running as I wanted to, but I was testing failover senarios as the controller should be able to work independently of any other device as long it has power.
    My config is kind of simple Arduino Mega with W5500 Ethernet module configured as MQTT GW
    And the problem is that when network or HA/MQTT broker is unavailable when Arduino is restarted/booting it tries to reconnect to MQTT broker constantly and interupts the main loop everytime for around 3s which makes board basically unresponsive.

    00:45:07.094 -> 549536 !GWT:RMQ:FAIL
    00:45:07.094 -> Loop count = 179
    00:45:07.094 -> 549538 GWT:TPC:IP=192.168.11.111
    00:45:08.107 -> 550542 GWT:RMQ:CONNECTING...
    00:45:10.130 -> 552547 !GWT:RMQ:FAIL
    00:45:10.130 -> Loop count = 180
    00:45:10.130 -> 552550 GWT:TPC:IP=192.168.11.111

    Any ideas how I could disable reconnect function if that happens for example after 3 attempts? So the controller can work normally even if there is no connection to MQTT broker?

    OldSurferDudeO Offline
    OldSurferDudeO Offline
    OldSurferDude
    wrote on last edited by
    #70

    @linkinpio

    I don't have a solution, but I, too, may run into that problem. So I'm curious how it can be resolved.

    Perhaps post in the Hardware or Development sections. Fortunately, some really knowledgeable people (eg. @mfalkvidd and @NeverDie ) monitor pretty much all of the forum subjects.

    OSD

    mfalkviddM 1 Reply Last reply
    0
    • L linkinpio

      Hello,
      I'm new here so sorry if this is not the right place for this question.
      But I am trying to build Arduino MQTT GW to act as a relay/light controller and report/interact with HomeAssistant.
      I think I have worked out most of my problems and got it running as I wanted to, but I was testing failover senarios as the controller should be able to work independently of any other device as long it has power.
      My config is kind of simple Arduino Mega with W5500 Ethernet module configured as MQTT GW
      And the problem is that when network or HA/MQTT broker is unavailable when Arduino is restarted/booting it tries to reconnect to MQTT broker constantly and interupts the main loop everytime for around 3s which makes board basically unresponsive.

      00:45:07.094 -> 549536 !GWT:RMQ:FAIL
      00:45:07.094 -> Loop count = 179
      00:45:07.094 -> 549538 GWT:TPC:IP=192.168.11.111
      00:45:08.107 -> 550542 GWT:RMQ:CONNECTING...
      00:45:10.130 -> 552547 !GWT:RMQ:FAIL
      00:45:10.130 -> Loop count = 180
      00:45:10.130 -> 552550 GWT:TPC:IP=192.168.11.111

      Any ideas how I could disable reconnect function if that happens for example after 3 attempts? So the controller can work normally even if there is no connection to MQTT broker?

      electrikE Offline
      electrikE Offline
      electrik
      wrote on last edited by
      #71

      @linkinpio in the pubsubclient library, you can change the timeout on connecting to the mqtt broker. Default it is 15s, reducing it to e.g. 2s will increase the availability of the main loop.

      L 1 Reply Last reply
      1
      • OldSurferDudeO OldSurferDude

        @linkinpio

        I don't have a solution, but I, too, may run into that problem. So I'm curious how it can be resolved.

        Perhaps post in the Hardware or Development sections. Fortunately, some really knowledgeable people (eg. @mfalkvidd and @NeverDie ) monitor pretty much all of the forum subjects.

        OSD

        mfalkviddM Online
        mfalkviddM Online
        mfalkvidd
        Mod
        wrote on last edited by mfalkvidd
        #72

        @OldSurferDude yes I have read it already. But unfortunately my impression is that my knowledge of how the code works is woefully insufficient.

        1 Reply Last reply
        0
        • electrikE electrik

          @linkinpio in the pubsubclient library, you can change the timeout on connecting to the mqtt broker. Default it is 15s, reducing it to e.g. 2s will increase the availability of the main loop.

          L Offline
          L Offline
          linkinpio
          wrote on last edited by
          #73

          @electrik thank you for your suggestion, but are you talking about MQTT_SOCKET_TIMEOUT ?
          If yes I did set this to 1s

          #define MQTT_SOCKET_TIMEOUT 1
          

          and did not change a thing

          21:32:05.081 -> Loop count = 153
          21:32:05.081 -> 471258 GWT:TPC:IP=192.168.11.111
          21:32:06.098 -> 472262 GWT:RMQ:CONNECTING...
          21:32:08.071 -> 474266 !GWT:RMQ:FAIL
          21:32:08.071 -> Loop count = 154
          

          If not please do let me know what you had in mind so I can check it out.

          @OldSurferDude thanks for confirming that I have not missed anything when doing a resarch on this matter, if there will be no new suggestions I think I might have to post it to Development as dont see anyone suggesting any good workaround or a solution.

          1 Reply Last reply
          0
          • electrikE Offline
            electrikE Offline
            electrik
            wrote on last edited by
            #74

            No, you're right, it doesn't solve your issue. It does increase the availability of the main loop though.

            L 1 Reply Last reply
            0
            • electrikE electrik

              No, you're right, it doesn't solve your issue. It does increase the availability of the main loop though.

              L Offline
              L Offline
              linkinpio
              wrote on last edited by
              #75

              @electrik
              Have you tested this in your enviroment/sketch?
              Did it increase availability for your sketch?
              If yes what was the single loop time before and after changing this?
              I did test is on my example and it does not increase the availability for the main loop for my sketch, also you can see the printout from the serial monitor, the loop is run in the same 3s intervals like before, so maybe I am doing something wrong?

              1 Reply Last reply
              0
              • electrikE Offline
                electrikE Offline
                electrik
                wrote on last edited by
                #76

                Yes, that's what I meant. But the value seems to be 2 seconds in your log. Where have you placed the define?
                You can also use

                setSocketTimeout(1)
                

                On the pubsubclient object.

                L 1 Reply Last reply
                0
                • electrikE electrik

                  Yes, that's what I meant. But the value seems to be 2 seconds in your log. Where have you placed the define?
                  You can also use

                  setSocketTimeout(1)
                  

                  On the pubsubclient object.

                  L Offline
                  L Offline
                  linkinpio
                  wrote on last edited by
                  #77

                  @electrik I've put the

                  #define MQTT_SOCKET_TIMEOUT 1
                  

                  in the beginning where all other #defined constants are and it does not seeams to take this config.
                  I tried to use

                  setSocketTimeout(1)
                  

                  but I can't seems to get the sketch to work with <PubSubClient.h>
                  I've added

                  EthernetClient ethClient;
                  PubSubClient client(ethClient);
                  client.setSocketTimeout(1);
                  

                  but getting following error.

                  GatewayW5x00MQTTClient.ino:172:1: error: 'client' does not name a type
                   client.setSocketTimeout(1);
                   ^~~~~~
                  
                  exit status 1
                  
                  Compilation error: 'client' does not name a type
                  

                  any ideas?

                  1 Reply Last reply
                  0
                  • electrikE Offline
                    electrikE Offline
                    electrik
                    wrote on last edited by
                    #78

                    @linkinpio said in 💬 Building a MQTT Gateway:

                    client.setSocketTimeout(1);

                    That is the right syntax, but you have to move the line in setup()

                    L 1 Reply Last reply
                    0
                    • electrikE electrik

                      @linkinpio said in 💬 Building a MQTT Gateway:

                      client.setSocketTimeout(1);

                      That is the right syntax, but you have to move the line in setup()

                      L Offline
                      L Offline
                      linkinpio
                      wrote on last edited by
                      #79

                      @electrik
                      tried it but also no luck

                      C:\Users\p.\Documents\Arduino\GatewayW5x00MQTTClient\GatewayW5x00MQTTClient.ino: In function 'void setup()':
                      C:\Users\p.\Documents\Arduino\GatewayW5x00MQTTClient\GatewayW5x00MQTTClient.ino:213:10: error: 'class PubSubClient' has no member named 'setSocketTimeout'
                         client.setSocketTimeout(1);
                                ^~~~~~~~~~~~~~~~
                      
                      exit status 1
                      
                      Compilation error: 'class PubSubClient' has no member named 'setSocketTimeout'```
                      1 Reply Last reply
                      0
                      • electrikE Offline
                        electrikE Offline
                        electrik
                        wrote on last edited by
                        #80

                        This looks like you're not using the latest (development) version of the mysensors library. That has the latest pubsubclient library included.
                        Looking at the intervals between the errors again, I'm doubtful if it will make a big difference though. The mysensors library will need to be modified to fulfill your request I guess

                        1 Reply Last reply
                        0
                        • L Offline
                          L Offline
                          linkinpio
                          wrote on last edited by
                          #81

                          I think I'm ussing the latest one
                          d8876975-0cfc-4787-b700-255b1eccec88-image.png
                          but as you said I don't think it will fix my issue though.
                          But looking at the examples I found that they have a connect function which then can be used to reconnect whenever needed not constantly like it happens now,

                          But not sure how I could integrate that functionality in this sketch though, as I'm not that experienced with programming :/
                          To you think it will be possible?

                          /*
                           Reconnecting MQTT example - non-blocking
                          
                           This sketch demonstrates how to keep the client connected
                           using a non-blocking reconnect function. If the client loses
                           its connection, it attempts to reconnect every 5 seconds
                           without blocking the main loop.
                          
                          */
                          
                          #include <SPI.h>
                          #include <Ethernet.h>
                          #include <PubSubClient.h>
                          
                          // Update these with values suitable for your hardware/network.
                          byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
                          IPAddress ip(172, 16, 0, 100);
                          IPAddress server(172, 16, 0, 2);
                          
                          void callback(char* topic, byte* payload, unsigned int length) {
                            // handle message arrived
                          }
                          
                          EthernetClient ethClient;
                          PubSubClient client(ethClient);
                          
                          long lastReconnectAttempt = 0;
                          
                          boolean reconnect() {
                            if (client.connect("arduinoClient")) {
                              // Once connected, publish an announcement...
                              client.publish("outTopic","hello world");
                              // ... and resubscribe
                              client.subscribe("inTopic");
                            }
                            return client.connected();
                          }
                          
                          void setup()
                          {
                            client.setServer(server, 1883);
                            client.setCallback(callback);
                          
                            Ethernet.begin(mac, ip);
                            delay(1500);
                            lastReconnectAttempt = 0;
                          }
                          
                          
                          void loop()
                          {
                            if (!client.connected()) {
                              long now = millis();
                              if (now - lastReconnectAttempt > 5000) {
                                lastReconnectAttempt = now;
                                // Attempt to reconnect
                                if (reconnect()) {
                                  lastReconnectAttempt = 0;
                                }
                              }
                            } else {
                              // Client connected
                          
                              client.loop();
                            }
                          
                          }
                          
                          
                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          14

                          Online

                          11.7k

                          Users

                          11.2k

                          Topics

                          113.0k

                          Posts


                          Copyright 2019 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