-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjson.ino
300 lines (259 loc) · 10.5 KB
/
json.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/************************************************************************************
* JSON related stuff
************************************************************************************/
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include <WiFiClientSecure.h>
#include "WiFi.h"
/************************************************************************************
* JSON structure setup for sondehub.
*
* Example:
* {
* "software_name": "ttnhabbridge", # Receiving software name
* "software_version": "0.0.1", # Receiving software version
* "uploader_callsign": "foobar", # Mandatory - TTN station name?
* "uploader_position": [ -34.0, 138.0, 0 ], # Optional - TTN station location, if available
* "uploader_radio": "???", # Optional - Any other details
* "uploader_antenna": "???", # Optional - other rx details
* "snr": 11.79, # Optional - Receiver metadata - SNR
* "frequency": 434.201003, # Optional - Receiver Metadata - RX Frequency
* "modulation": "LoRaWAN - TTNv3", # Optional, but recommended - Modulation type
* "time_received": "2022-04-18T04:36:59.899304Z", # Time the packet was received on the TTN network
* "datetime": "2022-04-18T04:36:58.000000Z", # Date/time reported by the payload itself. Use todays UTC date if no date available.
* "payload_callsign": "CALLSIGN_HERE", # Callsign of the payload
* "frame": 6, # Optional - Frame number
* "lat": -34.1, # Mandatory - Position
* "lon": 138.1,
* "alt": 100.0,
* "temp": 30, # Some examples of optional fields
* "sats": 0,
* "batt": 3.15,
* }
*
*
* Station info JSON
*{
"software_name": "string",
"software_version": "string",
"uploader_callsign": "string",
"uploader_position": [
0,
0,
0
],
"uploader_antenna": "string",
"uploader_contact_email": "string",
"mobile": true
}
*
************************************************************************************/
/************************************************************************************
* Upload your current position to the Sondehub server
************************************************************************************/
void postStationToServer()
{
if (WiFi.status() == WL_CONNECTED)
{
HTTPClient https;
#if defined(USE_GPS)
processGPSData();
#endif
https.begin(JSON_URL_LISTENERS);
https.addHeader("Content-Type", "application/json");
https.addHeader("accept", "text/plain");
DynamicJsonDocument doc(350);
// Add values in the document
doc["software_name"] = "TBTracker-RX";
doc["software_version"] = TBTRACKER_VERSION;
doc["uploader_callsign"] = CALLSIGN;
doc["uploader_antenna"] = ANTENNA_USED;
doc["uploader_radio"] = RADIO_USED;
doc["uploader_contact_email"] = UPLOADER_EMAIL;
doc["mobile"] = I_AM_MOBILE;
JsonArray uploader_position = doc.createNestedArray("uploader_position");
// Add the uploader position to the JSON by populating the nested array
uploader_position.add(Telemetry.uploader_position[0]);
uploader_position.add(Telemetry.uploader_position[1]);
uploader_position.add(Telemetry.uploader_position[2]);
String json;
serializeJson(doc, json);
// Use the lines below for debugging. Be sure that the total length of the JSON
// doen NOT exceed 350
// Serial.println(); Serial.print("JSON length: "); Serial.println(json.length());
// Serial.println(json);
Serial.println();
Serial.println();
Serial.println(F("Uploading your position to Sondehub:"));
int httpResponseCode = https.PUT(json);
// Display the result of the JSON upload
if(httpResponseCode>0)
{
String response = https.getString();
Serial.print(httpResponseCode); Serial.print(F(" - ")); Serial.println(response);
}
else
{
Serial.printf("Error code: %d\n",httpResponseCode);
Serial.printf("Error occurred while sending HTTP POST: %s\n", https.errorToString(httpResponseCode).c_str());
}
https.end();
}
}
/************************************************************************************
// Get SSDV records from the upload queue and post the SSDV data to the SondeHub server
************************************************************************************/
void postSSDVToServer()
{
if (WiFi.status() == WL_CONNECTED)
{
char packetBuf[256]; // Contains the hex encoded packet
char base64_data[512];
size_t base64_length;
// Get a SSDV packet from the queue
if( xQueueReceive( ssdv_Queue,
packetBuf,
( TickType_t ) 1) == pdPASS )
{
HTTPClient https;
// packetBuf now contains a copy of the first item in the queue
// Add the http headers
https.begin("http://ssdv.habhub.org/api/v0/packets");
https.addHeader("Content-Type", "application/json");
https.addHeader("Accept", "application/json");
https.addHeader("charsets", "utf-8");
// code the packet into base64
base64_encode(packetBuf, 256, &base64_length, base64_data);
base64_data[base64_length] = '\0';
DynamicJsonDocument doc(1024);
// Add values in the document
doc["type"] = "packet";
doc["packet"] = base64_data;
doc["encoding"] = "base64";
doc["received"] = "2023-03-17";
doc["receiver"] = CALLSIGN;
String json;
serializeJson(doc, json);
Serial.println();
Serial.print(F("JSON length SSDV: ")); Serial.println(json.length());
int httpResponseCode = https.POST(json);
// Print the results to the Serial console
if(httpResponseCode <= 0)
{
Serial.printf("Error code: %d\n",httpResponseCode);
Serial.printf("Error occurred while sending HTTP POST: %s\n", https.errorToString(httpResponseCode).c_str());
}
// cleanup
https.end();
}
}
}
/************************************************************************************
// Post the telemetry data to the SondeHub upload queue
************************************************************************************/
void putTelemetryinQueue()
{
DynamicJsonDocument doc(1024);
// Add values in the document
// Add the "dev" flag to the JSON if you only want to test the validity of the
if (devflag) doc["dev"] = "true";
doc["software_name"] = "TBTracker-RX";
doc["software_version"] = TBTRACKER_VERSION;
doc["uploader_callsign"] = CALLSIGN;
doc["time_received"] = Telemetry.time_received;
doc["payload_callsign"] = Telemetry.payload_callsign;
doc["datetime"] = Telemetry.datetime;
doc["lat"] = Telemetry.lat;
doc["lon"] = Telemetry.lon;
doc["alt"] = Telemetry.alt;
doc["frequency"] = Telemetry.frequency;
doc["rssi"] = Telemetry.rssi;
doc["snr"] = Telemetry.snr;
doc["modulation"] = LoRaSettings.ModeString;
doc["raw"] = Telemetry.raw;
doc["uploader_antenna"] = ANTENNA_USED;
doc["uploader_radio"] = RADIO_USED;
if (UPLOAD_YOUR_POSITION)
{
// Add the uploader position to the JSON
JsonArray uploader_position = doc.createNestedArray("uploader_position");
uploader_position.add(Telemetry.uploader_position[0]);
uploader_position.add(Telemetry.uploader_position[1]);
uploader_position.add(Telemetry.uploader_position[2]);
}
// Add non standard data from the payload to the JSON
if (Telemetry.extraFields)
{
// We need to check for 6, 8, 9, A, B, R, S
if (Telemetry.lastField.indexOf("6") >= 0) doc["sats"] = Telemetry.sats;
if (Telemetry.lastField.indexOf("8") >= 0) doc["heading"] = Telemetry.heading;
if (Telemetry.lastField.indexOf("9") >= 0) doc["batt"] = Telemetry.batt;
if ( (Telemetry.lastField.indexOf("A") >= 0) || (Telemetry.lastField.indexOf("B") >= 0) ) doc["temp"] = Telemetry.temp;
if (Telemetry.lastField.indexOf("R") >= 0) doc["pressure"] = Telemetry.pressure;
if (Telemetry.lastField.indexOf("S") >= 0) doc["humidity"] = Telemetry.humidity;
#if defined(PAYLOAD_COMMENT)
doc["comment"] = Telemetry.comment;
#endif
}
String json;
serializeJson(doc, json);
json = "[" + json + "]";
// JSON is ready here
// Put it in the Telemetry queue
char jbuf[1024];
json.toCharArray(jbuf,json.length()+1);
// Add the packet to the queue. do not wait if thge queue is full
if (telemetry_Queue != NULL)
{
if (xQueueSend(telemetry_Queue, jbuf, 0) == pdPASS)
{
Telemetry.uploadResult = "Telemetry packet added to upload queue.";
}
else
{
Telemetry.uploadResult = "Could not upload telemetry. Queue is full.";
}
}
}
/************************************************************************************
// Retrieve a record from the telemetry queue and upload to Sondehub
************************************************************************************/
void postTelemetryToServer()
{
if (WiFi.status() == WL_CONNECTED)
{
String json;
// Get a record from the telemetry queue
char jbuf[1024];
// Get a SSDV packet from the queue
if( xQueueReceive( telemetry_Queue,
jbuf,
( TickType_t ) 1) == pdPASS )
{
HTTPClient https;
https.begin(JSON_URL);
https.addHeader("Content-Type", "application/json");
https.addHeader("accept", "text/plain");
json = jbuf;
Serial.println();
Serial.print(F("JSON length: ")); Serial.println(json.length());
int httpResponseCode = https.PUT(json);
// Print the results to the Serial console
if(httpResponseCode>0)
{
String response = https.getString();
Serial.print("\nUpload result: ");
Serial.print(httpResponseCode); Serial.print(" - ");
Serial.println(response);
// Telemetry.uploadResult = response;
}
else
{
Serial.printf("Error code: %d\n",httpResponseCode);
Serial.printf("Error occurred while sending HTTP POST: %s\n", https.errorToString(httpResponseCode).c_str());
// Telemetry.uploadResult = https.errorToString(httpResponseCode);
}
https.end();
}
}
}