-
Notifications
You must be signed in to change notification settings - Fork 6
/
lorawan_client.cpp
234 lines (207 loc) · 5.06 KB
/
lorawan_client.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
#include "lorawan_client.h"
#define ECHO(str) if(echo){Serial.print(str);}
#define ECHOLN(str) if(echo){Serial.println(str);}
LoRaWANClient::LoRaWANClient() {
ss.begin(9600);
ss.setTimeout(SERIAL_WAIT_TIME);
}
bool LoRaWANClient::connect(bool force_reconnect){
int waitTime=INIT_WAIT_TIME;
String cmd;
ss.listen();
sendCmd("\n", "", NULL, false);
while (ss.available() > 0) {
char ch = ss.read();
Serial.print(ch);
}
if(!force_reconnect)
{
Serial.println("Checking if already joined or not ... ");
if (!sendCmd("lorawan get_join_status", "unjoined", NULL, true, waitTime)) {
Serial.println("already joined.");
return true;
}
Serial.println("unjoined.");
}
//
// LoRa module status clear
//
if (!sendCmd("mod factory_reset", "Ok", NULL, true, waitTime)) {
Serial.println("Request Failed");
return false;
}
if (!sendCmd("mod set_echo off", "Ok", NULL, true, waitTime)) {
Serial.println("Request Failed");
return false;
}
if (!sendCmd("mod save", "Ok", NULL, true, waitTime)) {
Serial.println("Request Failed");
return false;
}
if (!sendCmd("mod reset", "", NULL, true, waitTime)) {
Serial.println("Request Failed");
return false;
}
//
// LoRa module various value get
//
if (!sendCmd("mod get_hw_model", "", NULL, true, waitTime)) {
Serial.println("Request Failed");
return false;
}
if (!sendCmd("mod get_ver", "", NULL, true, waitTime)) {
Serial.println("Request Failed");
return false;
}
if (!sendCmd("lorawan get_deveui", "", NULL, true, waitTime)) {
Serial.println("Request Failed");
return false;
}
// LoRa module join to Network Server by OTAA
//
int retry=0;
while (!sendCmd("lorawan join otaa", "accepted", NULL, true, JOIN_RETRY_INTERVAL)) {
retry++;
Serial.print("'lorawan join otaa' Failed (");
Serial.print(retry);
Serial.print("/");
Serial.print(JOIN_RETRY_MAX);
Serial.println(")");
if(retry == JOIN_RETRY_MAX)
{
Serial.println("Exceeded JOIN_RETRY_MAX attempts.");
return false;
}
}
return true;
}
bool LoRaWANClient::sendCmd(String cmd, String waitStr, CALLBACK p, bool echo, int waitTime){
unsigned long tim;
String str;
ECHO("sendCmd: ");
ECHOLN(cmd);
ss.listen();
ss.print(cmd);
ss.print('\r');
delay(100);
tim = millis() + waitTime - 100;
while (millis() < tim) {
if(ss.available() > 0) {
char ch = ss.read();
ECHO(ch);
str += String(ch);
checkRx(&str, p);
if (commandCompleted(cmd, str)) {
break;
}
}
}
if (waitStr == NULL) return true;
if (str.indexOf(waitStr) >= 0) return true;
return false;
}
void LoRaWANClient::checkRx(String* rsp, CALLBACK p) {
if (!rsp || !p) {
return;
}
int rxIdx = rsp->indexOf("> rx");
if (rxIdx >= 0) {
String str = rsp->substring(rxIdx + 5 /* length of "> rx " */);
int crIdx = str.indexOf("\n");
if (crIdx >= 0) {
p(str.substring(str.indexOf(" ") + 1, crIdx));
rsp->remove(0, rxIdx);
rsp->remove(0, crIdx);
}
}
}
bool LoRaWANClient::commandCompleted(String cmd, String rsp) {
if (cmd.indexOf("lorawan tx") < 0) {
return rsp.indexOf("\n> ") >= 0;
}
return (rsp.indexOf("> tx_ok") >= 0 || rsp.indexOf("> err") >= 0);
}
bool LoRaWANClient::sendData(char *data, short port, CALLBACK p, bool echo){
int i,j;
char cmdLine[32];
char payload[MAX_PAYLOAD_SIZE*2+1]="";
char tmp[3]="";
ECHO("sending '");
ECHO(data);
ECHO("' to port ");
ECHOLN(port);
for(i=0, j=0; i< MAX_PAYLOAD_SIZE ; i++, j+=2)
{
if(data[i] == 0x00)
{
payload[j] = 0x00;
break;
}
sprintf(tmp,"%0x",data[i]);
strcat(payload, tmp);
}
sprintf(cmdLine, "lorawan tx ucnf %d %s", port, payload);
ECHOLN(cmdLine);
if(sendCmd(cmdLine, "tx_ok", p, echo, NETWORK_WAIT_TIME))
{
ECHOLN(" ... sent.");
return true;
}
else
{
ECHOLN(" ... failed.");
return false;
}
}
bool LoRaWANClient::sendData(unsigned long data, short port, CALLBACK p, bool echo){
int i,j;
char cmdLine[32];
char tmp[3]="";
ECHO("sending '");
ECHO(data);
ECHO("' to port ");
ECHOLN(port);
sprintf(cmdLine, "lorawan tx ucnf %d %08lx", port, data);
// ECHOLN(cmdLine);
if(sendCmd(cmdLine, "tx_ok", p, echo, NETWORK_WAIT_TIME))
{
ECHOLN(" ... sent.");
return true;
}
else
{
ECHOLN(" ... failed.");
return false;
}
}
bool LoRaWANClient::sendBinary(byte *data_pointer, int data_size, short port, CALLBACK p, bool echo){
char cmdLine[MAX_PAYLOAD_SIZE*2+30], tmp[]="00";
int i;
byte *b;
b=data_pointer;
if (data_size > MAX_PAYLOAD_SIZE)
{
ECHO("ERROR: size of data (");
ECHO(data_size);
ECHOLN(" bytes) too big. ");
return false;
}
sprintf(cmdLine, "lorawan tx ucnf %d ", port);
for(i=0;i<data_size;i++,b++)
{
sprintf(tmp, "%02x", *b );
strcat(cmdLine, tmp);
}
ECHOLN(cmdLine);
if(sendCmd(cmdLine, "tx_ok", p, echo, NETWORK_WAIT_TIME))
{
ECHOLN(" ... sent.");
return true;
}
else
{
ECHOLN(" ... failed.");
return false;
}
return true;
}