-
Notifications
You must be signed in to change notification settings - Fork 0
/
Honeywell.h
255 lines (153 loc) · 5.39 KB
/
Honeywell.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
String HWusername = "HONEYWELL USER ID";
String HWpassword = "HONEYWELL PASSWORD";
String applicationId = "91db1612-73fd-4500-91b2-e63b069b185c"; //this is one all apps use
String baseApiURL = "https://mytotalconnectcomfort.com/WebApi/api/";
String apiSessions = "Session";
String apiLocations = "locations";
String apiDevices = "devices";
String timeURL = "http://worldtimeapi.org/api/timezone/Europe/Amsterdam"; // change to your country if needed
//to store these before making a call
String sessionId;
String userId;
String locationId="";
String deviceId;
//update the array size with rooms size
int numberOfRooms = 4;
String deviceNames[4] = {"room 1","room 2","room 3","room 4"}; //add rooms here
double deviceMaxTemps[4] = {25,21,21,21}; // set max temp so iot does not go to high
String deviceIDs[4];
double degreesSet[4];
double currentDegrees[4];
String devicesToIncrease[4];
String getTimePlus10Min(){
HTTPClient http;
StaticJsonDocument<200> doc;
int httpResponseCode;
http.begin(timeURL);
httpResponseCode = http.GET();
if(httpResponseCode!=200){
Serial.print("Get Time - Http get Failed, Error Code");
Serial.println(httpResponseCode);
}
//Serial.println(http.getString());
DynamicJsonDocument jsonTime(20048);
deserializeJson(jsonTime, http.getString());
int getTime = jsonTime["unixtime"].as<int>();
getTime+= jsonTime["dst_offset"].as<int>();
getTime+= jsonTime["raw_offset"].as<int>();
getTime+=600;
http.end(); //Free the resources
return String(year(getTime)) + "-" + String(month(getTime)) + "-" + String(day(getTime)) + "T" + String(hour(getTime)) + ":" + String(minute(getTime));
}
boolean getLocationId(){
//only do if Location ID not populated
if(locationId!=""){
return true;
}
HTTPClient http;
http.begin(baseApiURL+apiLocations+"?userId=" + userId + "&allData=true");
http.addHeader("sessionId", sessionId);
int httpResponseCode = http.GET();
if(httpResponseCode!=200){
Serial.print("Location - Http Post Failed, Error Code");
Serial.println(httpResponseCode);
return false;
}
DynamicJsonDocument location(20048);
deserializeJson(location, http.getString());
http.end(); //Free the resources
for (JsonObject item : location.as<JsonArray>()) {
locationId = item["locationID"].as<String>();
}
http.end();
return true;
}
boolean startEvoSession() {
HTTPClient http;
//here we set the Session ID, User ID and Location ID which is needed to make calls
StaticJsonDocument<200> doc;
int httpResponseCode;
http.begin(baseApiURL+apiSessions);
http.addHeader("Content-Type", "application/json");
http.addHeader("Content-Length", String(HWusername.length()+HWpassword.length()+applicationId.length()));
doc["username"] = HWusername;
doc["password"] = HWpassword;
doc["applicationid"] = applicationId;
String requestBody;
serializeJson(doc, requestBody);
httpResponseCode = http.POST(requestBody);
if(httpResponseCode!=200){
Serial.print("Session / UserID - Http Post Failed, Error Code");
Serial.println(httpResponseCode);
return false;
}
StaticJsonDocument<768> sessionUserId;
deserializeJson(sessionUserId, http.getString());
sessionId = sessionUserId["sessionId"].as<String>();
userId = sessionUserId["userInfo"]["userID"].as<String>();
http.end(); //Free the resources
delay(10000); //for honeywell timeout avoidance
if(!getLocationId()){
return false;
}
return true;
}
boolean createDeviceArray() {
HTTPClient http;
if(!startEvoSession()){
return false;
}
String httpContent;
checkReconnectWifi();
delay(10000); //for honeywell timeout avoidance
http.begin(baseApiURL+apiDevices + "?locationID=" + locationId + "&allData=true");
http.addHeader("sessionId", sessionId);
int httpResponseCode = http.GET();
if(httpResponseCode!=200){
Serial.print("Create Array - Http Get Failed, Error Code");
Serial.println(httpResponseCode);
return false;
}
DynamicJsonDocument devices(20048);
deserializeJson(devices, http.getString());
http.end(); //Free the resources
int i = 0; //for building array below
for (JsonObject item : devices.as<JsonArray>()) {
for(int j=0;j<numberOfRooms;j++){
if(item["name"].as<String>() == deviceNames[j]){
deviceIDs[j] = item["deviceID"].as<String>();
degreesSet[j] = item["thermostat"]["changeableValues"]["heatSetpoint"]["value"].as<double>();
currentDegrees[j] = item["thermostat"]["indoorTemperature"].as<double>();
i++;
break;
}
}
}
return true;
}
boolean setEvoHomeDeviceValue(String value, double celciusValue){
HTTPClient http;
if(value=="Error4321"){
return false;
}
StaticJsonDocument<200> doc;
int httpResponseCode;
http.begin(baseApiURL+apiDevices + "/" + value + "/thermostat/changeableValues/heatSetpoint?changeTag=RichiesRobots");
http.addHeader("Content-Type", "application/json");
http.addHeader("Content-Length", "150");
http.addHeader("sessionId", sessionId);
doc["value"] = celciusValue;
doc["status"] = "Temporary";
doc["nextTime"] = getTimePlus10Min();
String requestBody;
serializeJson(doc, requestBody);
httpResponseCode = http.PUT(requestBody);
if(httpResponseCode!=201){
Serial.print("Set Temp - Http Put Failed, Error Code");
Serial.println(httpResponseCode);
return false;
}
http.end(); //Free the resources
delay(10000); //delay else Honeywell will block if you set another again too soon
return true;
}