Advertisement
pleasedontcode

WifiCommunication

Feb 1st, 2024
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 3.27 KB | Source Code | 0 0
  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: WifiCommunication
  13.     - Source Code compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-02-01 09:19:30
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The system shall establish a WiFi connection on */
  21.     /* power-up using the SSID "test" and password */
  22.     /* "123456". If the network is unavailable, it shall */
  23.     /* create an access point with random network data. */
  24. /****** SYSTEM REQUIREMENT 2 *****/
  25.     /* When the push button connected to pin D4 is */
  26.     /* pressed, the system shall reset itself. */
  27. /****** END SYSTEM REQUIREMENTS *****/
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <EasyButton.h>    //https://github.com/evert-arias/EasyButton
  31. #include <WiFi.h>
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36. void establishWiFiConnection();
  37. void createAccessPoint();
  38.  
  39. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  40. const uint8_t button_PushButton_PIN_D4 = 4;
  41.  
  42. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  43. EasyButton button(button_PushButton_PIN_D4);
  44.  
  45. void setup(void)
  46. {
  47.     // put your setup code here, to run once:
  48.     button.begin(); // Initialize the EasyButton object
  49.  
  50.     // Establish Wi-Fi connection
  51.     establishWiFiConnection();
  52. }
  53.  
  54. void loop(void)
  55. {
  56.     // put your main code here, to run repeatedly:
  57.     button.read(); // Read the button state
  58.  
  59.     // Check if button is pressed to reset the system
  60.     if (button.isPressed())
  61.     {
  62.         // Reset the system
  63.         Serial.println("Resetting the system...");
  64.         delay(1000);
  65.         ESP.restart();
  66.     }
  67.  
  68.     // Add your code logic depending on the button state
  69.  
  70. }
  71.  
  72. void establishWiFiConnection()
  73. {
  74.     // Connect to the Wi-Fi network with the specified SSID and password
  75.     const char* ssid = "test";
  76.     const char* password = "123456";
  77.     WiFi.begin(ssid, password);
  78.  
  79.     Serial.print("Connecting to Wi-Fi");
  80.  
  81.     // Wait until connected to the Wi-Fi network or timeout exceeded
  82.     int timeout = 10; // Timeout in seconds
  83.     while (WiFi.status() != WL_CONNECTED && timeout > 0)
  84.     {
  85.         delay(1000);
  86.         Serial.print(".");
  87.         timeout--;
  88.     }
  89.  
  90.     // If Wi-Fi connection failed, create an access point with random network data
  91.     if (WiFi.status() != WL_CONNECTED)
  92.     {
  93.         createAccessPoint();
  94.     }
  95.     else
  96.     {
  97.         Serial.println("Connected to Wi-Fi");
  98.         Serial.print("IP address: ");
  99.         Serial.println(WiFi.localIP());
  100.     }
  101. }
  102.  
  103. void createAccessPoint()
  104. {
  105.     // Create an access point with random network data
  106.     const char* ssid = "My_AP";
  107.     const char* password = "My_Password";
  108.     WiFi.softAP(ssid, password);
  109.  
  110.     Serial.println("Created access point");
  111.     Serial.print("IP address: ");
  112.     Serial.println(WiFi.softAPIP());
  113. }
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement