💬 Building a MQTT Gateway
-
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
-
@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.
@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 1and 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 = 154If 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.
-
No, you're right, it doesn't solve your issue. It does increase the availability of the main loop though.
@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? -
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 usesetSocketTimeout(1)On the pubsubclient object.
@electrik I've put the
#define MQTT_SOCKET_TIMEOUT 1in the beginning where all other #defined constants are and it does not seeams to take this config.
I tried to usesetSocketTimeout(1)but I can't seems to get the sketch to work with <PubSubClient.h>
I've addedEthernetClient 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 typeany ideas?
-
@linkinpio said in 💬 Building a MQTT Gateway:
client.setSocketTimeout(1);
That is the right syntax, but you have to move the line in setup()
-
@linkinpio said in 💬 Building a MQTT Gateway:
client.setSocketTimeout(1);
That is the right syntax, but you have to move the line in setup()
@electrik
tried it but also no luckC:\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'``` -
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 -
I think I'm ussing the latest one

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(); } }