-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwx.cpp
142 lines (113 loc) · 4.06 KB
/
wx.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
/* look up current weather
*/
#include "HamClock.h"
static const char wx_base[] = "/ham/HamClock/wx.pl";
/* look up current weather info for the given location.
* if wip is filled ok return true, else return false with short reason in ynot[]
*/
bool getCurrentWX (const LatLong &ll, bool is_de, WXInfo *wip, char ynot[])
{
WiFiClient wx_client;
char line[100];
bool ok = false;
resetWatchdog();
// get
if (wifiOk() && wx_client.connect(svr_host, HTTPPORT)) {
updateClocks(false);
resetWatchdog();
// query web page
snprintf (line, sizeof(line), _FX("%s?is_de=%d&lat=%g&lng=%g"), wx_base, is_de, ll.lat_d, ll.lng_d);
Serial.println (line);
httpGET (wx_client, svr_host, line);
// skip response header
if (!httpSkipHeader (wx_client)) {
strcpy_P (ynot, PSTR("WX timeout"));
goto out;
}
// init response
memset (wip, 0, sizeof(*wip));
// crack response
uint8_t n_found = 0;
while (n_found < N_WXINFO_FIELDS && getTCPLine (wx_client, line, sizeof(line), NULL)) {
// Serial.printf (_FX("WX: %s\n"), line);
updateClocks(false);
// check for error message in which case abandon further search
if (sscanf (line, "error=%[^\n]", ynot) == 1)
goto out;
// find start of data value after =
char *vstart = strchr (line, '=');
if (!vstart)
continue;
vstart++;
// check for content line
if (strncmp_P (line, PSTR("city="), 5) == 0) {
strncpy (wip->city, vstart, sizeof(wip->city)-1);
n_found++;
} else if (strncmp_P (line, PSTR("temperature_c="), 14) == 0) {
wip->temperature_c = atof (vstart);
n_found++;
} else if (strncmp_P (line, PSTR("humidity_percent="), 17) == 0) {
wip->humidity_percent = atof (vstart);
n_found++;
} else if (strncmp_P (line, PSTR("wind_speed_mps="), 15) == 0) {
wip->wind_speed_mps = atof (vstart);
n_found++;
} else if (strncmp_P (line, PSTR("wind_dir_name="), 14) == 0) {
strncpy (wip->wind_dir_name, vstart, sizeof(wip->wind_dir_name)-1);
n_found++;
} else if (strncmp_P (line, PSTR("clouds="), 7) == 0) {
strncpy (wip->clouds, vstart, sizeof(wip->clouds)-1);
n_found++;
} else if (strncmp_P (line, PSTR("conditions="), 11) == 0) {
strncpy (wip->conditions, vstart, sizeof(wip->conditions)-1);
n_found++;
} else if (strncmp_P (line, PSTR("attribution="), 12) == 0) {
strncpy (wip->attribution, vstart, sizeof(wip->attribution)-1);
n_found++;
}
// Serial.printf (_FX("WX %d: %s\n"), n_found, line);
}
if (n_found < N_WXINFO_FIELDS) {
strcpy_P (ynot, PSTR("No WX data"));
goto out;
}
// ok!
ok = true;
} else {
strcpy_P (ynot, PSTR("WX connection failed"));
}
// clean up
out:
wx_client.stop();
resetWatchdog();
printFreeHeap (F("getCurrentWX"));
return (ok);
}
/* display current DE weather in the given box.
* this is used by updateWiFi() for persistent display, use showDEWX() for transient display
*/
bool updateDEWX (const SBox &box)
{
char ynot[32];
WXInfo wi;
bool ok = getCurrentWX (de_ll, true, &wi, ynot);
if (ok)
plotWX (box, DE_COLOR, wi);
else
plotMessage (box, DE_COLOR, ynot);
return (ok);
}
/* display current DX weather in the given box.
* this is used by updateWiFi() for persistent display, use showDXWX() for transient display
*/
bool updateDXWX (const SBox &box)
{
char ynot[32];
WXInfo wi;
bool ok = getCurrentWX (dx_ll, false, &wi, ynot);
if (ok)
plotWX (box, DX_COLOR, wi);
else
plotMessage (box, DX_COLOR, ynot);
return (ok);
}