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!
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.
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
- Avrphreak
- Posts: 3
- Joined: Thu Apr 15, 2021 3:15 pm
- brubell
- Learn User Page
- Posts: 2010
- Joined: Fri Jul 17, 2015 10:33 pm
Re: Adafruitio MQTT library retain help requested
Yes, here's what the flow looks like...Do I need to give the feeds a specific name?
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
What conflicts does it give you?because the AdafruitIO library gives me conflicts with other libraries i use.
- Avrphreak
- Posts: 3
- Joined: Thu Apr 15, 2021 3:15 pm
Re: Adafruitio MQTT library retain help requested
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:
The section of code giving the trouble is :
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!
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
#include <WiFi.h>
#include <ArduinoJson.h>
#include "Adafruit_BME280.h"
#include <Adafruit_Sensor.h>
#include "config.h"
#include <HTTPClient.h>
Code: Select all
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 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!
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.