-
Notifications
You must be signed in to change notification settings - Fork 9
/
STA.ino
159 lines (116 loc) · 3.35 KB
/
STA.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
/*
STA mode operation defined here
Device connects to service and gets current and forecast information periodically
*/
#define OWN_RT_HOST "api.openweathermap.org"
#define EC_HOST "weather.gc.ca"
#define HTTP_PORT 80
#define WEB_CONNECTION_TIMEOUT 60000
// ------------------------
void setupSTA() {
// ------------------------
log("Connecting to " + String(config.ssid));
WiFi.disconnect();
WiFi.mode(WIFI_STA);
delay(100);
WiFi.begin(config.ssid, config.password);
long startTime = millis();
while (WiFi.status() != WL_CONNECTED) {
delay(200);
if( (millis() - startTime) > WEB_CONNECTION_TIMEOUT) {
log("Wifi connect failed");
wInfo.error = true;
wInfo.msg = "Wifi connection failed";
return;
}
}
IPAddress myIP = WiFi.localIP();
char tbuf[20];
sprintf(tbuf, "%i.%i.%i.%i", myIP[0], myIP[1], myIP[2], myIP[3]);
log("myIP: " + String(tbuf));
}
// ------------------------
boolean getOWData(boolean forecast) {
// ------------------------
if(WiFi.status() != WL_CONNECTED) {
wInfo.error = true;
wInfo.msg = "Wifi not connected";
return false;
}
// reinitialize json storage
websiteData = "";
log("Connecting to Open weather server");
log(" " + String(OWN_RT_HOST) + ":" + String(HTTP_PORT));
// Use WiFiClient class to create TCP connections
WiFiClient client;
long startTime = millis();
while( !client.connect(OWN_RT_HOST, HTTP_PORT) ) {
delay(100);
if( (millis() - startTime) > WEB_CONNECTION_TIMEOUT) {
log("Server connect failed");
client.stop();
return false;
}
}
delay(100);
if(forecast) {
log("Requesting forecast...");
client.print("GET /data/2.5/forecast/daily?cnt=2&id=" + String(config.fCityid) +
"&APPID=" + String(config.apiKey) +
"&units=" + ((config.unit == CELSIUS)? "metric" : "imperial") +
" HTTP/1.1\r\n" +
"Host: " + OWN_RT_HOST + "\r\n" +
"Connection: close\r\n\r\n");
}
else {
log("Requesting data...");
client.print("GET /data/2.5/weather?id=" + String(config.cityID) +
"&APPID=" + String(config.apiKey) +
"&units=" + ((config.unit == CELSIUS)? "metric" : "imperial") +
" HTTP/1.1\r\n" +
"Host: " + OWN_RT_HOST + "\r\n" +
"Connection: close\r\n\r\n");
}
client.flush();
delay(1000);
// Read all the lines of the reply from server into buffer
while(client.available()) {
websiteData += client.readStringUntil('\r');
delay(50);
}
log("Read: " + String(websiteData.length()) + " bytes");
// extract the json part from response
int index = websiteData.indexOf("\n\n");
if(index > 0)
websiteData = websiteData.substring(index + 2);
client.stop();
delay(100);
if(forecast)
return postProcess();
else
return (websiteData.length() > 0);
}
// ------------------------
boolean postProcess() {
// ------------------------
String tmpStr;
int dt1Index = extractValue("dt", tmpStr, true);
if(dt1Index < 0)
return false;
long dt1 = tmpStr.toInt();
log("Found dt1: " + String(dt1));
int dt2Index = extractValue("dt", tmpStr, true, dt1Index);
if(dt2Index < 0)
return true;
long dt2 = tmpStr.toInt();
log("Found dt2: " + String(dt2));
// which DT are we closer to
if( (wInfo.currTime > dt1) && ((wInfo.currTime - dt1) > (dt2 - wInfo.currTime)) ) {
// dt2 rules
websiteData = websiteData.substring(dt2Index);
}
else {
websiteData = websiteData.substring(0, dt2Index);
}
return true;
}