Repository navigation
Expand file tree
/
Copy pathesp8266-module_code.ino
More file actions
94 lines (75 loc) · 2.24 KB
/
Copy pathesp8266-module_code.ino
File metadata and controls
94 lines (75 loc) · 2.24 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
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
// WiFi
const char* ssid = "";
const char* password = "";
// API
const char* apiUsername = "";
const char* apiPassword = "";
// API endpoint
const char* apiUrl = "https://skopje.pulse.eco/rest/current";
const char* targetSensorId = "3d5e32fb-3c41-427f-b6ef-ed19e84026dd"; // Bilo koj sensor
void setup() {
Serial.begin(9600);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
}
void loop() {
fetchPm10Data();
delay(600000); // Fetch sekoi 10 minuti
}
void fetchPm10Data() {
if (WiFi.status() == WL_CONNECTED) {
WiFiClientSecure client;
client.setInsecure();
HTTPClient https;
if (https.begin(client, apiUrl)) {
https.setAuthorization(apiUsername, apiPassword);
int httpCode = https.GET();
if (httpCode == HTTP_CODE_OK) {
WiFiClient* stream = https.getStreamPtr();
String pm10Objects[100]; // ima 74 pm10 sensori
int objectCount = 0;
String currentObject = "";
bool isReadingObject = false;
//Pominuva niz site gi barame
while (stream->connected() || stream->available()) {
if (stream->available()) {
char c = stream->read();
if (c == '{') {
isReadingObject = true;
currentObject = "";
}
if (isReadingObject) {
currentObject += c;
}
if (c == '}') {
isReadingObject = false;
if (currentObject.indexOf("\"type\":\"pm10\"") != -1) {
if (objectCount < 100) {
pm10Objects[objectCount++] = currentObject;
}
}
}
}
}
for (int i = 0; i < objectCount; i++) {
if (pm10Objects[i].indexOf(targetSensorId) != -1) {
DynamicJsonDocument doc(8096);
DeserializationError error = deserializeJson(doc, pm10Objects[i]);
if (!error) {
const char* value = doc["value"];
Serial.println(value);
return;
}
}
}
}
https.end();
}
}
}