Created
October 19, 2024 21:13
-
-
Save ivesdebruycker/908643b2859d76c7164d27697598f8c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file main-esp8266-wifi-relay.cpp | |
* @brief ESP8266 Wifi Relay Controller | |
* | |
* This file contains the implementation of an ESP8266-based relay controller | |
* | |
* @details | |
* - To install the ESP8266 board, (using Arduino 1.6.4+): | |
* - Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs": | |
* http://arduino.esp8266.com/stable/package_esp8266com_index.json | |
* - Open the "Tools -> Board -> Board Manager" and click install for the ESP8266 | |
* - Select your ESP8266 in "Tools -> Board" | |
* | |
* @author Ives | |
* | |
* @section Dependencies | |
* - ESP8266WiFi.h | |
* - ESP8266WebServer.h | |
* | |
* @section Configuration | |
* - WiFi SSID and password | |
* | |
* @section Pins | |
* - RELAY1: D7 | |
* - RELAY2: D6 | |
* - RELAY3: D5 | |
* - RELAY4: D0 | |
* | |
*/ | |
// Include libraries for WiFi and web server | |
#include <ESP8266WiFi.h> | |
#include <ESP8266WebServer.h> | |
// Define the WiFi network | |
const char *ssid = "MyNetworkName"; | |
const char *password = "PasswordForMyNetwork"; | |
// Define the relay pins | |
const int RELAY1 = D7; | |
const int RELAY2 = D6; | |
const int RELAY3 = D5; | |
const int RELAY4 = D0; | |
// Create a WiFi client | |
WiFiClient espClient; | |
// Create a web server on port 80 | |
ESP8266WebServer server(80); | |
/** | |
* @brief Connect to the WiFi network | |
*/ | |
void setup_wifi() | |
{ | |
delay(10); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
// Connect to the WiFi network | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
// Wait for the connection | |
while (WiFi.status() != WL_CONNECTED) | |
{ | |
delay(500); | |
Serial.print("."); | |
} | |
// Print the IP address | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
/** | |
* @brief Handle root path | |
*/ | |
void handleRoot() | |
{ | |
server.send(200, "text/plain", "ESP8266 Relay Controller"); | |
} | |
/** | |
* @brief Handle turning relay on | |
*/ | |
void handleRelayOn() | |
{ | |
digitalWrite(RELAY4, HIGH); | |
server.send(200, "text/plain", "Relay is ON"); | |
} | |
/** | |
* @brief Handle turning relay off | |
*/ | |
void handleRelayOff() | |
{ | |
digitalWrite(RELAY4, LOW); | |
server.send(200, "text/plain", "Relay is OFF"); | |
} | |
/** | |
* @brief Setup web server routes | |
*/ | |
void setup_server() | |
{ | |
server.on("/", handleRoot); | |
server.on("/relay/on", handleRelayOn); | |
server.on("/relay/off", handleRelayOff); | |
server.begin(); | |
Serial.println("HTTP server started"); | |
} | |
void setup() | |
{ | |
// Initialize the relay pins | |
pinMode(RELAY1, OUTPUT); | |
pinMode(RELAY2, OUTPUT); | |
pinMode(RELAY3, OUTPUT); | |
pinMode(RELAY4, OUTPUT); | |
// Initialize the serial port | |
Serial.begin(115200); | |
// Connect to the WiFi network | |
setup_wifi(); | |
// Setup the web server | |
setup_server(); | |
} | |
void loop() | |
{ | |
// Handle web server requests | |
server.handleClient(); | |
// Add a small delay to prevent CPU overload | |
delay(10); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment