Howdy all. Quoted below is my code in question. I’m building a project where I have a bunch of ESP32’s doing temp and humidity monitoring of animal enclosures for a rescue. It’s all pretty straight forward. But what I can’t wrap my head around is that on occasion this ESP32 starts getting a return code of -13 from the publish event. I can’t figure out what it is. I thought that it might be related to some sketchy wifi so my first thought was to re-initiate the wifi. I moved that part of the code into it’s own function so I could call it. But it doesn’t seem to behave the way I think it ought to. I usually get stuck in my while loop waiting for the wifi to reconnect with a status of WL_IDLE_STATUS. I’m sure I’m doing something basically dumb. Could someone find it in their heart to set me on the right path? ha
#include <AWS_IOT.h>
#include <WiFi.h>
#include "DHT.h"
#define DHTPIN 16 // what digital pin we're connected to
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
AWS_IOT hornbill; // AWS_IOT instance
char WIFI_SSID[]="snip";
char WIFI_PASSWORD[]="snip";
char HOST_ADDRESS[]="snip";
char CLIENT_ID[]= "IoT-Temperature-Device";
char TOPIC_NAME[]= "stack1/cage1";
char payload[512];
char rcvdPayload[512];
int tick=0,msgCount=0,msgReceived = 0;
int hornbillStatus;
void setup() {
Serial.begin(115200);
delay(2000);
connect_wifi();
if(hornbill.connect(HOST_ADDRESS,CLIENT_ID)== 0) // Connect to AWS using Host Address and Client ID
{
Serial.println("Connected to AWS");
delay(1000);
}
else
{
Serial.println("AWS connection failed, Check the HOST Address");
while(1);
}
delay(2000);
dht.begin(); //Initialize the DHT11 sensor
}
void connect_wifi() {
// WiFi.config(ip,gateway,subnet);
WiFi.mode(WIFI_STA);
Serial.print("Attempting to connect to SSID: ");
Serial.println(WIFI_SSID);
WiFi.disconnect();
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.println("Waiting for connection...");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print("WiFi Status: ");
Serial.println(wl_status_to_string(WiFi.status()));
delay(1000);
}
Serial.println(wl_status_to_string(WiFi.status()));
Serial.println("Connected to wifi");
}
void loop() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
delay(2000);
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
delay(2000);
// Read temperature as Fahrenheit (isFahrenheit = true)
//float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h)) {
Serial.println("Failed to read humidity from sensor!");
}
else if (isnan(t)) {
Serial.println("Failed to read temperature from sensor!");
}
else
{
sprintf(payload,"{\"state\":{\"reported\":{\"Humidity\":\"%f\",\"Temperature\":\"%f\"}}}",h,t); // Create the payload for publishing
hornbillStatus = hornbill.publish(TOPIC_NAME,payload);
if(hornbillStatus == 0) // Publish the message(Temp and humidity)
{
Serial.print("Message publish successful:");
Serial.println(payload);
}
else if( hornbillStatus == -13)
{
Serial.print("Caught the error -13 code. Resetting connection.");
connect_wifi();
}
else
{
Serial.print("Publish failed. Error code: ");
Serial.println(hornbillStatus);
}
// publish the temp and humidity every 5 seconds.
vTaskDelay(1000 / portTICK_RATE_MS);
}
}
const char* wl_status_to_string(wl_status_t status) {
switch (status) {
case WL_NO_SHIELD: return "WL_NO_SHIELD";
case WL_IDLE_STATUS: return "WL_IDLE_STATUS";
case WL_NO_SSID_AVAIL: return "WL_NO_SSID_AVAIL";
case WL_SCAN_COMPLETED: return "WL_SCAN_COMPLETED";
case WL_CONNECTED: return "WL_CONNECTED";
case WL_CONNECT_FAILED: return "WL_CONNECT_FAILED";
case WL_CONNECTION_LOST: return "WL_CONNECTION_LOST";
case WL_DISCONNECTED: return "WL_DISCONNECTED";
}
}