-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper_functions.h
159 lines (128 loc) · 5.88 KB
/
helper_functions.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
////**************************************************************************************************
//// D B G P R I N T *
////**************************************************************************************************
//// Send a line of info to serial output. Works like vsprintf(), but checks the DEBUG flag. *
//// Print only if DEBUG flag is true. Always returns the formatted string. *
////**************************************************************************************************
char* dbgprint ( const char* format, ... )
{
static char sbuf[DEBUG_BUFFER_SIZE] ; // For debug lines
va_list varArgs ; // For variable number of params
va_start ( varArgs, format ) ; // Prepare parameters
vsnprintf ( sbuf, sizeof(sbuf), format, varArgs ) ; // Format the message
va_end ( varArgs ) ; // End of using parameters
if ( DEBUG ) // DEBUG on?
{
Serial.print ( "D: " ) ; // Yes, print prefix
Serial.println ( sbuf ) ; // and the info
}
return sbuf ; // Return stored string
}
void displayWeather(){
Serial.println("displayWeather()");
String payload_data;
payload_data = String(weather_payload);
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject( payload_data );
json.printTo(Serial);
if (json.success()) {
bool API_failed = false;
Serial.println("\nparsed json");
//p.key// is a const char* pointing to the key
//p.value// is a JsonVariant
int response_code = json["cod"];
if(response_code != 200){
Serial.println( "API Error!" );
Serial.println( json["message"].as<char*>() );
LEDdisplay.ScrollText( json["message"].as<char*>() );
}else{
LEDdisplay.ScrollText(" WEATHER IN ");
LEDdisplay.ScrollText( json["name"].as<char*>() );
LEDdisplay.ScrollText(" IS ");
LEDdisplay.ScrollText( json["weather"][0]["main"].as<char*>() );
// API returns mixed case, that we might want to change something like this:
// String _WeatherType;
// _WeatherType = String( json["name"].as<char*>() );
// _WeatherType.toUpperCase();
// LEDdisplay.ScrollText( _WeatherType );
LEDdisplay.ScrollText(" TEMPERATURE-- ");
LEDdisplay.ScrollText( json["main"]["temp"].as<char*>() );
LEDdisplay.ScrollText(" HUMIDITY-- ");
LEDdisplay.ScrollText( json["main"]["humidity"].as<char*>() );
LEDdisplay.ScrollText(" ");
}
} else {
dbgprint("failed to load json config");
}
}
void getWeather(){
HTTPClient http;
Serial.print("[HTTP] begin...\n");
char url[500];
strcpy(url, "http://");
strcat(url, "api.openweathermap.org/data/2.5/weather?q=");
strcat(url, apiLocation);
strcat(url, "&units=metric&APPID="); //change the temp unit here, refer to the API docs
strcat(url, apiKey);
http.begin(url); //HTTP
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
payload.toCharArray(weather_payload, 700);
//weather_payload = payload;
Serial.println(payload);
//cool - lets display it!!
displayWeather();
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
DeviceMode == 0; //Sets the tickertape back to clock mode
weather_busy = 0;
}
void parseWeather(){
weather_busy = 1;
unsigned long currentMillis = millis();
LEDdisplay.writeDigitRaw(6, 0b0000000000000000); //clear seconds seperators for now
LEDdisplay.writeDisplay();
if (currentMillis - p_weather_Millis >= p_weather_interval ) {
p_weather_interval = 1000 * 60 * 60; //one hour intervals
p_weather_Millis = currentMillis;
LEDdisplay.FillTextBuffer(" WAIT ");
Serial.println("Weather Pressed");
getWeather();
} else {
LEDdisplay.FillTextBuffer(" ");
displayWeather();
DeviceMode = 0; //Sets the tickertape back to clock mode
weather_busy = 0;
}
}
void SaveConfig(){
// Serial.println("saving config");
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
String _bright = String(_brightness);
_bright.toCharArray(Brightness, 40);
json["Brightness"] = Brightness;
json["WelcomeText"] = WelcomeText;
json["apiKey"] = apiKey;
json["apiLocation"] = apiLocation;
json["charTimezone"] = charTimezone;
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("failed to open config file for writing");
}
json.printTo(Serial);
json.printTo(configFile);
configFile.close();
//end save
}