0

Adafruitio MQTT library retain help requested
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

Adafruitio MQTT library retain help requested

by Avrphreak on Sat May 08, 2021 6:11 am

Hello, I am looking for a retain functionality arduino example to use in my weather station project.

I want to use the lightweight MQTT library by Adafruit for this because the AdafruitIO library gives me conflicts with other libraries i use.

I am able to publish and subscribe to values but I would like to use some form of retain so I can put my Esp32 to sleep and only check for updates every 15 minutes or so.

Can anyone show an arduino example on how to do this please?
Do I need to give the feeds a specific name?

I did read the page with the retain (get) description but I don't understand how to use it with the MQTT library.

Any help is much appreciated!

Avrphreak
 
Posts: 3
Joined: Thu Apr 15, 2021 3:15 pm

Re: Adafruitio MQTT library retain help requested

by brubell on Mon May 10, 2021 10:44 am

Do I need to give the feeds a specific name?

Yes, here's what the flow looks like...

For a feed named username/feed/counter, you'd:
1) Subscribe to username/feed/counter
2) Publish (can be any character or string) to the feed: username/feed/counter/get
3) We will immediately receive a message on our uname/f/counter subscription with the last value that was published to the counter feed.

https://io.adafruit.com/api/docs/mqtt.h ... -get-topic

because the AdafruitIO library gives me conflicts with other libraries i use.


What conflicts does it give you?

brubell
 
Posts: 1383
Joined: Fri Jul 17, 2015 10:33 pm

Re: Adafruitio MQTT library retain help requested

by Avrphreak on Wed May 12, 2021 11:45 am

Thank you for your reply!

The errors are (PlatformIO in VScode):
'HTTPClient' was not declared in this scope
identifier "HTTPClient" is undefined
'http' was not declared in this scope

The #includes are:

Code: Select all | TOGGLE FULL SIZE
#include <WiFi.h>

#include <ArduinoJson.h>
#include "Adafruit_BME280.h"
#include <Adafruit_Sensor.h>
#include "config.h"
#include <HTTPClient.h>


The section of code giving the trouble is :
Code: Select all | TOGGLE FULL SIZE
if ((WiFi.status() == WL_CONNECTED)) { //Check the current connection status
    HTTPClient http;
   
 
    http.begin(current); //Specify the URL
    int httpCode = http.get();  //Make the request

    String payload;
    if (httpCode > 0) { //Check for the returning code
      const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(1) + 2*JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(4) + JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(6) + JSON_OBJECT_SIZE(12) + 600;
      DynamicJsonDocument doc(capacity);
     
      String payload = http.getString();
      //Serial.println(httpCode);
      //Serial.println(payload);


I am accessing Openweathermap for weather data which is shown on a Nextion display controlled by an ESP 32, on top of that i plan two remote ESP's with BME280 sensors which will send their data to an MQTT broker. The first ESP will retrieve that data and show on different pages on the Nextion.
I am able to do all of those functions seperately using seperate codes and the AdafruitIO library.
But when combined i run into these errors. This is the reason i want to try use the MQTT library, which in itself i got working but without the retain.

In the meantime i at least had success using the PubSubclient library in combination with HiveMQ MQTT broker in publishing the data (retained) and retrieving it again.
I still need to integrate that section of code into my main weather station code and see whether this results in conflicts.
However, i would like to use the Adafruitio MQTT broker because i just love the dashboard as well.

I will try and let you know.
Thanks again!

Avrphreak
 
Posts: 3
Joined: Thu Apr 15, 2021 3:15 pm

Please be positive and constructive with your questions and comments.