Advertisement
pleasedontcode

WiFi Button rev_01

Mar 9th, 2024
64
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 compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-03-09 14:11:34
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Build a smart switch, until 4 wifi nets to be */
  21.     /* used. Needs to scan all wifi and select the first */
  22.     /* one that connect. If no connect, stay in loop */
  23.     /* until connect. To be used with Tuya. send */
  24.     /* callmebot when button change the state and serial */
  25.     /* to sim800l */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <EasyButton.h>    //https://github.com/evert-arias/EasyButton
  30. #include <TuyaWifi.h>    //https://github.com/tuya/tuya-wifi-mcu-sdk-arduino-library
  31. #include <WiFi.h>    //WiFi library for ESP32
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36. void connectToWifi(void);
  37.  
  38. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  39. const uint8_t Sensor_PushButton_PIN_D4 = 4;
  40.  
  41. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  42. EasyButton button(Sensor_PushButton_PIN_D4);
  43. TuyaWifi my_device;
  44.  
  45. // Flag to track if WiFi is connected
  46. bool wifiConnected = false;
  47.  
  48. void setup(void)
  49. {
  50.   // put your setup code here, to run once:
  51.  
  52.   pinMode(Sensor_PushButton_PIN_D4, INPUT_PULLUP);
  53.  
  54.   // Initialize the EasyButton object
  55.   button.begin();
  56.  
  57.   // Initialize the TuyaWifi object
  58.   unsigned char pid[] = {"ma67l9sgmdyg3d2k"};
  59.   unsigned char mcu_ver[] = {"1.0.0"};
  60.  
  61.   my_device.init(pid, mcu_ver);
  62.  
  63.   // Connect to WiFi
  64.   connectToWifi();
  65. }
  66.  
  67. void loop(void)
  68. {
  69.   // put your main code here, to run repeatedly:
  70.  
  71.   // Read the button state
  72.   button.read();
  73.  
  74.   // Check if the button is pressed
  75.   if (button.isPressed())
  76.   {
  77.     // Button is pressed
  78.     // Add your code here to handle the button press event
  79.  
  80.     // Send callmebot message
  81.     // Implement serial communication with SIM800L
  82.   }
  83.  
  84.   // Handle the TuyaWifi library operations
  85.   my_device.uart_service();
  86.  
  87.   delay(10); // Add a small delay to avoid excessive loop iterations
  88. }
  89.  
  90. void connectToWifi(void)
  91. {
  92.   // Scan available WiFi networks
  93.   int numNetworks = WiFi.scanNetworks();
  94.  
  95.   // Check if any networks are found
  96.   if (numNetworks > 0)
  97.   {
  98.     // Loop through each network
  99.     for (int i = 0; i < numNetworks; i++)
  100.     {
  101.       // Get the SSID of the network
  102.       String ssid = WiFi.SSID(i);
  103.  
  104.       // Add your logic to select and connect to the first available network
  105.       // You can use the WiFi.begin() function to connect to a specific network
  106.  
  107.       // Set wifiConnected flag to true if successfully connected
  108.       wifiConnected = true;
  109.       break;
  110.     }
  111.   }
  112.  
  113.   // If no networks are found or failed to connect, stay in loop until connected
  114.   while (!wifiConnected)
  115.   {
  116.     // Retry the connection
  117.     // You can add a delay here if needed to avoid excessive connection attempts
  118.     connectToWifi();
  119.   }
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement