I'm currently working on a project involving the ESP32, creating an automatic plant watering system using Adafruit and the Arduino IDE. The issue I'm encountering is that the ESP32 intermittently disconnects from the router approximately every 20-30 minutes. This disconnection causes the project to pause until I manually reset the ESP32 by pressing the reset button.
Any advice or solutions to ensure the ESP32 maintains a stable connection to the router and continues to send moisture readings to Adafruit without manual intervention would be greatly appreciated.
Here is the code I am using(I will take out the wifi details):
Disconnects will happen and should be coded for..
I would move the Wifi and mqtt connect out of setup and into the loop..
A system state machine should be added that would not only keep track of the above but can also handle the watering..
Your loop logic right now is confusing, looks like you only want to water for 750ms when sensor say too as you break quickly out of the loop..
But yes, disconnects can happen and if all your connection code resides in setup you must reset in order to reconnect..
#include <WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
// WiFi parameters
#define WLAN_SSID "Sensitive info "
#define WLAN_PASS "Sensitive info"
// Adafruit IO
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "ElectroMasterOP"
#define AIO_KEY "aio_OuuD72fe69CKuZ7gvDMObSdTyf65"
WiFiClient client;
//Connects to output_value2 feed
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
Adafruit_MQTT_Publish OutPut_Value = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/OutPut_Value2");
byte moisture;
//Creates variables for watering function.
const int relay = 5;
const int sensor_pin = 32;
const int led = 18;
int output_value;
//Creates statefunction and its cases.
enum State {
INIT,
CONNECT_WIFI,
CONNECT_MQTT,
READ_SENSOR,
WATER_PLANT,
PUBLISH_DATA
};
//Starts the statefunction up.
State currentState = INIT;
void setup() {
Serial.begin(115200); //Connects to serial monitor
Serial.println(F("Adafruit IO Example"));
pinMode(led, OUTPUT);
pinMode(relay, OUTPUT);
digitalWrite(relay, LOW);
}
void loop() {
switch (currentState) {
case INIT:
Serial.println(F("Initializing...")); //Initialization
currentState = CONNECT_WIFI; //Next step start-off
break;//Goes to next step
case CONNECT_WIFI:
//Connects to wifi using the parameters
Serial.print(F("Connecting to WiFi: "));
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
//Checks for issues then reconnects if so.
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
WiFi.begin(WLAN_SSID, WLAN_PASS);
}
Serial.println();
//Prints local IP/allows user to know its ready to go on.
Serial.println(F("WiFi connected"));
Serial.print(F("IP address: "));
Serial.println(WiFi.localIP());
currentState = CONNECT_MQTT;//Next step start-off
break;//Allowing to go to next step
case CONNECT_MQTT:
Serial.print(F("Connecting to Adafruit IO... "));
//Connects to adafruit using AIO_KEY put in at the top.
if (mqtt.connect() == 0) {
Serial.println(F("Adafruit IO Connected!"));
currentState = READ_SENSOR;//Next step start-off
} else {
//Retrying because there were errors
Serial.println(F("Connection failed, retrying..."));
delay(10000);
}
break;//Allowing to go to next step
case READ_SENSOR:
digitalWrite(relay, LOW);//Turns relay/pump off
output_value = analogRead(sensor_pin);
output_value = map(output_value, 550, 0, 0, 100);//Checks moisture
Serial.print("Moisture : ");//Printing value
Serial.print(output_value);
Serial.println("%");
delay(50);
if (output_value <= -100) {
currentState = WATER_PLANT;//Next step start-off(forking off No.1)
} else {
currentState = PUBLISH_DATA;//Next step start-off(forking off No.2)
}
break;//Allowing to go to next step
case WATER_PLANT:
//No.1
digitalWrite(led, HIGH);
digitalWrite(relay, HIGH);//Pump on/Water pouring
Serial.println("Current Flowing");
delay(1250);//Water pours for 1250ms
digitalWrite(relay, LOW);//Pump off.
Serial.println("Pouring stopped");
digitalWrite(led, LOW);
currentState = PUBLISH_DATA;//Next step start-off(No.2)
break;//Allowing to go to next step
case PUBLISH_DATA:
//No.2
//Connect to adafruit.
if (!mqtt.ping(3)) {
// Reconnect to Adafruit IO
if (!mqtt.connected()) {
currentState = CONNECT_MQTT;
break;
}
}
//Print on serial monitor and puts output_value+700 in the output_value2 feed to be plotted into a graph
Serial.print("Output Value: ");
Serial.println(output_value);
moisture = output_value + 700;
if (!OutPut_Value.publish(output_value + 700)) {
//Fails data send/retrys
Serial.println(F("Failed to send data"));
} else {
Serial.println(F("Data sent!"));
Serial.println("--------------------------");
}
delay(2000);
currentState = READ_SENSOR;//Loops again from reading sensor
break;//Allowing to go to start of loop.
}
}