forked from Snootlab/Akeru_ARM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Akeru.cpp
519 lines (456 loc) · 9.67 KB
/
Akeru.cpp
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/* Akeru.h - v4.1 [ARM version]
*
* Copyleft Snootlab 2016 - inspired by TD1208 lib by IoTHerd (C) 2016
*
* Akeru is a library for Sigfox TD1208 use with the Arduino platform.
* The library is designed to use hardware serial for serial communication between TD1208 module and Arduino.
* i.e. on Arduino/Genuino Zero : Serial1 on D0/D1, Serial2 on D12/D10, Serial3 on D5/D2
* Current library coverage is:
* - AT command
* - Sigfox payload transfer
* - TD1208 temperature read
* - TD1208 ID read
* - TD1208 supply voltage read
* - TD1208 hardware version read
* - TD1208 firmware version read
* - TD1208 power read
* - TD1208 power set
* - TD1208 downlink request
* - Data conversion in hexadecimal
*/
#include "Akeru.h"
Akeru::Akeru(Uart *uart)
{
serialPort = uart;
_lastSend = -1;
}
void Akeru::echoOn()
{
_cmdEcho = true;
}
void Akeru::echoOff()
{
_cmdEcho = false;
}
bool Akeru::begin()
{
// Let the modem warm up a bit
delay(2000);
_lastSend = -1;
// Check TD1208 communication
if (sendAT())
{
sendATCommand(ATECHOOFF, ATCOMMAND_TIMEOUT, nullptr); // disable cmd echo
return true;
}
else return false;
}
bool Akeru::isReady()
{
// IMPORTANT WARNING. PLEASE READ BEFORE MODIFYING THE CODE
//
// The Sigfox network operates on public frequencies. To comply with
// radio regulation, it can send radio data a maximum of 1% of the time
// to leave room to other devices using the same frequencies.
//
// Sending a message takes about 6 seconds (it's sent 3 times for
// redundancy purposes), meaning the interval between messages should
// be 10 minutes.
//
// Also make sure your send rate complies with the restrictions set
// by the particular subscription contract you have with your Sigfox
// network operator.
//
// FAILING TO COMPLY WITH THESE CONSTRAINTS MAY CAUSE YOUR MODEM
// TO BE BLOCKED BY YOUR SIFGOX NETWORK OPERATOR.
//
// You've been warned!
unsigned long currentTime = millis();
if(currentTime >= _lastSend && (currentTime - _lastSend) <= 600000) return false;
else return true;
}
bool Akeru::sendAT()
{
return sendATCommand(ATCOMMAND, ATCOMMAND_TIMEOUT, nullptr);
}
// Payload must be a String formatted in hexadecimal, 12 bytes max, use toHex()
bool Akeru::sendPayload(const String payload)
{
if (!isReady()) return false; // prevent user from sending to many messages
String message = (String) ATSIGFOXTX + payload;
String data = "";
if (sendATCommand(message, ATSIGFOXTX_TIMEOUT, &data))
{
if (_cmdEcho)
{
Serial.println(data);
}
_lastSend = millis();
return true;
}
else
{
return false;
}
}
bool Akeru::getTemperature(int *temperature)
{
String data = "";
if (sendATCommand(ATTEMPERATURE, ATCOMMAND_TIMEOUT, &data))
{
*temperature = data.toInt();
return true;
}
else
{
return false;
}
}
bool Akeru::getID(String *id)
{
String data = "";
if (sendATCommand(ATID, ATCOMMAND_TIMEOUT, &data))
{
*id = data;
return true;
}
else
{
return false;
}
}
bool Akeru::getVoltage(float *voltage)
{
String data = "";
if (sendATCommand(ATVOLTAGE, ATCOMMAND_TIMEOUT, &data))
{
*voltage = data.toFloat();
return true;
}
else
{
return false;
}
}
bool Akeru::getHardware(String *hardware)
{
String data = "";
if (sendATCommand(ATHARDWARE, ATCOMMAND_TIMEOUT, &data))
{
*hardware = data;
return true;
}
else
{
return false;
}
}
bool Akeru::getFirmware(String *firmware)
{
String data = "";
if (sendATCommand(ATFIRMWARE, ATCOMMAND_TIMEOUT, &data))
{
*firmware = data;
return true;
}
else
{
return false;
}
}
bool Akeru::getPower(int *power)
{
String message = (String) ATPOWER + '?';
String data = "";
if (sendATCommand(message, ATCOMMAND_TIMEOUT, &data))
{
*power = data.toInt();
return true;
}
else
{
return false;
}
}
// Power value: 0...14
bool Akeru::setPower(int power)
{
String message = (String) ATPOWER + "=" + power;
String data = "";
if (sendATCommand(message, ATCOMMAND_TIMEOUT, &data))
{
Serial.println(data);
return true;
}
else
{
return false;
}
}
bool Akeru::receive(String *data)
{
if (!isReady()) return false;
if (sendATCommand(ATDOWNLINK, ATSIGFOXTX_TIMEOUT, data))
{
// Restart serial interface
serialPort->begin(9600);
delay(200);
// Read response
String response = "";
unsigned int startTime = millis();
volatile unsigned int currentTime = millis();
volatile char rxChar = '\0';
// RX management : two ways to break the loop
// - Timeout
// - Receive
do
{
if (serialPort->available() > 0)
{
rxChar = (char)serialPort->read();
response.concat(rxChar);
}
currentTime = millis();
}while(((currentTime - startTime) < ATDOWNLINK_TIMEOUT) && response.endsWith(DOWNLINKEND) == false);
serialPort->end();
if (_cmdEcho)
{
Serial.println(response);
}
// Now that we have the full answer we can look for the received bytes
if (response.length() != 0)
{
// Search for transmission start
int index = response.indexOf("=");
// Get the chunk, it's 8 bytes long + separating spaces = 23 chars
String chunk = response.substring(index, index+24);
if (chunk.length() > 0)
{
// Remove the spaces
chunk.replace(" ", "");
*data = chunk;
return true;
}
}
}
else
{
return false;
}
}
String Akeru::toHex(int i)
{
byte * b = (byte*) & i;
String bytes = "";
for (int j=0; j<2; j++)
{
if (b[j] <= 0xF) // single char
{
bytes.concat("0"); // add a "0" to make sure every byte is read correctly
}
bytes.concat(String(b[j], 16));
}
return bytes;
}
String Akeru::toHex(unsigned int ui)
{
byte * b = (byte*) & ui;
String bytes = "";
for (int i=0; i<2; i++)
{
if (b[i] <= 0xF) // single char
{
bytes.concat("0"); // add a "0" to make sure every byte is read correctly
}
bytes.concat(String(b[i], 16));
}
return bytes;
}
String Akeru::toHex(long l)
{
byte * b = (byte*) & l;
String bytes = "";
for (int i=0; i<4; i++)
{
if (b[i] <= 0xF) // single char
{
bytes.concat("0"); // add a "0" to make sure every byte is read correctly
}
bytes.concat(String(b[i], 16));
}
return bytes;
}
String Akeru::toHex(unsigned long ul)
{
byte * b = (byte*) & ul;
String bytes = "";
for (int i=0; i<4; i++)
{
if (b[i] <= 0xF) // single char
{
bytes.concat("0"); // add a "0" to make sure every byte is read correctly
}
bytes.concat(String(b[i], 16));
}
return bytes;
}
String Akeru::toHex(float f)
{
byte * b = (byte*) & f;
String bytes = "";
for (int i=0; i<4; i++)
{
if (b[i] <= 0xF) // single char
{
bytes.concat("0"); // add a "0" to make sure every byte is read correctly
}
bytes.concat(String(b[i], 16));
}
return bytes;
}
String Akeru::toHex(double d)
{
byte * b = (byte*) & d;
String bytes = "";
for (int i=0; i<4; i++)
{
if (b[i] <= 0xF) // single char
{
bytes.concat("0"); // add a "0" to make sure every byte is read correctly
}
bytes.concat(String(b[i], 16));
}
return bytes;
}
String Akeru::toHex(char c)
{
byte *b = (byte*) & c;
String bytes = "";
if (b[0] <= 0xF) // single char
{
bytes.concat("0"); // add a "0" to make sure every byte is read correctly
}
bytes.concat(String(b[0], 16));
return bytes;
}
String Akeru::toHex(char *c, int length)
{
byte * b = (byte*) c;
String bytes = "";
for (int i=0; i<length; i++)
{
if (b[i] <= 0xF) // single char
{
bytes.concat("0"); // add a "0" to make sure every byte is read correctly
}
bytes.concat(String(b[i], 16));
}
return bytes;
}
bool Akeru::sendATCommand(const String command, const int timeout, String *dataOut)
{
// Start serial interface
serialPort->begin(9600);
delay(200);
// Add CRLF to the command
String ATCommand = "";
ATCommand.concat(command);
ATCommand.concat("\r\n");
if (_cmdEcho)
{
Serial.print((String)"\n>> " + ATCommand);
}
// Send the command : need to write/read char by char because of echo
for (int i = 0; i < ATCommand.length(); ++i)
{
serialPort->print(ATCommand.c_str()[i]);
serialPort->read();
}
if (_cmdEcho)
{
Serial.print("<< ");
}
// Read response
String response = "";
unsigned int startTime = millis();
volatile unsigned int currentTime = millis();
volatile char rxChar = '\0';
// RX management : two ways to break the loop
// - Timeout
// - Receive
do
{
if (serialPort->available() > 0)
{
rxChar = (char)serialPort->read();
response.concat(rxChar);
}
currentTime = millis();
}while(((currentTime - startTime) < timeout) && response.endsWith(ATOK) == false);
serialPort->end();
if (_cmdEcho)
{
Serial.println(response);
}
// Split the response
int index = 0;
String firstData = "";
String secondData = "";
if (response.length() != 0)
{
// Split CRLF
do
{
// Save previous index
int previous = index;
// Get next index
index = response.indexOf("\r\n", index);
// Check that index change
if (previous != index)
{
// Get the chunk
String chunk = response.substring(previous+1, index);
if (chunk.length() > 0)
{
// Remove \r\n
chunk.replace("\r\n", "");
if (firstData != "")
{
secondData = chunk;
}
else if (firstData == "" && secondData == "")
{
firstData = chunk;
}
else
{
Serial.println("ERROR on rx frame");
return false;
}
}
}
// Increment index
if (index >= 0) index++;
} while (index < response.length() && index >= 0);
}
else
{
return false;
}
// Check if we have data on the first string and OK on second = data + OK
if (firstData != "" && secondData == ATOK)
{
*dataOut = firstData;
return true;
}
// Check if we have only an OK
else if (firstData == ATOK && secondData == "")
{
return true;
}
else
{
Serial.println("Wrong AT response");
return false;
}
}