-
Notifications
You must be signed in to change notification settings - Fork 3
/
LWM2M_example_4G.ino
341 lines (298 loc) · 10.5 KB
/
LWM2M_example_4G.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*MIT License
Copyright (c) 2021 Telit
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/* Sketch that uses the functionality of Telit Charlie board.
This application allows to detect a freefall and to send an email that warns about it with the GPS coordinates.
To detect the freefall, the registers of the accelerometer BMA400 by Bosch Sensortec were written following
the document "How to generate freefall interrupt using BMA400" by Bosch Sensortec.
Sketch for 4G products only->AT+WS46=28
author: Cristina Ceron
*/
#include <ME310.h>
#include <BMA400.h>
using namespace me310;
float x = 0, y = 0, z = 0;
const byte INTERRUPT = ACC_INT_1;
bool isr = false;
bool radius = false;
const char *resp;
#ifndef ARDUINO_TELIT_SAMD_CHARLIE
#define ON_OFF 6 /*Select the GPIO to control ON_OFF*/
#endif
/*
* If a Telit-Board Charlie is not in use, the ME310 class needs the Uart Serial instance in the constructor, that will be used to communicate with the modem.\n
* Please refer to your board configuration in variant.h file.
* Example:
* Uart Serial1(&sercom4, PIN_MODULE_RX, PIN_MODULE_TX, PAD_MODULE_RX, PAD_MODULE_TX, PIN_MODULE_RTS, PIN_MODULE_CTS);
* ME310 myME310 (Serial1);
*/
ME310 myME310;
void setup() {
char pin[] = "XXXX";
ME310::return_t rc;
pinMode(ON_OFF, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
myME310.begin(115200);
delay(1000);
myME310.powerOn();
delay(5000);
// issue command at#reboot
myME310.module_reboot();
delay(10000);
Serial.println("Telit Test AT LWM2M accelerometer");
myME310.powerOn();
Serial.println("ME310 ON");
//issue command AT+CMEE=2 and wait for answer or timeout
myME310.report_mobile_equipment_error(2);
//issue command AT+CPIN? in read mode, check that the SIM is inserted and the module is not waiting for the PIN
myME310.read_enter_pin();
// read response in 1 array position
char *resp = (char*)myME310.buffer_cstr(1);
if(resp != NULL)
{
if (strcmp(resp, "+CPIN: SIM PIN") == 0)
{
Serial.println("Insert SIM PIN");
//issue command AT+CPIN=pin
rc = myME310.enter_pin(pin);
if (rc == ME310::RETURN_VALID)
{
Serial.println("PIN inserted");
//issue command AT+CREG? to check the network status
Serial.println("Network status");
rc = myME310.read_eps_network_registration_status();
if (rc == ME310::RETURN_VALID)
{
//if +CEREG!=0,1 and +CEREG!=0,5, wait 3 second than retry the reading
resp = (char*)myME310.buffer_cstr(1);
Serial.println(resp);
while(resp != NULL)
{
if ((strcmp(resp, "+CEREG: 0,1") != 0) && (strcmp(resp, "+CEREG: 0,5") != 0))
{
delay(3000);
rc = myME310.read_eps_network_registration_status();
if(rc != ME310::RETURN_VALID)
{
Serial.println("ERROR");
Serial.println(myME310.return_string(rc));
break;
}
Serial.println(myME310.buffer_cstr(1));
resp = (char*)myME310.buffer_cstr(1);
}
else
{
break;
}
}
}
}
}
else if (strcmp(resp, "+CPIN: READY") == 0)
{
//issue command AT+CREG? to check the network status
Serial.println("Network status");
rc = myME310.read_eps_network_registration_status();
if (rc == ME310::RETURN_VALID)
{
//if +CEREG!=0,1 and +CEREG!=0,5, wait 3 second than retry the reading
resp = (char*)myME310.buffer_cstr(1);
Serial.println(resp);
while(resp != NULL)
{
if ((strcmp(resp, "+CEREG: 0,1") != 0) && (strcmp(resp, "+CEREG: 0,5") != 0))
{
delay(3000);
rc = myME310.read_eps_network_registration_status();
if(rc != ME310::RETURN_VALID)
{
Serial.println("ERROR");
Serial.println(myME310.return_string(rc));
break;
}
Serial.println(myME310.buffer_cstr(1));
resp = (char*)myME310.buffer_cstr(1);
}
else
{
break;
}
}
}
}
}
/*Registers setup for detecting freefall. Some register can be fine-tuned*/
Wire.begin();
while (!Serial);
Serial.println("BMA400 Raw Data");
while (1) {
if (bma400.isConnection()) {
bma400.initialize();
Serial.println("BMA400 is connected");
break;
} else {
Serial.println("BMA400 is not connected");
}
delay(2000);
}
uint8_t i = 0;
uint8_t *buf = (byte*)malloc(0xFF);
uint8_t rw = 1;
uint8_t reg = 0x3F;
uint8_t buf1[] = {0xF0, 0x01, 0x3F, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
uint16_t len = sizeof(buf1);
bma400.readwrite(reg, buf1, len, rw);
rw = 0;
reg = 0x00;
len = 0x50;
bma400.readwrite(reg, buf, len, rw);
i = 0;
Serial.println("\nDevice Initialization");
Serial.print("Buffer LEN=");
Serial.print(len, DEC);
Serial.print("\n");
if(buf != NULL)
{
while (i < len)
{
Serial.print("0x");
Serial.print(reg++, HEX);
Serial.print("\t0x");
Serial.print(buf[i++], HEX);
Serial.print("\n");
}
}
pinMode (INTERRUPT, INPUT);
digitalWrite (INTERRUPT, LOW); // internal pull-down resistor
attachInterrupt(digitalPinToInterrupt(INTERRUPT), freefall, RISING);
}
//Subroutine to manage the interrupt:
//the variable isr is set true
void freefall() {
Serial.print("\n***********\nFreefall Occurred\n*******\n");
isr = true;
}
/*Main function that manages the Telit module through AT commands sent by the microprocessor.
In this function if the isr variable is false the freefall not occurred, else the freefall occurred.
In the second case, the function does the following steps:
-enables the LWM2M client through AT#LWM2MENA=1,x, where x is the PDP context identifier
-waits the registration to the server LWM2M
-turns on the GPS writing the value 1 in 33211/0/0/0 through AT#LWM2MW=0,33211,0,0,0,1
-verifies the GPS configuration with AT$GPSCFG?
-switches the module's priority to GNSS priority with AT$GPSCFG=3,0
-waits for the GPS fix
-switches module's priority to WWAN priority with AT$GPSCFG=3,1
-waits 30 seconds in WWAN priority to allow the data sending to the server
-turns off the GPS writing the value 0 in 33211/0/0/0 with AT#LWM2MW=0,33211,0,0,0,0
-disables the client with AT#LWM2MENA=0
*/
void loop() {
int r1;
if (isr == true) {
bma400.read0x0e();
//enable the LWM2M client
Serial.println("Enabling and registering LWM2M client");
r1 = myME310.enableLWM2M(1, 1, ME310::TOUT_45SEC);
if (r1 == ME310::RETURN_VALID)
{
delay(20000);
Serial.println("Registered on server DM");
}
else
{
Serial.println("Enabling LWM2M returned error");
}
//turn on GPS
Serial.println("Writing 1 in resource 33211/0/0/0");
myME310.writeResource(0, 33211, 0, 0, 0, 1, ME310::TOUT_20SEC);
delay(3000);
//verify gps configuration
ME310::return_t r = myME310.read_gnss_configuration();
Serial.println(myME310.return_string(r));
Serial.println(myME310.buffer_cstr(1));
if (r == ME310::RETURN_VALID) {
delay(100);
//switch to GNSS priority
Serial.println("Switching to GNSS priority");
r = myME310.gnss_configuration(3, 0, ME310::TOUT_30SEC);
if (r == ME310::RETURN_VALID) {
Serial.println("Switched to GNSS");
} else {
Serial.println("Couldn't switch to GNSS");
}
/*While loop in which the MCU issue the command AT#LWM2MR=0,6,0,3
if the value f is 0.0< f < 20.00000, exit form while loop and set radius to true
else continue to issue the command AT#LWM2MR every 10 seconds
*/
while (radius == false) {
myME310.readResourcefloat(0, 6, 0, 3, 0, ME310::TOUT_10SEC);
resp = myME310.buffer_cstr(1);
if(resp != NULL)
{
char buffFloat[] = {resp[9], resp[10], resp[11], resp[12], resp[13], resp[14], resp[15], resp[16], resp[18], resp[19]};
//Serial.println(buffFloat);
float f = atof(buffFloat);
if (f > 0.0 && f < 20.00000)
{
radius = true;
}
else
{
//do nothing
}
delay(10000);
}
}
if (radius == true) {
//switch to WWAN priority
r = myME310.gnss_configuration(3, 1, ME310::TOUT_30SEC);
if (r == ME310::RETURN_VALID)
{
Serial.println("Switched to WWAN");
}
else
{
Serial.println("Couldn't switch to WWAN");
}
myME310.setResourceBool(0, 3200, 0, 5500, 0, 1, ME310::TOUT_30SEC);
delay(40000);
//turn off the GPS
Serial.println("Writing 0 in resource 33211/0/0/0");
r = myME310.writeResource(0, 33211, 0, 0, 0, 0, ME310::TOUT_20SEC);
delay(2000);
// The following line is used to restore the variable isr to false to do the sketch' test.
//If you don't need to do testing, please comment the following line with //
myME310.setResourceBool(0, 3200, 0, 5500, 0, 0, ME310::TOUT_20SEC);
delay(2000);
}
delay(2000);
Serial.println("Disabling client");
r = myME310.disableLWM2M(0, ME310::TOUT_10SEC);
if (r == ME310::RETURN_VALID) {
Serial.print("Client disabled");
}
exit(0);
}
}
else {
Serial.println("Freefall not occurred");
}
delay(2000);
}