HI, am using Adafruit IO with ESP32 and applying mqtt.subscribe. First 5 minutes I could toggle a switch in my dashboard to update the feeds and is sensed by ESP32, after 5 minutes however, nothing happens to the mqtt. subscribe. Thanks
Welcome to the forum
Do you think that it might help if you posted your sketch just in case there is anything obviously wrong ?
Here it is:
#include <WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
// WiFi credentials
#define WLAN_SSID "myssid" //"WLAN_SSID"
#define WLAN_PASS "mypassword" //"WIFI_PASSWORD"
// Adafruit IO credentials
#define IO_USERNAME "my_uname"
#define IO_KEY "my_key4"
// Create a WiFiClient object
WiFiClient wifiClient;
// Define the Adafruit IO Feed
Adafruit_MQTT_Client mqtt(&wifiClient, "io.adafruit.com", 1883, IO_USERNAME, IO_KEY);
Adafruit_MQTT_Subscribe HomeControl = Adafruit_MQTT_Subscribe(&mqtt, IO_USERNAME "/feeds/HomeControl");
void setup() {
Serial.begin(115200);
delay(10);
Serial.print("Connecting to WiFi");
WiFi.begin(WLAN_SSID,WLAN_PASS);
delay(2000);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
//Serial.print(".");
}
Serial.println(" Connected!");
Serial.println("IP address: "); Serial.println(WiFi.localIP());
/*
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
delay(1000);
WiFi.begin(WLAN_SSID, WLAN_PASS);
delay(2000);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
//Serial.print(".");
}
//Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(WiFi.localIP());
*/
// Add a delay before connecting to WiFi
//delay(1000);
// Connect to Wi-Fi
//WiFi.begin(ssid, password);
// Serial.print("Connecting to WiFi...");
// while (WiFi.status() != WL_CONNECTED) {
// delay(1000);
//Serial.print(".");
//}
//Serial.println();
// Serial.println("Connected to WiFi");
// Serial.println(WiFi.status());
// Connect to Adafruit IO
mqtt.subscribe(&HomeControl);
}
void loop() {
// Ensure the connection to Adafruit IO is maintained
if (!mqtt.connected()) {
connectToMQTT();
}
// Monitor the Adafruit IO feed
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &HomeControl) {
Serial.print("Received Feed Value: ");
Serial.println((char *)HomeControl.lastread);
}
}
}
void connectToMQTT() {
Serial.print("Connecting to MQTT...");
int8_t ret;
while ((ret = mqtt.connect()) != 0) {
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection...");
delay(5000);
}
Serial.println("Connected to MQTT");`Use code tags to format code for the forum`
}Use code tags to format code for the forum
#include <WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
// WiFi credentials
#define WLAN_SSID "myssid" //"WLAN_SSID"
#define WLAN_PASS "mypassword" //"WIFI_PASSWORD"
// Adafruit IO credentials
#define IO_USERNAME "my_uname"
#define IO_KEY "my_key4"
// Create a WiFiClient object
WiFiClient wifiClient;
// Define the Adafruit IO Feed
Adafruit_MQTT_Client mqtt(&wifiClient, "io.adafruit.com", 1883, IO_USERNAME, IO_KEY);
Adafruit_MQTT_Subscribe HomeControl = Adafruit_MQTT_Subscribe(&mqtt, IO_USERNAME "/feeds/HomeControl");
void setup() {
Serial.begin(115200);
delay(10);
Serial.print("Connecting to WiFi");
WiFi.begin(WLAN_SSID,WLAN_PASS);
delay(2000);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
//Serial.print(".");
}
Serial.println(" Connected!");
Serial.println("IP address: "); Serial.println(WiFi.localIP());
/*
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
delay(1000);
WiFi.begin(WLAN_SSID, WLAN_PASS);
delay(2000);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
//Serial.print(".");
}
//Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(WiFi.localIP());
*/
// Add a delay before connecting to WiFi
//delay(1000);
// Connect to Wi-Fi
//WiFi.begin(ssid, password);
// Serial.print("Connecting to WiFi...");
// while (WiFi.status() != WL_CONNECTED) {
// delay(1000);
//Serial.print(".");
//}
//Serial.println();
// Serial.println("Connected to WiFi");
// Serial.println(WiFi.status());
// Connect to Adafruit IO
mqtt.subscribe(&HomeControl);
}
void loop() {
// Ensure the connection to Adafruit IO is maintained
if (!mqtt.connected()) {
connectToMQTT();
}
// Monitor the Adafruit IO feed
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &HomeControl) {
Serial.print("Received Feed Value: ");
Serial.println((char *)HomeControl.lastread);
}
}
}
void connectToMQTT() {
Serial.print("Connecting to MQTT...");
int8_t ret;
while ((ret = mqtt.connect()) != 0) {
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection...");
delay(5000);
}
Serial.println("Connected to MQTT");
}
The code works for the first 5 minutes toggling a switch in my Adafruit IO dashboard. Thanks!
Sorry, after 5 minutes on inactivity, the mqtt.subscribe does not work.
Sorry, after 5 minutes of inactivity, the mqtt.subscribe does not work
What do your serial.print statements say?
Initially, serial displays normally, after 5 minutes of inactivity the it kinda hangs.
What's the last thing it says?
Connecting to WiFi Connected!
IP address:
192.168.5.228
Connecting to MQTT...Connected to MQTT
Received Feed Value: AC_ON
Received Feed Value: AC_OFF
Received Feed Value: AC_ON
Received Feed Value: AC_OFF
Hi again.
I try to toggle the switch and the serial monitor captures the right response from the ESP32, If i leave this for 5 minutes and try to toggle the switch, nothing happens and I will need to reset the ESP32 again before it starts responding. Thanks
There are two possible reasons:
- Your code does not send PING message periodically
- Your Arduino run out of memory because the memory is not well managed
You can try MQTTClient library by Joel Gaehwiler => see this ESP32 with MQTT tutorial
Hi,
Yes, I added this and it worked, thanks!`
// ping the server to keep the mqtt connection alive
if(! mqtt.ping()) {
mqtt.disconnect();
}
Please mark it as solution. Other people can quickly find it.
The weird thing here is that I have been using this code for about 4 months with no issues and no need for the ping function. Thanks again.
Just did. Thanks!
If your code PUBLISH message periodically within keep-alive time, you do not need to PING.
I have not check Adafruit MQTT library yet, but PING message should be managed by library, not application. If the library does not manage PING message, I think it is not good library implementation.
I do PUBLISH messages but did not want to overdo this to avoid filling-up the feed so I use a switch to turn- ON or OFF when I need to PUBLISH.
I agree with you on the PING on library.