-
Notifications
You must be signed in to change notification settings - Fork 0
/
avoids.ino
704 lines (487 loc) · 18.8 KB
/
avoids.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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
void StartTime(){
// Note: The ESP8266 Time Zone does not function e.g. ,0,"time.nist.gov"
configTime(TZone * 3600, 0, "pool.ntp.org", "time.nist.gov");
// Change this line to suit your time zone, e.g. USA EST configTime(-5 * 3600, 0, "pool.ntp.org", "time.nist.gov");
// Change this line to suit your time zone, e.g. AUS configTime(8 * 3600, 0, "pool.ntp.org", "time.nist.gov");
Serial.println(F("\nWaiting for time"));
while (!time(nullptr)) {
delay(500);
}
Serial.println("Time set");
}
//tAKE A PICTURIII
// Check if photo capture was successful
bool checkPhoto( fs::FS &fs ) {
File f_pic = fs.open( FILE_PHOTO );
unsigned int pic_sz = f_pic.size();
return ( pic_sz > 100 );
}
// Capture Photo and Save it to SPIFFS
void capturePhotoSaveSpiffs( void ) {
//camera_fb_t * fb = NULL; // pointer
bool ok = 0; // Boolean indicating if the picture has been taken correctly
do {
// Take a photo with the camera
Serial.println("Taking a photo...");
//digitalWrite(LED_BUILTIN, LOW);
Serial.println("LED turned ON");
fb = esp_camera_fb_get();
delay(500);
//digitalWrite(LED_BUILTIN, HIGH);
Serial.println("LED turned OFF");
if (!fb) {
Serial.println("Camera capture failed");
return;
}
// Photo file name
Serial.printf("Picture file name: %s\n", FILE_PHOTO);
File file = SPIFFS.open(FILE_PHOTO, FILE_WRITE);
// Insert the data in the photo file
if (!file) {
Serial.println("Failed to open file in writing mode");
}
else {
file.write(fb->buf, fb->len); // payload (image), payload length
Serial.print("The picture has been saved in ");
Serial.print(FILE_PHOTO);
Serial.print(" - Size: ");
Serial.print(file.size());
Serial.println(" bytes");
}
// Close the file
file.close();
delay(300);
esp_camera_fb_return(fb);
// check if file has been correctly saved in SPIFFS
ok = checkPhoto(SPIFFS);
} while ( !ok );
}
//################Send foto telegram
bool isMoreDataAvailable();
byte getNextByte();
File filey = SPIFFS.open(FILE_PHOTO, "r");
//################Send foto telegram
//////////////////////////////// manda foto usando SPIFFS
bool isMoreDataAvailable()
{
return filey.available();
}
byte getNextByte()
{ return filey.read();
}
///////////////////////////////
void sendPhotoTelegram()
{
File filey = SPIFFS.open(FILE_PHOTO, "r");
int newmsgX = bot.getUpdates(bot.last_message_received );
idX = bot.messages[0].chat_id;//Armazenara o ID do Usuario à Váriavel.
if (filey)
{
Serial.println(FILE_PHOTO);
Serial.println("....");
Serial.println(filey.read());
Serial.println(filey.size());
//Content type for PNG image/png
String sent = bot.sendPhotoByBinary(idX, "image/jpeg", filey.size(),
isMoreDataAvailable,
getNextByte, nullptr, nullptr);
if (sent)
{
Serial.println("foto enviada ao telegram");
bot.sendMessage(id, "Take a picture", "");
filey.close();
}
else
{
Serial.println("n enviado");
}
}
else
{
// if the file didn't open, print an error:
Serial.println("error opening photo");
bot.sendMessage(id, "n fique triste tente denovo erro ao acessar o arquivo", "");
}
filey.close();
Serial.println("done funcao , ultima foto enviada do SPIFFS");
}
//////Send camera to telegram
bool isMoreDataAvailablex();
byte *getNextBuffer();
int getNextBufferLen();
bool dataAvailable = false;
void camtelegram( void ){
camera_fb_t * fb = NULL; // pointer
// Take Picture with Camera
fb = esp_camera_fb_get();
if (!fb)
{
Serial.println("Camera capture failed");
bot.sendMessage(id, "Camera capture failed", "");
return;
}
dataAvailable = true;
Serial.println("Sending cam");
bot.sendPhotoByBinary(id, "image/jpeg", fb->len,
isMoreDataAvailablex, nullptr,
getNextBuffer, getNextBufferLen);
Serial.println("mais nada com a cam!");
esp_camera_fb_return(fb);
}
bool isMoreDataAvailablex()
{
if (dataAvailable)
{
dataAvailable = false;
return true;
}
else
{
return false;
}
}
byte *getNextBuffer()
{
if (fb)
{
return fb->buf;
}
else
{
return nullptr;
}
}
int getNextBufferLen()
{
if (fb)
{
return fb->len;
}
else
{
return 0;
}
}
// Sound sensor code
void readVibra(){
valorvibra = digitalRead(vibra);
//Serial.println(valorvibra);
//Serial.println("||");
//Serial.println(analogRead(vibra));
if (valorvibra == 0) {
Serial.println(valorvibra);
clap_counter++;
if (clap_counter > 0) {
//takeNewPhoto = true;
if (led_state) {
led_state = false;
color_counter++;// LED was on, now off
if(color_counter > 8){ color_counter = 0;}
changeColor();
//clap_counter = 0;
// sound_value = 0;
Serial.println("Clap on");
Serial.println(color_counter);
delay(2000);
}
else {
led_state = true;
ledoff();
ledState = "ledoff";
Serial.println("Clap off");
delay(1000);
}}
delay(500);
// Serial.println("sound");
// Serial.println(valorvibra);
// Serial.println(color_counter);
Serial.println("||");
Serial.println(clap_counter);
}}
void changeColor(){
//bot.sendMessage(id, "Alguem bateu palmas, acendendo a luz, mudando intensidade, mudando de cor", "");//Envia uma Mensagem para a pessoa que enviou o Comando.
Serial.println("Luz acionada vibrando");
//muda de cor
if (color_counter == 0)
{
ledState = "ledoff";
}
if (color_counter == 1)
{
ledState = "green";
}
if (color_counter == 2)
{
ledState = "blue";
}
if (color_counter == 3)
{
ledState = "balls";
}
if (color_counter == 4)
{
ledState = "tetris";
}
if (color_counter == 5)
{
ledState = "rainbow2";
}
if (color_counter == 6)
{
ledState = "zebra";
}
if (color_counter == 7)
{
ledState = "clock";
}
if (color_counter == 8)
{
ledState = "btc";
}
//sendPhotoTelegram();
}
void verifica(){
takeNewPhoto = true;
// sendPhotoTelegram();
time_t now = time(nullptr);
time_now = String(ctime(&now)).substring(0,24);
String msg = "Hora:";
msg += time_now;
msg += ",";
msg += "\n";
msg += "Temperatura:";
msg += msg.concat(readDHTTemperature());
msg += "C,";
msg += "\n";
msg += "Umidade:";
msg += msg.concat(readDHTHumidity());
msg += "%,";
msg += "\n";
msg += "Pressao:";
msg += msg.concat(readDHTPressao());
msg += " Pa,";
msg += "\n";
msg += "CO2:";
msg += msg.concat(readCO2());
msg += " PPM,";
msg += "\n";
bot.sendMessage(id, msg, "");
addFile(SPIFFS, climaPath, msg.c_str());
}
void connect()//Funçao para Conectar ao wifi e verificar à conexao.
{
if (WiFi.status() != WL_CONNECTED)//Caso nao esteja conectado ao WiFi, Ira conectarse
{
WiFi.begin(ssid.c_str(), pass.c_str());
//WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
delay(10);
}
}
String crono(){
String segundos;
cSegundos++;
segundos = cSegundos / 100;
delay(1000);
return segundos;
}
String shiba(){
//const String site = "https://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD&e=Coinbase";
const String site = "https://www.bitstamp.net/api/v2/ticker/shibusd";
http.begin(site);
int httpCode = http.GET();
Serial.println(site); //Get crypto price from API
DynamicJsonDocument doc4(2000);
deserializeJson(doc4, http.getString());
// JsonObject obj = doc4.as<JsonObject>();
Serial.print("HTTP Status Code: ");
Serial.println(httpCode);
String SHIBAUSDPrice = doc4["last"].as<String>();
SHIBAUSDPrice += ":24h:";
SHIBAUSDPrice += doc4["percent_change_24"].as<String>();
http.end();
Serial.print("SHIBAUSD Price: ");
Serial.println(SHIBAUSDPrice.toDouble());
http.end();
return SHIBAUSDPrice;
http.end();
delay(5000);
}
String ltc(){
//const String site = "https://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD&e=Coinbase";
const String site = "https://www.bitstamp.net/api/v2/ticker/ltcusd";
http.begin(site);
int httpCode = http.GET();
Serial.println(site); //Get crypto price from API
DynamicJsonDocument doc3(2000);
deserializeJson(doc3, http.getString());
// JsonObject obj = doc3.as<JsonObject>();
Serial.print("HTTP Status Code: ");
Serial.println(httpCode);
String LTCUSDPrice = doc3["last"].as<String>();
LTCUSDPrice += ":24h:";
LTCUSDPrice += doc3["percent_change_24"].as<String>();
http.end();
Serial.print("LTCUSD Price: ");
Serial.println(LTCUSDPrice.toDouble());
Serial.println(LTCUSDPrice);
http.end();
return LTCUSDPrice;
delay(5000);
}
String eth(){
//const String site = "https://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD&e=Coinbase";
const String site = "https://www.bitstamp.net/api/v2/ticker/ethusd";
http.begin(site);
int httpCode = http.GET();
Serial.println(site); //Get crypto price from API
DynamicJsonDocument doc2(2000);
deserializeJson(doc2, http.getString());
JsonObject obj = doc2.as<JsonObject>();
Serial.print("HTTP Status Code: ");
Serial.println(httpCode);
String ETHUSDPrice = obj["last"].as<String>();
ETHUSDPrice += ":24h:";
ETHUSDPrice += obj["percent_change_24"].as<String>();
http.end();
Serial.print("ETHUSD Price: ");
Serial.println(ETHUSDPrice.toDouble());
http.end();
return ETHUSDPrice;
http.end();
delay(5000);
}
String verifica2(){
time_t now = time(nullptr);
time_now = String(ctime(&now)).substring(0,24);
//addFile(SPIFFS, loggerPath, msg.c_str());
DynamicJsonDocument root(200);
//JsonObject root = jsonBuffer.as<JsonObject>();
//JsonArray& arr = jb.createArray();
root["Temperatura"] = readDHTTemperature();
root["Umidade"] = readDHTHumidity();
root["Pressao"] = readDHTPressao();
root["CO2"] = readCO2();
root["Hora"] = time_now;
String output;
output = ",";
serializeJson(root, output);
output += "";
addFile(SPIFFS, loggerPath, output.c_str());
//##Obrigado santocyber por essa gambiarra aqui manda um pix [email protected] , acabei usando tambem JSON.stringfy no proprio javascript
return output;
}
String getSensorReadings(){
DynamicJsonDocument jsonBuffer(2000);
//####Serializacao dos sensores OK
jsonBuffer["Temperatura"] = readDHTTemperature();
jsonBuffer["Umidade"] = readDHTHumidity();
jsonBuffer["Pressao"] = readDHTPressao();
jsonBuffer["CO2"] = readCO2();
String output2;
serializeJson(jsonBuffer, output2);
return output2;
}
void configureEvents() {
events.onConnect([](AsyncEventSourceClient *client){
if(client->lastId()){
Serial.printf("Client connections. Id: %u\n", client->lastId());
}
// and set reconnect delay to 1 second
client->send("Ola da MushLight , um alo para o SantoCyber tambem",NULL,millis(),1000);
});
server.addHandler(&events);
}
String btc(){
const String url = "http://api.coindesk.com/v1/bpi/currentprice/BTC.json";
http.begin(url);
int httpCode = http.GET(); //Get crypto price from API
StaticJsonDocument<2000> doc;
DeserializationError error = deserializeJson(doc, http.getString());
if (error) //Display error message if unsuccessful
{
Serial.print(F("deserializeJson Failed"));
Serial.println(error.f_str());
delay(2500);
// return;
}
Serial.print("HTTP Status Code: ");
Serial.println(httpCode);
String BTCUSDPrice = doc["bpi"]["USD"]["rate_float"].as<String>();
http.end();
Serial.print("BTCUSD Price: "); //Display current price on serial monitor
Serial.println(BTCUSDPrice.toDouble());
http.end();
return String(BTCUSDPrice.toDouble());
delay(5000); //Sleep for 15 minutes
}
String doge(){
const String url = "https://api.bitfinex.com/v1/pubticker/doge:usd";
http.begin(url);
int httpCode = http.GET(); //Get crypto price from API
StaticJsonDocument<2000> doc;
DeserializationError error = deserializeJson(doc, http.getString());
if (error) //Display error message if unsuccessful
{
Serial.print(F("deserializeJson Failed"));
Serial.println(error.f_str());
delay(2500);
// return;
}
Serial.print("HTTP Status Code: ");
Serial.println(httpCode);
String DOGEPrice = doc["last_price"].as<String>();
http.end();
Serial.print("Doge Price: "); //Display current price on serial monitor
Serial.println(DOGEPrice.toDouble());
http.end();
return DOGEPrice;
delay(5000); //Sleep for 15 minutes
}
String xmr(){
const String url = "https://api.bitfinex.com/v1/pubticker/xmrusd";
http.begin(url);
int httpCode = http.GET(); //Get crypto price from API
StaticJsonDocument<2000> doc;
DeserializationError error = deserializeJson(doc, http.getString());
if (error) //Display error message if unsuccessful
{
Serial.print(F("deserializeJson Failed"));
Serial.println(error.f_str());
delay(2500);
// return;
}
Serial.print("HTTP Status Code: ");
Serial.println(httpCode);
String XMRPrice = doc["last_price"].as<String>();
http.end();
Serial.print("XMR Price: "); //Display current price on serial monitor
Serial.println(XMRPrice.toDouble());
http.end();
return String(XMRPrice.toDouble());
delay(5000); //Sleep for 15 minutes
}
String dolar(){
const String url = "https://economia.awesomeapi.com.br/json/last/USD";
http.begin(url);
int httpCode = http.GET(); //Get crypto price from API
StaticJsonDocument<2000> doc;
DeserializationError error = deserializeJson(doc, http.getString());
if (error) //Display error message if unsuccessful
{
Serial.print(F("deserializeJson Failed"));
Serial.println(error.f_str());
delay(2500);
// return;
}
Serial.print("HTTP Status Code: ");
Serial.println(httpCode);
String dolarPrice = doc["USDBRL"]["bid"].as<String>();
dolarPrice += ":24h:";
dolarPrice += doc["pctChange"].as<String>();
http.end();
Serial.print("Dolar Price: "); //Display current price on serial monitor
Serial.println(dolarPrice.toDouble());
http.end();
return String(dolarPrice.toDouble());
delay(5000); //Sleep for 15 minutes
}