-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathStreamPerformanceTest.ino
More file actions
198 lines (171 loc) · 6.75 KB
/
StreamPerformanceTest.ino
File metadata and controls
198 lines (171 loc) · 6.75 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* The Realtime Database Stream performance test example.
*
* This example will show how fast your device gets the Stream event.
*
* You will get all continouse data changes events from your Realtime Database without data lost.
*
* Open the index.html file with web browser and follow the instructions on that page to test.
*
* For the complete usage guidelines, please read README.md or visit https://github.com/mobizt/FirebaseClient
*
*/
#define ENABLE_USER_AUTH
#define ENABLE_DATABASE
#include <FirebaseClient.h>
#include "ExampleFunctions.h" // Provides the functions used in the examples.
#define WIFI_SSID "WIFI_AP"
#define WIFI_PASSWORD "WIFI_PASSWORD"
#define API_KEY "Web_API_KEY"
#define USER_EMAIL "USER_EMAIL"
#define USER_PASSWORD "USER_PASSWORD"
#define DATABASE_URL "URL"
void processData(AsyncResult &aResult);
SSL_CLIENT ssl_client, stream_ssl_client;
using AsyncClient = AsyncClientClass;
AsyncClient aClient(ssl_client), streamClient(stream_ssl_client);
UserAuth user_auth(API_KEY, USER_EMAIL, USER_PASSWORD, 3000 /* expire period in seconds (<3600) */);
FirebaseApp app;
RealtimeDatabase Database;
uint32_t expected_count = 0, counter = 0, sum = 0, delayMs = 0;
uint8_t ledPin = 0;
int option = 0;
bool sendAck = false, ledStatus = false;
String msg, masterMs, deviceIP;
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();
ssl_client.setInsecure();
stream_ssl_client.setInsecure();
initializeApp(aClient, app, getAuth(user_auth), auth_debug_print, "🔐 authTask");
app.getApp<RealtimeDatabase>(Database);
Database.url(DATABASE_URL);
Database.get(streamClient, "/test/performance", processData, true, "streamTask");
IPAddress ip = WiFi.localIP(); // Can use toString
char buff[20];
snprintf(buff, 20, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
deviceIP = buff;
}
void loop()
{
app.loop();
if (app.ready() && sendAck)
{
sendAck = false;
Database.set<String>(aClient, "/test/ack", msg, processData, "setTask");
}
}
void processData(AsyncResult &aResult)
{
// To maintain the authentication and async tasks
app.loop();
if (aResult.isEvent())
{
Firebase.printf("Event task: %s, msg: %s, code: %d\n", aResult.uid().c_str(), aResult.eventLog().message().c_str(), aResult.eventLog().code());
}
if (aResult.isDebug())
{
Firebase.printf("Debug task: %s, msg: %s\n", aResult.uid().c_str(), aResult.debug().c_str());
}
if (aResult.isError())
{
Firebase.printf("Error task: %s, msg: %s, code: %d\n", aResult.uid().c_str(), aResult.error().message().c_str(), aResult.error().code());
}
if (aResult.available())
{
RealtimeDatabaseResult &stream = aResult.to<RealtimeDatabaseResult>();
if (stream.isStream())
{
if (stream.type() == 0 /* null */)
return;
if (stream.type() == 5 /* string */ && stream.dataPath() == "/chat")
{
String op = stream.to<String>();
if (op.indexOf("hello-") > -1)
{
option = 0;
int start = op.indexOf("-");
masterMs = op.substring(start + 1);
sendAck = true;
msg = "hello-" + masterMs + "-" + deviceIP;
}
if (op.indexOf("print-all-") > -1 || op.indexOf("print-count-") > -1)
{
// print-all-expectedCount-masterMs
// print-count-expectedCount-masterMs
option = op.indexOf("print-all-") > -1 ? 1 : 2;
int start = op.indexOf("-");
start = op.indexOf("-", start + 1);
int end = op.indexOf("-", start + 1);
masterMs = op.substring(op.lastIndexOf("-") + 1);
expected_count = op.substring(start + 1, end).toInt();
sendAck = true;
msg = "accepted-" + masterMs + "-" + deviceIP;
if (option == 1)
Serial.println();
}
else if (op.indexOf("blink-") > -1)
{
// blink-pin-delayMs-expectedCount-helloMs
option = 3;
int start = op.indexOf("-");
int end = op.indexOf("-", start + 1);
ledPin = op.substring(start + 1, end).toInt();
start = op.indexOf("-", start + 1);
end = op.indexOf("-", start + 1);
delayMs = op.substring(start + 1, end).toInt();
start = op.indexOf("-", start + 1);
end = op.indexOf("-", start + 1);
expected_count = op.substring(start + 1, end).toInt();
masterMs = op.substring(op.lastIndexOf("-") + 1);
pinMode(ledPin, OUTPUT);
Firebase.printf("\nBlink LED: %d, Delay: %d ms\n", ledPin, delayMs);
sendAck = true;
msg = "accepted-" + masterMs + "-" + deviceIP;
}
if (option > 0)
{
// Clear counter and sum values
counter = 0;
sum = 0;
}
}
else
{
counter++;
if (stream.type() == 1 /* int */ && stream.dataPath().length())
{
sum += stream.to<int>();
if (option == 1)
Firebase.printf("counter: %d\ndata: %d\nsum: %d\n", counter, stream.to<int>(), sum);
else if (option == 3)
{
// Blink
digitalWrite(ledPin, ledStatus);
ledStatus = !ledStatus;
delay(delayMs);
}
if (expected_count == counter)
{
sendAck = true;
msg = "done-" + masterMs + "-" + deviceIP;
if (option == 2)
Firebase.printf("\ncounter: %d\nsum: %d\n", counter, sum);
Serial.println("Done!");
}
}
}
}
}
}