#include <FirebaseESP32.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>
// Provide the token generation process info.
#include "addons/TokenHelper.h"
#include "addons/RTDBHelper.h"
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define API_KEY "AIzaSyAnIyBy4OkkpnzfaqwVfdFFsntPQZNqgoc"
#define DATABASE_URL "https://votersdatabase-default-rtdb.firebaseio.com/"
#define LED1_PIN 12
#define LED2_PIN 14
#define LDR_PIN 36
FirebaseJson json;
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
unsigned long sendDataPrevMillis=0;
bool signupOK = false;
int ldrData = 0;
float voltage = 0.0;
unsigned long count = 0;
void setup() {
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();
Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
config.api_key = API_KEY;
config.database_url = DATABASE_URL;
if(Firebase.signUp(&config, &auth, "" , ""))
{
Serial.println("Firebase signup OK");
signupOK = true;
}
else
{
Serial.printf( "%s\n", config.signer.signupError.message.c_str() );
}
config.token_status_callback = tokenStatusCallback;
Firebase.begin(&config, &auth);
Firebase.reconnectNetwork(true);
//Firebase.Json(true);
}
String defaultPath = "1CJZMmYwsDLgfE5_KTIlcwavROm_LYBryyCkcgagFdVc";
int cnt = 2;
int val = 123456;
void loop()
{
/*
String path2 = defaultPath + "/Candidates";
String nan = "John Doe" + String(cnt++);
json.set("ID", "2");
json.set("CandiID", "C2");
json.set("ElectID", "E1");
json.set("FingID", "F1");
// Define the path to store JSON data
String path = defaultPath + "/Voting/" + 2;
// Send JSON data to Firebase
if ( Firebase.setJSON(fbdo, path.c_str(), json))
{
Serial.println("Data sent to Firebase successfully");
}
else
{
Serial.print("Failed to send data to Firebase: ");
Serial.println(fbdo.errorReason());
}
delay(10000);
*/
/*
String path = defaultPath + "/record";
// Retrieve JSON data from Firebase
if (Firebase.getJSON(fbdo, path.c_str())) {
Serial.println("Data retrieved from Firebase:");
Serial.println(fbdo.jsonString());
// Deserialize the retrieved JSON
//StaticJsonDocument<200> doc;
//deserializeJson(doc, fbdo.jsonString());
//String sensor = doc["name"];
//bool temperature = doc["attendance"];
//unsigned long timestamp = doc["timestamp"];
//Serial.print("Sensor: ");
//Serial.println(sensor);
//Serial.print("Temperature: ");
//Serial.println(temperature);
//Serial.print("Timestamp: ");
//Serial.println(timestamp);
} else {
Serial.print("Failed to retrieve data from Firebase: ");
Serial.println(fbdo.errorReason());
}
delay(100000); // Wait for 10 seconds before next iteration
*/
if (Firebase.ready() && signupOK &&
(millis() - sendDataPrevMillis > 15000 || sendDataPrevMillis == 0))
{
sendDataPrevMillis = millis();
String path = defaultPath + "/Fingers";
//Firebase.getJSON(fbdo, path.c_str()
if (Firebase.getString(fbdo, path.c_str()))
{
Serial.println(fbdo.stringData());
Serial.println();
Serial.println(fbdo.jsonString());
//Deserialize the retrieved JSON
StaticJsonDocument<1024> doc1;
DeserializationError error1 = deserializeJson(doc1, fbdo.stringData());
if(error1)
{
Serial.print("Failed to parse JSON2: ");
Serial.println(error1.f_str());
}
for (JsonVariant value : doc1.as<JsonArray>()) {
if (!value.isNull()) {
// Extract data from each JSON object in the array
bool attendance = value["attendance"];
const char* name = value["name"];
unsigned long timestamp = value["timestamp"];
// Print data to Serial
Serial.print("Name: ");
Serial.println(name);
Serial.print("Attendance: ");
Serial.println(attendance ? "Present" : "Absent");
Serial.print("Timestamp: ");
Serial.println(timestamp);
Serial.println();
}
}
// Count non-null entries in the JSON array
int count = 0;
for (JsonVariant value : doc1.as<JsonArray>()) {
if (!value.isNull()) {
count++;
}
}
// Print the count
Serial.print("Number of non-null entries: ");
Serial.println(count);
}
else
{
Serial.println(fbdo.errorReason());
}
}
else
{
Serial.println("Firebase not ready");
}
delay(10000);
}
esp:0
esp:2
esp:4
esp:5
esp:12
esp:13
esp:14
esp:15
esp:16
esp:17
esp:18
esp:19
esp:21
esp:22
esp:23
esp:25
esp:26
esp:27
esp:32
esp:33
esp:34
esp:35
esp:3V3
esp:EN
esp:VP
esp:VN
esp:GND.1
esp:D2
esp:D3
esp:CMD
esp:5V
esp:GND.2
esp:TX
esp:RX
esp:GND.3
esp:D1
esp:D0
esp:CLK