Current Lua downloadable firmware will be posted here

User avatar
By madarax64
#21324 Hello El42,
Okay, I'm glad to hear it worked for you. In effect, all you need do is call the loop() function on your MQTTClient instance regularly. This is necessary to "service" the MQTT library routines in the background, which maintain your MQTT connection. In the sample code I provided, this is done by saying
Code: Select allclient.loop();
In essence, you could write your program like this:

Code: Select all#include <ESP8266WiFi.h>
#include <PubSubClient.h>

...// declare SSID, password, etc etc
long lastPublishTime = 0L; //variable keeping track of the last time (since powered up, [milliseconds]) a publish was made
long interval = 60000L; //60 seconds, expressed in milliseconds
PubSubClient client(<server>);
...//other variables and stuff

void setup() {
//connect to WiFi, and do other init tasks here - like sample code
}

void loop() {
 if ((millis() - lastPublishTime) >= interval) {
          //interval has elapsed, send message to topic
          client.publish("topic","message");
          lastPublishTime = millis(); //update the last publish time
    }

client.loop(); //service MQTT loop to maintain connection

}//end loop function



The millis() function returns the amount of time the device has been powered up for, in milliseconds. So what we're doing here is that we're using the millis() function (whose return value is ever-increasing with time) and a tracking variable. When the difference between the current time (as reported by the millis() function) and the last publish time (contained in the lastPublishTime variable) becomes greater than or equal to the update interval (60 seconds), a publish is then made to the specified topic. The last publish time is then updated with the current time. Therefore, until the difference between the two is up to 60000 (meaning 60 seconds) no other publish is made. Once the difference is large enough, a publish is made, and then the tracking variable (lastPublishTime) is updated to allow for another run.
However, you'll notice that whether or not the publish is made, the client.loop() function is called. This is to "service" the MQTT engine provided the library and is absolutely necessary to keep your MQTT client working/connected/happy. The way you were doing it before, the client.loop() function wasn't being called at all, so the MQTT engine was not able to function long beyond the initial time it was connected.

Ps:
Also, try to keep the actions in your main Arduino loop() function as short as possible, so that loop() "ends" quickly enough for the ESP itself to service the background WiFi engine. If that can't be helped, you can add calls to the delay() function within the long-running code if you can manage that, or the yield() function, which is like delay(0) but still gives the chip time to do all the other housekeeping tasks it needs to do asides from run your code. If you don't do that, the ESP may have trouble actually maintaining the WiFi connection (not to talk of the MQTT broker connection) which could be problematic.
The below excerpt is taken from the Github page of the ESP8266 Arduino Port:

Timing and delays

millis and micros return the number of milliseconds and microseconds elapsed after reset, respectively.

delay pauses the sketch for a given number of milliseconds and allows WiFi and TCP/IP tasks to run. delayMicroseconds pauses for a given number of microseconds.

Remember that there is a lot of code that needs to run on the chip besides the sketch when WiFi is connected. WiFi and TCP/IP libraries get a chance to handle any pending events each time the loop() function completes, OR when delay(...) is called. If you have a loop somewhere in your sketch that takes a lot of time (>50ms) without calling delay(), you might consider adding a call to delay function to keep the WiFi stack running smoothly.

There is also a yield() function which is equivalent to delay(0). The delayMicroseconds function, on the other hand, does not yield to other tasks, so using it for delays more than 20 milliseconds is not recommended.


That said, give this a shot and let's see how it goes.
Good luck!
User avatar
By El42
#21337 Hello madarax64,
Thank you for quick (and detailed) support. I'll study the loop and timer management.
For the future I think could be better to move this topic in the section ESP8266 ARDUINO where, if necessary, I'll insert a new post to trace progress or issues. Are you agree?

Regards
User avatar
By ucy74
#21919
madarax64 wrote:...and now the MQTT client on the ESP goes offline at about 727 seconds consistently...

Code: Select all...
m=MQTT.Client("esp",120,”user","pass")
...

Value 120 quits the client after 120 second of inactivity.
Value 0 will be much better - everleast.
Thats all.