Advertisement
pleasedontcode

**WiFi Firebase** rev_01

Jan 22nd, 2025
76
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 Firebase**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-01-22 10:26:19
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* I want to push the data in two different databases */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <WiFi.h>
  27. #include <FirebaseESP32.h>
  28. #include <HTTPClient.h>
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. // WiFi and Firebase configuration
  35. const char* ssid = "your_SSID"; // Replace with your network SSID
  36. const char* password = "your_PASSWORD"; // Replace with your network password
  37. #define FIREBASE_HOST "your_project_id.firebaseio.com" // Replace with your Firebase project ID
  38. #define FIREBASE_AUTH "your_database_secret" // Replace with your Firebase database secret
  39.  
  40. FirebaseData firebaseData;
  41.  
  42. // Function to connect to WiFi
  43. void connectToWiFi() {
  44.     WiFi.begin(ssid, password);
  45.     while (WiFi.status() != WL_CONNECTED) {
  46.         delay(1000);
  47.     }
  48. }
  49.  
  50. // Function to push data to Firebase
  51. void pushDataToFirebase(const String& path, const String& data) {
  52.     if (Firebase.setString(firebaseData, path, data)) {
  53.         Serial.println("Data pushed to Firebase successfully");
  54.     } else {
  55.         Serial.print("Error pushing data: ");
  56.         Serial.println(firebaseData.errorReason());
  57.     }
  58. }
  59.  
  60. // Function to push data to another database (HTTP example)
  61. void pushDataToAnotherDatabase(const String& url, const String& data) {
  62.     HTTPClient http;
  63.     http.begin(url);
  64.     http.addHeader("Content-Type", "application/json");
  65.    
  66.     int httpResponseCode = http.POST(data);
  67.     if (httpResponseCode > 0) {
  68.         String response = http.getString();
  69.         Serial.println(httpResponseCode);
  70.         Serial.println(response);
  71.     } else {
  72.         Serial.print("Error on sending POST: ");
  73.         Serial.println(httpResponseCode);
  74.     }
  75.     http.end();
  76. }
  77.  
  78. void setup(void) {
  79.     Serial.begin(115200);
  80.     connectToWiFi();
  81.     Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
  82.     Firebase.reconnectWiFi(true);
  83. }
  84.  
  85. void loop(void) {
  86.     // Example data to push
  87.     String data = "{\"value\": 123}"; // Replace with your actual data
  88.  
  89.     // Push data to Firebase
  90.     pushDataToFirebase("/data/path", data); // Replace with your Firebase path
  91.  
  92.     // Push data to another database
  93.     pushDataToAnotherDatabase("http://example.com/api/data", data); // Replace with your actual API endpoint
  94.  
  95.     delay(10000); // Wait for 10 seconds before the next loop
  96. }
  97.  
  98. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement