-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwx.cpp
111 lines (86 loc) · 2.78 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
/* look up current weather
*/
#include "HamClock.h"
static const char wx_base[] = "/ham/HamClock/wx.pl";
#define WX_STAYUP 15000 // time for wx display to stay up, millis() -- nice if == GPATH_LINGER
/* look up current weather info for the given location.
* if wip is filled ok return true, else return false with short reason in ynot[]
*/
static bool getCurrentWX (float lat, float lng, bool is_de, WXInfo *wip, char ynot[])
{
char line[128];
WiFiClient wx_client;
bool ok = false;
Serial.println(wx_base);
resetWatchdog();
// get
if (wifiOk() && wx_client.connect(svr_host, HTTPPORT)) {
updateClocks(false);
resetWatchdog();
// query web page
snprintf (line, sizeof(line), "%s?is_de=%d&lat=%g&lng=%g", wx_base, is_de, lat, lng);
Serial.println (line);
httpGET (wx_client, svr_host, line);
// skip response header
if (!httpSkipHeader (wx_client)) {
strcpy_P (ynot, PSTR("WX header error"));
goto out;
}
// crack response
uint8_t n_found = 0;
while (n_found < N_WXINFO_FIELDS && getTCPLine (wx_client, line, sizeof(line), NULL)) {
updateClocks(false);
// check for error message in which case abandon further search
if (sscanf (line, "error=%[^\n]", ynot) == 1)
goto out;
// check for normal messages
if (sscanf (line, "city=%[^\n]", wip->city) == 1
|| sscanf (line, "temperature_c=%f", &wip->temperature_c) == 1
|| sscanf (line, "humidity_percent=%f", &wip->humidity_percent) == 1
|| sscanf (line, "wind_speed_mps=%f", &wip->wind_speed_mps) == 1
|| sscanf (line, "wind_dir_name=%[^\n]", wip->wind_dir_name) == 1
|| sscanf (line, "clouds=%[^\n]", wip->clouds) == 1
|| sscanf (line, "conditions=%[^\n]", wip->conditions) == 1
|| sscanf (line, "attribution=%[^\n]", wip->attribution) == 1)
n_found++;
}
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 the current DX weather in plot1_b then arrange to revert after WX_STAYUP
*/
void showDXWX()
{
char ynot[32];
WXInfo wi;
if (getCurrentWX (rad2deg(dx_ll.lat), rad2deg(dx_ll.lng), false, &wi, ynot))
plotWX (plot1_b, DX_COLOR, wi);
else
plotMessage (plot1_b, DX_COLOR, ynot);
revertPlot1 (WX_STAYUP);
}
/* display the current DE weather in plot1_b then arrange to revert after WX_STAYUP
*/
void showDEWX()
{
char ynot[32];
WXInfo wi;
if (getCurrentWX (rad2deg(de_ll.lat), rad2deg(de_ll.lng), true, &wi, ynot))
plotWX (plot1_b, DE_COLOR, wi);
else
plotMessage (plot1_b, DE_COLOR, ynot);
revertPlot1 (WX_STAYUP);
}