Advertisement
pleasedontcode

WiFi LED Control rev_02

Oct 29th, 2024
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: WiFi LED Control
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-10-29 15:34:21
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* when push button turn on else turn off */
  21. /****** SYSTEM REQUIREMENT 2 *****/
  22.     /* push button when is pressed led is on and display */
  23.     /* in webbrovser */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  28. #include <WiFi.h>        // Include WiFi library for web server
  29. #include <WebServer.h>   // Include WebServer library for handling HTTP requests
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t pushbutonled_PushButton_PIN_D4 = 4; // Push button pin
  37. const uint8_t ledPin = 2; // LED pin
  38.  
  39. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  40. // Initialize EasyButton instance for the defined pin
  41. EasyButton button(pushbutonled_PushButton_PIN_D4); // Instance for button on pin D4
  42.  
  43. // Web server instance
  44. WebServer server(80); // Create a web server on port 80
  45.  
  46. // Callback function to be called when the button is pressed
  47. void onButtonPressed()
  48. {
  49.     Serial.println("Button pressed");
  50.     digitalWrite(ledPin, HIGH); // Turn on the LED
  51.     server.send(200, "text/plain", "Button is pressed! LED is ON"); // Send response to web browser
  52. }
  53.  
  54. // Callback function to be called when the button is released
  55. void onButtonReleased()
  56. {
  57.     Serial.println("Button released");
  58.     digitalWrite(ledPin, LOW); // Turn off the LED
  59.     server.send(200, "text/plain", "Button is released! LED is OFF"); // Send response to web browser
  60. }
  61.  
  62. void setup(void)
  63. {
  64.     // Initialize Serial for debugging purposes
  65.     Serial.begin(115200);
  66.     Serial.println();
  67.     Serial.println(">>> EasyButton example <<<");
  68.  
  69.     // Set up the button pin as input with pull-up resistors
  70.     pinMode(pushbutonled_PushButton_PIN_D4, INPUT_PULLUP);
  71.     pinMode(ledPin, OUTPUT); // Set LED pin as output
  72.  
  73.     // Initialize the button instance
  74.     button.begin();
  75.  
  76.     // Add callback functions for button presses
  77.     button.onPressed(onButtonPressed);
  78.     button.onReleased(onButtonReleased);
  79.  
  80.     // Connect to WiFi
  81.     WiFi.begin("your_SSID", "your_PASSWORD"); // Replace with your WiFi credentials
  82.     while (WiFi.status() != WL_CONNECTED) {
  83.         delay(500);
  84.         Serial.print(".");
  85.     }
  86.     Serial.println("Connected to WiFi");
  87.  
  88.     // Start the web server
  89.     server.on("/", []() {
  90.         server.send(200, "text/plain", "Welcome to the Button Control Server!");
  91.     });
  92.     server.begin();
  93. }
  94.  
  95. void loop(void)
  96. {
  97.     // Continuously read the status of the button
  98.     button.read();
  99.     server.handleClient(); // Handle incoming web requests
  100. }
  101.  
  102. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement