Libelium is offering new technical support channels, to contact with de Technical Support team to solve any issue our doubt please contact with your sales agent or visit our contact site



Post a new topicPost a reply Page 1 of 3   [ 22 posts ]
Go to page 1, 2, 3  Next
Author Message
 Post subject: Transmision y recepcion de datos GPRS PHP
PostPosted: Mon Apr 06, 2015 3:50 pm 

Joined: Tue Sep 09, 2014 3:23 pm
Posts: 20
Estimados buen dia,

Como siempre solicitando su ayuda, en este caso para que me guíen sobre si esta bien el codigo tanto de arduino como de php para poder transmitir datos con el modulo GPRS quad band SIM900. El problema es que lo corro sin problemas en arduino pero en php no se recibe nada no hay datos recibidos rescatados con el método get, es como si nunca los hubiese envido, quisiera me colaboren revisando el código haber si esta bien o mal o que me puede estar faltando.

A continuación el código:

Código arduino:

int8_t answer;
int onModulePin= 2;

char data[512];
int data_size;

char aux_str[100];
char aux;
int x = 0;


void setup(){
pinMode(onModulePin, OUTPUT);
Serial.begin(115200);

Serial.println("Starting...");
power_on();

delay(3000);

// sets the PIN code
sendATcommand("AT+CPIN=1111", "OK", 2000);

delay(3000);
//sendATcommand("AT+CGATT?", "OK", 2000);
while (sendATcommand2("AT+CREG?", "+CREG: 0,1", "+CREG: 0,5", 2000) == 0);


sendATcommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 2000);
sendATcommand("AT+SAPBR=3,1,\"APN\",\"internet.claro.com.ec\"", "OK", 2000);
//sendATcommand("AT+SAPBR=3,1,\"USER\",\"\"", "OK", 2000);
//sendATcommand("AT+SAPBR=3,1,\"PWD\",\"\"", "OK", 2000);

while (sendATcommand("AT+SAPBR=1,1", "OK", 20000) == 0)
{
delay(5000);
}


}
void loop(){
// Initializes HTTP service
answer = sendATcommand("AT+HTTPINIT", "OK", 10000);
if (answer == 1)
{
// Sets CID parameter
answer = sendATcommand("AT+HTTPPARA=\"CID\",1", "OK", 5000);
if (answer == 1)
{
// Sets url
answer = sendATcommand("AT+HTTPPARA=\"URL\",\"http://www.gsmedicalassistant.com/sensor.php?pir=99&tem=239\"", "OK", 5000);
if (answer == 1)
{
// Starts GET action
answer = sendATcommand("AT+HTTPACTION=0", "+HTTPACTION:0,200", 10000);
if (answer == 1)
{
Serial.println("Datos enviados");
}
else
{
Serial.println("Error getting the url");
}

}
else
{
Serial.println("Error setting the url");
}
}
else
{
Serial.println("Error setting the CID");
}
}
else
{
Serial.println("Error initializating");
}

sendATcommand("AT+HTTPTERM", "OK", 5000);

delay(5000);

}

void power_on(){

uint8_t answer=0;

// checks if the module is started
answer = sendATcommand("AT", "OK", 2000);
if (answer == 0)
{
// power on pulse
digitalWrite(onModulePin,HIGH);
delay(3000);
digitalWrite(onModulePin,LOW);

// waits for an answer from the module
while(answer == 0){
// Send AT every two seconds and wait for the answer
answer = sendATcommand("AT", "OK", 2000);
}
}

}


int8_t sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout){

uint8_t x=0, answer=0;
char response[100];
unsigned long previous;

memset(response, '\0', 100); // Initialize the string

delay(100);

while( Serial.available() > 0) Serial.read(); // Clean the input buffer

Serial.println(ATcommand); // Send the AT command


x = 0;
previous = millis();

// this loop waits for the answer
do{
if(Serial.available() != 0){
response[x] = Serial.read();
x++;
// check if the desired answer is in the response of the module
if (strstr(response, expected_answer1) != NULL)
{
answer = 1;
}
}
// Waits for the asnwer with time out
}
while((answer == 0) && ((millis() - previous) < timeout));

return answer;
}



int8_t sendATcommand2(char* ATcommand, char* expected_answer1,
char* expected_answer2, unsigned int timeout){

uint8_t x=0, answer=0;
char response[100];
unsigned long previous;

memset(response, '\0', 100); // Initialize the string

delay(100);

while( Serial.available() > 0) Serial.read(); // Clean the input buffer

Serial.println(ATcommand); // Send the AT command

x = 0;
previous = millis();

// this loop waits for the answer
do{
if(Serial.available() != 0){
response[x] = Serial.read();
x++;
// check if the desired answer 1 is in the response of the module
if (strstr(response, expected_answer1) != NULL)
{
answer = 1;
}
// check if the desired answer 2 is in the response of the module
if (strstr(response, expected_answer2) != NULL)
{
answer = 2;
}
}
// Waits for the asnwer with time out
}while((answer == 0) && ((millis() - previous) < timeout));

return answer;
}



Código PHP para recibir los datos.


<?php
$pir = $_GET['pir'];
$tem = $_GET['tem'];
echo "<p>Datos GPRS</p>";

echo "<p>$tem</p>";
echo "<p>$pir</p>";
?>


Top
 Profile  
 
 Post subject: Re: Transmision y recepcion de datos GPRS PHP
PostPosted: Tue Apr 07, 2015 7:48 am 

Joined: Mon Sep 28, 2009 12:06 pm
Posts: 11366
Hola geova,

¿Podría copiarnos los resultados que ve por el puerto serie?

¿El código se queda atascado en algún momento?

Gracias!


Top
 Profile  
 
 Post subject: Re: Transmision y recepcion de datos GPRS PHP
PostPosted: Fri Apr 10, 2015 2:49 am 

Joined: Tue Sep 09, 2014 3:23 pm
Posts: 20
Hola libelium-dev

mira esta es la respuesta que me da el puerto serial.

Starting...
AT
AT+CPIN=1111
AT+CREG?
AT+SAPBR=3,1,"Contype","GPRS"
AT+SAPBR=3,1,"APN","internet.claro.com.ec"
AT+SAPBR=1,1
AT+HTTPINIT
AT+HTTPPARA="CID",1
AT+HTTPPARA="URL","http://www.gsmedicalassistant.com/sensor.php?pir=99&tem=239"
AT+HTTPACTION=0
Datos enviados
AT+HTTPTERM



y el código a ningún momento se queda atascado.


Top
 Profile  
 
 Post subject: Re: Transmision y recepcion de datos GPRS PHP
PostPosted: Fri Apr 10, 2015 7:52 am 

Joined: Mon Sep 28, 2009 12:06 pm
Posts: 11366
Hola geova,

El problema puede estar con el php. ¿Has probado a hacer la prueba contra nuestro servidor?

"GET /test-get-post.php?counter=1&data=2 HTTP/1.1\r\nHost: test.libelium.com\r\nContent-Length: 0\r\n\r\n"

Prueba y nos dices.

Un saludo


Top
 Profile  
 
 Post subject: Re: Transmision y recepcion de datos GPRS PHP
PostPosted: Sun Apr 12, 2015 1:45 am 

Joined: Tue Sep 09, 2014 3:23 pm
Posts: 20
Hola Libelium.

Esta bien el codigo si lo mando asi??


// Sets url
answer = sendATcommand("AT+HTTPPARA=\"URL\",\"http://test.libelium.com/test-get-post.php?counter=1991&data=20\"", "OK", 5000);


Top
 Profile  
 
 Post subject: Re: Transmision y recepcion de datos GPRS PHP
PostPosted: Mon Apr 13, 2015 8:26 am 

Joined: Mon Sep 28, 2009 12:06 pm
Posts: 11366
Hola geova,

Sí, parece todo corrector.

¿Te funciona?

Un saludo


Top
 Profile  
 
 Post subject: Re: Transmision y recepcion de datos GPRS PHP
PostPosted: Tue Apr 14, 2015 4:07 am 

Joined: Tue Sep 09, 2014 3:23 pm
Posts: 20
Hola Libelium.

Pues lamentablemente no funciona intento enviar pero no consigo realizarlo con éxito, no se que pueda estar sucediendo. Me podrían colaborar con una directriz de porque no funciona porfa .....


Top
 Profile  
 
 Post subject: Re: Transmision y recepcion de datos GPRS PHP
PostPosted: Tue Apr 14, 2015 7:55 am 

Joined: Mon Sep 28, 2009 12:06 pm
Posts: 11366
Hola geova,

Podemos confirmarte que este código funciona, ¿puedes probarlo? Por favor, si n ote funciona pon lo que ves por el puerto serie y una foto de como tiene montado todo.

Gracias.

Code:
/*
 *  Description: This example shows how to do a GET action to a url.
 *  This example shows the AT commands (and the answers of the module) used
 *  to work with HTTP. For more information about the AT commands,
 *  refer to the AT command manual.
 *
 *  Copyright (C) 2013 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.2
 *  Author: Alejandro Gallego && Marcos Martinez
 */


int8_t answer;
int onModulePin= 2;

char data[512];
int data_size;

char aux_str[100];
char aux;
int x = 0;

char pin[]="*******";
char apn[]="*******";
char user_name[]="*******";
char password[]="*******";

char url[ ]="test.libelium.com/test-get-post.php?a=1&b=2";


void setup(){

    pinMode(onModulePin, OUTPUT);
    Serial.begin(115200);   

    Serial.println("Starting...");
    power_on();

    delay(3000);
   
    //sets the PIN code
    snprintf(aux_str, sizeof(aux_str), "AT+CPIN=%s", pin);
    sendATcommand(aux_str, "OK", 2000);

    delay(3000);

    while (sendATcommand2("AT+CREG?", "+CREG: 0,1", "+CREG: 0,5", 2000) == 0);

    // sets APN , user name and password
    sendATcommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 2000);
    snprintf(aux_str, sizeof(aux_str), "AT+SAPBR=3,1,\"APN\",\"%s\"", apn);
    sendATcommand(aux_str, "OK", 2000);
   
    snprintf(aux_str, sizeof(aux_str), "AT+SAPBR=3,1,\"USER\",\"%s\"", user_name);
    sendATcommand(aux_str, "OK", 2000);
   
    snprintf(aux_str, sizeof(aux_str), "AT+SAPBR=3,1,\"PWD\",\"%s\"", password);
    sendATcommand(aux_str, "OK", 2000);

    while (sendATcommand("AT+SAPBR=1,1", "OK", 20000) == 0)
    {
        delay(5000);
    }


}
void loop(){
    // Initializes HTTP service
    answer = sendATcommand("AT+HTTPINIT", "OK", 10000);
    if (answer == 1)
    {
        // Sets CID parameter
        answer = sendATcommand("AT+HTTPPARA=\"CID\",1", "OK", 5000);
        if (answer == 1)
        {
            // Sets url
           
            snprintf(aux_str, sizeof(aux_str), "AT+HTTPPARA=\"URL\",\"%s\"", url);
           
            answer = sendATcommand(aux_str, "OK", 5000);
            if (answer == 1)
            {
                // Starts GET action
                answer = sendATcommand("AT+HTTPACTION=0", "+HTTPACTION:0,200", 10000);
               
                if (answer == 1)
                {
                    x=0;
                    do{
                        sprintf(aux_str, "AT+HTTPREAD=%d,100", x);
                        if (sendATcommand2(aux_str, "+HTTPREAD:", "ERROR", 30000) == 1)
                        {
                            data_size = 0;
                            while(Serial.available()==0);
                            aux = Serial.read();
                            do{
                                data_size *= 10;
                                data_size += (aux-0x30);
                                while(Serial.available()==0);
                                aux = Serial.read();       
                            }while(aux != 0x0D);

                            Serial.print("Data received: ");
                            Serial.println(data_size);

                            if (data_size > 0)
                            {
                                while(Serial.available() < data_size);
                                Serial.read();

                                for (int y = 0; y < data_size; y++)
                                {
                                    data[x] = Serial.read();
                                    x++;
                                }
                                data[x] = '\0';
                            }
                            else
                            {
                                Serial.println("Download finished");   
                            }
                        }
                        else if (answer == 2)
                        {
                            Serial.println("Error from HTTP");
                        }
                        else
                        {
                            Serial.println("No more data available");
                            data_size = 0;
                        }
                       
                        sendATcommand("", "+HTTPACTION:0,200", 20000);
                    }while (data_size > 0);
                   
                    Serial.print("Data received: ");
                    Serial.println(data);
                }
                else
                {
                    Serial.println("Error getting the url");
                }
            }
            else
            {
                Serial.println("Error setting the url");
            }
        }
        else
        {
            Serial.println("Error setting the CID");
        }   
    }
    else
    {
        Serial.println("Error initializating");
    }
   
    sendATcommand("AT+HTTPTERM", "OK", 5000);

    delay(5000);

}

void power_on(){

    uint8_t answer=0;

    // checks if the module is started
    answer = sendATcommand("AT", "OK", 2000);
    if (answer == 0)
    {
        // power on pulse
        digitalWrite(onModulePin,HIGH);
        delay(3000);
        digitalWrite(onModulePin,LOW);

        // waits for an answer from the module
        while(answer == 0){ 
            // Send AT every two seconds and wait for the answer   
            answer = sendATcommand("AT", "OK", 2000);   
        }
    }

}


int8_t sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout){

    uint8_t x=0,  answer=0;
    char response[100];
    unsigned long previous;

    memset(response, '\0', 100);    // Initialize the string

    delay(100);

    while( Serial.available() > 0) Serial.read();    // Clean the input buffer

    Serial.println(ATcommand);    // Send the AT command


        x = 0;
    previous = millis();

    // this loop waits for the answer
    do{
        if(Serial.available() != 0){   
            response[x] = Serial.read();
            x++;
            // check if the desired answer is in the response of the module
            if (strstr(response, expected_answer1) != NULL)   
            {
                answer = 1;
            }
        }
        // Waits for the asnwer with time out
    }
    while((answer == 0) && ((millis() - previous) < timeout));   

    return answer;
}



int8_t sendATcommand2(char* ATcommand, char* expected_answer1,
char* expected_answer2, unsigned int timeout){

    uint8_t x=0,  answer=0;
    char response[100];
    unsigned long previous;

    memset(response, '\0', 100);    // Initialize the string

    delay(100);

    while( Serial.available() > 0) Serial.read();    // Clean the input buffer

    Serial.println(ATcommand);    // Send the AT command


        x = 0;
    previous = millis();

    // this loop waits for the answer
    do{       
        if(Serial.available() != 0){   
            response[x] = Serial.read();
            x++;
            // check if the desired answer 1 is in the response of the module
            if (strstr(response, expected_answer1) != NULL)   
            {
                answer = 1;
            }
            // check if the desired answer 2 is in the response of the module
            if (strstr(response, expected_answer2) != NULL)   
            {
                answer = 2;
            }
        }
        // Waits for the asnwer with time out
    }while((answer == 0) && ((millis() - previous) < timeout));   

    return answer;
}


Un saludo.


Top
 Profile  
 
 Post subject: Re: Transmision y recepcion de datos GPRS PHP
PostPosted: Fri Apr 17, 2015 5:51 am 

Joined: Tue Sep 09, 2014 3:23 pm
Posts: 20
Estimados,

la respuesta en el puerto serial es:

Starting...
AT
AT+CPIN=1111
AT+CREG?
AT+SAPBR=3,1,"Contype","GPRS"
AT+SAPBR=3,1,"APN","internet.claro.com.ec"
AT+SAPBR=3,1,"USER",""
AT+SAPBR=3,1,"PWD",""
AT+SAPBR=1,1
AT+HTTPINIT
AT+HTTPPARA="CID",1
AT+HTTPPARA="URL","test.libelium.com/test-get-post.php?a=1&b=2"
AT+HTTPACTION=0
AT+HTTPREAD=0,100
Data received: 22

AT+HTTPREAD=22,100
No more data available

Data received: method::get;a::1;b::2;
AT+HTTPTERM
AT+HTTPINIT
AT+HTTPPARA="CID",1
AT+HTTPPARA="URL","test.libelium.com/test-get-post.php?a=1&b=2"
AT+HTTPACTION=0
AT+HTTPREAD=0,100
Data received: 22

AT+HTTPREAD=22,100
No more data available

Data received: method::get;a::1;b::2;
AT+HTTPTERM
AT+HTTPINIT


Significa que esta bien?


Top
 Profile  
 
 Post subject: Re: Transmision y recepcion de datos GPRS PHP
PostPosted: Fri Apr 17, 2015 7:45 am 

Joined: Mon Sep 28, 2009 12:06 pm
Posts: 11366
Hola geova,

Sí, el código te está funcionando correctamente. Por lo que si ahora lo implementas con tus datos, no deberías tener ningún problema.

Un saludo.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post a new topicPost a reply Page 1 of 3   [ 22 posts ]
Go to page 1, 2, 3  Next


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Libelium theme based on 610nm Style by Daniel St. Jules of http://www.gamexe.net


© Libelium Comunicaciones Distribuidas S.L. | Terms of use.