Advertisement
pleasedontcode

"Wi-Fi Sync" rev_03

Jun 11th, 2024
389
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: "Wi-Fi Sync"
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-06-11 15:53:49
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Develop ESP32 code for LED blinking patterns on */
  21.     /* two ESP32 devices using GPIO pins 23, 22, 21, and */
  22.     /* 19 for Yellow, Red, Green and  Blue LEDs with pre- */
  23.     /* programmed delays. Integrate Firebase for web */
  24.     /* hosting, authentication, and real-time database. */
  25. /****** SYSTEM REQUIREMENT 2 *****/
  26.     /* Firebase authentication to both esp32 devices and */
  27.     /* logins are not hardcoded but authenticated by */
  28.     /* firebase. user UIDs are stored in Firebase user */
  29.     /* can register email/password for login or use */
  30.     /* google login. */
  31. /****** END SYSTEM REQUIREMENTS *****/
  32.  
  33. /****** DEFINITION OF LIBRARIES *****/
  34. #include <WiFi.h>
  35. #include <FirebaseESP32.h>  //https://github.com/mobizt/Firebase-ESP32
  36. #include <addons/TokenHelper.h> // Include the token helper header
  37. #include <addons/RTDBHelper.h>  // Include the RTDB helper header
  38.  
  39. /****** FUNCTION PROTOTYPES *****/
  40. void setup(void);
  41. void loop(void);
  42. void updateOutputs(void);
  43. void blinkLEDs(void);
  44.  
  45. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  46. const uint8_t GPIO23_LED_PIN_D4 = 23;
  47. const uint8_t GPIO22_LED_PIN_D13 = 22;
  48. const uint8_t GPIO21_LED_PIN_D14 = 21;
  49. const uint8_t GPIO19_LED_PIN_D16 = 19;
  50.  
  51. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  52. /***** used to store raw data *****/
  53. bool GPIO23_LED_PIN_D4_rawData = 0;
  54. bool GPIO22_LED_PIN_D13_rawData = 0;
  55. bool GPIO21_LED_PIN_D14_rawData = 0;
  56. bool GPIO19_LED_PIN_D16_rawData = 0;
  57.  
  58. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  59. /***** used to store data after characteristic curve transformation *****/
  60. float GPIO23_LED_PIN_D4_phyData = 0.0;
  61. float GPIO22_LED_PIN_D13_phyData = 0.0;
  62. float GPIO21_LED_PIN_D14_phyData = 0.0;
  63. float GPIO19_LED_PIN_D16_phyData = 0.0;
  64.  
  65. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  66. FirebaseData fbdo;
  67. FirebaseAuth auth;
  68. FirebaseConfig config;
  69.  
  70. /* WiFi credentials */
  71. #define WIFI_SSID "WIFI_AP"
  72. #define WIFI_PASSWORD "WIFI_PASSWORD"
  73.  
  74. /* Firebase project API Key */
  75. #define API_KEY "API_KEY"
  76.  
  77. /* Firebase Realtime Database URL */
  78. #define DATABASE_URL "URL" //<databaseName>.firebaseio.com or <databaseName>.<region>.firebasedatabase.app
  79.  
  80. /* Firebase user Email and password */
  81. #define USER_EMAIL "USER_EMAIL"
  82. #define USER_PASSWORD "USER_PASSWORD"
  83.  
  84. unsigned long previousMillis = 0; // will store last time LED was updated
  85. const long interval = 1000; // interval at which to blink (milliseconds)
  86.  
  87. void setup(void)
  88. {
  89.     // Initialize serial communication
  90.     Serial.begin(115200);
  91.  
  92.     // Initialize GPIO pins as outputs
  93.     pinMode(GPIO23_LED_PIN_D4, OUTPUT);
  94.     pinMode(GPIO22_LED_PIN_D13, OUTPUT);
  95.     pinMode(GPIO21_LED_PIN_D14, OUTPUT);
  96.     pinMode(GPIO19_LED_PIN_D16, OUTPUT);
  97.  
  98.     // Connect to Wi-Fi
  99.     WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  100.     Serial.print("Connecting to Wi-Fi");
  101.     while (WiFi.status() != WL_CONNECTED)
  102.     {
  103.         Serial.print(".");
  104.         delay(300);
  105.     }
  106.     Serial.println();
  107.     Serial.print("Connected with IP: ");
  108.     Serial.println(WiFi.localIP());
  109.  
  110.     // Configure Firebase
  111.     config.api_key = API_KEY;
  112.     auth.user.email = USER_EMAIL;
  113.     auth.user.password = USER_PASSWORD;
  114.     config.database_url = DATABASE_URL;
  115.  
  116.     // Enable automatic reconnect to Wi-Fi
  117.     Firebase.reconnectWiFi(true);
  118.     fbdo.setResponseSize(4096);
  119.     config.token_status_callback = tokenStatusCallback; // see addons/TokenHelper.h
  120.     Firebase.begin(&config, &auth);
  121. }
  122.  
  123. void loop(void)
  124. {
  125.     // Refresh output data
  126.     updateOutputs();
  127.     // Blink LEDs based on interval
  128.     blinkLEDs();
  129. }
  130.  
  131. void updateOutputs()
  132. {
  133.     // Update the state of the LEDs
  134.     digitalWrite(GPIO23_LED_PIN_D4, GPIO23_LED_PIN_D4_rawData);
  135.     digitalWrite(GPIO22_LED_PIN_D13, GPIO22_LED_PIN_D13_rawData);
  136.     digitalWrite(GPIO21_LED_PIN_D14, GPIO21_LED_PIN_D14_rawData);
  137.     digitalWrite(GPIO19_LED_PIN_D16, GPIO19_LED_PIN_D16_rawData);
  138. }
  139.  
  140. void blinkLEDs()
  141. {
  142.     unsigned long currentMillis = millis();
  143.  
  144.     // Check if the interval has passed
  145.     if (currentMillis - previousMillis >= interval)
  146.     {
  147.         // Save the last time the LEDs were updated
  148.         previousMillis = currentMillis;
  149.  
  150.         // Toggle the state of the LEDs
  151.         GPIO23_LED_PIN_D4_rawData = !GPIO23_LED_PIN_D4_rawData;
  152.         GPIO22_LED_PIN_D13_rawData = !GPIO22_LED_PIN_D13_rawData;
  153.         GPIO21_LED_PIN_D14_rawData = !GPIO21_LED_PIN_D14_rawData;
  154.         GPIO19_LED_PIN_D16_rawData = !GPIO19_LED_PIN_D16_rawData;
  155.     }
  156. }
  157.  
  158. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement