Ok, adesso che la parte facile (SMS) è fatta... vediamo di domare la parte dati.
Ok, now that the easy part (SMS) is done ... let's consider the data connection task.
The steps required to establish a data connection are:
Initialize a PDP context by entering the APN of your operator
Login with username and password (if necessary)
Activate PDP Context
At this point, initial setup is complete, to establish a connection from here, these are the steps:
Configure the server to connect to its port number
Start the connection
Send data
Close the connection
Clear the counters of the connection
For the first test of the connection you can use the the following sketch:
In my case, the operator was TIM Telecom Italia.
The sketch is modified by cellular passthrough. The library NewSoftSerial is attached to the bottom.
Gli step obbligatori per stabilire una connessione dati sono:
Inizializzare un contesto PDP inserendo il l'apn del proprio operatore
Autenticarsi con username e password (se necessari)
Attivare il contesto PDP
A questo punto il setup iniziale è terminato, per stabilire una connessione a partire da qui i passi saranno:
Configurare il server a cui connettersi con la relativa porta
Iniziare la connessione
Spedire i dati
Chiudere la connessione
Azzerare i contatori della connessione
Il primo test di connessione lo facciamo con il seguente sketch:
Nel mio caso, l'operatore è TIM di Telecom Italia.
Lo sketch è modificato dal cellular passthrough. La libreria NewSoftSerial è quella allegata in fondo alla pagina.
#include <NewSoftSerial.h> //Include the NewSoftSerial library to send serial commands to the cellular module.
#include <string.h>
#define BUFFSIZ 90
char at_buffer[BUFFSIZ];
int GPRS_registered;
int GPRS_AT_ready;
char incoming_char=0;
char buffer[60];
String myString="1";
NewSoftSerial cell(2,3);
void InizializzaReteGPRS(){
Serial.println("Setting up PDP Context");
cell.println("AT+CGDCONT=1,\"IP\",\"ibox.tim.it\"");
delay(1000);
while(cell.available()){
incoming_char=cell.read();
Serial.print(incoming_char);
}
Serial.println("Sending null password"); //cell.println("AT+CGPCO=0,\"none\",\"none\", 1");
delay(1000);
while(cell.available()){
incoming_char=cell.read();
Serial.print(incoming_char);
}
Serial.println("Activating PDP Context");
cell.println("AT+CGACT=1,1");
delay(1000);
while(cell.available()){
incoming_char=cell.read();
Serial.print(incoming_char);
}
Serial.println("Configuring TCP connection to TCP Server");
cell.println("AT+SDATACONF=1,\"TCP\",\"www.google.com\",5000");
delay(1000);
while(cell.available()){
incoming_char=cell.read();
Serial.print(incoming_char);
}
Serial.println("Start TCP connection to TCP Server");
cell.println("AT+SDATASTART=1,1");
delay(1000);
while(cell.available()){
incoming_char=cell.read();
Serial.print(incoming_char);
}
Serial.println("Checking Connection Status\n");
cell.println("AT+SDATASTATUS=1");
delay(1000);
while(cell.available()){
incoming_char=cell.read();
Serial.print(incoming_char);
}
}
// Do system wide initialization here in this function
void setup()
{
Serial.begin(9600);
cell.begin(9600);
Serial.println("Starting SM5100B Communication...");
delay(5000);
/* Currently GPRS is not registered and AT is not ready */
GPRS_registered = 0;
GPRS_AT_ready = 0;
}
/* Reads AT String from the SM5100B GSM/GPRS Module */
void loop() {
/* If called for the first time, loop until GPRS and AT is ready */
if(GPRS_registered == 0){
InizializzaReteGPRS();
GPRS_registered=1;
}
if(cell.available() >0)
{
incoming_char=cell.read();
Serial.print(incoming_char);
}
if(Serial.available() >0)
{
incoming_char=Serial.read();
cell.print(incoming_char);
}
}
A
fter the initialization phase, you can test the connection by sending the following command from hyperterminal:
AT+SDATASTATUS=1which will answer by something like:
+SOCKSTATUS: 1,0,2130,0,0,0
the second zero of the response tells us that the socket N. 1 is not yet connected.
You can follow these steps:
Configure the server to connect with its port number:
AT+SDATACONF=1,"TCP","www.google.com",5000
Start the connection:
AT+SDATASTART=1,1
Send data:
AT+SDATATSEND=1,10
write data followed by ctrl+Z. Data must be equal to the size specified by sdatasend, in this case, 10 byte
Close the connection:
AT+SDATASTART=1,0
If you feel the article has been helpful and would like to express your gratitude with a symbolic donation of € 3, justclick the button below, thanks.
#include <NewSoftSerial.h> //Include the NewSoftSerial library to send serial commands to the cellular module.
#include <string.h>
#define BUFFSIZ 90
char at_buffer[BUFFSIZ];
int GPRS_registered;
int GPRS_AT_ready;
char incoming_char=0;
char buffer[60];
String myString="1";
NewSoftSerial cell(2,3);
void InizializzaReteGPRS(){
Serial.println("Setting up PDP Context");
cell.println("AT+CGDCONT=1,\"IP\",\"ibox.tim.it\"");
delay(1000);
while(cell.available()){
incoming_char=cell.read();
Serial.print(incoming_char);
}
Serial.println("Sending null password"); //cell.println("AT+CGPCO=0,\"none\",\"none\", 1");
delay(1000);
while(cell.available()){
incoming_char=cell.read();
Serial.print(incoming_char);
}
Serial.println("Activating PDP Context");
cell.println("AT+CGACT=1,1");
delay(1000);
while(cell.available()){
incoming_char=cell.read();
Serial.print(incoming_char);
}
Serial.println("Configuring TCP connection to TCP Server");
cell.println("AT+SDATACONF=1,\"TCP\",\"www.google.com\",5000");
delay(1000);
while(cell.available()){
incoming_char=cell.read();
Serial.print(incoming_char);
}
Serial.println("Start TCP connection to TCP Server");
cell.println("AT+SDATASTART=1,1");
delay(1000);
while(cell.available()){
incoming_char=cell.read();
Serial.print(incoming_char);
}
Serial.println("Checking Connection Status\n");
cell.println("AT+SDATASTATUS=1");
delay(1000);
while(cell.available()){
incoming_char=cell.read();
Serial.print(incoming_char);
}
}
// Do system wide initialization here in this function
void setup()
{
Serial.begin(9600);
cell.begin(9600);
Serial.println("Starting SM5100B Communication...");
delay(5000);
/* Currently GPRS is not registered and AT is not ready */
GPRS_registered = 0;
GPRS_AT_ready = 0;
}
/* Reads AT String from the SM5100B GSM/GPRS Module */
void loop() {
/* If called for the first time, loop until GPRS and AT is ready */
if(GPRS_registered == 0){
InizializzaReteGPRS();
GPRS_registered=1;
}
if(cell.available() >0)
{
incoming_char=cell.read();
Serial.print(incoming_char);
}
if(Serial.available() >0)
{
incoming_char=Serial.read();
cell.print(incoming_char);
}
}
Dopo la prima fase di inizializzazione, è possibile verificare la connessione mandando da hyperterminal il comando:
AT+SDATASTATUS=1
che risponderà qualcosa del tipo:
+SOCKSTATUS: 1,0,2130,0,0,0
il secondo zero della risposta ci dice che il socket n. 1 non è ancora connesso.
Manualmente è possibile seguire questi passaggi:
Configurare il server a cui connettersi con la relativa porta:
AT+SDATACONF=1,"TCP","www.google.com",5000
Iniziare la connessione:
AT+SDATASTART=1,1
Spedire i dati:
AT+SDATATSEND=1,10
scrivere la stringa che si vuole inviare seguita da control+Z, attenzione a non inserire più caratteri in byte di quelli indicati nel secondo parametro del comando sdatasend, in questo caso, 10 byte
Chiudere la connessione:
AT+SDATASTART=1,0
Se ritenete che l'articolo vi sia stato utile e volete esprimere la vostra riconoscenza con una donazione simbolica di 3 euro, vi basterà cliccare il pulsante sottostante, grazie.