CCCPaste

WifiCommunication

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: WifiCommunication
	- Source Code compiled for: ESP32 DevKit V1
	- Source Code created on: 2024-02-01 09:19:30

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* The system shall establish a WiFi connection on */
	/* power-up using the SSID "test" and password */
	/* "123456". If the network is unavailable, it shall */
	/* create an access point with random network data. */
/****** SYSTEM REQUIREMENT 2 *****/
	/* When the push button connected to pin D4 is */
	/* pressed, the system shall reset itself. */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include     //https://github.com/evert-arias/EasyButton
#include 

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void establishWiFiConnection();
void createAccessPoint();

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t button_PushButton_PIN_D4 = 4;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
EasyButton button(button_PushButton_PIN_D4);

void setup(void)
{
    // put your setup code here, to run once:
    button.begin(); // Initialize the EasyButton object

    // Establish Wi-Fi connection
    establishWiFiConnection();
}

void loop(void)
{
    // put your main code here, to run repeatedly:
    button.read(); // Read the button state

    // Check if button is pressed to reset the system
    if (button.isPressed())
    {
        // Reset the system
        Serial.println("Resetting the system...");
        delay(1000);
        ESP.restart();
    }

    // Add your code logic depending on the button state

}

void establishWiFiConnection()
{
    // Connect to the Wi-Fi network with the specified SSID and password
    const char* ssid = "test";
    const char* password = "123456";
    WiFi.begin(ssid, password);

    Serial.print("Connecting to Wi-Fi");

    // Wait until connected to the Wi-Fi network or timeout exceeded
    int timeout = 10; // Timeout in seconds
    while (WiFi.status() != WL_CONNECTED && timeout > 0)
    {
        delay(1000);
        Serial.print(".");
        timeout--;
    }

    // If Wi-Fi connection failed, create an access point with random network data
    if (WiFi.status() != WL_CONNECTED)
    {
        createAccessPoint();
    }
    else
    {
        Serial.println("Connected to Wi-Fi");
        Serial.print("IP address: ");
        Serial.println(WiFi.localIP());
    }
}

void createAccessPoint()
{
    // Create an access point with random network data
    const char* ssid = "My_AP";
    const char* password = "My_Password";
    WiFi.softAP(ssid, password);

    Serial.println("Created access point");
    Serial.print("IP address: ");
    Serial.println(WiFi.softAPIP());
}