Development v12

Warning - Product Update

All the resources present on this section belong to Waspmote v12, Waspmote Plug & Sense! v12 and Meshlium v3.8.

If you have a Waspmote v15, Waspmote Plug & Sense! v15 or Meshlium v4.0 please go to the new Development Section.

Check what is your version and what are the differences between the old and the new one in this document.

» 3G 11: UDP client in single connection

This example shows how to create a UDP client in single connection.

Required Materials

1 x Waspmote
1 x battery
1 x 3G + GPS module
1 x GPRS/3G antenna
1 x SIM card

Notes

- 3G module must be powered with a battery
- When 3G is in use, interrupts can't be used
- Only non transparent mode
- This example can be executed in Waspmote v12 and Waspmote v15

Code

/*  
 *  ------  3G_11 - UDP client in single connection  -------- 
 *  
 *  Explanation: This example shows how to create a UDP client 
 *  in single connection
 *  
 *  Copyright (C) 2016 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:           1.3
 *  Design:            David Gascón 
 *  Implementation:    Alejandro Gállego
 */

#include "Wasp3G.h"
#include <WaspFrame.h>

char test_string[]="Test string from Waspmote!\r\n";

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

char IP[] = "IP";
uint16_t port = 80;

int8_t answer;

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

void loop()
{

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

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

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

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

            // 5. configures UDP connection
            USB.print(F("Setting connection..."));
            answer = _3G.configureTCP_UDP();
            if (answer == 1)
            {
                USB.println(F("Done"));

                USB.print(F("Opening UDP socket..."));
                // 6. opens a UDP socket
                answer = _3G.createSocket(UDP_CLIENT);
                if (answer == 1)
                {
                    USB.println(F("Connected"));
                    if(_3G.getIP() == 1)
                    {
                        // if configuration is success shows the IP address
                        USB.print(F("IP address: ")); 
                        USB.println(_3G.buffer_3G);
                    }


                    //************************************************
                    //             Send a string of text
                    //************************************************

                    USB.print(F("Sending test string..."));
                    // 7. sends 'test_string'
                    answer = _3G.sendData(test_string, IP, port);
                    if (answer == 1) 
                    {
                        USB.println(F("Done"));
                    }
                    else if (answer == 0)
                    {
                        USB.println(F("Fail"));
                    }
                    else 
                    {
                        USB.print(F("Fail. Error code: "));
                        USB.println(answer, DEC);
                        USB.print(F("CME or IP error code: "));
                        USB.println(_3G.CME_CMS_code, DEC);
                    }


                    //************************************************
                    //             Send a ASCII frame
                    //************************************************

                    USB.print(F("Sending a frame...")); 
                    // create new frame (ASCII)
                    frame.createFrame(ASCII,"Waspmote_Pro"); 
                    // add frame fields
                    frame.addSensor(SENSOR_STR, test_string); 
                    frame.addSensor(SENSOR_BAT, PWR.getBatteryLevel());

                    // 8. sends a frame
                    answer = _3G.sendData(frame.buffer, IP, port, frame.length);
                    if (answer == 1) 
                    {
                        USB.println(F("Done"));
                    }
                    else if (answer == 0)
                    {
                        USB.println(F("Fail"));
                    }
                    else 
                    {
                        USB.print(F("Fail. Error code: "));
                        USB.println(answer, DEC);
                        USB.print(F("CME or IP error code: "));
                        USB.println(_3G.CME_CMS_code, DEC);
                    }


                    USB.print(F("Closing UDP socket..."));  
                    // 9. closes socket
                    if (_3G.closeSocket() == 1)
                    {
                        USB.println(F("Done"));
                    }
                    else
                    {
                        USB.println(F("Fail"));
                    }
                }
                else if (answer <= -4)
                {
                    USB.print(F("Connection failed. Error code: "));
                    USB.println(answer, DEC);
                    USB.print(F("CME error code: "));
                    USB.println(_3G.CME_CMS_code, DEC);
                }
                else 
                {
                    USB.print(F("Connection failed. Error code: "));
                    USB.println(answer, DEC);
                }           
            }
            else if (answer <= -10)
            {
                USB.print(F("Configuration failed. Error code: "));
                USB.println(answer, DEC);
                USB.print(F("CME error code: "));
                USB.println(_3G.CME_CMS_code, DEC);
            }
            else 
            {
                USB.print(F("Configuration failed. Error code: "));
                USB.println(answer, DEC);
            }
        }
        else
        {
            USB.println(F("3G module cannot connect to the network..."));
        }
    }
    else
    {
        // Problem with the communication with the 3G module
        USB.println(F("3G module not started"));
    }

    // 10. Powers off the 3G module
    _3G.OFF();

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

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

}

Output

Serial monitor in Waspmote:
H#
USB port started...
**************************
3G module ready...
Setting PIN code...
PIN code accepted
3G module connected to the network...
Setting connection...Done
Opening UDP socket...Conected
IP address: 10.115.190.121
Sending test string...Done
Sending a frame...Done
Closing UDP socket...Done
Sleeping...
USB port started...
**************************
3G module ready...
Setting PIN code...
PIN code accepted
3G module connected to the network...
Setting connection...Done
Opening UDP socket...Conected
IP address: 10.125.155.160
Sending test string...Done
Sending a frame...Done
Closing UDP socket...Done
Sleeping...

Monitoring the UDP connection in the PC (linux):

Connection from 10.115.190.121 port 55555 [udp/*] accepted
Test string from Waspmote!
<=>�##356897458#Waspmote_Pro#0#STR:This is a UDP test message with frame in ASCII#BAT:70#
Connection from 10.125.155.160 port 55555 [udp/*] accepted
Test string from Waspmote!
<=>�##356897458#Waspmote_Pro#0#STR:This is a UDP test message with frame in ASCII#BAT:72#

Quick Publish: