This repository has been archived by the owner on Aug 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModemClass.cpp
328 lines (257 loc) · 6.31 KB
/
ModemClass.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
/*
Class example for communicating the the Telstra LPWAN modem.
*/
#include <stdlib.h>
#include <stdarg.h>
#include <Arduino.h>
// To have the Regexp.h line below work, firstly
// 1) Download the file http://gammon.com.au/Arduino/Regexp.zip
// 2) Within the Arduino IDE, use Sketch > Include Library > Add .ZIP Library
// and select the Regexp.zip file you just downloaded
// #include "Regexp.h"
#include "ModemClass.h"
SoftwareSerial _LoRaSerial(8, 9);
//AltSoftSerial _LoRaSerial;
LoRaModem::LoRaModem()
{
_LoRaSerial.begin(9600);
_LoRaSerial.setTimeout(100);
};
/******************
_sendSerial
Generic private method for sending serial data to the modem, used by everything.
*/
void LoRaModem::_sendSerial(String message)
{
DEBUG_PRINT("> " + message);
_LoRaSerial.read();
_LoRaSerial.println(message);
};
/******************
_checkresponse
Generic private method for reading back from the modem, and qualifying the response against a Regex
*/
int LoRaModem::_checkresponse(const char* checkVal, int call_timeout = rx_timeout, int _trim = 0)
{
modemResp[0] = 0;
unsigned long startTime = millis();
int i = 0;
while (i < rx_buffer - 1)
{
if (_trim == 1)
{
// _LoRaSerial.find("Wait"); // toss the verbosity.
}
int rxcount = _LoRaSerial.readBytes(&modemResp[i], rx_buffer - i);
i += rxcount;
modemResp[i] = 0;
if (millis() - startTime > call_timeout)
{
_rspMs.Target(modemResp, i);
char result = _rspMs.Match(checkVal, 0);
if (result == REGEXP_MATCHED)
{
//Terminate the string
DEBUG_PRINT(modemResp);
return 0;
}
else
{
DEBUG_PRINT(F("Unexpected or no response"));
DEBUG_PRINT(modemResp);
return 1;
}
}
}
};
// Check AT, just check if the modem is alive
int LoRaModem::checkAT() {
_sendSerial("AT");
return _checkresponse("%+AT: OK", rx_timeout_fast);
};
// Find out current modem ID
int LoRaModem::checkID() {
_sendSerial("AT+ID");
if (_checkresponse("%+ID: DevAddr (.+)\r\n%+ID: DevEui (.+)\r\n%+ID: AppEui (.+)"))
{
return 1;
}
_rspMs.GetCapture (DevAddr, 0);
_rspMs.GetCapture (DevEui, 1);
_rspMs.GetCapture (AppEui, 2);
String temp_s;
temp_s = String(DevAddr);
temp_s.replace(":", " ");
temp_s.toCharArray(DevAddr, 12);
temp_s = String(DevEui);
temp_s.replace(":", " ");
temp_s.toCharArray(DevEui, 24);
temp_s = String(AppEui);
temp_s.replace(":", " ");
temp_s.toCharArray(AppEui, 24);
};
// Find out current network data class
int LoRaModem::getDR() {
_DR[0] = '0';
_sendSerial("AT+DR");
if (_checkresponse("%+DR: DR(.+)\r\n", rx_timeout_fast))
{
return 1;
}
_rspMs.GetCapture (_DR, 0);
return _DR[0];
};
// Set DR, where DR is "DR0", "DR1", "DR2" or "DR3"
int LoRaModem::setDR(String DR) {
_sendSerial("AT+DR=" + DR);
if (_checkresponse("%+DR: DR.+", rx_timeout_fast))
{
return 1;
}
return 0;
};
// Disable ADR
int LoRaModem::disableADR() {
_sendSerial("AT+ADR=OFF");
if (_checkresponse("%+ADR: OFF.+", rx_timeout_fast))
{
return 1;
}
return 0;
};
// Set the modem ID
int LoRaModem::setID(String addr, String dev) {
_sendSerial("AT+ID=DevAddr,\"" + addr + "\"");
if (_checkresponse("%+ID: DEVADDR .+", rx_timeout_fast))
{
return 1;
}
_sendSerial("AT+ID=DevEui,\"" + dev + "\"");
if (_checkresponse("%+ID: DEVEUI .+", rx_timeout_fast))
{
return 1;
}
return 0;
};
// Set the modem keys
int LoRaModem::setKeys(String NWSKey, String AppSKey) {
_sendSerial("AT+KEY=NWKSKEY,\"" + NWSKey + "\"");
if (_checkresponse("%+KEY: NWKSKEY .+", rx_timeout_fast))
{
return 1;
}
_sendSerial("AT+KEY=APPSKEY,\"" + AppSKey + "\"");
if (_checkresponse("%+KEY: APPSKEY .+", rx_timeout_fast))
{
return 1;
}
return 0;
};
// Set the payload port #
int LoRaModem::setPort(String portNum) {
_sendSerial("AT+PORT=" + portNum);
if (_checkresponse("%+PORT: OK.+", rx_timeout_fast))
{
return 1;
}
return 0;
};
// Send a message, don't expect an ACK
int LoRaModem::Msg(String message) {
_sendSerial("AT+MSG=\"" + message + "\"");
if (_checkresponse(".+MSG: Done"))
{
return 1;
}
return 0;
};
// Send a message, expect an ACK
int LoRaModem::cMsg(String message) {
_sendSerial("AT+CMSG=\"" + message + "\"");
if (_checkresponse(".+ACK Received.+CMSG: Done"))
{
return 1;
}
return 0;
};
// Send a HEX string message, don't expect an ACK
int LoRaModem::MsgHEX(String message) {
_sendSerial("AT+MSGHEX=\"" + message + "\"");
if (_checkresponse(".+MSGHEX: Done"))
{
return 1;
}
return 0;
};
// Send a HEX string message, expect an ACK
int LoRaModem::cMsgHEX(String message) {
_sendSerial("AT+CMSGHEX=\"" + message + "\"");
if (_checkresponse(".+ACK Received.+CMSGHEX: Done"))
{
return 1;
}
return 0;
};
/******************
cMsgBytes
Send a byte array message, expect an ACK
*/
int LoRaModem::cMsgBytes(uint8_t * bytes, int16_t length) {
_LoRaSerial.read();
_LoRaSerial.print("AT+CMSGHEX=\"");
do {
prnt("%02x", *bytes++);
} while (--length);
_LoRaSerial.println("\"");
if (_checkresponse(".+ACK Received.+CMSGHEX: Done"))
{
return 1;
}
return 0;
};
int LoRaModem::lowPower() {
_sendSerial("AT+LOWPOWER");
if (_checkresponse(".+LOWPOWER: SLEEP.+", rx_timeout_fast))
{
return 1;
}
return 0;
};
// Reset the modem
int LoRaModem::Reset() {
_sendSerial("AT+RESET");
if (_checkresponse(".+RESET: OK.+ 1.7.1"))
{
return 1;
}
return 0;
};
// Extract the downlink payload (if any) and convert to ASCII
String LoRaModem::getAscii() {
char buf[100];
char result = _rspMs.Match(".+RX: \"(.+)\".+", 0);
if (result == REGEXP_MATCHED)
{
_rspMs.GetCapture (buf, 0);
String ASCIIValue = "";
for (int i = 0; i < strlen(buf) + 1; i += 3) {
char val = buf[i] > 0x39 ? (buf[i] - 55) * 16 : (buf[i] - '0') * 16;
val += buf[i + 1] > 0x39 ? (buf[i + 1] - 55) : (buf[i + 1] - '0');
ASCIIValue += val;
}
return ASCIIValue;
}
else
{
DEBUG_PRINT(F("No ASCII payload..."));
return "";
}
};
void LoRaModem::prnt(char * fmt, ... ) {
char buf[8]; // resulting string limited to 7 chars
va_list args;
va_start (args, fmt );
vsnprintf(buf, 8, fmt, args);
va_end (args);
_LoRaSerial.print(buf);
}