Skip to content

Commit f16a0e0

Browse files
committed
Added PlaneSpotterDemo
1 parent 3b50ff6 commit f16a0e0

File tree

6 files changed

+710
-0
lines changed

6 files changed

+710
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

examples/.DS_Store

8 KB
Binary file not shown.
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
#include "AdsbExchangeClient.h"
2+
3+
4+
AdsbExchangeClient::AdsbExchangeClient() {
5+
6+
}
7+
8+
void AdsbExchangeClient::updateVisibleAircraft(String searchQuery) {
9+
JsonStreamingParser parser;
10+
parser.setListener(this);
11+
WiFiClient client;
12+
13+
// http://public-api.adsbexchange.com/VirtualRadar/AircraftList.json?lat=47.437691&lng=8.568854&fDstL=0&fDstU=20&fAltL=0&fAltU=5000
14+
const char host[] = "public-api.adsbexchange.com";
15+
String url = "/VirtualRadar/AircraftList.json?" + searchQuery;
16+
17+
const int httpPort = 80;
18+
if (!client.connect(host, httpPort)) {
19+
Serial.println("connection failed");
20+
return;
21+
}
22+
23+
24+
Serial.print("Requesting URL: ");
25+
Serial.println(url);
26+
27+
// This will send the request to the server
28+
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
29+
"Host: " + host + "\r\n" +
30+
"Connection: close\r\n\r\n");
31+
32+
int retryCounter = 0;
33+
while(!client.available()) {
34+
Serial.println(".");
35+
delay(1000);
36+
retryCounter++;
37+
if (retryCounter > 10) {
38+
return;
39+
}
40+
}
41+
42+
int pos = 0;
43+
boolean isBody = false;
44+
char c;
45+
46+
int size = 0;
47+
client.setNoDelay(false);
48+
while(client.connected()) {
49+
while((size = client.available()) > 0) {
50+
c = client.read();
51+
if (c == '{' || c == '[') {
52+
isBody = true;
53+
}
54+
if (isBody) {
55+
parser.parse(c);
56+
}
57+
}
58+
}
59+
endDocument();
60+
}
61+
62+
String AdsbExchangeClient::getFrom() {
63+
if (from[CURRENT].length() >=4) {
64+
int firstComma = from[CURRENT].indexOf(",");
65+
return from[CURRENT].substring(5, firstComma);
66+
}
67+
return "";
68+
}
69+
String AdsbExchangeClient::getFromIcao() {
70+
if (from[CURRENT].length() >=4) {
71+
return from[CURRENT].substring(0,4);
72+
}
73+
return "";
74+
}
75+
String AdsbExchangeClient::getTo() {
76+
if (to[CURRENT].length() >=4) {
77+
int firstComma = to[CURRENT].indexOf(",");
78+
return to[CURRENT].substring(5, firstComma);
79+
}
80+
return "";
81+
}
82+
83+
String AdsbExchangeClient::getToIcao() {
84+
if (to[CURRENT].length() >=4) {
85+
return to[CURRENT].substring(0,4);
86+
}
87+
return "";
88+
}
89+
String AdsbExchangeClient::getAltitude(){
90+
return altitude[CURRENT];
91+
}
92+
double AdsbExchangeClient::getDistance() {
93+
return distance[CURRENT];
94+
95+
}
96+
String AdsbExchangeClient::getAircraftType() {
97+
return aircraftType[CURRENT];
98+
99+
}
100+
String AdsbExchangeClient::getOperatorCode() {
101+
return operatorCode[CURRENT];
102+
}
103+
104+
double AdsbExchangeClient::getHeading() {
105+
return heading[CURRENT];
106+
}
107+
108+
void AdsbExchangeClient::whitespace(char c) {
109+
110+
}
111+
112+
void AdsbExchangeClient::startDocument() {
113+
counter = 0;
114+
currentMinDistance = 1000.0;
115+
}
116+
117+
void AdsbExchangeClient::key(String key) {
118+
currentKey = key;
119+
}
120+
121+
void AdsbExchangeClient::value(String value) {
122+
/*String from = "";
123+
String to = "";
124+
String altitude = "";
125+
String aircraftType = "";
126+
String currentKey = "";
127+
String operator = "";
128+
129+
130+
"Type": "A319",
131+
"Mdl": "Airbus A319 112",
132+
133+
"From": "LSZH Z\u00c3\u00bcrich, Zurich, Switzerland",
134+
"To": "LEMD Madrid Barajas, Spain",
135+
"Op": "Swiss International Air Lines",
136+
"OpIcao": "SWR",
137+
"Dst": 6.23,
138+
"Year": "1996"
139+
*/
140+
if (currentKey == "Id") {
141+
counter++;
142+
} else if (currentKey == "From") {
143+
from[TEMP] = value;
144+
} else if (currentKey == "To") {
145+
to[TEMP] = value;
146+
} else if (currentKey == "OpIcao") {
147+
operatorCode[TEMP] = value;
148+
} else if (currentKey == "Dst") {
149+
distance[TEMP] = value.toFloat();
150+
} else if (currentKey == "Mdl") {
151+
aircraftType[TEMP] = value;
152+
} else if (currentKey == "Trak") {
153+
heading[TEMP] = value.toFloat();
154+
} else if (currentKey == "Alt") {
155+
altitude[TEMP] = value;
156+
} else if (currentKey == "Trt") {
157+
if (distance[TEMP] < currentMinDistance) {
158+
currentMinDistance = distance[TEMP];
159+
Serial.println("Found a closer aircraft");
160+
from[CURRENT] = from[TEMP];
161+
to[CURRENT] = to[TEMP];
162+
altitude[CURRENT] = altitude[TEMP];
163+
distance[CURRENT] = distance[TEMP];
164+
aircraftType[CURRENT] = aircraftType[TEMP];
165+
operatorCode[CURRENT] = operatorCode[TEMP];
166+
heading[CURRENT] = heading[TEMP];
167+
}
168+
}
169+
Serial.println(currentKey + "=" + value);
170+
}
171+
172+
int AdsbExchangeClient::getNumberOfVisibleAircrafts() {
173+
return counter;
174+
}
175+
176+
void AdsbExchangeClient::endArray() {
177+
178+
}
179+
180+
void AdsbExchangeClient::endObject() {
181+
182+
}
183+
184+
void AdsbExchangeClient::endDocument() {
185+
Serial.println("Flights: " + String(counter));
186+
if (counter == 0 && lastSightingMillis < millis() - MAX_AGE_MILLIS) {
187+
for (int i = 0; i < 2; i++) {
188+
from[i] = "";
189+
to[i] = "";
190+
altitude[i] = "";
191+
distance[i] = 1000.0;
192+
aircraftType[i] = "";
193+
operatorCode[i] = "";
194+
heading[i] = 0.0;
195+
}
196+
} else if (counter > 0) {
197+
lastSightingMillis = millis();
198+
}
199+
}
200+
201+
boolean AdsbExchangeClient::isAircraftVisible() {
202+
return counter > 0 || lastSightingMillis > millis() - MAX_AGE_MILLIS;
203+
}
204+
205+
void AdsbExchangeClient::startArray() {
206+
207+
}
208+
209+
void AdsbExchangeClient::startObject() {
210+
211+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**The MIT License (MIT)
2+
3+
Copyright (c) 2015 by Daniel Eichhorn
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+
23+
See more at http://blog.squix.ch
24+
*/
25+
26+
#pragma once
27+
28+
#include <JsonListener.h>
29+
#include <JsonStreamingParser.h>
30+
#include <ESP8266WiFi.h>
31+
#include <WiFiClient.h>
32+
33+
#define CURRENT 0
34+
#define TEMP 1
35+
36+
#define MAX_AGE_MILLIS 15000
37+
38+
39+
class AdsbExchangeClient: public JsonListener {
40+
private:
41+
int counter = 0;
42+
String currentKey = "";
43+
String from[2] = {"", ""};
44+
String to[2] = {"", ""};
45+
String altitude[2] = {"", ""};
46+
double distance[2] = {0.0, 0.0};
47+
double currentMinDistance = 1000.0;
48+
String aircraftType[2] = {"", ""};
49+
String operatorCode[2] = {"", ""};
50+
double heading[2] = {0.0, 0.0};
51+
long lastSightingMillis = 0;
52+
53+
public:
54+
AdsbExchangeClient();
55+
56+
void updateVisibleAircraft(String searchQuery);
57+
58+
String getFrom();
59+
String getFromIcao();
60+
String getTo();
61+
String getToIcao();
62+
String getAltitude();
63+
double getDistance();
64+
String getAircraftType();
65+
String getOperatorCode();
66+
double getHeading();
67+
int getNumberOfVisibleAircrafts();
68+
boolean isAircraftVisible();
69+
70+
virtual void whitespace(char c);
71+
72+
virtual void startDocument();
73+
74+
virtual void key(String key);
75+
76+
virtual void value(String value);
77+
78+
virtual void endArray();
79+
80+
virtual void endObject();
81+
82+
virtual void endDocument();
83+
84+
virtual void startArray();
85+
86+
virtual void startObject();
87+
};

0 commit comments

Comments
 (0)