-
Notifications
You must be signed in to change notification settings - Fork 5
/
firebase_chat_user_2.ino
89 lines (71 loc) · 2.54 KB
/
firebase_chat_user_2.ino
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
#include <ESP8266WiFi.h>
#include <FirebaseESP8266.h>
#include <addons/TokenHelper.h>
/* 1. Define the WiFi credentials */
#define WIFI_SSID "Redmi"
#define WIFI_PASSWORD "ZXCV@098"
//For the following credentials, see examples/Authentications/SignInAsUser/EmailPassword/EmailPassword.ino
/* 2. Define the API Key */
#define API_KEY "AIzaSyBGzWjAUjVNDBKwcvR1ZVm7LEpgzWjX6NQ"
/* 3. Define the RTDB URL */
#define DATABASE_URL "https://fir-demo-56b8f-default-rtdb.firebaseio.com/" //<databaseName>.firebaseio.com or <databaseName>.<region>.firebasedatabase.app
/* 4. Define the user Email and password that alreadey registerd or added in your project */
#define USER_EMAIL "[email protected]"
#define USER_PASSWORD "password"
//Define Firebase Data object
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
unsigned long lastReadTime = 0;
String prevData = "";
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();
/* 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.begin(&config, &auth);
//Comment or pass false value when WiFi reconnection will control by your code or third party library
Firebase.reconnectWiFi(true);
}
void loop()
{
if (Firebase.ready() && Serial.available())
{
//Your text messages
String user1Data = Serial.readString();
//Serial.printf("User 2 : %s\n", Firebase.setString(fbdo, "/user2/data",user1Data) ? user1Data : fbdo.errorReason().c_str());
Firebase.setString(fbdo, "/user2/data",user1Data);
Serial.print("User 2 :");
Serial.println(user1Data);
}
if(millis() - lastReadTime > 2000)
{
//Reading text messages
Firebase.getString(fbdo, "/user1/data");
String user2Data = fbdo.to<String>();
if(prevData != user2Data)
{
Serial.println("User 1 : "+user2Data);
}
prevData = user2Data;
lastReadTime = millis();
}
}