CCCPaste

WiFi Relay rev_70

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: WiFi Relay
	- Source Code NOT compiled for: ESP32 DevKit V1
	- Source Code created on: 2024-08-08 05:51:15

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Configure WiFi and timezone settings with web page */
	/* available via both AP and STA */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include     //https://github.com/evert-arias/EasyButton
#include          //https://github.com/rafaelnsantos/Relay
#include 
#include     //https://github.com/stblassitude/Adafruit_SSD1306_Wemos_OLED.git
#include  // Include U8g2_for_Adafruit_GFX library
#include  // Include WiFi library for WiFi functionality
#include  // Include WebServer library for handling web requests

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void handleRoot(); // Function to handle root URL
void configureWiFi(); // Function to configure WiFi

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t test_PushButton_PIN_D4        = 4;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t relay_RelayModule_Signal_PIN_D13        = 13;

/***** DEFINITION OF I2C PINS *****/
const uint8_t myDisplay_SSD1306OledDisplay_I2C_PIN_SDA_D21        = 21;
const uint8_t myDisplay_SSD1306OledDisplay_I2C_PIN_SCL_D22        = 22;
const uint8_t myDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS      = 0x3C; // Default I2C address for SSD1306

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
bool relay_RelayModule_Signal_PIN_D13_rawData = 0;

/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
float relay_RelayModule_Signal_PIN_D13_phyData = 0.0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Initialize EasyButton object for the push button
EasyButton button(test_PushButton_PIN_D4); // Using the constructor to initialize the button

// Initialize Relay object on pin 13 with a period of 5 seconds
Relay relay(relay_RelayModule_Signal_PIN_D13, 5); // Using the constructor to initialize the relay

// Initialize Adafruit_SSD1306 object for the OLED display
#define OLED_RESET -1 // Reset pin not used
Adafruit_SSD1306 display(myDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS, myDisplay_SSD1306OledDisplay_I2C_PIN_SDA_D21, myDisplay_SSD1306OledDisplay_I2C_PIN_SCL_D22); // Using the constructor to initialize the display

// Initialize U8G2_FOR_ADAFRUIT_GFX object for font support
U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx; // Using the constructor to initialize U8G2_FOR_ADAFRUIT_GFX

// Initialize WebServer object
WebServer server(80); // Create a web server on port 80

void setup(void)
{
    Serial.begin(9600); // Initialize serial communication for debugging

    pinMode(test_PushButton_PIN_D4, INPUT_PULLUP);
    pinMode(relay_RelayModule_Signal_PIN_D13, OUTPUT);

    // Configure WiFi and start the web server
    configureWiFi();
    server.on("/", handleRoot); // Define the root URL handler
    server.begin(); // Start the server

    // Initialize the button
    button.begin(); // Call begin to set up the button
    button.onPressed([]() { // Lambda function for button press event
        Serial.println("Button pressed");
    });

    // Set relay mode to automatic
    relay.setRelayMode(relayModeAutomatic); // Set the relay to automatic mode

    // Initialize the display
    display.begin(SSD1306_SWITCHCAPVCC, myDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS); // Initialize with I2C address
    display.clearDisplay(); // Clear the display buffer
    display.display(); // Show the display buffer on the screen

    // Initialize U8G2_FOR_ADAFRUIT_GFX with the display
    u8g2_for_adafruit_gfx.begin(display); // Connect U8G2 procedures to Adafruit GFX
}

void loop(void)
{
    // put your main code here, to run repeatedly:
    updateOutputs(); // Refresh output data
    button.read(); // Continuously read the status of the button
    relay.loop(); // Call relay loop to manage relay timing

    // Handle client requests
    server.handleClient(); // Check for incoming client requests

    // Example of using U8G2_FOR_ADAFRUIT_GFX to display text
    display.clearDisplay(); // Clear the display
    u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR14_tf); // Set font
    u8g2_for_adafruit_gfx.setCursor(0, 20); // Set cursor position
    u8g2_for_adafruit_gfx.print("Hello World"); // Print text
    u8g2_for_adafruit_gfx.setCursor(0, 40); // Set cursor position
    u8g2_for_adafruit_gfx.print("Umlaut ÄÖÜ"); // Print text with umlaut
    display.display(); // Show the display buffer
    delay(2000); // Delay for 2 seconds
}

void updateOutputs()
{
    digitalWrite(relay_RelayModule_Signal_PIN_D13, relay_RelayModule_Signal_PIN_D13_rawData);
}

// Function to configure WiFi
void configureWiFi() {
    const char* ssid = "your_SSID"; // Replace with your WiFi SSID
    const char* password = "your_PASSWORD"; // Replace with your WiFi password

    WiFi.mode(WIFI_AP_STA); // Set WiFi mode to AP and STA
    WiFi.softAP("ESP32_AP"); // Start an access point
    WiFi.begin(ssid, password); // Connect to the WiFi network

    // Wait for connection
    while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.println("Connecting to WiFi...");
    }
    Serial.println("Connected to WiFi");
}

// Function to handle root URL
void handleRoot() {
    server.send(200, "text/html", "

Welcome to ESP32 Web Server

Configure your settings here.

"); } /* END CODE */