ArduinoGeneral

Sending data via a GET com SIM808 request

userHead michandres_28 2019-09-10 11:24:22 4439 Views6 Replies
hola a todos, muy buen blog!!!!! recientemente me encuentro desarrollando un proyecto con una tarjeta programable arduino Mega y el sim808 en el cual consiste tomar datos del gps y enviarlos por medio de una petición GET los datos se envía tipo json, cuando envió datos de prueba (constante!) todo funciona bien pero cuando creo la cadena con los datos reales y variables este demora un tiempo (8 segundos) y no envía nada anexo el código de ejecución con el arduino y el código de quien me recibe la información.

"Hello everyone, very good blog!!!!! recently I find myself developing a project with a programmable card arduino Mega and the sim808 in which it consists of taking gps data and sending it by means of a GET request the data is sent type json, when it sent test data (constant!) everything works well but when I create the string with the actual and variable data this takes a while (8 seconds) and does not send anything appended the execution code with the Arduino and the code of the person who receives me."
Code: Select all
#include <SoftwareSerial.h>
#include <DFRobot_sim808.h>
SoftwareSerial mySerial(11, 10);
DFRobot_SIM808 sim808(&mySerial);
#define MESSAGE_LENGTH 300
char message[MESSAGE_LENGTH];
int messageIndex = 0;
char MESSAGE[300];
char lat[12];
char lon[12];
char year[12];
char month[12];
char day[12];
char hour[12];
char minute[12];
char second[12];
char gprsBuffer[64];
char datetime[24];
char buffer[1000];
void inicio();
void getGPS();
void envinternet();

void setup()
{
  mySerial.begin(19200);
  Serial.begin(19200);
  delay(500);
  inicio();
}
void inicio ()
{
  while(sim808.init())
  {
      Serial.print("Sim808 OFF\r\n");
      delay(1000);
      mySerial.println("AT+CFUN=1,1");
  }
  delay(3000);
  Serial.println("OK");
  delay(200);
}

void getGPS()
{
   while(!sim808_check_with_cmd("AT+CGPSPWR=1\r\n", "OK\r\n", CMD))
   {
      Serial.println("FAIL GPS");
   }
    Serial.println("GPS OK");
    delay(200);
    while(!sim808_check_with_cmd("AT+CGPSSTATUS?\r\n", "+CGPSSTATUS: Location 3D Fix",CMD))
    {
      Serial.println("adq data ");
    }
    delay(500);
    mySerial.println("AT+CGPSout=32");
    while(!sim808.getGPS());
    mySerial.println("AT+CGPSOUT=0");
    delay(300);
    Serial.print("latitude:");
    Serial.println(sim808.GPSdata.lat,6);
    Serial.print("longitude:");
    Serial.println(sim808.GPSdata.lon,6);
    Serial.print("Año:");
    Serial.print(sim808.GPSdata.year);
    Serial.print("Mes:");
    Serial.print(sim808.GPSdata.month);
    Serial.print("Dia:");
    Serial.print(sim808.GPSdata.day);
    Serial.print("Hora:");
    Serial.print(sim808.GPSdata.hour);
    Serial.print("Minuto:");
    Serial.print(sim808.GPSdata.minute);
    Serial.print("Segundo:");
    Serial.print(sim808.GPSdata.second);
    Serial.println();
 
    float la = sim808.GPSdata.lat;
    float lo = sim808.GPSdata.lon;
    float ws = sim808.GPSdata.year;
    float mo = sim808.GPSdata.month;
    float da = sim808.GPSdata.day;
    float ho = sim808.GPSdata.hour;
    float mi = sim808.GPSdata.minute;
    float se = sim808.GPSdata.second;
     
    dtostrf(la, 4, 6, lat);
    dtostrf(lo, 4, 6, lon);
    dtostrf(ws, 4, 0, year);
    dtostrf(mo, 2, 0, month);
    dtostrf(da, 2, 0, day);
    dtostrf(ho, 2, 0, hour);
    dtostrf(mi, 2, 0, minute);
    dtostrf(se, 2, 0, second);
   
sprintf(MESSAGE,"Latitude=%s&Longitud=%s&Año=%s&Mes=%s&Dia=%s&Hora=%s&minuto=%s&segundo=%s ", lat, lon, year,month,day, hour, minute, second);
    delay(200);
 }

void   envinternet(){

//-----------------------------//---Test data------//----------------- 
//char http_cmd[] = "GET /datos.json?Latitud=9.678&longitud=15&ano=2019&mes=7&dia=20&hora=10&segundos=14 HTTP/1.1\r\nHost: d3n5q9vmna3dck.cloudfront.net\r\n\r\n";

//------------//--------------//--------------------

--------------------------First method----------------------------------------------
  char http_cmd[300]="GET /datos.json?Latitude=";
  char host[80]="HTTP/1.1\r\nHost: d3n5q9vmna3dck.cloudfront.net\r\n\r\n"; 
 strcat(http_cmd,lat);
 strcat(http_cmd,"&Longitud=");
 strcat(http_cmd,lon);
 strcat(http_cmd,"&ano=");
 strcat(http_cmd,year);
                                               //continued with each of the values obtained by the gps then i ended the string 
                                                                with the host
  strcat(http_cmd," "); 
  strcat(http_cmd,host);
   Serial.println(http_cmd);
----------------------------Second method--------------------------------------------------

 char http_cmd[300]="GET /datos.json?";
  char host[80]="HTTP/1.1\r\nHost: d3n5q9vmna3dck.cloudfront.net\r\n\r\n"; 
  strcat(http_cmd,MESSAGE);     
  strcat(http_cmd," "); 
  strcat(http_cmd,host);

--------------------------SEND GET------------------------------------
  while(!sim808.join(F("ba.amx"), F("comcel"), F("comcel"))) { 
      Serial.println("Sim808 join network error");
      delay(2000);
  }

  Serial.print("IP Address is ");
  Serial.println(sim808.getIPAddress());

  if(!sim808.connect(TCP,"d3n5q9vmna3dck.cloudfront.net", 80)) {   
      Serial.println("Connect error");
  }else{
      Serial.println("Connect mbed.org success");                //So far, I have no problem with anything, everything 
                                                                                                                 works as it should.
  }
  Serial.println("waiting to fetch...");
  sim808.send(http_cmd, sizeof(http_cmd)-1);
  while (true) {
      int ret = sim808.recv(buffer, sizeof(buffer)-1);
      if (ret <= 0){
          Serial.println("fetch over...");  
          break;
      }
      buffer[ret] = '\0';
      Serial.print("Recv: ");
      Serial.print(ret);
      Serial.print(" bytes: ");
      Serial.println(buffer);
      break;
  }
  sim808.close();
  sim808.disconnect();
}
And this is the code of who receives me
Code: Select all
(function ($) {
          $.getJSON('http://d3n5q9vmna3dck.cloudfront.net/datos.json', (response) => {
              console.log("data", response);
              const table = $("<table></table>")
              const tableHead = $("<thead></thead>")
              const tableHeadFila = $("<tr></tr>")
              const encabezados = ["Longitud", "Latitud", "Año", "Mes", "Dia", "Hora", "Segundos"]

              encabezados.forEach(encabezado => {
                    const td = $("<th></th>")
                    td.append(encabezado)
                    tableHeadFila.append(td)

              })

              const tableBody = $("<tbody></tbody>")


              response.datos.reverse().forEach((dato) => {
              //response.datos.forEach((dato) => {
                const tr = $("<tr></tr>")
                
                Object.keys(dato).forEach(columna => {
                    const td = $("<td></td>")

                    td.append(dato[columna])
                    tr.append(td)
                })

                tableBody.append(tr)
              })

              tableHead.append(tableHeadFila);
              table.append(tableHead)
              table.append(tableBody)
              console.log(table)
              $("div[role=main]").html(table)
          })
        })($)
La conexión TTL esta bien, los otros ejemplos funcionan bien, el llamado al GPS responde bien, la alimentacion es con un cargador 12v a 2A, en la librería cpp aparece int DFRobot SIM808 :: send ( const char * str, int len) podría estar ahí mi error? si alguien puede indicarme como solucionar esto le agradecería, antemano muchas gracias por su atención.
"The TTL connection is fine, the other examples work well, the call to GPS responds well, the power supply is with a charger 12v to 2A, in the bookcase cpp appears int DFRobot SIM808 :: send ( const char * str, int len) could be there my error? if anyone can tell me how to fix this I would appreciate it, in advance thank you very much for your attention."
2019-09-12 05:54:12 I'm sorry I don't know what happens to the image I sent you the answer here:

funcionando bien
freeMemory()=4937
GPS encendido
adquiriendo datos
enviando mensaje
freeMemory()=4164
155
GET /datos.json?Latitude=7.820793&Longitud=-72.512383&Año=2019&Mes= 9&Dia=11&Hora=20&minuto=41&segundo=43 HTTP/1.1
Host: d3n5q9vmna3dck.cloudfront.net
156
freeMemory()=4144
IP Address is 10.166.248.223
Connect mbed.org success
waiting to fetch...
fetch over...
freeMemory()=4937
userHeadPic michandres_28
2019-09-12 05:46:51 Hi Robert, I did what recommended me after that also append a Liberia to try to know if the problem was SRAM, the option memset() is a little confusing since you suggested it to leave in zero the char http cmd apply it this way and other ways but the resu It was the same, i do not send anything I wonder it is because if I can send a constant character without modification and I can not send one by myself.

I noticed several things
1- As I had a memory overflow I decided to remove Serial.println from each gps data

2-I tested all the liveries some commands work me others I don't think it's because of the model that I'm using sim808 EVB-V3.2, the Mega Series 1,2,3 ports I don't allow you to use the example tcp conecxion only SoftwareSerial

3- sim808.send(const char* str, int len) is in the DFRobot.cpp library you have knowledge of this!!!!! how can I decrypt it?
4- During the conduct of these tests I notice that it takes approximately 10 seconds to tell me that it could not be sent (as if you were looking or trying to send)

5- Perform tests with the methods but I am still not sure what I did I hope they can correct me it also sent the module response on serialport

Be attentive to any comments thank you.
Code: Select all
   Serial.print("freeMemory()=");
   Serial.println(freeMemory());
   char prueba[] = "GET /datos.json? 
   Latitude=7.898078&Longitude=-72.487091&Año=2019&Mes=8&Dia=30&Hora=0&minuto=14&segundo=38 
   HTTP/1.1\r\nHost: d3n5q9vmna3dck.cloudfront.net\r\n\r\n";
   char http_cmd[400];
   char host[100]="HTTP/1.1\r\nHost: d3n5q9vmna3dck.cloudfront.net\r\n\r\n";
   Serial.println(strlen(prueba));
   memset(MESSAGE, 0, 400);
   sprintf(MESSAGE, "GET /datos.json?Latitude=%s&Longitud=%s&Año=%s&Mes=%s&Dia=%s&Hora=%s&minuto=%s&segundo=%s ", lat, lon, year, month, day, hour, minute, second);
   strcat(http_cmd,MESSAGE);
   strcat(http_cmd,host);
   Serial.println(http_cmd);
   Serial.println(strlen(http_cmd));
   Serial.print("freeMemory()=");
   Serial.println(freeMemory());
   
  while(!sim808.join(F("ba.amx"), F("comcel"), F("comcel"))) {
      Serial.println("Sim808 join network error");
      delay(2000);
Image
userHeadPic michandres_28
2019-09-11 13:54:32 Yeah no problem at all. Let me know if any progress was made. If not I can look at it again and explore some other avenues. userHeadPic robert
2019-09-11 05:35:03 hello robert, thank you for resonder: the first thing is the reset of Serial.println(http cmd) of the first method is:
GET /datos.json?7.820792&longitud=-72.512466&Año=2019&Mes= 9&dia=10&hora=20&minuto= 7&segundo= 1 HTTP/1.1
Host: d3n5q9vmna3dck.cloudfront.net
This is what I get no matter what method I use.
Next is that when I load the file, I use only one of the two methods or the test data, just add them to give evidence of the ways I've used and finally when you specify that I should make a call to which it refers.
I'm going to investigate memset(), see what answers I get, first and foremost thank you very much for your advice.
userHeadPic michandres_28
2019-09-10 15:50:59 After reviewing your code I noticed there are a problems with how you handle your string arrays. Firstly your code logic induced serious memory overhead where it did not need to. But more importantly, there are some bigger problems. You can not redelcare the http_command in the same scope which you do in the second method. Instead you should just set all of its values in memory to zero and then concatenate the parts you would like. I suggest the start to your looks like this:
Code: Select all
//-----------------------------//---Test data------//----------------- 
//char http_cmd[] = "GET /datos.json?Latitud=9.678&longitud=15&ano=2019&mes=7&dia=20&hora=10&segundos=14 HTTP/1.1\r\nHost: d3n5q9vmna3dck.cloudfront.net\r\n\r\n";
  Serial.println(" IN HERE 2222222222!");

 //This will set http_cmd to just lat and long and year
 char http_cmd[300]="GET /datos.json?Latitude=";
 char host[80]="HTTP/1.1\r\nHost: d3n5q9vmna3dck.cloudfront.net\r\n\r\n"; 
 strcat(http_cmd,lat);
 strcat(http_cmd,"&Longitud=");
 strcat(http_cmd,lon);
 strcat(http_cmd,"&ano=");
 strcat(http_cmd,year);

  strcat(http_cmd," "); 
  strcat(http_cmd,host);
  Serial.println(http_cmd);

  //YOU SHOULD CALL A GET REQUEST HERE FOR JUST THE LAT/LONG/YEAR data





  //This "resets" http_cmd in memory and then assigns it MESSAGE value  
  memset(http_cmd, 0, 300);
  strcat(http_cmd,"HTTP/1.1\r\nHost: d3n5q9vmna3dck.cloudfront.net\r\n\r\n");
  strcat(http_cmd,MESSAGE);     
  strcat(http_cmd," "); 
  strcat(http_cmd,host);



 //YOU SHOULD CALL A GET REQUEST HERE FOR JUST THE MESSAGE value
userHeadPic robert
2019-09-10 14:52:10 Hey I am looking into your code now. Could you tell me the output of your Serial.println(http_cmd); in the first method. It will help with the debugging process. Thanks, userHeadPic robert