0% found this document useful (0 votes)
92 views4 pages

Important Notes Firebase

This Arduino code connects to Firebase to send sensor data. It defines WiFi credentials, Firebase authentication details, and configures the Firebase client. Sensor values for GSR, temperature, and accelerometer data are written to the Realtime Database every second.

Uploaded by

gejetec922
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views4 pages

Important Notes Firebase

This Arduino code connects to Firebase to send sensor data. It defines WiFi credentials, Firebase authentication details, and configures the Firebase client. Sensor values for GSR, temperature, and accelerometer data are written to the Realtime Database every second.

Uploaded by

gejetec922
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

// FIREBASE CODE

// pre

/// FireBase connection


#include <Arduino.h>
#if defined(ESP32)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif
#include <Firebase_ESP_Client.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"

// Insert your network credentials ‫غيرهم حسب الشبكة‬


#define WIFI_SSID "David"
#define WIFI_PASSWORD "mmmmlllld"

// Insert Authorized Email and Corresponding Password


#define USER_EMAIL "[email protected]"
#define USER_PASSWORD "123456"

// Insert Firebase project API Key


#define API_KEY "AIzaSyCHU39KJMTWUhjcRpT9CJaMOhTzTrHJ8lE"

// Insert RTDB URLefine the RTDB URL */


#define DATABASE_URL "https://esp32paula-steam-default-rtdb.europe-
west1.firebasedatabase.app/"

//Define Firebase Data object


FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;

unsigned long sendDataPrevMillis = 0;


int count = 0;

String uid;

///setup
/// FIREBASE ///
Serial.begin(115200);
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();
/* Assign the api key (required) */
config.api_key = API_KEY;

/* Assign the RTDB URL (required) */


config.database_url = DATABASE_URL;

auth.user.email = USER_EMAIL;
auth.user.password = USER_PASSWORD;

Firebase.reconnectWiFi(true);
fbdo.setResponseSize(4096);

// Assign the callback function for the long running token generation task
config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h

// Assign the maximum retry of token generation


config.max_token_generation_retry = 5;

// Initialize the library with the Firebase authen and config


Firebase.begin(&config, &auth);

// Getting the user UID might take a few seconds


Serial.println("Getting User UID");
while ((auth.token.uid) == "") {
Serial.print('.');
delay(1000);
}
// Print user UID
uid = auth.token.uid.c_str();
Serial.print("User UID: ");
Serial.print(uid);

////////////////////////////////

//loop

if (Firebase.isTokenExpired()){
Firebase.refreshToken(&config);
Serial.println("Refresh token");
}

///// FIREBASE CONNECCTION


if (Firebase.ready() && (millis() - sendDataPrevMillis > 1000 ||
sendDataPrevMillis == 0)){
sendDataPrevMillis = millis();
// Write an Int number on the database path test/int
if (Firebase.RTDB.setInt(&fbdo, "GSR/value", gsrValue)){
Serial.println("PASSED");
Serial.println("PATH: " + fbdo.dataPath());
Serial.println("TYPE: " + fbdo.dataType());
}
else {
Serial.println("FAILED");
Serial.println("REASON: " + fbdo.errorReason());
}
}
// temp

if (Firebase.RTDB.setFloat(&fbdo, "temp/value", tempC)){


Serial.println("PASSED");
Serial.println("PATH: " + fbdo.dataPath());
Serial.println("TYPE: " + fbdo.dataType());
}
else {
Serial.println("FAILED");
Serial.println("REASON: " + fbdo.errorReason());
}

if (Firebase.RTDB.setFloat(&fbdo, "AccXX/value", event.acceleration.x)){


Serial.println("PASSED");
Serial.println("PATH: " + fbdo.dataPath());
Serial.println("TYPE: " + fbdo.dataType());
}
else {
Serial.println("FAILED");
Serial.println("REASON: " + fbdo.errorReason());
}

// Write an Int number on the database path test/int


if (Firebase.RTDB.setFloat(&fbdo, "AccYY/value", event.acceleration.y)){
Serial.println("PASSED");
Serial.println("PATH: " + fbdo.dataPath());
Serial.println("TYPE: " + fbdo.dataType());
}
else {
Serial.println("FAILED");
Serial.println("REASON: " + fbdo.errorReason());
}

// Write an Int number on the database path test/int


if (Firebase.RTDB.setFloat(&fbdo, "AccZZ/value", event.acceleration.z)){
Serial.println("PASSED");
Serial.println("PATH: " + fbdo.dataPath());
Serial.println("TYPE: " + fbdo.dataType());
}
else {
Serial.println("FAILED");
Serial.println("REASON: " + fbdo.errorReason());
}

You might also like