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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
// Licence: MIT
#include <Arduino.h>
#include <RadioLib.h>
#include <Wire.h>
#include <SparkFun_u-blox_GNSS_Arduino_Library.h>
#include <SD.h>
// TODO: read these settings from the SD Card
constexpr char CALLSIGN[] = "HB9EGM";
constexpr int SSID = 12;
constexpr int DATE_TYPE_CODE = 0; // compressed geolocation
constexpr int PATH_CODE = 2; // metropolitan mobile
constexpr char SYMBOL_TABLE_IDENTIFIER = '/';
constexpr char SYMBOL_CODE_BICYCLE = 'b';
constexpr long TX_INTERVAL = 15000;
#if 0
File myFile;
constexpr int SD_CS = 17;
#endif
// SX1278 has the following connections:
// NSS pin: PD14 (Arduino 10)
// DIO0 pin: PF3 (Arduino 8)
// RESET pin: PF15 (Arduino 9)
RFM96 radio = new Module(10, 8, 9);
HardwareSerial SerialGNSS(PD6, PD5);
SFE_UBLOX_GNSS gnss;
long lastGnssPoll = 0;
long lastPositionReport = 0;
constexpr size_t MAX_REPORT_LEN = 32;
size_t report_len = 0;
uint8_t report[MAX_REPORT_LEN];
const char* digits = " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-./?@";
#define callsign_int_t uint32_t
// This function is from aprs434.github.io,
// MIT License, Copyright (c) 2022 Serge Y. Stroobandt, ON4AA
callsign_int_t encodeCallsign(const char *callsign) {
// Encode CCCC according to aprs434.github.io/code/codec.cpp
const int b = 37;
callsign_int_t result;
callsign_int_t weight;
int i, j, k, ch;
result = 0, weight = 1;
for (i = j = strlen(callsign); i > 0; i--) { // iterate over input string
ch = toupper(callsign[i-1]);
for (k = 0; k < strlen(digits); k++) { // lookup char index
if (digits[k] == ch) {
break;
}
}
if (k == strlen(digits)) // ignore if ch not found in digits
continue;
result = result + ((k) * weight);
weight = weight * b;
}
return result; // result can be pow(42,51)!
}
// This function is from ESP32 lora.tracker,
// MIT License, Copyright (c) 2020 Peter Buchegger
static void ax25_base91enc(char *s, uint8_t n, uint32_t v) {
/* Creates a Base-91 representation of the value in v in the string
* pointed to by s, n-characters long. String length should be n+1.
*/
for (s += n, *s = '\0'; n; n--) {
*(--s) = v % 91 + 33;
v /= 91;
}
}
static char letterize(int x) {
return (char) x + 65;
}
static char* get_mh(double lat, double lon, int size) {
static char locator[11];
double LON_F[]={20,2.0,0.083333,0.008333,0.0003472083333333333};
double LAT_F[]={10,1.0,0.0416665,0.004166,0.0001735833333333333};
int i;
lon += 180;
lat += 90;
if (size <= 0 || size > 10) size = 6;
size/=2; size*=2;
for (i = 0; i < size/2; i++){
if (i % 2 == 1) {
locator[i*2] = (char) (lon/LON_F[i] + '0');
locator[i*2+1] = (char) (lat/LAT_F[i] + '0');
} else {
locator[i*2] = letterize((int) (lon/LON_F[i]));
locator[i*2+1] = letterize((int) (lat/LAT_F[i]));
}
lon = fmod(lon, LON_F[i]);
lat = fmod(lat, LAT_F[i]);
}
locator[i*2]=0;
return locator;
}
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
#if 0
pinMode(SD_CS, OUTPUT);
if (!SD.begin(SD_CS)) {
Serial.println("SD init failed!");
return;
}
#endif
Wire.begin();
SerialGNSS.begin(9600);
if (!gnss.begin(SerialGNSS)) {
while (true) {
Serial.println(F("Check GNSS"));
delay(4000);
}
}
//gnss.enableDebugging(); // Uncomment this line to enable helpful debug messages on Serial
Serial.print(F("Version: "));
byte versionHigh = gnss.getProtocolVersionHigh();
Serial.print(versionHigh);
Serial.print(".");
byte versionLow = gnss.getProtocolVersionLow();
Serial.println(versionLow);
Serial.print(F("[RFM] Init "));
int state = radio.begin(433.900);
if (state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while (true);
}
if (radio.setBandwidth(125.0) == RADIOLIB_ERR_INVALID_BANDWIDTH) {
Serial.println(F("Selected bandwidth is invalid for this module!"));
while (true);
}
if (radio.setSpreadingFactor(12) == RADIOLIB_ERR_INVALID_SPREADING_FACTOR) {
Serial.println(F("Selected spreading factor is invalid for this module!"));
while (true);
}
if (radio.setCodingRate(5) == RADIOLIB_ERR_INVALID_CODING_RATE) {
Serial.println(F("Selected coding rate is invalid for this module!"));
while (true);
}
// NOTE: value 0x34 is reserved for LoRaWAN networks and should not be used
if (radio.setSyncWord(0x14) != RADIOLIB_ERR_NONE) {
Serial.println(F("Unable to set sync word!"));
while (true);
}
if (radio.setOutputPower(16) == RADIOLIB_ERR_INVALID_OUTPUT_POWER) {
Serial.println(F("Selected output power is invalid for this module!"));
while (true);
}
if (radio.setPreambleLength(8) == RADIOLIB_ERR_INVALID_PREAMBLE_LENGTH) {
Serial.println(F("Selected preamble length is invalid for this module!"));
while (true);
}
// radio.setRfSwitchPins(4, 5);
}
void loop()
{
const auto now = millis();
if (now - lastGnssPoll > 1000) {
lastGnssPoll = now;
// longitude and latitude are in degrees*1e7
const long latitude = gnss.getLatitude();
Serial.print(F("Lat: "));
Serial.print(latitude);
const long longitude = gnss.getLongitude();
Serial.print(F(" Long: "));
Serial.print(longitude);
char *locator = get_mh((double)latitude/1e7, (double)longitude/1e7, 10);
Serial.print(F(" Loc: "));
Serial.print(locator);
const long altitude = gnss.getAltitude();
Serial.print(F(" Alt: "));
Serial.print(altitude);
Serial.print(F(" (mm)"));
const long speed = gnss.getGroundSpeed();
Serial.print(F(" Speed: "));
Serial.print(speed);
Serial.print(F(" (mm/s)"));
// 1m/s = 900/463 knots
const double speed_kn = speed / 1000.0 * 900.0 / 463.0;
const long heading = gnss.getHeading();
Serial.print(F(" Heading: "));
Serial.print(heading);
Serial.print(F(" (degrees * 10^-5)"));
const byte SIV = gnss.getSIV();
Serial.print(F(" SIV: "));
Serial.print(SIV);
Serial.print(F(" TX in: "));
Serial.print(TX_INTERVAL - (now - lastPositionReport));
Serial.println();
if (now - lastPositionReport > TX_INTERVAL) {
lastPositionReport = now;
digitalWrite(LED_BUILTIN, HIGH);
// Encode LoRa message according to aprs434.github.io
const uint32_t callsign_EEEE = encodeCallsign(CALLSIGN);
report_len = 0;
// Callsign encoded as CCCC
report[report_len++] = (callsign_EEEE >> 24) & 0xFF;
report[report_len++] = (callsign_EEEE >> 16) & 0xFF;
report[report_len++] = (callsign_EEEE >> 8) & 0xFF;
report[report_len++] = callsign_EEEE & 0xFF;
// D SSID Path Code and Data Type Code
report[report_len++] = SSID * 16 + PATH_CODE * 4 + DATE_TYPE_CODE;
// / Symbol Table Identifier
report[report_len++] = SYMBOL_TABLE_IDENTIFIER;
// XXXX Base91 Longitude
// YYYY Base91 Latitude
uint32_t aprs_lat = 900000000 - latitude;
aprs_lat = aprs_lat / 26 - aprs_lat / 2710 + aprs_lat / 15384615;
uint32_t aprs_lon = 900000000 + longitude / 2;
aprs_lon = aprs_lon / 26 - aprs_lon / 2710 + aprs_lon / 15384615;
char tmp_base91[5];
ax25_base91enc(tmp_base91, 4, aprs_lat);
for (int i=0; i<4; i++) {
report[report_len++] = tmp_base91[i];
}
ax25_base91enc(tmp_base91, 4, aprs_lon);
for (int i=0; i<4; i++) {
report[report_len++] = tmp_base91[i];
}
// $ Symbol Code
report[report_len++] = SYMBOL_CODE_BICYCLE;
// cs Course and Speed
ax25_base91enc(tmp_base91, 1, heading * 10000 / 4);
report[report_len++] = tmp_base91[0];
ax25_base91enc(tmp_base91, 1, (uint32_t)(log1p(speed_kn) / 0.07696));
report[report_len++] = tmp_base91[0];
Serial.print(F("TX: "));
// This is the old custom report I used before adopting aprs434.github.io,
// it is just callsign SPACE locator sent as ASCII
Serial.print(CALLSIGN);
Serial.print(F(" "));
Serial.println(locator);
// Debug print
Serial.println(F("Bytes: "));
for (int i = 0; i < report_len; i++) {
Serial.print((uint32_t)report[i]);
}
Serial.println(F(""));
int state = radio.transmit(report, report_len);
if (state == RADIOLIB_ERR_NONE) {
Serial.print(F(" RFM OK Datarate: "));
Serial.print(radio.getDataRate());
Serial.println(F(" bps"));
}
else if (state == RADIOLIB_ERR_PACKET_TOO_LONG) {
Serial.println(F(" too long!"));
}
else if (state == RADIOLIB_ERR_TX_TIMEOUT) {
Serial.println(F(" timeout!"));
}
else {
Serial.print(F(" failed, code "));
Serial.println(state);
}
digitalWrite(LED_BUILTIN, LOW);
}
}
}
|