-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfirestore.cpp
More file actions
91 lines (78 loc) · 2.65 KB
/
firestore.cpp
File metadata and controls
91 lines (78 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <WiFi.h>
#include <Firebase_ESP_Client.h>
#include "addons/TokenHelper.h"
// Replace these with your WiFi credentials
const char* ssid = "Dialog 4G 370";
const char* password = "Fc5814ED";
// Replace these with your Firebase project credentials
#define API_KEY "AIzaSyAxgTKliDO4MNop_gZEH_led3kpI2MlfBs"
#define FIREBASE_PROJECT_ID "first-year-hardware-project"
#define USER_EMAIL "your_email@example.com"
#define USER_PASSWORD "your_password"
#define DATABASE_URL "https://first-year-hardware-project.firebaseio.com" // Add your Firebase database URL
// Initialize Firebase data object
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
void FirestoreTokenStatusCallback(TokenInfo info) {
Serial.printf("Token Info: type = %s, status = %s\n", getTokenType(info), getTokenStatus(info));
}
void firebaseInit() {
config.api_key = API_KEY;
config.database_url = DATABASE_URL; // Set the database URL
auth.user.email = USER_EMAIL;
auth.user.password = USER_PASSWORD;
config.token_status_callback = FirestoreTokenStatusCallback;
Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);
}
String getFormattedDate() {
time_t now;
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
return "";
}
char buffer[11];
strftime(buffer, sizeof(buffer), "%Y-%m-%d", &timeinfo);
return String(buffer);
}
String getFormattedTime() {
time_t now;
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
return "";
}
char buffer[9];
strftime(buffer, sizeof(buffer), "%H:%M:%S", &timeinfo);
return String(buffer);
}
void firestoreDataUpdate() {
if (WiFi.status() == WL_CONNECTED && Firebase.ready()) {
String collectionPath = "time";
FirebaseJson content;
content.set("fields/date/stringValue", getFormattedDate());
content.set("fields/time/stringValue", getFormattedTime());
if (Firebase.Firestore.createDocument(&fbdo, FIREBASE_PROJECT_ID, "", collectionPath.c_str(), content.raw())) {
Serial.printf("Document created: %s\n\n", fbdo.payload().c_str());
} else {
Serial.println(fbdo.errorReason());
}
}
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
configTime(0, 0, "pool.ntp.org", "time.nist.gov"); // Configure time service
firebaseInit();
}
void loop() {
firestoreDataUpdate();
delay(60000); // Delay for a minute before next reading
}