/********* 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: WiFi Button - Source Code compiled for: ESP32 DevKit V1 - Source Code created on: 2024-03-09 14:11:34 ********* Pleasedontcode.com **********/ /****** SYSTEM REQUIREMENTS *****/ /****** SYSTEM REQUIREMENT 1 *****/ /* Build a smart switch, until 4 wifi nets to be */ /* used. Needs to scan all wifi and select the first */ /* one that connect. If no connect, stay in loop */ /* until connect. To be used with Tuya. send */ /* callmebot when button change the state and serial */ /* to sim800l */ /****** END SYSTEM REQUIREMENTS *****/ /****** DEFINITION OF LIBRARIES *****/ #include //https://github.com/evert-arias/EasyButton #include //https://github.com/tuya/tuya-wifi-mcu-sdk-arduino-library #include //WiFi library for ESP32 /****** FUNCTION PROTOTYPES *****/ void setup(void); void loop(void); void connectToWifi(void); /***** DEFINITION OF DIGITAL INPUT PINS *****/ const uint8_t Sensor_PushButton_PIN_D4 = 4; /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/ EasyButton button(Sensor_PushButton_PIN_D4); TuyaWifi my_device; // Flag to track if WiFi is connected bool wifiConnected = false; void setup(void) { // put your setup code here, to run once: pinMode(Sensor_PushButton_PIN_D4, INPUT_PULLUP); // Initialize the EasyButton object button.begin(); // Initialize the TuyaWifi object unsigned char pid[] = {"ma67l9sgmdyg3d2k"}; unsigned char mcu_ver[] = {"1.0.0"}; my_device.init(pid, mcu_ver); // Connect to WiFi connectToWifi(); } void loop(void) { // put your main code here, to run repeatedly: // Read the button state button.read(); // Check if the button is pressed if (button.isPressed()) { // Button is pressed // Add your code here to handle the button press event // Send callmebot message // Implement serial communication with SIM800L } // Handle the TuyaWifi library operations my_device.uart_service(); delay(10); // Add a small delay to avoid excessive loop iterations } void connectToWifi(void) { // Scan available WiFi networks int numNetworks = WiFi.scanNetworks(); // Check if any networks are found if (numNetworks > 0) { // Loop through each network for (int i = 0; i < numNetworks; i++) { // Get the SSID of the network String ssid = WiFi.SSID(i); // Add your logic to select and connect to the first available network // You can use the WiFi.begin() function to connect to a specific network // Set wifiConnected flag to true if successfully connected wifiConnected = true; break; } } // If no networks are found or failed to connect, stay in loop until connected while (!wifiConnected) { // Retry the connection // You can add a delay here if needed to avoid excessive connection attempts connectToWifi(); } }