-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweather_API.ino
More file actions
268 lines (224 loc) · 7.32 KB
/
weather_API.ino
File metadata and controls
268 lines (224 loc) · 7.32 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include "SPI.h"
#include "WiFi.h"
char ssid[] = "jonghyun"; // your network SSID (name)
char pass[] = "86701240"; // your network password
String location = "Gangnam"; // your desired location
// initialize the library instance:
WiFiServer server(80);
WiFiClient client;
const unsigned long requestInterval = 60000; // delay between requests (1 min)
IPAddress hostIp;
uint8_t ret;
boolean requested; // whether you've made a request since connecting
unsigned long lastAttemptTime = 0; // last time you connected to the server, in milliseconds
String currentLine = ""; // string to hold the text from server
String tempString = ""; // string to hold temp
String humString = ""; // string to hold humidity
String timeString = ""; // string to hold timestamp
String pressureString = "";
boolean readingTemp = false; // if you're currently reading the temp
boolean readingHum = false; // if you're currently reading the humidity
boolean readingTime = false; // if you're currently reading the timestamp
boolean readingPressure = false;
int temp = 0;
void setup() {
// reserve space for the strings:
currentLine.reserve(100);
tempString.reserve(10);
humString.reserve(10);
timeString.reserve(20);
Serial.begin(115200);
delay(10);
// Connect to an AP with WPA/WPA2 security
Serial.println("Connecting to WiFi....");
WiFi.begin(ssid, pass); // Use this if your wifi network requires a password
//WiFi.begin(ssid); // Use this if your wifi network is unprotected.
server.begin();
Serial.println("Connect success!");
Serial.println("Waiting for DHCP address");
// Wait for DHCP address
while(WiFi.localIP() == INADDR_NONE) {
Serial.print(".");
delay(300);
}
Serial.println("\n");
printWifiData();
connectToServer();
}
void loop()
{
if (client.connected()) {
while (client.available()) {
// read incoming bytes:
char inChar = client.read();
// add incoming byte to end of line:
currentLine += inChar;
// if you get a newline, clear the line:
//Serial.println("trying to parse...");
if (inChar == '\n') {
//Serial.print("clientReadLine = ");
//Serial.println(currentLine);
currentLine = "";
}
// LOOKING FOR TEMPERATURE DATA
// if the current line ends with <temperature value=, it will
// be followed by the temp:
if ( currentLine.endsWith("<temperature value=")) {
// temperatue data is beginning. Clear the temp string:
readingTemp = true;
tempString = "";
}
// PULLING TEMPERATURE DATA
// if you're currently reading the bytes for temperature,
// add them to the temperature string:
if (readingTemp) {
if (inChar != 'm') { // if you see 'm', you're done reading temp
tempString += inChar;
}
else {
readingTemp = false;
Serial.print("- Temperature: ");
Serial.print(getInt(tempString)-273);
Serial.println((char)176); //degree symbol
}
}
if ( currentLine.endsWith("<humidity value=")) {
// Humidity reading is beginning. Clear the humidity string:
readingHum = true;
humString = "";
}
if (readingHum) {
if (inChar != 'u') {// if you see 'u', you're done reading humidity
humString += inChar;
}
else {
readingHum = false;
Serial.print("- Humidity: ");
Serial.print(getInt(humString));
Serial.println((char)37); //percent sign
}
}
if ( currentLine.endsWith("<lastupdate value=")) {
// timestamp is beginning. Clear the timestamp string:
readingTime = true;
timeString = "";
}
if (readingTime) {
if (inChar != '/') { // if you see '/', you're done reading timestamp
timeString += inChar;
}
else {
readingTime = false;
Serial.print("- Last update: ");
Serial.println(timeString.substring(2,timeString.length()-1));
}
}
if ( currentLine.endsWith("<pressure value=")) {
readingPressure = true;
pressureString = "";
}
if (readingPressure) {
if (inChar != 'u') {
pressureString += inChar;
}
else {
readingPressure = false;
Serial.print("- Pressure: ");
Serial.print(getInt(pressureString));
Serial.println("hPa");
}
}
if ( currentLine.endsWith("</current>")) {
delay(10000);
client.stop();
connectToServer();
//Serial.println("Disconnected from Server.\n");
}
}
}
else if (millis() - lastAttemptTime > requestInterval) {
// if you're not connected, and request interval time have passed since
// your last connection, then attempt to connect again to get new data:
connectToServer();
}
}
void connectToServer() {
Serial.println("connecting to server...");
String content = "";
if (client.connect(hostIp, 80)) {
Serial.println("Connected! Making HTTP request to api.openweathermap.org for "+location+"...");
//Serial.println("GET /data/2.5/weather?q="+location+"&mode=xml");
// make HTTP GET request to Facebook:
client.println("GET /data/2.5/weather?q="+location+"&mode=xml");
// declare correct server
client.print("HOST: api.openweathermap.org\n");
client.println("User-Agent: launchpad-wifi");
client.println("Connection: close");
client.println();
Serial.println("Weather information for "+location);
}
// note the time of this connect attempt:
lastAttemptTime = millis();
}
void printHex(int num, int precision) {
char tmp[16];
char format[128];
sprintf(format, "%%.%dX", precision);
sprintf(tmp, format, num);
Serial.print(tmp);
}
void printWifiData() {
// print your WiFi shield's IP address:
Serial.println();
Serial.println("IP Address Information:");
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
printHex(mac[5], 2);
Serial.print(":");
printHex(mac[4], 2);
Serial.print(":");
printHex(mac[3], 2);
Serial.print(":");
printHex(mac[2], 2);
Serial.print(":");
printHex(mac[1], 2);
Serial.print(":");
printHex(mac[0], 2);
Serial.println();
// print your subnet mask:
IPAddress subnet = WiFi.subnetMask();
Serial.print("NetMask: ");
Serial.println(subnet);
// print your gateway address:
IPAddress gateway = WiFi.gatewayIP();
Serial.print("Gateway: ");
Serial.println(gateway);
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
ret = WiFi.hostByName("api.openweathermap.org", hostIp);
Serial.print("ret: ");
Serial.println(ret);
Serial.print("Host IP: ");
Serial.println(hostIp);
Serial.println("");
}
int getInt(String input){ // This function converts String to Integer.
// This allows you to perform math on the string data we extracted from XML.
int i = 2;
while(input[i] != '"'){
i++;
}
input = input.substring(2,i);
char carray[20];
//Serial.println(input);
input.toCharArray(carray, sizeof(carray));
//Serial.println(carray);
temp = atoi(carray);
//Serial.println(temp);
return temp;
}