MQTT publishing with the ESP8266 + Arduino

This is more or less a follow-up on my previous post about using the ESP8266 to MQTT-enable an Arduino (or a JeeNode in my case). The first time I installed the espduino library there was no publish available yet; one day later it was, so this afternoon I installed the latest version in the Arduino IDE and gave publishing a try.

All I had to do was adding some additional code to the example:

int reportInterval = 10 * 1000;
unsigned long now = 0;
unsigned long nextPub = reportInterval;

void loop() {
  esp.process();

  // will work for 49 days; that's OK.
  now = millis();
  if (now >= nextPub) {
    nextPub += reportInterval;
    String payload = "Payload ";
    payload += now;
    char char_array[payload.length()+1];
    payload.toCharArray(char_array, payload.length()+1);
    esp.publish("topic", char_array, payload.length()+1, 0, 0);
  }
}

The result:

Espduino libaray publishing

I noticed that the keepalive packets are still being sent even though the publishing is done every 10 seconds; I can’t recall seeing that behavior with other MQTT clients – as far as I know keepalive packets are only needed to ping the broker when there hasn’t been any other communication within the keepalive interval, but I might be wrong about that. I’ll have to check that some day.

For now this is not a real issue but it probably will be when the library will be used in a setup that’s battery powered. Another question I have is: what would be an acceptable keepalive interval for a battery operated MQTT client? For some sensors even a value of 3600 seconds would suffice I guess..

Last thing on my mind is a good auto-reconnect feature for this; a closed connection (e.g. because your Wifi AP has been down for a short period) should not be a reason to power cycle all those ESP connected devices in your house..!

Tagged , , , . Bookmark the permalink.

7 Responses to MQTT publishing with the ESP8266 + Arduino

  1. Bogdan says:

    Thank you for the example.
    Hope my esp will arrive soon .
    Have a nice weekend.

  2. Jon Archer says:

    So far I’m impressed with the ESP devices, however I’m really struggling with the sample code from the git repo you suggested, did this just work out of the box for you? obviously after changing the AP details and MQTT broker details.

  3. StuartP says:

    ESP’s are great, but I cant see them being used for battery powered sensors.

    • Depends on what they’ll be used for I guess – it seems they have deep sleep mode, so until someone has something useful to say about that it’s too early to decide it won’t work. I’ve got JeeNode+XBee2 sensors running on batteries for more than a year, sending a sensor value with 10 sec. interval; so why wouldn’t a ESP8266 be able to send just 2 sensor values a day on batteries. We’ll see.

  4. Jon Archer says:

    Funnily enough I pulled the latest version after seeing this post and all came to life…

    Seems a little hit and miss of when it actually connects to the broker, but I guess it’s just a little refinement required.

    So many possibilities with this device….

Leave a Reply

Your email address will not be published. Required fields are marked *