/* * ----- GPRS_13 - Non transparent TCP server in single connection ------- * * Explanation: This example shows how to create a non-transparent TCP * server in single connection and manage strings * * Copyright (C) 2014 Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Version: 0.4 * Design: David Gascón * Implementation: Alejandro Gállego */ #include "WaspGPRS_Pro.h" char apn[] = "apn"; char login[] = "login"; char password[] = "password"; char test_string[]="Hello Waspmote!\n"; char test_string2[]="Go to sleep Waspmote\n"; int ok; int answer; unsigned long time; void setup() { USB.println(F("**************************")); // 1. sets operator parameters GPRS_Pro.set_APN(apn, login, password); // And shows them GPRS_Pro.show_APN(); USB.println(F("**************************")); } void loop() { // setup for Serial port over USB: USB.ON(); USB.println(F("USB port started...")); // 2. activates the GPRS_SIM908 module: answer = GPRS_Pro.ON(); if ((answer == 1) || (answer == -3)) { USB.println(F("GPRS_Pro module ready...")); // 3. sets pin code: USB.println(F("Setting PIN code...")); // **** must be substituted by the SIM code if (GPRS_Pro.setPIN("****") == 1) { USB.println(F("PIN code accepted")); } else { USB.println(F("PIN code incorrect")); } // 4. waits for connection to the network: answer = GPRS_Pro.check(180); if (answer == 1) { USB.println(F("GPRS_Pro module connected to the network...")); // 5. configures IP connection USB.print(F("Setting connection...")); answer = GPRS_Pro.configureGPRS_TCP_UDP(SINGLE_CONNECTION, NON_TRANSPARENT); if (answer == 1) { USB.println(F("Done")); // if configuration is success shows the IP address USB.print(F("Configuration success. IP address: ")); USB.println(GPRS_Pro.IP_dir); // 6. create a TCP server // “server_port” must be substituted by the desired port USB.print(F("Creating TCP server...")); answer=GPRS_Pro.createSocket(TCP_SERVER,"server_port"); if (answer == 1) { USB.println(F("Done")); ok = 0; while (ok == 0) { time = millis(); // 7. waits for TCP client data: while ((GPRS_Pro.manageIncomingData() != 3) && ((millis() - time) < 300000)) { // Condition to avoid an overflow (DO NOT REMOVE) if (millis() < time) { time = millis(); } } if ((millis() - time) < 300000) { // checks the received string and compares: if (strcmp(GPRS_Pro.buffer_GPRS, test_string) == 0) { GPRS_Pro.sendData("=>Hello developer!\n\r"); time = millis(); } if (strcmp(GPRS_Pro.buffer_GPRS, test_string2) == 0) { GPRS_Pro.sendData("=>Bye developer\n\r"); ok = 1; time = millis(); } } else { ok = 1; } } // 8. closes the client: USB.print(F("Closing client...")); if (GPRS_Pro.closeSocket() == 1) { USB.println(F("Done")); } else { USB.println(F("Fail")); } // 9. closes the TCP server: USB.print(F("Closing server TCP...")); if (GPRS_Pro.closeSocket(8) == 1) { USB.println(F("Done")); } else { USB.println(F("Fail")); } } else if (answer == -2) { USB.print(F("Connection failed. Error code: ")); USB.println(answer, DEC); USB.print(F("CME error code: ")); USB.println(GPRS_Pro.CME_CMS_code, DEC); } else { USB.print(F("Connection failed. Error code: ")); USB.println(answer, DEC); } } else if (answer < -14) { USB.print(F("Configuration failed. Error code: ")); USB.println(answer, DEC); USB.print(F("CME error code: ")); USB.println(GPRS_Pro.CME_CMS_code, DEC); } else { USB.print(F("Configuration failed. Error code: ")); USB.println(answer, DEC); } } else { USB.println(F("GPRS_Pro module cannot connect to the network")); } } else { USB.println(F("GPRS_Pro module not ready")); } // 10. powers off the GPRS_Pro module: GPRS_Pro.OFF(); USB.println(F("Sleeping...")); // 11. sleeps one hour: PWR.deepSleep("00:01:00:00", RTC_OFFSET, RTC_ALM1_MODE1, ALL_OFF); } GPRS 13: Non transparent TCP server in single connection | Libelium

Development

» GPRS 13: Non transparent TCP server in single connection

This example shows how to create a non-transparent TCP server in single connection and manage strings

Required Materials

1 x Waspmote
1 x Battery
1 x GPRS_Pro module
1 x GPRS/3G antenna
1 x SIM card

Notes

- GPRS_Pro module must be powered with a battery
- When GPRS_Pro is in use, interrupts can't be used
- This example can be executed in Waspmote v12 and Waspmote v15

Code

/*  
 *  ----- GPRS_13 - Non transparent TCP server in single connection ------- 
 *  
 *  Explanation: This example shows how to create a non-transparent TCP
 *  server in single connection and manage strings
 *  
 *  Copyright (C) 2014 Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details. 
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 *  
 *  Version:           0.4
 *  Design:            David Gascón 
 *  Implementation:    Alejandro Gállego
 */

#include "WaspGPRS_Pro.h"

char apn[] = "apn";
char login[] = "login";
char password[] = "password";

char test_string[]="Hello Waspmote!\n";
char test_string2[]="Go to sleep Waspmote\n";
int ok;
int answer;
unsigned long time;

void setup()
{   
    USB.println(F("**************************"));
    // 1. sets operator parameters
    GPRS_Pro.set_APN(apn, login, password);
    // And shows them
    GPRS_Pro.show_APN();
    USB.println(F("**************************"));
}

void loop()
{

    // setup for Serial port over USB:
    USB.ON();
    USB.println(F("USB port started..."));

    // 2. activates the GPRS_SIM908 module:
    answer = GPRS_Pro.ON(); 
    if ((answer == 1) || (answer == -3))
    { 
        USB.println(F("GPRS_Pro module ready..."));

        // 3. sets pin code:
        USB.println(F("Setting PIN code..."));
        // **** must be substituted by the SIM code
        if (GPRS_Pro.setPIN("****") == 1) 
        {
            USB.println(F("PIN code accepted"));
        }
        else
        {
            USB.println(F("PIN code incorrect"));
        }

        // 4. waits for connection to the network:
        answer = GPRS_Pro.check(180);    
        if (answer == 1)
        { 
            USB.println(F("GPRS_Pro module connected to the network..."));

            // 5. configures IP connection
            USB.print(F("Setting connection..."));
            answer = GPRS_Pro.configureGPRS_TCP_UDP(SINGLE_CONNECTION, NON_TRANSPARENT);
            if (answer == 1)
            {
                USB.println(F("Done"));

                // if configuration is success shows the IP address
                USB.print(F("Configuration success. IP address: ")); 
                USB.println(GPRS_Pro.IP_dir); 

                // 6. create a TCP server
                //  “server_port” must be substituted by the desired port
                USB.print(F("Creating TCP server..."));  
                answer=GPRS_Pro.createSocket(TCP_SERVER,"server_port");
                if (answer == 1)
                {
                    USB.println(F("Done"));

                    ok = 0;
                    while (ok == 0) 
                    {
                        time = millis();
                        // 7. waits for TCP client data:
                        while ((GPRS_Pro.manageIncomingData() != 3) && ((millis() - time) < 300000))
                        {
                            // Condition to avoid an overflow (DO NOT REMOVE)
                            if (millis() < time)
                            {
                                time = millis();	
                            }
                        }

                        if ((millis() - time) < 300000)
                        {
                            // checks the received string and compares:
                            if (strcmp(GPRS_Pro.buffer_GPRS, test_string) == 0)
                            {
                                GPRS_Pro.sendData("=>Hello developer!\n\r");
                                time = millis();
                            }
                            if (strcmp(GPRS_Pro.buffer_GPRS, test_string2) == 0)
                            {
                                GPRS_Pro.sendData("=>Bye developer\n\r");
                                ok = 1;
                                time = millis();
                            }
                        }
                        else
                        {
                            ok = 1;     
                        }
                    }

                    // 8. closes the client:
                    USB.print(F("Closing client...")); 
                    if (GPRS_Pro.closeSocket() == 1)
                    {
                        USB.println(F("Done"));
                    }
                    else
                    {
                        USB.println(F("Fail"));
                    }

                    // 9. closes the TCP server:
                    USB.print(F("Closing server TCP...")); 
                    if (GPRS_Pro.closeSocket(8) == 1)
                    {
                        USB.println(F("Done"));
                    }
                    else
                    {
                        USB.println(F("Fail"));
                    }
                }
                else if (answer == -2)
                {
                    USB.print(F("Connection failed. Error code: "));
                    USB.println(answer, DEC);
                    USB.print(F("CME error code: "));
                    USB.println(GPRS_Pro.CME_CMS_code, DEC);
                }
                else 
                {
                    USB.print(F("Connection failed. Error code: "));
                    USB.println(answer, DEC);
                }           
            }
            else if (answer < -14)
            {
                USB.print(F("Configuration failed. Error code: "));
                USB.println(answer, DEC);
                USB.print(F("CME error code: "));
                USB.println(GPRS_Pro.CME_CMS_code, DEC);
            }
            else 
            {
                USB.print(F("Configuration failed. Error code: "));
                USB.println(answer, DEC);
            }
        }
        else
        {
            USB.println(F("GPRS_Pro module cannot connect to the network"));     
        }
    }
    else
    {
        USB.println(F("GPRS_Pro module not ready"));    
    }

    // 10. powers off the GPRS_Pro module:
    GPRS_Pro.OFF();

    USB.println(F("Sleeping..."));

    // 11. sleeps one hour:
    PWR.deepSleep("00:01:00:00", RTC_OFFSET, RTC_ALM1_MODE1, ALL_OFF);

}

Output

H#
USB port started...
GPRS_Pro module ready...
Setting PIN code...
PIN code accepted
GPRS_Pro module connected to the network...
Setting connection...Done
Configuration success. IP address: 84.77.198.38
Creating TCP server...Done
Closing client...Done
Closing server TCP...Done
Sleeping...

Creating a TCP connection in the PC (linux) to the server IP:
alejandro@libelium-desktop:~$ nc 84.77.198.38 55556
Hello Waspmote!
=>Hello developer!
Go to sleep Waspmote
=>Bye developer
alejandro@libelium-desktop:~$

Quick Publish: