Socket

Socket

Connecting to TCP or UDP server 

Module can connect to server through GSM connection. So server IP address and domain port number have to be specified. The GSM module needs time to turn on so GSM status must be checked or delay initiated before trying to connect to any server.

Connecting to TCP or UDP server
main() { Delay(10000); // Delay 10 sec for GSM to turn on. while(get_val(Sensor_SignalPDPActive) == 0){ // wait for GSM to turn on Delay(1000); } socket(CREATE_TCP, "domain.com", 1234); // Connect to socket while(1){ Delay(1000); } }

Sending data through socket

When module is connected to server then messages could be send to server. 

Sending data to server
new messageTCP[] = "Connected to TCP!!!"; main() { while(get_val(Sensor_SignalPDPActive) == 0){ // wait for GSM to turn on Delay(1000); } if (socket(STATUS) == CONNECTED) // Connect to socket { // if connected do... socket(SEND, strlen(messageTCP), messageTCP) ; // Send message through socket } while(1){ Delay(1000); } }

Reading data from socket

To read data from socket it's best to use callback case Socket_data_received and then read socket buffer. Other callback cases could be used too. Socket_closed case could report that connection is gone and Socket_sent could be used to confirm that message has been sent successfully.

Reading data from server
new socketRx[1520]; new Data_sent_flag = 0, howMuchWasActuallyReceived; public callback (sk) { switch(sk) { case Socket_data_received: // Socket data received callback event { socket(READ, socketRx, sizeof(socketRx), howMuchWasActuallyReceived); // Read socket buffer into socketRx array } case Socket_closed: // Socket closed callback event { write_buf(SMS, 0, "+3706xxxxxxx", "Disconnected"); // Send SMS to "+370xxxxxxx" with "Disconnected" message } case Socket_sent : // Socket closed callback event { Data_sent_flag=1; // set flag to indicate that data sent } } return 0; }

Checking Status

It is good practice to protect program by checking socket connection status. If  NO_INTERNET or SOCKET_SHUT is returned then program should try to connect or reconnect. If PENDING_RX or PENDING_TX then program should wait before sending or reading new data. if CONNECTED then this case could be skipped.

Checking connection status
new stat = 0; new socketRx[1520]; new socketTx[1520]; new howMuchWasActuallyReceived; main(){ stat = socket(STATUS); // check connection status if((stat == NO_INTERNET) || (stat == SOCKET_SHUT) ){ // if not connected while(socket(CREATE_TCP, "domain.com", 1234)!=CONNECTED){ // establish connection Delay(1000); // wait for connection } } else if((stat == PENDING_RX) || (stat == PENDING_TX)){ // if data pending while((socket(STATUS)==PENDING_RX)||(socket(STATUS)==PENDING_TX)){ // wait while data is pending delay(100); } } socket(SEND, 10, socketTx); socket(READ, socketRx, sizeof(socketRx), howMuchWasActuallyReceived); while(1){ Delay(1000); } }


Closing the socket

When internet connection is no longer necessary then it should be closed. it is possible to wait for flag

internet connection closure
new data_send_flag=0; main(){ while(1){ if(data_send_flag==1){ //Raise the flag on your own, somewhere in your code, when you'll think that you no longer need internet connection for script. socket(CLOSE); } Delay(1000); } }