I need too program a bunch of ESP8266 modules and use them in my home automation system.
Essentially the program is very simple, when it receives a message it calls a function then it returns to the main loop and the program starts over.
The problem is that I need to stay inside the function for a long time (1 to 20 minutes) and the MQTT connection times out.
I modified the base code that I use for the ESP8266 module (criticisms and suggestions are appreciated) and I added a function that waits X minutes to simplify the debugging. After the function has been called the connection times out, I tried to reconnect to the server but I can only publish the message inside the function and when the program returns to the main loop it can’t receive any message.
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "UniFi-HomeKit"; // Enter WiFi name
const char* password = ":)"; // Enter WiFi password
const char* mqttServer = "10.10.20.10";
const int mqttPort = 1883;
const char* mqttUser = "matteo";
const char* mqttPassword = ":)";
const int ledPin = 2; // the number of the LED pin
const int buzzer = 12; // the number of the BUZZER pin
const int button = 5; // the number of the BUTTON pin
const int oneMinute = (1 * 60 * 1000);
const int twentyMinutes = (20 * 60 * 1000);
const int tenSeconds = 10000;
int buttonState = 0;
WiFiClient espClient;
PubSubClient client(espClient);
void connectToWiFi()
{
WiFi.mode(WIFI_STA); //access point mode disabled
WiFi.begin(ssid, password);
Serial.println("");
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.printf("RSSI: %d dBm\n", WiFi.RSSI());
}
void connectToMQTT()
{
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
Serial.println("");
Serial.print("Connecting to MQTT server");
delay(200);
Serial.print(".");
delay(200);
Serial.print(".");
delay(200);
Serial.println(".");
while (!client.connected()) {
if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {
Serial.println("Connected to MQTT server");
} else {
delay(300);
digitalWrite(ledPin, HIGH);
delay(300);
digitalWrite(ledPin, LOW);
Serial.println("");
Serial.print("failed with state ");
Serial.print(client.state());
}
}
}
void startConnection()
{
delay(2000);
connectToWiFi();
connectToMQTT();
}
void ledBlink (int interval)
{
digitalWrite(buzzer, HIGH);
digitalWrite(ledPin, LOW);
delay(interval);
digitalWrite(buzzer, LOW);
digitalWrite(ledPin, HIGH);
delay(interval);
}
void wait()
{
delay(oneMinute);
Serial.println("Time elapsed");
if (!client.connected())
{
Serial.println("Not connected to MQTT server, connecting again");
connectToMQTT();
}
client.publish("waitTest/START", "OFF");
Serial.println("Publish OFF");
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
if (!strncmp((char *)payload, "ON", length))
{
Serial.println("ON");
wait();
}
else
{
Serial.println("Invalid message");
}
}
void setup() {
Serial.begin(115200);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the BUZZER pin as an output:
pinMode(buzzer, OUTPUT);
// initialize the BUTTON pin as an input:
pinMode(button, INPUT_PULLUP);
startConnection();
client.subscribe("waitTest/START");
}
void loop() {
client.loop();
if ((WiFi.status() == WL_CONNECTION_LOST) || (WiFi.status() == WL_DISCONNECTED))
{
Serial.println("WiFi connection lost, connecting again");
connectToWiFi();
}
if (!client.connected())
{
Serial.println("MQTT connection lost, connecting again");
connectToMQTT();
}
delay(300);
}
What can I do to resolve the problem?
Thanks