-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobals.h
382 lines (335 loc) · 11.8 KB
/
globals.h
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#ifdef USE_API
#include <WiFi.h>
#else
#include <WiFiClientSecure.h>
#endif
#include <math.h>
#include <limits.h>
#include <pgmspace.h>
#include "mbedtls/md.h"
#include "esp_wpa2.h"
#include "WiFi.h"
#include "driver/i2s.h"
#include "soc/i2s_reg.h"
#include "time.h"
#include "freertos/queue.h"
#include "Freenove_WS2812_Lib_for_ESP32.h"
#include "credentials.h"
#define DEBUG true
const uint8_t END_READING = 0;
const uint8_t START_READING = 1;
const uint8_t BAD_READING = 2;
const uint8_t COUGH_DETECTED = 3;
// LED
const uint8_t LEDS_COUNT = 1;
const uint8_t LEDS_PIN = 18;
const uint8_t CHANNEL = 0;
// Button
const uint8_t CAPTURE_BUTTON = 32;
const uint8_t BUTTON_THRESH = 1;
const uint16_t DEBOUNCE_US = 250000;
// I2S definitions
const uint16_t SAMPLE_RATE = 44100;
TickType_t xDelay = 32;
size_t bytes_read = 0;
esp_err_t err;
// Networking.
const char* server = "api.olvera-dev.com"; // Example utilizing public webserver.
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 3600;
// Get MAC ADDRESS and SHA256 of MACADDRESS to submit for memo in account creation.
const char *mac_address = (char*) WiFi.macAddress().c_str();
byte sha_256_result[32];
char sha_256_result_string[64];
#ifdef USE_API
WiFiClient net = WiFiClientSecure();
#else
WiFiClientSecure net = WiFiClientSecure();
#endif
const i2s_port_t I2S_PORT = I2S_NUM_0;
int32_t sample = 0;
double output = 0;
double energy = 0;
double snap_one_second = 0;
Freenove_ESP32_WS2812 strip = Freenove_ESP32_WS2812(LEDS_COUNT, LEDS_PIN, CHANNEL, TYPE_RGB);
Freenove_ESP32_WS2812* p_strip = &strip;
static const char ODEV_CERT_CA[] PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----
MIIF3jCCA8agAwIBAgIQAf1tMPyjylGoG7xkDjUDLTANBgkqhkiG9w0BAQwFADCB
iDELMAkGA1UEBhMCVVMxEzARBgNVBAgTCk5ldyBKZXJzZXkxFDASBgNVBAcTC0pl
cnNleSBDaXR5MR4wHAYDVQQKExVUaGUgVVNFUlRSVVNUIE5ldHdvcmsxLjAsBgNV
BAMTJVVTRVJUcnVzdCBSU0EgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMTAw
MjAxMDAwMDAwWhcNMzgwMTE4MjM1OTU5WjCBiDELMAkGA1UEBhMC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2kMVn54CVVeWFPFSPCeHlCjtHzoBN6J2/FNQwISbxmtOuowhT6KO
VWKR82kV2LyI48SqC/3vqOlLVSoGIG1VeCkZ7l8wXEskEVX/JJpuXior7gtNn3/3
ATiUFJVDBwn7YKnuHKsSjKCaXqeYalltiz8I+8jRRa8YFWSQEg9zKC7F4iRO/Fjs
8PRF/iKz6y+O0tlFYQXBl2+odnKPi4w2r78NBc5xjeambx9spnFixdjQg3IM8WcR
iQycE0xyNN+81XHfqnHd4blsjDwSXWXavVcStkNr/+XeTWYRUc+ZruwXtuhxkYze
Sf7dNXGiFSeUHM9h4ya7b6NnJSFd5t0dCy5oGzuCr+yDZ4XUmFF0sbmZgIn/f3gZ
XHlKYC6SQK5MNyosycdiyA5d9zZbyuAlJQG03RoHnHcAP9Dc1ew91Pq7P8yF1m9/
qS3fuQL39ZeatTXaw2ewh0qpKJ4jjv9cJ2vhsE/zB+4ALtRZh8tSQZXq9EfX7mRB
VXyNWQKV3WKdwrnuWih0hKWbt5DHDAff9Yk2dDLWKMGwsAvgnEzDHNb842m1R0aB
L6KCq9NjRHDEjf8tM7qtj3u1cIiuPhnPQCjY/MiQu12ZIvVS5ljFH4gxQ+6IHdfG
jjxDah2nGN59PRbxYvnKkKj9
-----END CERTIFICATE-----
)EOF";
// Button structure.
struct Button {
const uint8_t PIN;
volatile uint8_t pressed_count;
volatile bool pressed;
};
// Timer Class.
class timer{
int64_t current;
int64_t previous;
int64_t mcurrent;
int64_t mprevious;
public:
int utimer(int64_t delta_t){
this->current = esp_timer_get_time(); // Returns the time in microseconds.
if( (this->current - this->previous) >= delta_t ){
this->previous = this->current;
return 1;
}
return 0;
}
int mtimer(int64_t delta_t){
this->mcurrent = esp_timer_get_time() / 1000;
if( (this->mcurrent - this->mprevious) >= delta_t ){
this->mprevious = this->mcurrent;
return 1;
}
return 0;
}
};
void set_device_info(){
mbedtls_md_context_t ctx;
mbedtls_md_type_t md_type = MBEDTLS_MD_SHA256;
const size_t mac_length = strlen(mac_address);
mbedtls_md_init(&ctx);
mbedtls_md_setup(&ctx, mbedtls_md_info_from_type(md_type), 0);
mbedtls_md_starts(&ctx);
mbedtls_md_update(&ctx, (const unsigned char *) mac_address, mac_length);
mbedtls_md_finish(&ctx, sha_256_result);
mbedtls_md_free(&ctx);
}
void get_device_info(){
int j = 0;
for(int i= 0; i< sizeof(sha_256_result); i++){
j += sprintf(sha_256_result_string+j, "%02x", (int)sha_256_result[i]);
}
Serial.printf("Hash: %s\n\r", sha_256_result_string);
}
#ifdef USE_API
void send_device_data(String payload){
if( !net.connect(api_end_point, 9090) ){
Serial.printf("Failed to connect.\n\r");
}
Serial.printf("Connecting to %s\n\r", api_end_point);
// Make HTTP request.
net.printf("POST http://%s/v1/send-espirometer-data HTTP/1.0\n", api_end_point);
net.printf("Host: %s\n", api_end_point);
net.printf("User-Agent: IoT\n");
net.printf("Content-Type: application/x-www-form-urlencoded\n");
// Length will be calculated, not a fan of this method since it will constantly allocate memory leading to memory leaks at some point.
String message = "fileid=4127209"; // First section. Hardcoded file.
//message = message + "&memo=" + String(sha_256_result_string); // Add HASH.
message = message + "&filecontents=" + payload;
message = message + "&pubkey=" + String(DER_PUB_KEY) + "&privkey=" + String(DER_PRIV_KEY);
Serial.printf("Message: %s, Message Length: %d\n\r", message.c_str(), message.length());
net.printf("Content-Length: %d\n\n", message.length());
net.printf("%s", message.c_str());
net.printf("\n");
while (net.connected()) {
String line = net.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
// if there are incoming bytes available
// from the server, read them and print them:
while (net.available()) {
char c = net.read();
Serial.printf("%c", c);
}
Serial.println();
net.stop();
}
// Basic example posting to my server. Will leave this here for examples sake, but will leave commented out.
// This servers as a verification that account exists.
void send_test_message(){
if( !net.connect(api_end_point, 9090) ){
Serial.printf("Failed to connect.\n\r");
}
Serial.printf("Connecting to %s\n\r", api_end_point);
// Make HTTP request.
net.printf("POST http://%s/v1/get-account/memo HTTP/1.0\n", api_end_point);
net.printf("Host: %s\n", api_end_point);
net.printf("User-Agent: IoT\n");
net.printf("Content-Type: application/x-www-form-urlencoded\n");
// Length will be calculated.
String message = "accountid=3590940&memo="; // Definitely don't like doing this.
//message = message + String("auto-created account");
message = message + String(sha_256_result_string);
Serial.printf("Message: %s, Message Length: %d\n\r", message.c_str(), message.length());
net.printf("Content-Length: %d\n\n", message.length());
net.printf("%s", message.c_str());
net.printf("\n");
while (net.connected()) {
String line = net.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
// if there are incoming bytes available
// from the server, read them and print them:
while (net.available()) {
char c = net.read();
Serial.printf("%c", c);
}
Serial.println();
net.stop();
}
#else
// Basic example posting to my server. Will leave this here for examples sake, but will leave commented out.
void send_test_message(){
net.setCACert(ODEV_CERT_CA);
if( !net.connect(server, 443) ){
Serial.printf("Failed to connect.\n\r");
}
Serial.printf("Connecting to %s\n\r", server);
// Make HTTP request.
net.printf("POST https://%s/spirometer/check.php?test=hello HTTP/1.0\n", server);
net.printf("Host: %s\n", server);
net.printf("User-Agent: IoT\n");
net.printf("Content-Type: application/x-www-form-urlencoded\n");
// Length will be calculated.
net.printf("Content-Length: 15\n\n");
net.printf("test=helloworld");
net.printf("\n");
while (net.connected()) {
String line = net.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
// if there are incoming bytes available
// from the server, read them and print them:
while (net.available()) {
char c = net.read();
Serial.printf("%c", c);
}
Serial.println();
net.stop();
}
#endif
// LED Status Functions
// --------------------------------------------
// Reset LED and corresponding function pointer
void reset_LED(Freenove_ESP32_WS2812* s){
s->setLedColorData(0, 0, 0, 0);
s->show();
}
void (*preset_LED)(Freenove_ESP32_WS2812*) = &reset_LED;
// Set LED.
void set_LED(Freenove_ESP32_WS2812* s, int R, int G, int B){
s->setLedColorData(0, R, G, B);
s->show();
}
void (*pset_LED)(Freenove_ESP32_WS2812*, int, int, int) = &set_LED;
// SSID and PASS are stored in a credentials.h, this file is not shared on git for obvious reasons.
// Simply create your own file.
void connect_to_wifi(){
timer wifi_timer;
uint8_t counter = 0;
uint16_t networks = 0;
WiFi.mode(WIFI_STA);
networks = WiFi.scanNetworks();
if (networks == 0) {
Serial.println("No reachable networks found.\n\r");
}
else {
Serial.printf("%d reachable networks found.\n\r", networks);
for (int i = 0; i < networks; ++i) {
if( WiFi.SSID(i) == ssid ){
HOME_NET = 1;
}
else if( WiFi.SSID(i) == school_ssid ){
SCHOOL_NET = 1;
}
}
}
Serial.println("");
// Delete the scan result to free memory for code below.
WiFi.scanDelete();
//connect to WiFi
if ( HOME_NET ){
WiFi.disconnect(true);
Serial.printf("Connecting to %s", ssid);
WiFi.begin(ssid, wifi_password);
}
else if ( SCHOOL_NET ){
WiFi.disconnect(true);
Serial.printf("Connecting to %s", school_ssid);
esp_wpa2_config_t config = WPA2_CONFIG_INIT_DEFAULT();
esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY)); //provide identity
esp_wifi_sta_wpa2_ent_set_username((uint8_t *)EAP_USERNAME, strlen(EAP_USERNAME)); //provide username
esp_wifi_sta_wpa2_ent_set_password((uint8_t *)EAP_PASSWORD, strlen(EAP_PASSWORD)); //provide password
esp_wifi_sta_wpa2_ent_enable(&config);
WiFi.begin(school_ssid);
}
while( WiFi.status() != WL_CONNECTED ) {
if( counter >= 30 ){
Serial.printf("Taking too long. Likely connection will fail. Restarting.\n\r");
esp_restart();
}
delay(250);
Serial.printf(" .");
counter++;
}
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
Serial.printf("Connected to: %s\n\r", WiFi.SSID());
send_test_message();
}
// https://cplusplus.com/reference/ctime/asctime/
// https://cplusplus.com/reference/ctime/tm/
char* alt_asctime(const struct tm *timeptr){
static char result[26];
sprintf(result, "%d-%02d-%02d %d:%d:%d",
1900 + timeptr->tm_year,
1 + timeptr->tm_mon,
timeptr->tm_mday,
timeptr->tm_hour,
timeptr->tm_min,
timeptr->tm_sec
);
return result;
}
char* get_local_time(){
struct tm timeinfo;
while(!getLocalTime(&timeinfo)){
Serial.printf("Failed to obtain time.\n\r");
}
return alt_asctime(&timeinfo);
}