Skip to content

Instantly share code, notes, and snippets.

@ivesdebruycker
Created October 19, 2024 21:00
Show Gist options
  • Save ivesdebruycker/611a868d8bc6cb3a2e041a5588e09fe0 to your computer and use it in GitHub Desktop.
Save ivesdebruycker/611a868d8bc6cb3a2e041a5588e09fe0 to your computer and use it in GitHub Desktop.
/**
* @file main.cpp
* @brief ESP8266 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
*
* @section Configuration
* - WiFi SSID and password
* - MQTT server IP address
*
* @section Pins
* - RELAY1: D7
* - RELAY2: D6
* - RELAY3: D5
* - RELAY4: D0
*
* @section Functions
* - setup_wifi(): Connects to the WiFi network.
* - setup(): Initializes the hardware and connects to WiFi.
* - loop(): Main loop that controls the relay.
*/
// Include the ESP8266 WiFi library
#include <ESP8266WiFi.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;
/**
* @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());
}
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();
}
void loop()
{
digitalWrite(RELAY4, HIGH); // Turn the relay on
delay(1000); // Wait for 1 second
digitalWrite(RELAY4, LOW); // Turn the relay off
delay(1000); // Wait for 1 second
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment