-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
562 lines (441 loc) · 19.1 KB
/
main.cpp
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
/*
Written by Jonathan Riches, inspired by many. Apologies for anyone's code portions I used and haven't credited!
This code isn't great, but it does work...
References: -
https://circuits4you.com
https://arduinojson.org/v6/api/json/deserializejson/
https://shkspr.mobi/blog/2019/02/tado-api-guide-updated-for-2019/
http://blog.scphillips.com/posts/2017/01/the-tado-api-v2/
The Espressif / Arduino documentation / examples
https://github.com/esp8266/Arduino/tree/master/libraries
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h> // https://arduinojson.org/
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 4 // Digital pin connected to the DHT sensor - change as required
#define DHTTYPE DHT22 // Sensor Type DHT 22 (AM2302)
DHT_Unified dht(DHTPIN, DHTTYPE);
//##################################################################################################################
/* Set these to your desired credentials. */
// WiFi
const char *ssid = "your-ssid";
const char *password = "your_password";
// Tado
const char *tlogin = "your_tado_login";
const char *tpassword = "your_tado_password";
String thome = "00000"; // your_home_number"
String tzone = "1"; // zone_id
String tdevice = "VA1234567890"; //device ID to offset
// End of credential / user setup block
//##################################################################################################################
float actTemp = 20; // Actual temperature of the room from ESP8266 Sensor
float txoffset =0; // Existing offset value returned by Tado
float toffset = 0; // Offset value to send to Tado
char toffsetS[] = "T0000"; // Converted value to actually submit to Tado
//Web/Server address to read/write from
const char *host = "auth.tado.com";
const char *host2 = "my.tado.com";
const int httpsPort = 443; //HTTPS= 443 and HTTP = 80
//SHA1 finger print of certificate use web browser to view and copy
// Will change if Tado update their certificates
const char fingerprint[] PROGMEM = "A3 D1 64 E8 F4 FE 55 A9 05 32 D4 15 A3 15 32 22 3F 9A 8A 70";
const char fingerprint2[] PROGMEM = "E3 F4 F5 81 49 0A 2D 49 BD 2C EE F6 E9 CA 9C B3 CE DE 39 8D";
//=======================================================================
// Power on setup
//=======================================================================
void setup()
{
dht.begin();
// Print temperature sensor details.
sensor_t sensor;
dht.temperature().getSensor(&sensor);
delay(1000);
Serial.begin(115200);
WiFi.mode(WIFI_OFF); //Prevents reconnection issue (taking too long to connect)
delay(1000);
WiFi.mode(WIFI_STA); //Only Station No AP, This line hides the viewing of ESP as wifi hotspot
WiFi.begin(ssid, password); //Connect to your WiFi router
Serial.println("");
Serial.print("Connecting");
// Wait for connection
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
//If connection successful show IP address in serial monitor
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //IP address assigned to your ESP
}
//=======================================================================
// Main Program Loop
//=======================================================================
void loop()
{
String Ttoken; //Declare string to hold Tado authentication token
WiFiClientSecure httpsClient; //Declare object of class WiFiClient
//#######################################################################################################################################
// Read local device temperature, set actTemp
sensors_event_t event;
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println(F("Error reading temperature!"));
}
else {
Serial.print(F("Temperature: "));
Serial.print(event.temperature);
Serial.println(F("°C"));
}
actTemp = (event.temperature - 0.4);
Serial.print(F("actTemp: "));
Serial.print(actTemp);
Serial.println(F("°C"));
// //##################################################################################################################################
// //Obtain Authentication Token from Tado
Serial.println(host);
Serial.printf("Using fingerprint '%s'\n", fingerprint);
httpsClient.setFingerprint(fingerprint);
httpsClient.setTimeout(15000); // 15 Seconds
delay(1000);
Serial.print("HTTPS Connecting");
int r = 0; //retry counter
while ((!httpsClient.connect(host, httpsPort)) && (r < 30))
{
delay(100);
Serial.print(".");
r++;
}
if (r == 30)
{
Serial.println("Connection failed");
}
else
{
Serial.println("Connected to web");
}
String getData, Link;
//POST Data
// Link = "/post";
Link = "/oauth/token";
Serial.print("requesting URL: ");
Serial.println(host);
String httpBody = (String("\r\n") +"client_id=tado-web-app&grant_type=password&scope=home.user&username=" + tlogin + "&password=" + tpassword + "&client_secret=wZaRN7rpjn3FoNyF5IFuxg9uMzYJcvOoQ8QWiIqS3hfk6gLhVlG57j5YNoZL2Rtc"
);
int httpBodyLength = httpBody.length()-2;
String httpText = (String("POST ") + Link + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Content-Type: application/x-www-form-urlencoded" + "\r\n" +
"Accept: */*" + "\r\n" +
"Cache-Control: no-cache" + "\r\n" +
"Host: auth.tado.com" + "\r\n" +
"Content-Length: " + httpBodyLength + "\r\n" +
"Connection: keep-alive" + "\r\n" +
httpBody +
+"Connection: close\r\n\r\n"
);
httpsClient.print(httpText);
Serial.println("HTTP request is: ");
Serial.println(httpText);
Serial.println("request sent");
while (httpsClient.connected())
{
String line = httpsClient.readStringUntil('\n');
if (line == "\r")
{
Serial.println("headers received");
break;
}
}
String linez;
String lineT;
linez = httpsClient.readStringUntil('\n'); //Read Line by Line
lineT = httpsClient.readStringUntil('\n'); //Read Line by Line
//httpsClient.stop
//Print response
// Serial.println("LineT:");
// Serial.println(lineT);
Ttoken = lineT.substring(17,916);
Serial.println("==========");
// Serial.println("Token Value:");
// Serial.println(Ttoken);
//########################################################################################################################################
// GET thermostat temperature from Tado
Serial.println(host2);
Serial.printf("Using fingerprint '%s'\n", fingerprint2);
httpsClient.setFingerprint(fingerprint2);
httpsClient.setTimeout(15000); // 15 Seconds
delay(1000);
Serial.print("HTTPS Connecting");
int rr = 0; //retry counter
while ((!httpsClient.connect(host2, httpsPort)) && (rr < 30))
{
delay(100);
Serial.print(".");
rr++;
}
if (rr == 30)
{
Serial.println("Connection failed");
}
else
{
Serial.println("Connected to web");
}
Serial.print("requesting URL: ");
Serial.println(host2);
String httpText2 = (String("GET /api/v2/homes/" + thome + "/zones/" + tzone + "/state HTTP/1.1") + "\r\n" +
"Host: my.tado.com" + "\r\n" +
"Authorization: Bearer " + Ttoken + "\r\n" +
"Accept: */*" + "\r\n" +
"Cache-Control: no-cache" + "\r\n" +
"Host: my.tado.com" + "\r\n" +
"Connection: close\r\n\r\n"
);
httpsClient.print(httpText2);
Serial.println("HTTP request is: ");
Serial.println(httpText2);
Serial.println("request sent");
while (httpsClient.connected())
{
String line = httpsClient.readStringUntil('\n');
// Serial.println(line); //Print response
if (line == "\r")
{
Serial.println("headers received");
break;
}
}
Serial.println("reply was:");
Serial.println("==========");
String linez2;
String lineT2;
linez2 = httpsClient.readStringUntil('\n'); //Read Line by Line
lineT2 = httpsClient.readStringUntil('\n'); //Read Line by Line
Serial.println("\r\n lineT2");
Serial.println(lineT2);
Serial.println("\r\n");
//###################################################################################################################
// JSON split to get Tado Temp
// See https://arduinojson.org/v6/assistant/ and https://arduinojson.org/v6/api/json/deserializejson/
const size_t capacity = 3*JSON_OBJECT_SIZE(1) + 5*JSON_OBJECT_SIZE(2) + 4*JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(5) + JSON_OBJECT_SIZE(13) + 710;
DynamicJsonDocument doc(capacity);
//const char* json = "{\"tadoMode\":\"HOME\",\"geolocationOverride\":false,\"geolocationOverrideDisableTime\":null,\"preparation\":null,\"setting\":{\"type\":\"HEATING\",\"power\":\"ON\",\"temperature\":{\"celsius\":21,\"fahrenheit\":69.8}},\"overlayType\":null,\"overlay\":null,\"openWindow\":null,\"nextScheduleChange\":{\"start\":\"2019-12-22T00:00:00Z\",\"setting\":{\"type\":\"HEATING\",\"power\":\"ON\",\"temperature\":{\"celsius\":18,\"fahrenheit\":64.4}}},\"nextTimeBlock\":{\"start\":\"2019-12-22T00:00:00.000Z\"},\"link\":{\"state\":\"ONLINE\"},\"activityDataPoints\":{\"heatingPower\":{\"type\":\"PERCENTAGE\",\"percentage\":0,\"timestamp\":\"2019-12-21T14:12:53.522Z\"}},\"sensorDataPoints\":{\"insideTemperature\":{\"celsius\":21.48,\"fahrenheit\":70.66,\"timestamp\":\"2019-12-21T14:22:48.761Z\",\"type\":\"TEMPERATURE\",\"precision\":{\"celsius\":1,\"fahrenheit\":1}},\"humidity\":{\"type\":\"PERCENTAGE\",\"percentage\":52.4,\"timestamp\":\"2019-12-21T14:22:48.761Z\"}}}";
// Deserialize the JSON document
DeserializationError error = deserializeJson(doc, lineT2);
// Test if parsing succeeds.
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return;
}
// Fetch values
// Most are not required, so commented out but left in for reference
//const char* tadoMode = doc["tadoMode"]; // "HOME"
//bool geolocationOverride = doc["geolocationOverride"]; // false
//JsonObject setting = doc["setting"];
//const char* setting_type = setting["type"]; // "HEATING"
//const char* setting_power = setting["power"]; // "ON"
//float setting_temperature_celsius = setting["temperature"]["celsius"]; // 21
//float setting_temperature_fahrenheit = setting["temperature"]["fahrenheit"]; // 69.8
// const char* nextScheduleChange_start = doc["nextScheduleChange"]["start"]; // "2019-12-22T00:00:00Z"
// JsonObject nextScheduleChange_setting = doc["nextScheduleChange"]["setting"];
// const char* nextScheduleChange_setting_type = nextScheduleChange_setting["type"]; // "HEATING"
// const char* nextScheduleChange_setting_power = nextScheduleChange_setting["power"]; // "ON"
// int nextScheduleChange_setting_temperature_celsius = nextScheduleChange_setting["temperature"]["celsius"]; // 18
// float nextScheduleChange_setting_temperature_fahrenheit = nextScheduleChange_setting["temperature"]["fahrenheit"]; // 64.4
// const char* nextTimeBlock_start = doc["nextTimeBlock"]["start"]; // "2019-12-22T00:00:00.000Z"
// const char* link_state = doc["link"]["state"]; // "ONLINE"
// JsonObject activityDataPoints_heatingPower = doc["activityDataPoints"]["heatingPower"];
// const char* activityDataPoints_heatingPower_type = activityDataPoints_heatingPower["type"]; // "PERCENTAGE"
// int activityDataPoints_heatingPower_percentage = activityDataPoints_heatingPower["percentage"]; // 0
// const char* activityDataPoints_heatingPower_timestamp = activityDataPoints_heatingPower["timestamp"]; // "2019-12-21T14:12:53.522Z"
JsonObject sensorDataPoints_insideTemperature = doc["sensorDataPoints"]["insideTemperature"];
float sensorDataPoints_insideTemperature_celsius = sensorDataPoints_insideTemperature["celsius"]; // 21.48
// float sensorDataPoints_insideTemperature_fahrenheit = sensorDataPoints_insideTemperature["fahrenheit"]; // 70.66
// const char* sensorDataPoints_insideTemperature_timestamp = sensorDataPoints_insideTemperature["timestamp"]; // "2019-12-21T14:22:48.761Z"
// const char* sensorDataPoints_insideTemperature_type = sensorDataPoints_insideTemperature["type"]; // "TEMPERATURE"
// int sensorDataPoints_insideTemperature_precision_celsius = sensorDataPoints_insideTemperature["precision"]["celsius"]; // 1
// int sensorDataPoints_insideTemperature_precision_fahrenheit = sensorDataPoints_insideTemperature["precision"]["fahrenheit"]; // 1
// JsonObject sensorDataPoints_humidity = doc["sensorDataPoints"]["humidity"];
// const char* sensorDataPoints_humidity_type = sensorDataPoints_humidity["type"]; // "PERCENTAGE"
// float sensorDataPoints_humidity_percentage = sensorDataPoints_humidity["percentage"]; // 52.4
// const char* sensorDataPoints_humidity_timestamp = sensorDataPoints_humidity["timestamp"]; // "2019-12-21T14:22:48.761Z"
// Print values.
// Serial.print("setting_temperature_celsius: - ");
// Serial.println(setting_temperature_celsius);
Serial.print("sensorDataPoints_insideTemperature_celsius: ");
Serial.println(sensorDataPoints_insideTemperature_celsius);
//###################################################################################################################
// GET existing offset Value
Serial.println(host2);
Serial.printf("Using fingerprint '%s'\n", fingerprint2);
httpsClient.setFingerprint(fingerprint2);
httpsClient.setTimeout(15000); // 15 Seconds
delay(1000);
Serial.print("HTTPS Connecting");
int rrrr = 0; //retry counter
while ((!httpsClient.connect(host2, httpsPort)) && (rrrr < 30))
{
delay(100);
Serial.print(".");
rrrr++;
}
if (rrrr == 30)
{
Serial.println("Connection failed");
}
else
{
Serial.println("Connected to web");
}
Serial.print("requesting URL: ");
Serial.println(host2);
String httpText4 = (String("GET /api/v2/devices/" + tdevice + "/temperatureOffset HTTP/1.1") + "\r\n" +
"Host: my.tado.com" + "\r\n" +
"Authorization: Bearer " + Ttoken + "\r\n" +
"Accept: */*" + "\r\n" +
"Cache-Control: no-cache" + "\r\n" +
"Host: my.tado.com" + "\r\n" +
"Connection: close\r\n\r\n"
);
httpsClient.print(httpText4);
Serial.println("HTTP request is: ");
Serial.println(httpText4);
Serial.println("request sent");
while (httpsClient.connected())
{
String line = httpsClient.readStringUntil('\n');
// Serial.println(line); //Print response
if (line == "\r")
{
Serial.println("headers received");
break;
}
}
Serial.println("reply was:");
Serial.println("==========");
String linez4;
String lineT4;
linez4 = httpsClient.readStringUntil('\n'); //Read Line by Line
lineT4 = httpsClient.readStringUntil('\n'); //Read Line by Line
Serial.println("\r\n lineT4");
Serial.println(lineT4);
Serial.println("\r\n");
//################################################################################################################
// JSON split to get Tado Offset
const size_t capacity2 = JSON_OBJECT_SIZE(2) + 30;
DynamicJsonDocument doc2(capacity2);
//const char* json = "{\"celsius\":-21.8,\"fahrenheit\":-35.24}";
DeserializationError error2 = deserializeJson(doc2, lineT4);
// Test if parsing succeeds.
if (error2) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error2.c_str());
return;
}
txoffset = doc2["celsius"]; // -21.8
//###################################################################################################################
// Calculate offset value required
Serial.print("Actual Temp: ");
Serial.println(actTemp);
Serial.print("Current Offset: ");
Serial.println(txoffset);
Serial.println("\r\n");
Serial.print("Tado reading inc. current offset: ");
Serial.println(sensorDataPoints_insideTemperature_celsius);
Serial.println("\r\n");
toffset = actTemp - (sensorDataPoints_insideTemperature_celsius - txoffset);
Serial.print("Offset calculated: ");
Serial.println(toffset);
Serial.print("Offset to submit: ");
// Serial.printf("%.1f",toffset);
sprintf(toffsetS,"%.1f",toffset);
//Serial.println("toffsetS: ");
Serial.println(toffsetS);
//###################################################################################################################
// PUT Offset to Tado
Serial.println(host2);
Serial.printf("Using fingerprint '%s'\n", fingerprint2);
httpsClient.setFingerprint(fingerprint2);
httpsClient.setTimeout(15000); // 15 Seconds
delay(1000);
Serial.print("HTTPS Connecting");
int rrr = 0; //retry counter
while ((!httpsClient.connect(host2, httpsPort)) && (rrr < 30))
{
delay(100);
Serial.print(".");
rrr++;
}
if (rrr == 30)
{
Serial.println("Connection failed");
}
else
{
Serial.println("Connected to web");
}
Serial.print("requesting URL: ");
Serial.println(host2);
String httpBody3 = (String("{\r\n") +
" celsius: " + toffsetS
+ "\r\n" +
"}"
);
int httpBodyLength3 = httpBody3.length();
// Serial.print("httpBody3 Length: ");
// Serial.println(httpBodyLength3);
String httpText3 = (String("PUT /api/v2/devices/" + tdevice + "/temperatureOffset HTTP/1.1") + "\r\n" +
"Host: my.tado.com" + "\r\n" +
"Authorization: Bearer " + Ttoken + "\r\n" +
"Accept: */*" + "\r\n" +
"Cache-Control: no-cache" + "\r\n" +
"Host: my.tado.com" + "\r\n" +
"Content-Length: " + httpBodyLength3 + "\r\n" +
"Connection: keep-alive\r\n\r\n" +
httpBody3
);
httpsClient.print(httpText3);
Serial.println("HTTP request is: ");
Serial.println(httpText3);
Serial.println("request sent");
while (httpsClient.connected())
{
String line = httpsClient.readStringUntil('\n');
// Serial.println(line); //Print response
if (line == "\r")
{
Serial.println("headers received");
break;
}
}
Serial.println("reply was:");
Serial.println("==========");
String linez3;
String lineT3;
linez3 = httpsClient.readStringUntil('\n'); //Read Line by Line
lineT3 = httpsClient.readStringUntil('\n'); //Read Line by Line
// Serial.println("\r\n linez3");
// Serial.println(linez3);
// Serial.println("\r\n");
Serial.println("\r\n lineT3");
Serial.println(lineT3);
Serial.println("\r\n");
//###################################################################################################################
// Serial.println();
Serial.println("==========");
Serial.println("closing connection");
delay(300000); //POST Data at every 5 minute
}
//=======================================================================