-
Notifications
You must be signed in to change notification settings - Fork 0
/
satMessage.ino
327 lines (283 loc) · 9.09 KB
/
satMessage.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Data packet
uint16_t dataFormat = 2;
typedef struct __attribute__((__packed__)){
uint16_t dataFormat;
uint32_t packetStartTime;
float latitude;
float longitude;
uint8_t voltage;
uint8_t whistles;
uint8_t band[NBANDS][NCHAN];
uint8_t peakBand[NPEAKBANDS][NCHAN];
}iridiumStruct;
int makeSendBinaryDataPacket(){
int err = 0;
union{
byte b[sizeof(iridiumStruct)];
iridiumStruct binaryPacket;
}u;
u.binaryPacket.dataFormat = (uint16_t) dataFormat;
// u.binaryPacket.pointsPerPacket = NCHAN * NBANDS;
u.binaryPacket.packetStartTime = (uint32_t) packetStartTime;
u.binaryPacket.latitude = latitude;
u.binaryPacket.longitude = longitude;
Serial.print("Struct size: "); Serial.println(sizeof(iridiumStruct));
// Serial.print("Data format: "); Serial.println(u.binaryPacket.dataFormat);
Serial.print("Start time: "); Serial.println(u.binaryPacket.packetStartTime);
Serial.print("lat: "); Serial.println(u.binaryPacket.latitude);
Serial.print("lon: "); Serial.println(u.binaryPacket.longitude);
u.binaryPacket.voltage = (uint8_t) (voltage * 10);
Serial.print("V:"); Serial.println(u.binaryPacket.voltage);
u.binaryPacket.whistles = whistleCount;
// band levels
float spectrumLevel;
for (int i=0; i<NBANDS; i++){
if(meanBand[i][0]>0.000001){
spectrumLevel = 20*log10(meanBand[i][0] / fftCount) - (10 * log10(binwidth));
}
else{
spectrumLevel = -96 - (10 * log10(binwidth));
}
spectrumLevel = spectrumLevel - hydroCalLeft - gainDb;
u.binaryPacket.band[i][0] = (uint8_t) spectrumLevel;
Serial.print(u.binaryPacket.band[i][0]); Serial.print(" ");
if(NCHAN==2){
if(meanBand[i][1]>0.000001){
spectrumLevel = 20*log10(meanBand[i][1] / fftCount) - (10 * log10(binwidth));
}
else{
spectrumLevel = -96 - (10 * log10(binwidth));
}
spectrumLevel = spectrumLevel - hydroCalRight - gainDb;
u.binaryPacket.band[i][1] = (uint8_t) spectrumLevel;
Serial.print(u.binaryPacket.band[i][1]); Serial.print(" ");
}
}
// peak band processing
Serial.println();
Serial.println("Peak Bands");
for (int i=0; i<NPEAKBANDS; i++){
u.binaryPacket.peakBand[i][0] = (uint8_t) 20*log10(peakBand[i][0]) - (10 * log10(binwidth)) - hydroCalLeft - gainDb;
Serial.print(u.binaryPacket.peakBand[i][0]); Serial.print(" ");
if(NCHAN==2){
u.binaryPacket.peakBand[i][1] = (uint8_t) 20*log10(peakBand[i][1]) - (10 * log10(binwidth)) - hydroCalRight - gainDb;
Serial.print(u.binaryPacket.peakBand[i][1]); Serial.print(" ");
}
}
Serial.println();
Serial.print("w:");
Serial.print(u.binaryPacket.whistles); Serial.print(" ");
Serial.print("Sum Squares:"); Serial.println(sumOfSquares);
Serial.print("Count:"); Serial.println(sumOfSquaresCount);
Serial.print("Pos:"); Serial.println(posPeak);
Serial.print("Neg:"); Serial.println(negPeak);
// u.binaryPacket.peakPeak = (uint8_t) 20*log10((posPeak - negPeak) / 32768.0) - hydroCalLeft - gainDb;
// u.binaryPacket.rms = (uint8_t) 20*log10(sqrt(sumOfSquares / sumOfSquaresCount)) - hydroCalLeft - gainDb;
// Serial.print("peak-peak:");
// Serial.println(u.binaryPacket.peakPeak);
// Serial.print("rms:");
// Serial.println(u.binaryPacket.rms);
Serial.print("Data packet size: "); Serial.println(sizeof(u.binaryPacket));
uint8_t iridiumBuffer[sizeof(u.binaryPacket)];
for(uint8_t i=0; i<sizeof(u.binaryPacket); i++){
iridiumBuffer[i] = u.b[i];
if (u.b[i]<0x10) {Serial.print("0");} //print leading 0
Serial.print(u.b[i], HEX);
}
Serial.println();
size_t rxBufferSize = sizeof(rxBuffer);
size_t iridiumBufferSize = sizeof(iridiumBuffer);
if(introPeriod){
cDisplay();
display.println();
display.println("Send Iridium");
display.println(u.binaryPacket.packetStartTime);
display.display();
}
if(sendSatellite){
err = modem.sendReceiveSBDBinary(iridiumBuffer, iridiumBufferSize, rxBuffer, rxBufferSize);
if (err != ISBD_SUCCESS){
Serial.print("sendSBDText error: ");
Serial.println(err);
if(introPeriod){
cDisplay();
display.println("");
display.println("Send fail");
display.display();
}
if (err == ISBD_SENDRECEIVE_TIMEOUT){
Serial.println("Send timeout");
if(introPeriod){
display.println("Timeout");
display.display();
}
}
}
else{
if(introPeriod){
cDisplay();
display.println("");
display.println("Msg sent");
display.display();
}
if (rxBufferSize >0) parseMessage();
Serial.println("Msg Sent");
}
}
return err;
}
// dataPacket info
// https://github.com/loggerhead-instruments/medusa-AI-teensy/blob/main/README.md
void makeDataPacket(){
float spectrumLevel;
int iSpectrumLevel;
time_t packetTime = stopTime - rec_dur;
//char dateTime[50];
//sprintf(dateTime,"%04d%02d%02dT%02d%02d%02d", year(packetTime), month(packetTime), day(packetTime), hour(packetTime), minute(packetTime), second(packetTime));
// ASCII Packet
dataPacket = "";
if(modemType==SWARM) dataPacket = "$TD \"";
dataPacket += "{t:";
dataPacket += packetTime;
dataPacket += ",i:",
dataPacket += fileIDcounter;
dataPacket += ",d:";
dataPacket += rec_dur;
dataPacket += ",a:";
dataPacket += String(latitude, 4);
dataPacket += ",o:";
dataPacket += String(longitude, 4);
dataPacket += ",b:[";
for (int i=0; i<NBANDS; i++){
if(meanBand[i][0]>0.000001){
spectrumLevel = 20*log10(meanBand[i][0] / fftCount) - (10 * log10(binwidth));
}
else{
spectrumLevel = -96 - (10 * log10(binwidth));
}
spectrumLevel = spectrumLevel - hydroCalLeft - gainDb;
iSpectrumLevel = (int) spectrumLevel;
dataPacket += iSpectrumLevel;
if (i<NBANDS-1) dataPacket += ",";
}
// voltage
dataPacket += "],v:";
dataPacket += String((int) (voltage * 10));
// internal temperature
dataPacket += ",c:";
dataPacket += String((int) (temp.temperature * 10));
// internal pressure
dataPacket += ",p:";
dataPacket += String((int) (pressure.pressure));
// internal humidity
dataPacket += ",h:";
dataPacket += String((int) (humidity.relative_humidity));
dataPacket += "}";
// END OF TEENSY PACKET
// OPTIONAL CORAL PACKET
if (coralProcessing){
String tempCoral = String(coralPayload);
// make sure coral packet is not too large
if(dataPacket.length() + tempCoral.length() < 188){
dataPacket += tempCoral;
dataPacket.trim(); // remove /n
}
else{
dataPacket+="{C:";
dataPacket+=fileIDcounter;
dataPacket+=",sE:ov}";
}
}
// ADD CHECKSUM
if(modemType==SWARM) {
dataPacket += "\"";
uint8_t checksum = nmeaChecksum(&dataPacket[0], dataPacket.length());
dataPacket += "*";
if(checksum<17) dataPacket += "0";
dataPacket += String(checksum, HEX);
}
if(printDiags){
Serial.print("length:");
Serial.println(dataPacket.length());
Serial.println(dataPacket);
}
}
int sendDataPacket(){
int err = 0;
//int err = modem.sendSBDText("20180817T140000;26.4321;-82.3476;g:12;nh:[70,10,1,4]");
if(introPeriod){
cDisplay();
display.println("MSG");
display.println(dataPacket);
display.display();
}
if(modemType==IRIDIUM){
size_t rxBufferSize = sizeof(rxBuffer);
err = modem.sendReceiveSBDText(&dataPacket[0], rxBuffer, rxBufferSize);
if (err != ISBD_SUCCESS)
{
Serial.print("sendSBDText error: ");
Serial.println(err);
if(introPeriod){
cDisplay();
display.println("");
display.print("Send fail: ");
display.println(err);
display.display();
}
if (err == ISBD_SENDRECEIVE_TIMEOUT){
Serial.println("Send timeout");
if(introPeriod){
display.println("Timeout");
display.display();
}
}
}
else{
if(introPeriod){
cDisplay();
display.println("");
display.println("Msg sent");
display.display();
}
if (rxBufferSize >0) parseMessage();
Serial.println("Msg Sent");
}
}
if(modemType==SWARM){
Serial1.println(dataPacket);
}
return err;
}
void parseMessage(){
// process incoming message
// RM: Go into recovery mode (mode = 3)
// To Do: R30,S90,M360: Record 30 s, Sleep 90 s, Send Iridium every 360 seconds
Serial.write((char*) rxBuffer);
if((rxBuffer[0]=='R') & (rxBuffer[1]=='M')) recoveryMode(); // Go to recovery mode
}
uint8_t nmeaChecksum(const char *sz, size_t len){
size_t i = 0;
uint8_t cs;
if(sz[0]=='$') i++;
for(cs = 0; (i<len)&& sz[i]; i++){
cs ^=((uint8_t) sz[i]);
}
return cs;
}
void resetSignals(){
for (int i=0; i<NBANDS; i++){
meanBand[i][0] = 0;
peakBand[i][0] = 0;
if(NCHAN==2) {
meanBand[i][1] = 0;
peakBand[i][1] = -1000;
}
}
whistleCount = 0;
fftCount = 0;
sumOfSquares = 0;
posPeak = 0;
negPeak = 0;
sumOfSquaresCount = 0;
}