-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch_jul15c.ino
105 lines (86 loc) · 2.79 KB
/
sketch_jul15c.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <map>
// Replace with your network credentials
const char* ssid = "Eshal";
const char* password = "Eshal123";
// Firebase project credentials
const String firebaseHost = "https://chats-14529-default-rtdb.firebaseio.com/";
const String firebaseAuth = "AIzaSyCPy96R2oLf6ow_kdTLlIrkqvAUefPdSow";
String sender = "ESP32_1"; // Change this identifier for Board 2
std::map<String, bool> displayedMessages; // Track displayed messages
void setup() {
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to WiFi");
Serial.println("Enter your message:");
}
void loop() {
// Receive messages
receiveMessages();
// Check for input from the Serial Monitor
if (Serial.available() > 0) {
String message = Serial.readStringUntil('\n');
sendMessage(message, sender);
}
delay(10000); // Wait 10 seconds before checking for new messages again
}
void sendMessage(const String& message, const String& sender) {
HTTPClient http;
String url = firebaseHost + "/chats/messages.json?auth=" + firebaseAuth;
// Prepare the JSON payload
StaticJsonDocument<200> doc;
doc["message"] = message;
doc["sender"] = sender;
doc["timestamp"] = millis();
String payload;
serializeJson(doc, payload);
// Send HTTP POST request
http.begin(url);
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST(payload);
if (httpResponseCode > 0) {
Serial.println("Message sent successfully");
} else {
Serial.println("Failed to send message: " + String(httpResponseCode));
}
http.end();
}
void receiveMessages() {
HTTPClient http;
String url = firebaseHost + "/chats/messages.json?auth=" + firebaseAuth;
// Send HTTP GET request
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
String response = http.getString();
parseMessages(response);
} else {
Serial.println("Failed to get messages: " + String(httpResponseCode));
}
http.end();
}
void parseMessages(const String& response) {
StaticJsonDocument<2048> doc;
DeserializationError error = deserializeJson(doc, response);
if (error) {
Serial.println("Failed to parse JSON");
return;
}
for (JsonPair kv : doc.as<JsonObject>()) {
String messageId = kv.key().c_str();
if (displayedMessages.find(messageId) == displayedMessages.end()) {
JsonObject messageObj = kv.value().as<JsonObject>();
String message = messageObj["message"];
String sender = messageObj["sender"];
Serial.println("New message from " + sender + ": " + message);
displayedMessages[messageId] = true; // Mark this message as displayed
}
}
}