-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleParser.cpp
More file actions
99 lines (85 loc) · 1.96 KB
/
ExampleParser.cpp
File metadata and controls
99 lines (85 loc) · 1.96 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
#include "ExampleParser.h"
#include "JsonStreamingParser.h"
#include "JsonListener.h"
#include <WiFi.h>
#include <WiFiClient.h>
String CurrentKey;
void ExampleListener::whitespace(char c) {
Serial.println("whitespace");
}
void ExampleListener::startDocument() {
Serial.println("start document");
}
void ExampleListener::key(String key) {
CurrentKey=key;
}
void ExampleListener::value(String value) {
if (CurrentKey=="Temp")
{
Serial.println(value);
Temperature=value;
}
else
if (CurrentKey=="Wind")
{
Serial.println(value);
WindSpeed=value;
}
}
void ExampleListener::endArray() {
Serial.println("end array. ");
}
void ExampleListener::endObject() {
Serial.println("end object. ");
}
void ExampleListener::endDocument() {
Serial.println("end document. ");
}
void ExampleListener::startArray() {
Serial.println("start array. ");
}
void ExampleListener::startObject() {
Serial.println("start object. ");
}
void ExampleListener::doUpdate(String url) {
Serial.println("DoUpdate");
JsonStreamingParser parser;
parser.setListener(this);
WiFiClient client;
const int httpPort = 80;
if (!client.connect("www.buienradar.nl", httpPort)) {
Serial.println("connection failed");
return;
}
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: buienradar.nl\r\n" +
"Connection: close\r\n\r\n");
int retryCounter = 0;
while(!client.available()) {
delay(1000);
retryCounter++;
if (retryCounter > 10) {
return;
}
}
int pos = 0;
boolean isBody = false;
char c;
int size = 0;
client.setNoDelay(false);
while(client.connected()) {
while((size = client.available()) > 0) {
c = client.read();
Serial.print(c);
if (c == '{' || c == '[') {
isBody = true;
}
if (isBody) {
parser.parse(c);
}
}
}
}