-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.ino
214 lines (172 loc) · 7.69 KB
/
http.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
void http() {
String payload;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("scraping...");
Serial.println("scraping..");
//digitalWrite(2, HIGH); // turn the onboard LED on
if ((WiFi.status() == WL_CONNECTED)) { //Check the current connection status
HTTPClient http;
http.begin("https://www.sir.toscana.it/monitoraggio/stazioni.php?type=idro"); //Specify the URL
int httpCode = http.GET(); //Make the request
if (httpCode > 0) { //Check for the returning code
payload = http.getString();
Serial.println(httpCode);
//Serial.println(payload);
delay(300);
lcd.setCursor(0, 1);
lcd.print("OK!");
delay(300);
}
http.end(); //Free the resources
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("parsing...");
payload.replace("\"", ""); //remove quotations"
payload.replace(" ", " "); //remove  
//token = payload.substring(10414, 11211); //first attemp super unreliable
/* //second attemp unreliable
String omb_tur = getValue(payload, ',', 3180);
String omb_si = getValue(payload, ',', 2991);
float omb_tur_mapserv = omb_tur.toFloat();
float omb_si_mapserv = omb_si.toFloat();
*/
ind = payload.indexOf("TOS03005641"); //third attemp based on indexof and getvalue function ...OK
String omb_si = getValue_ind(payload, ',', 8);
Serial.print("Ponte del Garbo SI: ");
Serial.print(omb_si);
Serial.println(" [m szi] - Δh1");
ind = payload.indexOf("TOS03005641");
String omb_si_var = getValue_ind(payload, ',', 10);
Serial.print("var: ");
Serial.print(omb_si_var);
Serial.println(" Δh1");
ind = payload.indexOf("TOS01005672");
String omb_buon = getValue_ind(payload, ',', 8);
Serial.print("Buonconvento: ");
Serial.print(omb_buon);
Serial.println(" [m szi] - Δh1");
ind = payload.indexOf("TOS01005672");
String omb_buon_var = getValue_ind(payload, ',', 10);
Serial.print("var: ");
Serial.print(omb_buon_var);
Serial.println(" Δh1");
ind = payload.indexOf("TOS01005791");
String omb_sass = getValue_ind(payload, ',', 8);
Serial.print("Sasso: ");
Serial.print(omb_sass);
Serial.println(" [m szi] - Δh1");
ind = payload.indexOf("TOS01005791");
String omb_sass_var = getValue_ind(payload, ',', 10);
Serial.print("var: ");
Serial.print(omb_sass_var);
Serial.println(" Δh1");
ind = payload.indexOf("TOS03005831");
String omb_tur = getValue_ind(payload, ',', 8);
Serial.print("Ponte Tura: ");
Serial.print(omb_tur);
Serial.println(" [m szi] - Δh1");
ind = payload.indexOf("TOS03005831");
String omb_tur_var = getValue_ind(payload, ',', 10);
Serial.print("var: ");
Serial.print(omb_tur_var);
Serial.println(" Δh1");
ind = payload.indexOf("TOS01005822");
String omb_ist = getValue_ind(payload, ',', 8);
Serial.print("Istia: ");
Serial.print(omb_ist);
Serial.println(" [m szi] - Δh1");
ind = payload.indexOf("TOS01005822");
String omb_ist_var = getValue_ind(payload, ',', 10);
Serial.print("var: ");
Serial.print(omb_ist_var);
Serial.println(" Δh1");
ind = payload.indexOf("TOS16005841");
String omb_berr = getValue_ind(payload, ',', 8);
Serial.print("Berrettino: ");
Serial.print(omb_berr);
Serial.println(" [m szi] - Δh1");
ind = payload.indexOf("TOS16005841");
String omb_berr_var = getValue_ind(payload, ',', 10);
Serial.print("var: ");
Serial.print(omb_berr_var);
Serial.println(" Δh1");
ind = payload.indexOf("riferiti");
String tim = getValue_ind(payload, ' ', 3);
Serial.print("last read: ");
Serial.println(tim);
omb_si_float = omb_si.toFloat(); //float my strings in an electronic river
omb_si_var_float = omb_si_var.toFloat();
omb_buon_float = omb_buon.toFloat();
omb_buon_var_float = omb_buon_var.toFloat();
omb_sass_float = omb_sass.toFloat();
omb_sass_var_float = omb_sass_var.toFloat();
omb_tur_float = omb_tur.toFloat();
omb_tur_var_float = omb_tur_var.toFloat();
omb_ist_float = omb_ist.toFloat();
omb_ist_var_float = omb_ist_var.toFloat();
omb_berr_float = omb_berr.toFloat();
omb_berr_var_float = omb_berr_var.toFloat();
/*
int dat_len = dat.length() + 1;
char dat_array[dat_len];
dat.toCharArray(dat_array, dat_len);
*/
tim_float = tim.toFloat();
omb_si_led = mapf(omb_si_float, 0.3, 2.0, 0.0, 255.0); //floating point mapped from 0.3 is the "alarm" level
omb_buon_led = mapf(omb_buon_float, 0.3, 2.0, 0.0, 255.0);
omb_sass_led = mapf(omb_sass_float, 0.3, 2.0, 0.0, 255.0);
omb_tur_led = mapf(omb_tur_float, 0.3, 2.0, 0.0, 255.0);
omb_ist_led = mapf(omb_ist_float, 0.3, 2.0, 0.0, 255.0);
omb_berr_led = mapf(omb_berr_float, 0.3, 2.0, 0.0, 255.0);
omb_si_var_led = mapf(omb_si_var_float, 0, 1.0, 1000.0, 50.0); //floating point mapped from level of variation in 1h
omb_buon_var_led = mapf(omb_buon_var_float, 0, 1.0, 1000.0, 50.0);
omb_sass_var_led = mapf(omb_sass_var_float, 0, 1.0, 1000.0, 50.0);
omb_tur_var_led = mapf(omb_tur_var_float, 0, 1.0, 1000.0, 50.0);
omb_ist_var_led = mapf(omb_ist_var_float, 0, 1.0, 1000.0, 50.0);
omb_berr_var_led = mapf(omb_berr_var_float, 0, 1.0, 1000.0, 50.0);
omb_si_led = constrain(omb_si_led, 0, 255); // limits range of sensor values to between 0 and 255 to avoid neg values
omb_buon_led = constrain(omb_buon_led, 0, 255);
omb_sass_led = constrain(omb_sass_led, 0, 255);
omb_tur_led = constrain(omb_tur_led, 0, 255);
omb_ist_led = constrain(omb_ist_led, 0, 255);
omb_berr_led = constrain(omb_berr_led, 0, 255);
omb_si_var_led = constrain(omb_si_var_led, 50, 1000); // limits range of sensor values to between 0 and 1000 to avoid neg values?
omb_buon_var_led = constrain(omb_buon_var_led, 50, 1000);
omb_sass_var_led = constrain(omb_sass_var_led, 50, 1000);
omb_tur_var_led = constrain(omb_tur_var_led, 50, 1000);
omb_ist_var_led = constrain(omb_ist_var_led, 50, 1000);
omb_berr_var_led = constrain(omb_berr_var_led, 50, 1000);
omb_si_led_scal = fscale( 0, 255, 0, 255, omb_si_led, -2); //fscale to avoid exponential led brightness
omb_buon_led_scal = fscale( 0, 255, 0, 255, omb_buon_led, -2);
omb_sass_led_scal = fscale( 0, 255, 0, 255, omb_sass_led, -2);
omb_tur_led_scal = fscale( 0, 255, 0, 255, omb_tur_led, -2);
omb_ist_led_scal = fscale( 0, 255, 0, 255, omb_ist_led, -2);
omb_berr_led_scal = fscale( 0, 255, 0, 255, omb_berr_led, -2);
lcd.setCursor(0, 1);
lcd.print("OK!");
delay(300);
//print debug
Serial.println("-led vals-");
Serial.println(omb_si_led);
Serial.println(omb_buon_led);
Serial.println(omb_sass_led);
Serial.println(omb_tur_led);
Serial.println(omb_ist_led);
Serial.println(omb_berr_led);
Serial.println("-fscaled led vals-");
Serial.println(omb_si_led_scal);
Serial.println(omb_buon_led_scal);
Serial.println(omb_sass_led_scal);
Serial.println(omb_tur_led_scal);
Serial.println(omb_ist_led_scal);
Serial.println(omb_berr_led_scal);
Serial.println("mill led vars-");
Serial.println(omb_si_var_led);
Serial.println(omb_buon_var_led);
Serial.println(omb_sass_var_led);
Serial.println(omb_tur_var_led);
Serial.println(omb_ist_var_led);
Serial.println(omb_berr_var_led);
}
}