To change modes the ESP-01 needs to be reset via unplugging and
plugging
in the USB interface.
// Board is Generic ESP8266 Module
//From: https://www.instructables.com/ESP0101S-RELAY-MODULE-TUTORIAL/
// Modified by Deid Reimer 2020-02-06
#include <esp8266wifi.h>
// Variables for debugging
int restartCount = 0; // Number of times the wifi has been restarted.
int waitCount = 0; // Number of seconds waiting for input
// Credentials
const char* ssid = "wifi";
const char* password = "password";
// The relay is connected to GPIO0
#define RELAY 0
// Variables for wifi restart if wifi lost
unsigned long previousMillis = 0;
unsigned long interval = 30000;
// Current status of relay. On boot set low.
int value = LOW;
// Define the server on port 80
WiFiServer server(80);
void setup() {
// I set the serial monitor slow because I can't read faster so
// what's the point ...
Serial.begin(9600);
// Set the relay for output and default off
pinMode(RELAY, OUTPUT);
digitalWrite(RELAY, value);
// Connect to the WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Set wifi restart and persistent
WiFi.setAutoReconnect(true);
WiFi.persistent(true);
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print(WiFi.localIP());
Serial.println("/");
// Start the web server
server.begin();
Serial.println("Server started");
}
void loop() {
// Check if a client has connected delay and
// restart the loop if not.
WiFiClient client = server.available();
if (!client) {
//Serial.println("returning");
delay(10);
return;
}
// There is a connection, wait for the client to send a request
// and count and restart loop if too long a wait.
int j = 0;
while (!client.available()) {
delay(1000);
Serial.println(j++);
if (j > 10) {
waitCount = j;
client.flush();
return;
}
}
Serial.println("new client");
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
// Trash anything else
client.flush();
// Is the reguest ON, HIGH. Yes turn the relay on.
if (request.indexOf("/RELAY=ON") != -1) {
Serial.println("RELAY=ON");
digitalWrite(RELAY, HIGH);
value = HIGH;
}
// Or of OFF turn the relay off
if (request.indexOf("/RELAY=OFF") != -1) {
Serial.println("RELAY=OFF");
digitalWrite(RELAY, LOW);
value = LOW;
}
// Return the response - web page.
// http part
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
// this is a must. Separates http from html
client.println("");
// html part
client.println("");
client.println("");
client.println("ESP8266 RELAY Control V1.0 ");
client.println("");
client.println("");
client.print("Restarts: ");
client.println(restartCount);
client.print("waitCount: ");
client.println(waitCount);
client.println("");
client.print("Relay is now: ");
// clear wait for data count
waitCount = 0;
if (value == HIGH) {
client.print("On");
} else {
client.print("Off");
}
client.println("");
client.println("Turn OFF RELAY");
client.println("Turn ON RELAY");
client.println("");
delay(1);
Serial.println("Client disconnected");
Serial.println("");
// if WiFi is down, try reconnecting
unsigned long currentMillis = millis();
if ((WiFi.status() != WL_CONNECTED) && (currentMillis - previousMillis >= interval)) {
Serial.print(millis());
Serial.println("Reconnecting to WiFi...");
WiFi.disconnect();
WiFi.begin(ssid, password);
previousMillis = currentMillis;
//Alternatively, you can restart your board
//ESP.restart();
}
}
2022-02-07