Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* 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 Firebase**
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-01-22 10:26:19
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* I want to push the data in two different databases */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <WiFi.h>
- #include <FirebaseESP32.h>
- #include <HTTPClient.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- // WiFi and Firebase configuration
- const char* ssid = "your_SSID"; // Replace with your network SSID
- const char* password = "your_PASSWORD"; // Replace with your network password
- #define FIREBASE_HOST "your_project_id.firebaseio.com" // Replace with your Firebase project ID
- #define FIREBASE_AUTH "your_database_secret" // Replace with your Firebase database secret
- FirebaseData firebaseData;
- // Function to connect to WiFi
- void connectToWiFi() {
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- }
- }
- // Function to push data to Firebase
- void pushDataToFirebase(const String& path, const String& data) {
- if (Firebase.setString(firebaseData, path, data)) {
- Serial.println("Data pushed to Firebase successfully");
- } else {
- Serial.print("Error pushing data: ");
- Serial.println(firebaseData.errorReason());
- }
- }
- // Function to push data to another database (HTTP example)
- void pushDataToAnotherDatabase(const String& url, const String& data) {
- HTTPClient http;
- http.begin(url);
- http.addHeader("Content-Type", "application/json");
- int httpResponseCode = http.POST(data);
- if (httpResponseCode > 0) {
- String response = http.getString();
- Serial.println(httpResponseCode);
- Serial.println(response);
- } else {
- Serial.print("Error on sending POST: ");
- Serial.println(httpResponseCode);
- }
- http.end();
- }
- void setup(void) {
- Serial.begin(115200);
- connectToWiFi();
- Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
- Firebase.reconnectWiFi(true);
- }
- void loop(void) {
- // Example data to push
- String data = "{\"value\": 123}"; // Replace with your actual data
- // Push data to Firebase
- pushDataToFirebase("/data/path", data); // Replace with your Firebase path
- // Push data to another database
- pushDataToAnotherDatabase("http://example.com/api/data", data); // Replace with your actual API endpoint
- delay(10000); // Wait for 10 seconds before the next loop
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement