-
Notifications
You must be signed in to change notification settings - Fork 1
/
Domoticz.cpp
72 lines (61 loc) · 1.5 KB
/
Domoticz.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
#include "Domoticz.h"
DOMOTICZ::DOMOTICZ(char* host, int port, char* user, char* password){
_host = host;
_port = port;
_user = user;
_password = password;
}
DOMOTICZ::~DOMOTICZ(){
}
int DOMOTICZ::computeHumStat(float h) {
int hum_stat;
if (h > 70) {
hum_stat = 3;
} else if (h < 30) {
hum_stat = 2;
} else if (h >= 30 & h <= 45) {
hum_stat = 0;
} else if (h > 45 & h <= 70) {
hum_stat = 1;
}
return hum_stat;
}
String DOMOTICZ::buildUrl(float t, float h, int index) {
int hum_stat = computeHumStat(h);
String url = _host;
url += ":";
url += _port;
url += "/json.htm?username=";
url += _user;
url += "&password=";
url += _password;
url += "&type=command¶m=udevice&idx=";
url += String(index);
url += "&nvalue=0&svalue=";
url += String(t);
url += ";";
url += String(h);
url += ";";
url += String(hum_stat);
return url;
}
void DOMOTICZ::notify(float t, float h, int index){
String url = buildUrl(t, h, index);
std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
client->setInsecure();
Serial.print("Requesting URL: ");
Serial.println(url);
_http.begin(*client, url);
int httpCode = _http.GET();
if (httpCode) {
if (httpCode == 200) {
String payload = _http.getString();
Serial.println("Domoticz response ");
Serial.println(payload);
} else {
Serial.println(httpCode);
}
}
Serial.println("closing connection");
_http.end();
}