-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSocket_example.ino
186 lines (161 loc) · 6.35 KB
/
Socket_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
/*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 using sockets via AT commands
@details
In this example sketch, the use of sockets is shown through the commands offered by the ME310 library.\n
NOTE:\n
For correct operation it is necessary to set the correct APN.
@version
1.0.0
@note
@author
Cristina Desogus
@date
26/02/2021
*/
#include <ME310.h>
//#include <string.h>
#define APN "APN"
#ifndef ARDUINO_TELIT_SAMD_CHARLIE
#define ON_OFF 6 /*Select the GPIO to control ON_OFF*/
#endif
using namespace me310;
/*
* 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;
ME310::return_t rc; //Enum of return value methods
int cID = 1; //PDP Context Identifier
int connID = 1; //Socket connection identifier.
char ipProt[] = "IP"; //Packet Data Protocol type
char server[] = "modules.telit.com"; //echo server
int port = 10510;
void setup() {
pinMode(ON_OFF, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
myME310.begin(115200);
delay(3000);
myME310.module_reboot(); // issue command at#reboot
delay(10000);
Serial.println("Telit Test AT Socket command");
myME310.powerOn();
Serial.println("ME310 ON");
myME310.report_mobile_equipment_error(2); //issue command AT+CMEE=2 and wait for answer or timeout
myME310.read_enter_pin(); //issue command AT+pin? in read mode, check that the SIM is inserted and the module is not waiting for the PIN
char *resp = (char* )myME310.buffer_cstr(2);
if(resp != NULL)
{
if(strcmp(resp, "OK") == 0) // read response in 2 array position
{
Serial.println("Define PDP Context");
rc = myME310.define_pdp_context(cID, ipProt, APN); //issue command AT+CGDCONT=cid,PDP_type,APN
if(rc == ME310::RETURN_VALID)
{
myME310.read_define_pdp_context(); //issue command AT+CGDCONT=? (read mode)
Serial.print("pdp context read: ");
Serial.println(myME310.buffer_cstr(1)); //print second line of modem answer
Serial.print("gprs network registration status: ");
rc = myME310.read_gprs_network_registration_status(); //issue command AT+CGREG=? (read mode)
if(rc == ME310::RETURN_VALID)
{
resp = (char* )myME310.buffer_cstr(1);
Serial.println(resp);
while(resp != NULL)
{
if ((strcmp(resp, "+CGREG: 0,1") != 0) && (strcmp(resp, "+CGREG: 0,5") != 0))
{
delay(3000);
rc = myME310.read_gprs_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;
}
}
}
Serial.println("Activate context");
myME310.context_activation(cID, 1); //issue command AT#SGACT=cid,state and wait for answer or timeout
}
}
}
}
void loop() {
char data[] = "We are sending some data\nSome data OK\nAnother data";
Serial.print("Socket configuration: ");
ME310::return_t r = myME310.socket_configuration(connID, cID); //issue command AT#SCFG=connID,cID and wait for answer or timeout
Serial.println(myME310.return_string(r)); //returns a string with return_t codes
if(r == ME310::RETURN_VALID)
{
Serial.print("Socket dial: ");
r = myME310.socket_dial(connID, 0, port, server, 0, 0, 1, 0, 0, ME310::TOUT_1MIN); // issue commandAT#SD=connID,protocol,port,IPAddrServer,timeout
Serial.println(myME310.return_string(r)); //returns a string with return_t codes
if(r == ME310::RETURN_VALID)
{
delay(100);
Serial.print("Socket Status: ");
r = myME310.socket_status(connID, ME310::TOUT_10SEC); //issue command AT#SS=connID and wait for answer or timeout
delay(100);
Serial.println(myME310.return_string(r));
Serial.print("SEND: ");
r = myME310.socket_send_data_command_mode_extended(connID, (int) sizeof(data), data, 1, ME310::TOUT_30SEC);
Serial.println(myME310.return_string(r));
if(r != ME310::RETURN_VALID)
{
Serial.println("Send is failed");
}
else
{
Serial.print("Socket Listen: ");
r = myME310.socket_listen(connID, 0, port); //issue command AT#SL=connID,listenState(0 close socket listening),port and wait for answer or timeout
Serial.println(myME310.return_string(r));
delay(5000);
if(r == ME310::RETURN_VALID)
{
Serial.print("READ: ");
r = myME310.socket_receive_data_command_mode(connID, (int) sizeof(data), 0, ME310::TOUT_10SEC); //issue command AT#SRECV=connID,size and wait for answer or timeout
Serial.println(myME310.return_string(r));
if(r == ME310::RETURN_VALID)
{
Serial.print("Payload: <");
Serial.print((String)myME310.buffer_cstr_raw()); // print modem answer in raw mode
Serial.println(">");
}
else
{
Serial.println(myME310.return_string(r));
}
r = myME310.socket_listen(connID, 0, port);
}
else
{
Serial.println(myME310.return_string(r));
}
}
}
else
{
Serial.println(myME310.return_string(r));
}
}
Serial.println("The application has ended...");
exit(0);
}