Hi All. Noob here so please be gentle.
My first foray in to programming the ESP8266 directly from the Arduino IDE.
A simple relay controller (it's actually an ESP-01 board with a couple of LEDs with 1k resistors on the IO pins at present whilst I wait for the actual relay board). I filched the code from one of the multitude of online tutorials and away we go. Works like a dream.
But, after an indeterminate period the web server stops responding, I can still ping the device so it's evidently alive and after a power-cycle everything works as normal.
It doesn't seem to matter how often the web server is accessed by a page.
A flurry of Googling revealed others with similar issues but no really solid answers. I've reverted to V2.5.2 of the ESP8266 IDE add-on thingy as advised in one of the "solutions" and things do seem to have improved but it's just locked up again after 12 hours.
Power is solid, 12V 2A power block regulated down to 5V for the ESP-01 which has the 3.3V regulator on board.
I added some code to store the relay state in eeprom so it survives a restart and stuck in a timed hard reset, but it's not really a viable solution for the long term.
Thoughts, ideas?
Code below:-
/*
V1.0a added fixed IP address.
V1.0b / c added flash to retain the relay state over a reset.
*/
#include <ESP8266WiFi.h>
#include <ESP_EEPROM.h>
// Set up for the WiFi server
IPAddress ip(192, 168, 1, 50);
IPAddress gateway(192, 168, 1, 254);
IPAddress subnet(255, 255, 255, 0);
IPAddress DNS(192, 168, 1, 254);
const char* ssid = "<edited out>"; // fill in here your router or wifi SSID
const char* password = "<edited out>"; // fill in here your router or wifi password
#define RELAY 0 // relay connected to GPIO0
WiFiServer server(80);
// Set up for FLASH storage
byte eepromVar1 = 0;
unsigned long oldmillis = 0;
void setup()
{
Serial.begin(9600); // must be same baudrate with the Serial Monitor
EEPROM.begin(16); // 16 Bytes (minimum is 16)
EEPROM.get(0, eepromVar1); // Read the relay state from eeprom
Serial.print ("Getting relay value from eeprom ");
Serial.println (eepromVar1);
pinMode(RELAY,OUTPUT);
if (bitRead(eepromVar1,0) == 0) { digitalWrite(RELAY,LOW);} // and write it to the relay pins
else { digitalWrite(RELAY,HIGH);}
oldmillis = millis(); // Reset timer for brute force reset
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.config(ip, gateway, subnet, DNS);
delay (100);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}
// ********** Main Loop Below *******************
void loop()
{
/*
if (millis() - oldmillis >= 180000UL)
{
Serial.println("Resetting ESP");
ESP.restart();
}
*/
// Check if a client has connected
WiFiClient client = server.available();
if (!client)
{
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available())
{
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
// Match the request
int value = LOW;
if (request.indexOf("/RELAY=ON") != -1)
{
Serial.println("RELAY=ON");
bitSet(eepromVar1 , 0);
digitalWrite(RELAY,HIGH);
value = LOW;
}
if (request.indexOf("/RELAY=OFF") != -1)
{
Serial.println("RELAY=OFF");
bitClear(eepromVar1 , 0);
digitalWrite(RELAY,LOW);
value = HIGH;
}
// Write the eeprom
EEPROM.put(0, eepromVar1);
EEPROM.commit();
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // this is a must
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head><title>ESP8266 RELAY Control</title></head>");
client.print("Relay is now: ");
if(value == HIGH)
{
client.print("OFF");
}
else
{
client.print("ON");
}
client.println("<br><br>");
client.println("Turn <a href=\"/RELAY=OFF\">OFF</a> RELAY<br>");
client.println("Turn <a href=\"/RELAY=ON\">ON</a> RELAY<br>");
client.println("</html>");
delay(1);
Serial.println("Client disonnected");
Serial.println("");
}