-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathISS_API_Class.cpp
More file actions
207 lines (164 loc) · 6.9 KB
/
ISS_API_Class.cpp
File metadata and controls
207 lines (164 loc) · 6.9 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
// Space Station class functions which supports the main sketch
#include "ISS_API_Class.h" // Include the header with the function prototypes etc
/***************************************************************************************
** Connect to website and get the International Space Station pass times
***************************************************************************************/
void SpaceStation::getPasses(String latitude, String longitude, ISS_pass* passData)
{
this->passData = passData; // Make a copy of the pointer for this class
JsonStreamingParser json; // Create an instance of the parser
json.setListener(this); // Pass pointer to "this" SpaceStation class to the listener
// so it can call the support functions in this class
// Use WiFiClient class to create TCP connections
WiFiClient client;
// URL and port of the server
const char* host = "api.open-notify.org";
const int httpPort = 80;
// Connect as a client to the server
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// Built up the GET request
String url = "http://api.open-notify.org/iss-pass.json?lat=" + latitude + "&lon=" + longitude + "&n=" + PASSES;
// Send GET request
Serial.println("\nSending GET request to api.open-notify.org...\n");
client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
// Local variables for time-out etc
uint32_t timeout = millis();
char c = 0;
uint16_t ccount = 0;
Serial.println("=====> Header start <=====");
// Read the header that precedes the JSON, ends with \r\n
while ( client.available() > 0 || client.connected())
{
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("=====> Header end <=====");
break;
}
Serial.println(line); // Print the header to serial monitor for viewing
// Check for timeout
if ((millis() - timeout) > 5000UL)
{
Serial.println ("HTTP header timeout");
client.stop();
return;
}
}
// The JSON message should now be in the buffer for reading
// Read the JSON character by character and pass it to the JSON decoder
// The decoder will call the SpaceStation class during decoding, so we can
// save the decoded values
// Use OR since data may still be in the buffer when the client has disconnected!
while ( client.available() > 0 || client.connected())
{
while(client.available() > 0)
{
c = client.read(); // Read a received character
#ifdef SHOW_JSON // Optionally show the message with a simple formatter
Serial.print(c);
#endif
json.parse(c); // Pass to the parser, parser will call listener support functions as needed
// Check for timeout
if ((millis() - timeout) > 8000UL)
{
Serial.println ("JSON parse client timeout");
json.reset();
client.stop();
return;
}
yield();
}
}
json.reset();
client.stop();
}
/***************************************************************************************
** JSON Decoder library calls this when a key has been read
***************************************************************************************/
void SpaceStation::key(String key) {
currentKey = key;
//Serial.print("key: ");
//Serial.println(key);
}
/***************************************************************************************
** JSON Decoder library calls this when a value has been read
***************************************************************************************/
void SpaceStation::value(String val) {
// Test only:
//Serial.print("\nvaluePath :"); Serial.println(valuePath);
//Serial.print("currentParent :"); Serial.println(currentParent);
//Serial.print("currentKey :"); Serial.println(currentKey);
//Serial.print("arrayIndex :"); Serial.println(arrayIndex);
//Serial.print("Value :"); Serial.println(val);
if (currentParent == "")
{
if (currentKey == "message") passData->message = val;
return;
}
else
if (currentParent == "request")
{
if (currentKey == "datetime") passData->datetime = (uint32_t)val.toInt();
}
else
if (valuePath == "/response")
{
if (currentKey == "duration") passData->passDuration[arrayIndex] = (uint16_t)val.toInt();
else
if (currentKey == "risetime") passData->passRiseTime[arrayIndex] = (uint32_t)val.toInt();
}
}
/***************************************************************************************
** JSON Decoder library calls this when a start of document decoded
***************************************************************************************/
void SpaceStation::startDocument() {
currentParent = currentKey = "";
arrayIndex = 0;
ended = false;
//Serial.println("\nstart document");
}
/***************************************************************************************
** JSON Decoder library calls this when a end of document decoded
***************************************************************************************/
void SpaceStation::endDocument() {
ended = true;
//Serial.println("end document. ");
}
/***************************************************************************************
** JSON Decoder library calls this when a start of object decoded
***************************************************************************************/
void SpaceStation::startObject() {
currentParent = currentKey;
//Serial.println("start object. ");
}
/***************************************************************************************
** JSON Decoder library calls this when a end of object decoded
***************************************************************************************/
void SpaceStation::endObject() {
currentParent = "";
arrayIndex++;
//Serial.println("end object. ");
}
/***************************************************************************************
** JSON Decoder library calls this when an array of values has started
***************************************************************************************/
void SpaceStation::startArray() {
arrayIndex = 0;
valuePath = currentParent + "/" + currentKey;
//Serial.println("start array. ");
}
/***************************************************************************************
** JSON Decoder library calls this when an array of values has ended
***************************************************************************************/
void SpaceStation::endArray() {
valuePath = "";
//Serial.println("end array. ");
}
/***************************************************************************************
** JSON Decoder library calls this when a character is whitespace (not used here)
***************************************************************************************/
void SpaceStation::whitespace(char c) {
//Serial.println("whitespace");
}