Advertisement
pleasedontcode

Button Webserver rev_01

Oct 7th, 2024
69
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: Button Webserver
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-10-07 16:32:39
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* generate an arduino sketch for an ESP32-C6 and a */
  21.     /* W550 SPI1 connected, which handle a ethernet tcp */
  22.     /* socket server in port 3000 */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  27. #include <AsyncWebServer_ESP32_SC_W5500.h>  //https://github.com/khoih-prog/AsyncWebServer_ESP32_SC_W5500
  28. #include <ETH.h> // Include Ethernet library for ESP32
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t nose_PushButton_PIN_D4 = 4;
  36.  
  37. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  38. // Create an instance of EasyButton for the push button
  39. EasyButton button(nose_PushButton_PIN_D4);
  40.  
  41. // Create an instance of AsyncWebServer on port 3000
  42. AsyncWebServer server(3000);
  43.  
  44. /****** FUNCTION PROTOTYPES FOR BUTTON CALLBACKS *****/
  45. void onButtonPressed(void);
  46.  
  47. /****** SETUP FUNCTION *****/
  48. void setup(void)
  49. {
  50.     // Initialize serial communication
  51.     Serial.begin(115200);
  52.    
  53.     // Initialize the button
  54.     button.begin();
  55.    
  56.     // Attach the callback function to the button press event
  57.     button.onPressed(onButtonPressed);
  58.    
  59.     // Set the push button pin mode
  60.     pinMode(nose_PushButton_PIN_D4, INPUT_PULLUP);
  61.    
  62.     // Start the Ethernet connection
  63.     ETH.begin(); // Start Ethernet with default settings
  64.     ETH.config(IPAddress(192, 168, 1, 100), IPAddress(192, 168, 1, 1), IPAddress(255, 255, 255, 0)); // Static IP configuration
  65.  
  66.     // Start the server and define the root endpoint
  67.     server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  68.         request->send(200, "text/plain", "Hello from AsyncWebServer on port 3000!");
  69.     });
  70.    
  71.     // Start the server
  72.     server.begin();
  73. }
  74.  
  75. /****** LOOP FUNCTION *****/
  76. void loop(void)
  77. {
  78.     // Continuously read the button state
  79.     button.read();
  80. }
  81.  
  82. /****** BUTTON CALLBACK FUNCTION *****/
  83. void onButtonPressed(void)
  84. {
  85.     Serial.println("Button pressed");
  86. }
  87.  
  88. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement