-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLWM2M_Get_Object_example.ino
286 lines (252 loc) · 6.35 KB
/
LWM2M_Get_Object_example.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
/*Copyright (C) 2020 Telit Communications S.p.A. Italy - All Rights Reserved.*/
/* See LICENSE file in the project root for full license information. */
/**
@file
ME310.cpp
string.h
stdio.h
@brief
Sample test of the use of AT commands via ME310 library
@details
In this sketch, the operation of the get object functions in LWM2M objects is shown.
NOTE:\n
@version
1.0.0
@note
@author
Cristina Desogus
@date
24/01/2022
*/
#include <Arduino.h>
#include <ME310.h>
#include <string>
#define APN "web.omnitel.it"
#define ON_OFF 6 /*Select the GPIO to control ON_OFF*/
using namespace me310;
ME310 myME310;
ME310::return_t myRc;
char ipProt[] = "IP";
const char* pin = 0;
bool setupValid = true;
int CID = 1;
int count = 0;
void setup() {
pinMode(ON_OFF, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
delay(100);
Serial.begin(115200);
myME310.begin(115200);
delay(1000);
myME310.powerOn(ON_OFF);
delay(2000);
Serial.println("Start LWM2M example");
myRc = myME310.report_mobile_equipment_error(2); //issue command AT+CMEE=2 and wait for answer or timeout
if(checkSIM())
{
if(PDPContext())
{
if(registrationStatus())
{
if(LWM2MEnable())
{
Serial.println("Configuration completed successfully");
}
else
{
setupValid = false;
}
}
else
{
setupValid = false;
}
}
else
{
setupValid = false;
}
}
else
{
setupValid = false;
}
// put your setup code here, to run once:
}
void loop() {
if(!setupValid)
{
Serial.println("Configuration ERROR");
exit(0);
}
else
{
Serial.println("Get Object 6");
myRc = myME310.LWM2M_get_object(0,6); //issue command AT#LWM2MOBJGET=agentInstanceID,objectID
if(myRc != ME310::RETURN_VALID)
{
exit(0);
}
Serial.print("<");
Serial.print(myME310.buffer_cstr_raw());
Serial.println(">");
Serial.println("Get Object 6 instance 0");
myRc = myME310.LWM2M_get_object_instance(0,6,0); //issue command AT#LWM2MOBJGET=agentInstanceID,objectID,objectInstanceID
if(myRc != ME310::RETURN_VALID)
{
exit(0);
}
Serial.print("<");
Serial.print(myME310.buffer_cstr_raw());
Serial.println(">");
Serial.println("Get Object 6 instance 0 resource 1");
myRc = myME310.LWM2M_get_object_resource(0,6,0,1); //issue command AT#LWM2MOBJGET=agentInstanceID,objectID,objectInstanceID,resourceID
if(myRc != ME310::RETURN_VALID)
{
exit(0);
}
Serial.print("<");
Serial.print(myME310.buffer_cstr_raw());
Serial.println(">");
}
Serial.println("End Application.");
exit(0);
}
//! \brief Checks SIM
/*! \details
This function checks for the presence of the SIM and its management.
*
* \return returns true if the SIM and its management is done correctly, otherwise false.
*/
bool checkSIM()
{
bool rc = false;
Serial.println("Check SIM");
myRc = myME310.read_enter_pin(); //issue command AT+cpin? in read mode, check that the SIM is inserted and the module is not waiting for the PIN
String result = myME310.buffer_cstr(1);
if(result.endsWith("READY"))
{
Serial.println("SIM is READY");
delay(5000);
rc = true;
}
else if (result.endsWith("SIM PIN"))
{
if (pin != NULL)
{
myME310.enter_pin(pin); //issue command AT+cpin=PIN
result = myME310.buffer_cstr(1);
if(result.endsWith("READY"))
{
Serial.println("SIM is READY");
rc = true;
}
else
{
rc = false;
}
}
}
else
{
rc = false;
}
return rc;
}
//! \brief PDP Context management.
/*! \details
This function deals with the management of the PDP Context.
*
* \return returns true if the management is done correctly, otherwise false.
*/
bool PDPContext()
{
bool rc = false;
myRc = myME310.define_pdp_context(CID, ipProt, APN); //issue command AT+CGDCONT=cid,PDP_type,APN
if(myRc == ME310::RETURN_VALID)
{
rc = true;
}
else
{
rc = false;
}
return rc;
}
//! \brief Checks GPRS network registration status.
/*! \details
This function takes care of checking the GPRS network registration status.
*
* \return returns true if the checking is done correctly, otherwise false.
*/
bool registrationStatus()
{
bool rc = true;
Serial.println("GPRS network registration status");
myRc = myME310.read_gprs_network_registration_status(); //issue command AT+CGREG=? (read mode)
if(myRc == ME310::RETURN_VALID)
{
char *resp = (char*) myME310.buffer_cstr(1);
while(resp != NULL)
{
if ((strcmp(resp, "+CGREG: 0,1") != 0) && (strcmp(resp, "+CGREG: 0,5") != 0))
{
delay(3000);
myRc = myME310.read_gprs_network_registration_status();
if(myRc != ME310::RETURN_VALID)
{
Serial.print("Read GPRS network registration status: ");
Serial.println(myME310.return_string(myRc));
rc = false;
break;
}
Serial.println(myME310.buffer_cstr(1));
resp = (char* )myME310.buffer_cstr(1);
}
else
{
break;
}
}
}
else
{
rc = false;
}
return rc;
}
//! \brief Enable LWM2M
/*! \details
This function is responsible for managing the enable of the LWM2M, checking the existence of objects and creating a new instance for objects 3 and 3313.
*
* \return returns true if the management is done correctly, otherwise false.
*/
bool LWM2MEnable()
{
bool ret = false;
Serial.println("LWM2MEnable");
myRc = myME310.read_enableLWM2M(); //issue command AT#LWM2MENA?, in read mode.
String resp = (char*)myME310.buffer_cstr(1);
Serial.println(resp.c_str());
if(resp.endsWith("\"ACTIVE\""))
{
ret = true;
}
else
{
myRc = myME310.enableLWM2M(1,1);
delay(1000);
myRc = myME310.read_enableLWM2M(); //issue command AT#LWM2MENA?, in read mode.
String resp = (char*)myME310.buffer_cstr(1);
Serial.println(resp.c_str());
if(resp.endsWith("\"ACTIVE\""))
{
ret = true;
}
else
{
ret = false;
}
}
return ret;
}