Development

Communication / 3G + GPS:

» 3G 15b: Sending a frame to Meshlium

This example shows how to send frame to Meshlium

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

Code

/*  
 *  --------------- 3G_15b - Sending a frame to Meshlium  --------------- 
 *  
 *  Explanation: This example shows how to send frame to Meshlium
 *  
 *  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:           1.1 
 *  Design:            David Gascón 
 *  Implementation:    Alejandro Gállego
 */

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

char url[] = "pruebas.libelium.com";
int port = 80;

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

int8_t answer;

void setup()
{    
    USB.println(F("USB port started..."));

    USB.println(F("---******************************************************************************---"));
    USB.println(F("GET method to the libelium's test url with frame..."));
    USB.println(F("You can use this php to test the HTTP connection of the module."));
    USB.println(F("The php returns the parameters that the user sends with the URL."));
    USB.println(F("In this case the RTC time and the RTC temperature."));
    USB.println(F("---******************************************************************************---"));

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

}

void loop()
{

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

        // 3. set 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. wait for connection to the network:
        answer = _3G.check(180);    
        if (answer == 1)
        { 
            USB.println(F("3G module connected to the network..."));            

            // switch on RTC
            RTC.ON();
            // Create new frame (ASCII)
            frame.createFrame(ASCII,"Waspmote_PRO"); 
            // set frame fields (Time from RTC)
            frame.addSensor(SENSOR_TIME, RTC.getTime());
            // set frame fields (Temperature in Celsius sensor - float)
            frame.addSensor(SENSOR_IN_TEMP, (float) RTC.getTemperature());
            // show Frame
            frame.showFrame();            
            // switch off RTC
            RTC.OFF();            

            // 5. Sends the frame to Meshlium with GET
            USB.print(F("Sending the frame to Meshlium with GET method..."));

            answer = _3G.sendHTTPframe( url, port, frame.buffer, frame.length, GET);

            // Checks the answer
            if ( answer == 1)
            {
                USB.println(F("Done"));  
                USB.println(_3G.buffer_3G);
            }
            else if (answer < -14)
            {
                USB.print(F("Failed. Error code: "));
                USB.println(answer, DEC);
                USB.print(F("CME error code: "));
                USB.println(_3G.CME_CMS_code, DEC);
            }
            else 
            {
                USB.print(F("Failed. Error code: "));
                USB.println(answer, DEC);
            } 

            // switch on RTC
            RTC.ON();
            // Create new frame (ASCII)
            frame.createFrame(ASCII,"Waspmote_PRO"); 
            // set frame fields (Time from RTC)
            frame.addSensor(SENSOR_TIME, RTC.getTime());
            // set frame fields (Temperature in Celsius sensor - float)
            frame.addSensor(SENSOR_IN_TEMP, (float) RTC.getTemperature());
            // show Frame
            frame.showFrame();            
            // switch off RTC
            RTC.OFF();    

            // 6. Sends the frame to Meshlium with POST
            USB.print(F("Sending the frame to Meshlium with POST method..."));

            answer = _3G.sendHTTPframe( url, port, frame.buffer, frame.length, POST);

            // Checks the answer
            if ( answer == 1)
            {
                USB.println(F("Done"));  
                USB.println(_3G.buffer_3G);
            }
            else if (answer < -14)
            {
                USB.print(F("Failed. Error code: "));
                USB.println(answer, DEC);
                USB.print(F("CME error code: "));
                USB.println(_3G.CME_CMS_code, DEC);
            }
            else 
            {
                USB.print(F("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"));
    }

    // 7. powers off the 3G module
    _3G.OFF(); 

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

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

}

Output

E#
USB port started...
---******************************************************************************---
GET method to the libelium's test url with frame...
You can use this php to test the HTTP connection of the module.
The php returns the parameters that the user sends with the URL.
In this case the RTC time and the RTC temperature.
---******************************************************************************---

-------------------------------
APN: movistar.es
LOGIN: movistar
PASSWORD: movistar
-------------------------------

3G module ready...
Setting PIN code...
PIN code accepted
3G module connected to the network...
===============================
Current ASCII Frame:
Length: 74
Frame Type (decimal): 128
HEX: 3C 3D 3E 80 02 23 33 38 37 32 35 33 34 37 31 23 57 61 73 70 6D 6F 74 65 5F 50 52 4F 23 30 23 54 49 4D 45 3A 53 75 6E 2C 20 30 30 2F 30 31 2F 30 31 2C 20 30 30 3A 30 30 3A 34 32 23 49 4E 5F 54 45 4D 50 3A 32 39 2E 32 35 23
String: <=>€#387253471#Waspmote_PRO#0#TIME:Sun, 00/01/01, 00:00:42#IN_TEMP:29.25#
===============================
Sending the frame to Meshlium with GET method...Done
http/1.1 200 ok
date: fri, 06 jun 2014 09:59:19 gmt
server: apache
x-powered-by: php/5.3.28
content-type: text/html
transfer-encoding: chunked

d9
method::GET;
frame::3C3D3E80022333383732353334373123576173706D6F74655F50524F23302354494D453A53756E2C2030302F30312F30312C2030303A30303A343223494E5F54454D503A32392E323523;
TIME:Sun, 00/01/01, 00 , IN_TEMP:29.25 ,
0


===============================
Current ASCII Frame:
Length: 74
Frame Type (decimal): 128
HEX: 3C 3D 3E 80 02 23 33 38 37 32 35 33 34 37 31 23 57 61 73 70 6D 6F 74 65 5F 50 52 4F 23 31 23 54 49 4D 45 3A 53 75 6E 2C 20 30 30 2F 30 31 2F 30 31 2C 20 30 30 3A 30 31 3A 31 32 23 49 4E 5F 54 45 4D 50 3A 33 30 2E 35 30 23
String: <=>€#387253471#Waspmote_PRO#1#TIME:Sun, 00/01/01, 00:01:12#IN_TEMP:30.50#
===============================
Sending the frame to Meshlium with POST method...Done
http/1.1 200 ok
date: fri, 06 jun 2014 09:59:50 gmt
server: apache
x-powered-by: php/5.3.28
content-type: text/html
transfer-encoding: chunked

df
method::POST;
POST;frame::3C3D3E80022333383732353334373123576173706D6F74655F50524F23312354494D453A53756E2C2030302F30312F30312C2030303A30313A313223494E5F54454D503A33302E353023;
TIME:Sun, 00/01/01, 00 , IN_TEMP:30.50 ,
0


Sleeping...

Quick Publish: