CCCPaste

WiFi LED Control rev_02

/********* 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 LED Control
	- Source Code NOT compiled for: ESP32 DevKit V1
	- Source Code created on: 2024-10-29 15:34:21

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* when push button turn on else turn off */
/****** SYSTEM REQUIREMENT 2 *****/
	/* push button when is pressed led is on and display */
	/* in webbrovser */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 	//https://github.com/evert-arias/EasyButton
#include         // Include WiFi library for web server
#include    // Include WebServer library for handling HTTP requests

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t pushbutonled_PushButton_PIN_D4 = 4; // Push button pin
const uint8_t ledPin = 2; // LED pin

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Initialize EasyButton instance for the defined pin
EasyButton button(pushbutonled_PushButton_PIN_D4); // Instance for button on pin D4

// Web server instance
WebServer server(80); // Create a web server on port 80

// Callback function to be called when the button is pressed
void onButtonPressed()
{
    Serial.println("Button pressed");
    digitalWrite(ledPin, HIGH); // Turn on the LED
    server.send(200, "text/plain", "Button is pressed! LED is ON"); // Send response to web browser
}

// Callback function to be called when the button is released
void onButtonReleased()
{
    Serial.println("Button released");
    digitalWrite(ledPin, LOW); // Turn off the LED
    server.send(200, "text/plain", "Button is released! LED is OFF"); // Send response to web browser
}

void setup(void)
{
    // Initialize Serial for debugging purposes
    Serial.begin(115200);
    Serial.println();
    Serial.println(">>> EasyButton example <<<");

    // Set up the button pin as input with pull-up resistors
    pinMode(pushbutonled_PushButton_PIN_D4, INPUT_PULLUP);
    pinMode(ledPin, OUTPUT); // Set LED pin as output

    // Initialize the button instance
    button.begin();

    // Add callback functions for button presses
    button.onPressed(onButtonPressed);
    button.onReleased(onButtonReleased);

    // Connect to WiFi
    WiFi.begin("your_SSID", "your_PASSWORD"); // Replace with your WiFi credentials
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("Connected to WiFi");

    // Start the web server
    server.on("/", []() {
        server.send(200, "text/plain", "Welcome to the Button Control Server!");
    });
    server.begin();
}

void loop(void)
{
    // Continuously read the status of the button
    button.read();
    server.handleClient(); // Handle incoming web requests
}

/* END CODE */