Hello,
I am having a hard time with my ESP32 board the last few days.
I have built a custom soil moisture sensor with an ESP32 board that reads the soil moisture and post it through MQTT to HA. Then sleeps for 1 hour.
The whole project runs on a 18650 / 3000mAH battery using a battery shield V3 which has a 5V output.
As the esp32 has only one VCC I am using a GPIO pin to switch off the sensor too.
The soil moisture sensor was staying ON when esp was in deep sleep.
Something which was not really very good for power consumption and energy saving.
My sketch looks correct, at least to my eyes, but after a few hours of operating I am getting an error which I haven’t be able to solve.
1609060258: New client connected from 192.168.70.161 as plant-esp32 (p2, c1, k60, u'local-user').
1609060348: Client plant-esp32 has exceeded timeout, disconnecting.
I have read lots of forums and tried different versions of my sketch.
I tried different version of PubSubClient library.
I tried to set the MQTT_KEEPALIVE to 60
Unfortunately with no success.
After a couple of sensor readings, ESP fails to publish to HA (has exceed timeout, disconnecting), although it connects to broker.
My sketch looks like this:
#include <WiFi.h>
#define MQTT_KEEPALIVE 60
#include "PubSubClient.h" // Allows us to connect to, and publish to the MQTT broker
#define SensorPin A0
/* definitions for deepsleep */
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 3600 /* Time ESP32 will go to sleep for 2 minutes (in seconds) */
int minsensor = 1200; //drowned in water
int maxsensor = 2800; //dry soil
// WiFi
const char* ssid = "XXXX";
const char* wifi_password = "XXXXX";
// MQTT
// Make sure to update this for your own MQTT Broker!
const char* mqtt_server = "XXXXX";
const char* plant_topic = "plant/moisture";
const char* mqtt_username = "local-user";
const char* mqtt_password = "12345";
const char* clientID = "plant-esp32";
// Initialise the WiFi and MQTT Client objects
WiFiClient wifiClient;
PubSubClient client(mqtt_server, 1883, wifiClient); // 1883 is the listener port for the Broker
void connect_MQTT() {
Serial.print("Connecting to ");
Serial.println(ssid);
// Connect to the WiFi
WiFi.begin(ssid, wifi_password);
// Wait until the connection has been confirmed before continuing
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Debugging - Output the IP Address of the ESP
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
pinMode(13, OUTPUT); // sets the digital pin 13 as output
digitalWrite(13, HIGH); // sets the digital pin 13 on
delay(1000); //delay for a second
Serial.println("PIN 13 is ON");
// original values
float sensorValue = analogRead(SensorPin);
Serial.println("Sensor Value is: " + String(sensorValue) );
//map to percentage
float percentage = map(sensorValue, maxsensor, minsensor, 0, 100);
Serial.println("Percentage % is: " + String(percentage) );
// PUBLISH to the MQTT Broker
client.connect(clientID, mqtt_username, mqtt_password);
if (client.publish(plant_topic, String(percentage).c_str())) {
Serial.println("Moisture sent!");
//Serial.println(plant_topic);
}
// If the message failed to send, we will try again, as the connection may have broken.
else
{
Serial.println("Moisture failed to send. Reconnecting to MQTT Broker and trying again");
client.connect(clientID, mqtt_username, mqtt_password);
delay(100); // This delay ensures that client.publish doesn't clash with the client.connect call
client.publish(plant_topic, String(percentage).c_str());
Serial.println("Moisture sent on recconect!");
}
digitalWrite(13, LOW); // sets the digital pin 13 off
Serial.println("PIN 13 is OFF");
Serial.setTimeout(2000);
Serial.println("Preparing ESP32 to sleep for " + String(TIME_TO_SLEEP) + " Seconds");
Serial.println("Sleeping...");
ESP.deepSleep(TIME_TO_SLEEP * uS_TO_S_FACTOR);
}
void setup() {
Serial.begin(115200);
connect_MQTT();
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, wifi_password);
}
else
{
client.loop();
}
}
Any help / advise would be much appreciate and put me back to bed
thanks in advance
Makis