Advertisement
pleasedontcode

WiFi Relay rev_70

Aug 8th, 2024
260
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 Relay
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-08-08 05:51:15
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Configure WiFi and timezone settings with web page */
  21.     /* available via both AP and STA */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /****** DEFINITION OF LIBRARIES *****/
  25. #include <Wire.h>
  26. #include <EasyButton.h>    //https://github.com/evert-arias/EasyButton
  27. #include <Relay.h>         //https://github.com/rafaelnsantos/Relay
  28. #include <Adafruit_GFX.h>
  29. #include <Adafruit_SSD1306.h>    //https://github.com/stblassitude/Adafruit_SSD1306_Wemos_OLED.git
  30. #include <U8g2_for_Adafruit_GFX.h> // Include U8g2_for_Adafruit_GFX library
  31. #include <WiFi.h> // Include WiFi library for WiFi functionality
  32. #include <WebServer.h> // Include WebServer library for handling web requests
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37. void handleRoot(); // Function to handle root URL
  38. void configureWiFi(); // Function to configure WiFi
  39.  
  40. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  41. const uint8_t test_PushButton_PIN_D4        = 4;
  42.  
  43. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  44. const uint8_t relay_RelayModule_Signal_PIN_D13        = 13;
  45.  
  46. /***** DEFINITION OF I2C PINS *****/
  47. const uint8_t myDisplay_SSD1306OledDisplay_I2C_PIN_SDA_D21        = 21;
  48. const uint8_t myDisplay_SSD1306OledDisplay_I2C_PIN_SCL_D22        = 22;
  49. const uint8_t myDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS      = 0x3C; // Default I2C address for SSD1306
  50.  
  51. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  52. bool relay_RelayModule_Signal_PIN_D13_rawData = 0;
  53.  
  54. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  55. float relay_RelayModule_Signal_PIN_D13_phyData = 0.0;
  56.  
  57. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  58. // Initialize EasyButton object for the push button
  59. EasyButton button(test_PushButton_PIN_D4); // Using the constructor to initialize the button
  60.  
  61. // Initialize Relay object on pin 13 with a period of 5 seconds
  62. Relay relay(relay_RelayModule_Signal_PIN_D13, 5); // Using the constructor to initialize the relay
  63.  
  64. // Initialize Adafruit_SSD1306 object for the OLED display
  65. #define OLED_RESET -1 // Reset pin not used
  66. 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
  67.  
  68. // Initialize U8G2_FOR_ADAFRUIT_GFX object for font support
  69. U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx; // Using the constructor to initialize U8G2_FOR_ADAFRUIT_GFX
  70.  
  71. // Initialize WebServer object
  72. WebServer server(80); // Create a web server on port 80
  73.  
  74. void setup(void)
  75. {
  76.     Serial.begin(9600); // Initialize serial communication for debugging
  77.  
  78.     pinMode(test_PushButton_PIN_D4, INPUT_PULLUP);
  79.     pinMode(relay_RelayModule_Signal_PIN_D13, OUTPUT);
  80.  
  81.     // Configure WiFi and start the web server
  82.     configureWiFi();
  83.     server.on("/", handleRoot); // Define the root URL handler
  84.     server.begin(); // Start the server
  85.  
  86.     // Initialize the button
  87.     button.begin(); // Call begin to set up the button
  88.     button.onPressed([]() { // Lambda function for button press event
  89.         Serial.println("Button pressed");
  90.     });
  91.  
  92.     // Set relay mode to automatic
  93.     relay.setRelayMode(relayModeAutomatic); // Set the relay to automatic mode
  94.  
  95.     // Initialize the display
  96.     display.begin(SSD1306_SWITCHCAPVCC, myDisplay_SSD1306OledDisplay_I2C_SLAVE_ADDRESS); // Initialize with I2C address
  97.     display.clearDisplay(); // Clear the display buffer
  98.     display.display(); // Show the display buffer on the screen
  99.  
  100.     // Initialize U8G2_FOR_ADAFRUIT_GFX with the display
  101.     u8g2_for_adafruit_gfx.begin(display); // Connect U8G2 procedures to Adafruit GFX
  102. }
  103.  
  104. void loop(void)
  105. {
  106.     // put your main code here, to run repeatedly:
  107.     updateOutputs(); // Refresh output data
  108.     button.read(); // Continuously read the status of the button
  109.     relay.loop(); // Call relay loop to manage relay timing
  110.  
  111.     // Handle client requests
  112.     server.handleClient(); // Check for incoming client requests
  113.  
  114.     // Example of using U8G2_FOR_ADAFRUIT_GFX to display text
  115.     display.clearDisplay(); // Clear the display
  116.     u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR14_tf); // Set font
  117.     u8g2_for_adafruit_gfx.setCursor(0, 20); // Set cursor position
  118.     u8g2_for_adafruit_gfx.print("Hello World"); // Print text
  119.     u8g2_for_adafruit_gfx.setCursor(0, 40); // Set cursor position
  120.     u8g2_for_adafruit_gfx.print("Umlaut ÄÖÜ"); // Print text with umlaut
  121.     display.display(); // Show the display buffer
  122.     delay(2000); // Delay for 2 seconds
  123. }
  124.  
  125. void updateOutputs()
  126. {
  127.     digitalWrite(relay_RelayModule_Signal_PIN_D13, relay_RelayModule_Signal_PIN_D13_rawData);
  128. }
  129.  
  130. // Function to configure WiFi
  131. void configureWiFi() {
  132.     const char* ssid = "your_SSID"; // Replace with your WiFi SSID
  133.     const char* password = "your_PASSWORD"; // Replace with your WiFi password
  134.  
  135.     WiFi.mode(WIFI_AP_STA); // Set WiFi mode to AP and STA
  136.     WiFi.softAP("ESP32_AP"); // Start an access point
  137.     WiFi.begin(ssid, password); // Connect to the WiFi network
  138.  
  139.     // Wait for connection
  140.     while (WiFi.status() != WL_CONNECTED) {
  141.         delay(1000);
  142.         Serial.println("Connecting to WiFi...");
  143.     }
  144.     Serial.println("Connected to WiFi");
  145. }
  146.  
  147. // Function to handle root URL
  148. void handleRoot() {
  149.     server.send(200, "text/html", "<h1>Welcome to ESP32 Web Server</h1><p>Configure your settings here.</p>");
  150. }
  151.  
  152. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement