-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEsp8266WeatherHeltecLED.ino
210 lines (189 loc) · 6.13 KB
/
Esp8266WeatherHeltecLED.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include <Wire.h>
#include <SPI.h>
#include <U8g2lib.h>
const char* ssid = "mySSID";
const char* password = "hackme";
const char* httpRequest = "http://k7bbr.net/weather/raw.json";
void getWxWifi();
void parseJSON(String&);
void setDisplay(String *, String *, String *, String *);
void setDisplay(String&, String&);
void setDisplay(String&, String&, String&, String&);
void setDisplay(String&, String&, String&, String&, String&);
//U8g2 Contructor---------------------------------------------------------------------------------
U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ 16, /* clock=*/ 5, /* data=*/ 4);
void setup ()
{
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.print("Connecting..");
}
u8g2.begin();
}
void loop()
{
Serial.println();
getWxWifi();
}
void getWxWifi()
{
String payload;
String& payloadRef = payload;
if (WiFi.status() == WL_CONNECTED) //Check WiFi connection status
{
HTTPClient http; //Declare an object of class HTTPClient
http.begin(httpRequest); //Specify request destination
int httpCode = http.GET(); //Send the request
if (httpCode > 0) //Check the returning code
{
payload = http.getString(); //Get the request response payload
Serial.println(payloadRef); //Print the response payload ****** for DEBUG
}
http.end(); //Close connection
}
parseJSON(payloadRef);
}
void parseJSON(String& rpayloadRef)
{
// Allocate JsonBuffer
// Use arduinojson.org/assistant to compute the capacity.
const size_t bufferSize = JSON_OBJECT_SIZE(13) + 530;
DynamicJsonBuffer jsonBuffer(bufferSize);
// Parse JSON object
JsonObject& root = jsonBuffer.parseObject(rpayloadRef);
if (!root.success()) {
Serial.println(F("Parsing failed!"));
//return;
}
String time = root["time"]; // "Time: Sat Dec 15 03:50:55 2018"
String timeAbb = time.substring(6,25);
String& timeRef = timeAbb;
String dir = root["dir"]; // "Wind Direction: 270.00"
String& dirRef = dir;
String speed = root["speed"]; // "Wind Speed: 5.35"
String& speedRef = speed;
String gust = root["gust"]; // "Wind Gust: 3.90"
String& gustRef = gust;
String otempc = root["otempc"]; // "-1.40"
String otempf = root["otempf"]; // "29.48"
String& otempfRef = otempf;
String humid = root["humid"]; // "RH: 76.90%"
String& humidRef = humid;
String itempc = root["itempc"]; // "3.08"
String itempf = root["itempf"]; // "37.54"
String& itempfRef = itempf;
String pressure = root["pressure"]; // "Barometric Pressure: 30.16"
String& pressureRef = pressure;
String hrain = root["hrain"]; // "Hourly Rain Total: 0.00"
String& hrainRef = hrain;
String dayrain = root["dayrain"]; // "24 Hour Rain Total 0.00"
String& dayrainRef = dayrain;
String high = root["high"]; // "33.98"
String& highRef = high;
String low = root["low"]; // "32.90"
String& lowRef = low;
//root.printTo(Serial);
Serial.println();
Serial.println(time);
Serial.println(dir);
Serial.println(speed);
Serial.println(gust);
Serial.println(otempc);
Serial.println(otempf);
Serial.println(humid);
Serial.println(itempc);
Serial.println(itempf);
Serial.println(pressure);
Serial.println(hrain);
Serial.println(dayrain);
Serial.println(high);
Serial.println(low);
//Labels for data fields:-----------------------------------------------
String timeLabel = "K7BBR WX Station";
String& timeLabelRef = timeLabel;
String otempfLabel = "Outside Temp: ";
String& otempfLabelRef = otempfLabel;
String highLowLabel = "H/L Temp: ";
String& highLowLabelRef = highLowLabel;
String dirLabel = "Wind Dir: ";
String& dirLabelRef = dirLabel;
String speedLabel = "Speed: ";
String& speedLabelRef = speedLabel;
String humidLabel = "Humidity: ";
String& humidLabelRef = humidLabel;
String pressureLabel = "Pressure: ";
String& pressureLabelRef = pressureLabel;
String hrainLabel = "Hr Rain: ";
String& hrainLabelRef = hrainLabel;
String dayrainLabel = "24 hr Rain: ";
String& dayrainLabelRef = dayrainLabel;
for (int i = 0; i<2; i++)
{
setDisplay(timeLabelRef, timeRef);
delay(3000);
setDisplay(dirLabelRef, dirRef, speedLabelRef, speedRef, gustRef);
delay(3000);
setDisplay(otempfLabelRef, otempfRef, highLowLabelRef, highRef, lowRef);
delay(3000);
setDisplay(humidLabelRef, humidRef, pressureLabelRef, pressureRef);
delay(3000);
setDisplay(hrainLabelRef, hrainRef, dayrainLabelRef, dayrainRef);
delay(2000);
setDisplay(otempfLabelRef, otempfRef, highLowLabelRef, highRef, lowRef);
delay(4000);
}
}
void setDisplay(String *plabel1, String *pdata1, String *plabel2, String *pdata2)
{
u8g2.setFont(u8g2_font_t0_12_tr);
u8g2.setFontDirection(0);
u8g2.clearBuffer();
u8g2.setCursor(0, 15);
u8g2.print(*plabel1 + *pdata1);
u8g2.setCursor(0, 30);
u8g2.print(*plabel2 + *pdata2);
Serial.println(*plabel2 + *pdata2);
u8g2.sendBuffer();
}
void setDisplay(String& rlabel1, String& rdata2)
{
u8g2.setFont(u8g2_font_t0_12_tr);
u8g2.setFontDirection(0);
u8g2.clearBuffer();
u8g2.setCursor(0, 15);
u8g2.print(rlabel1);
u8g2.setCursor(0, 30);
u8g2.print(rdata2);
Serial.println(rdata2);
u8g2.sendBuffer();
}
void setDisplay(String& rlabel1, String& rdata1, String& rlabel2, String& rdata2)
{
u8g2.setFont(u8g2_font_t0_12_tr);
u8g2.setFontDirection(0);
u8g2.clearBuffer();
u8g2.setCursor(0, 15);
u8g2.print(rlabel1 + rdata1);
u8g2.setCursor(0, 30);
u8g2.print(rlabel2 + rdata2);
Serial.println(rlabel2 + rdata2);
u8g2.sendBuffer();
}
void setDisplay(String& rlabel1, String& rdata1, String& rlabel2, String& rdata2a, String& rdata2b) //Overloaded method for line 1 single data, line 2 double data
{
u8g2.setFont(u8g2_font_t0_12_tr);
u8g2.setFontDirection(0);
u8g2.clearBuffer();
u8g2.setCursor(0, 15);
u8g2.print(rlabel1 + rdata1);
u8g2.setCursor(0, 30);
u8g2.print(rlabel2 + rdata2a + "/" + rdata2b);
Serial.println(rlabel2 + rdata2a+ "/" + rdata2b);
u8g2.sendBuffer();
}