Advertisement
pleasedontcode

WiFi Button rev_92

Oct 1st, 2024
61
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 Button
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-10-01 21:29:52
  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 <WiFi.h> // Include WiFi library for network connectivity
  27. #include <EasyButton.h> //https://github.com/evert-arias/EasyButton
  28. #include <Adafruit_SSD1306.h>   //https://github.com/stblassitude/Adafruit_SSD1306_Wemos_OLED.git
  29. #include <U8g2_for_Adafruit_GFX.h>  //https://github.com/olikraus/U8g2_for_Adafruit_GFX
  30. #include "DisplayHandler.h" // Include the display handler header
  31. #include "EasyButtonHandler.h" // Include the EasyButton handler header
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36. void updateOutputs();
  37. void configureWiFi(); // Function to configure WiFi settings
  38.  
  39. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  40. const uint8_t test_PushButton_PIN_D4        = 4;
  41.  
  42. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  43. const uint8_t relay_RelayModule_Signal_PIN_D13      = 13;
  44.  
  45. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  46. bool    relay_RelayModule_Signal_PIN_D13_rawData        = 0;
  47.  
  48. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  49. float   relay_RelayModule_Signal_PIN_D13_phyData        = 0.0;
  50.  
  51. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  52. // Create an EasyButton object for the button
  53. EasyButton button1(test_PushButton_PIN_D4); // Initialize button with pin D4
  54.  
  55. // Create an Adafruit_SSD1306 object for the OLED display
  56. // The display object is now initialized in DisplayHandler.h
  57.  
  58. // Create a U8G2_FOR_ADAFRUIT_GFX object
  59. U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;
  60.  
  61. void setup(void)
  62. {
  63.     // put your setup code here, to run once:
  64.     Serial.begin(115200); // Initialize serial communication for debugging
  65.  
  66.     pinMode(test_PushButton_PIN_D4, INPUT_PULLUP);
  67.     pinMode(relay_RelayModule_Signal_PIN_D13, OUTPUT);
  68.  
  69.     // Initialize the button
  70.     button1.begin();
  71.     button1.onPressed(onButton1Pressed); // Set the callback for button press
  72.  
  73.     // Initialize the OLED display
  74.     initializeDisplay(); // Call the display initialization function
  75.  
  76.     // Configure WiFi settings
  77.     configureWiFi(); // Call the function to configure WiFi
  78. }
  79.  
  80. void loop(void)
  81. {
  82.     // put your main code here, to run repeatedly:
  83.     updateOutputs(); // Refresh output data
  84.     button1.read(); // Read the button state
  85. }
  86.  
  87. void updateOutputs()
  88. {
  89.     digitalWrite(relay_RelayModule_Signal_PIN_D13, relay_RelayModule_Signal_PIN_D13_rawData);
  90. }
  91.  
  92. // Callback function for button press
  93. void onButton1Pressed() {
  94.     Serial.println("Button1 pressed"); // Print message to serial monitor
  95. }
  96.  
  97. // Function to configure WiFi settings
  98. void configureWiFi() {
  99.     const char* ssid = "Your_SSID"; // Replace with your WiFi SSID
  100.     const char* password = "Your_PASSWORD"; // Replace with your WiFi password
  101.  
  102.     // Start WiFi in AP mode
  103.     WiFi.softAP(ssid, password);
  104.     Serial.println("Access Point started");
  105.  
  106.     // Start WiFi in STA mode
  107.     WiFi.begin(ssid, password);
  108.     while (WiFi.status() != WL_CONNECTED) {
  109.         delay(1000);
  110.         Serial.println("Connecting to WiFi...");
  111.     }
  112.     Serial.println("Connected to WiFi");
  113.    
  114.     // Set timezone (example: GMT+0)
  115.     configTime(0, 0, "pool.ntp.org", "time.nist.gov"); // Configure NTP server for time synchronization
  116. }
  117.  
  118. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement