UPDATED ESP32 CODE CODE CODE CODE
#include <Arduino.h>
#include <WiFi.h>
#include <FirebaseESP32.h>
// Provide the token generation process info.
#include <addons/TokenHelper.h>
// Provide the RTDB payload printing info and other helper functions.
#include <addons/RTDBHelper.h>
/* 1. Define the WiFi credentials */
#define WIFI_SSID "Galaxy M126967"
#define WIFI_PASSWORD "raji1234"
/* 2. Define the API Key */
#define API_KEY "26Sg2a5KBO7macmeRxeJKnUMw0GJlYbkQHIYGAog"
/* 3. Define the RTDB URL */
#define DATABASE_URL "https://iot-enabled-energy-meter-default-rtdb.asia-southeast1.firebasedatabase.app/" //<databaseName>.firebaseio.com or <databaseName>.<region>.firebasedatabase.app
/* database secret used in Firebase.setQueryIndex function */
#define DATABASE_SECRET "DATABASE_SECRET"
/* 4. Define the user Email and password that already registered or added in your project */
#define USER_EMAIL "brsanjeevadharsh@gmail.com"
#define USER_PASSWORD "USER_PASSWORD"
// Define Firebase Data object
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
unsigned long pushDataMillis = 0;
float lastPower = 0;
float lastEnergy = 0;
bool powerReceived = false;
bool energyReceived = false;
void setup()
{
Serial.begin(115200); // Initialize serial for debugging
Serial1.begin(9600); // Initialize serial for communication with Arduino
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(300);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
/* Assign the api key (required) */
config.api_key = API_KEY;
/* Assign the user sign in credentials */
auth.user.email = USER_EMAIL;
auth.user.password = USER_PASSWORD;
/* Assign the RTDB URL (required) */
config.database_url = DATABASE_URL;
/* Assign the callback function for the long running token generation task */
config.token_status_callback = tokenStatusCallback; // see addons/TokenHelper.h
Firebase.reconnectNetwork(true);
// Set SSL buffer size
fbdo.setBSSLBufferSize(4096, 1024);
Firebase.begin(&config, &auth);
// NTP time sync
configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
}
void loop()
{
// Firebase.ready() should be called repeatedly to handle authentication tasks.
if (Firebase.ready())
{
// Read data from Arduino
if (Serial1.available())
{
String receivedData = Serial1.readStringUntil('\n'); // Read a line from Serial1
receivedData.trim(); // Remove any leading/trailing whitespace
if (receivedData.startsWith("POWER_VALUE:"))
{
lastPower = receivedData.substring(12).toFloat(); // Extract power value
Serial.print("Received power: ");
Serial.println(lastPower);
powerReceived = true;
}
else if (receivedData.startsWith("ENERGY_VALUE:"))
{
lastEnergy = receivedData.substring(13).toFloat(); // Extract energy value
Serial.print("Received energy: ");
Serial.println(lastEnergy);
energyReceived = true;
}
if (powerReceived && energyReceived) {
sendToFirebase(lastPower, lastEnergy);
powerReceived = false;
energyReceived = false;
}
}
}
}
void sendToFirebase(float power, float energy)
{
FirebaseJson json;
json.add("timestamp", (uint32_t)time(nullptr));
json.add("power", power);
json.add("energy", energy);
Serial.print("Pushing data to Firebase: power = ");
Serial.print(power);
Serial.print(", energy = ");
Serial.println(energy);
if (Firebase.push(fbdo, "sensor_data", json))
Serial.println("Data pushed successfully.");
else
Serial.println(fbdo.errorReason());
}
Comments
Post a Comment