Skip to content

Sending data via HTTP #35

@antmariani

Description

@antmariani

Hello guys,
I want to send my HTTP request to this API:
http://www.ikomoto.com/adiramef/api/generic.php?add_to_db&latitude=" + String(lati) + "&longitude=" + String(longi) + "&device_name=Frigorifero" + " HTTP/1.1\r\n\r\n " ;

and I modified my code as below. However, when I send my request through the browser URL, the server receives it successfully,
but the code doesn't send my request successfully. Can someone please put some lights on what I am doing wrong here? Thanks in advance!

#include <DFRobot_SIM808.h>
DFRobot_SIM808 sim808(&Serial);

char buffer[256];
float lati, longi;

void setup()
{
Serial.begin(9600);

//******** Initialize sim808 module *************
while(!sim808.init()) {
    delay(3000);
    Serial.print("Sim808 init error\r\n");
}


//*********** Attempt DHCP *******************
while(!sim808.join(F("apn.fastweb.it"))) {
    Serial.println("Fastweb join network error");
    delay(3000);
}
Serial.println("Fastweb join network OK");


//************ Successful DHCP ****************
Serial.print("--> IP Address is ");
Serial.println(sim808.getIPAddress());
delay(2000);

//*********** Establish a TCP connection ************
while(!sim808.connect(TCP,"ikomoto.com", 80)) {
   Serial.println("--> Connect error www.ikomoto.com");
   delay(3000);
}
Serial.println("--> Connect www.ikomoto.com OK");   
  
delay(3000);
     
if(sim808.attachGPS())
    Serial.println("--> Open the GPS power success");
else
    Serial.println("Open the GPS power failure");  
} 

void loop()
{
//************** Get GPS data *******************
float lati, longi;
if (sim808.getGPS()) {
Serial.println(" ");
Serial.print("Latitudine rilevata :");
Serial.println(sim808.GPSdata.lat,10);
Serial.print("Longitudine rilevata :");
Serial.println(sim808.GPSdata.lon,10);
Serial.println();

  lati = sim808.GPSdata.lat,10;
  longi = sim808.GPSdata.lon,10;     

sim808.detachGPS();
  
while(!sim808.init()) {
    delay(3000);
    Serial.print("Sim808 init error\r\n");
}  

//************ Successful DHCP ****************
Serial.print("--> IP Address is ");
Serial.println(sim808.getIPAddress());

 while(!sim808.connect(TCP,"ikomoto.com", 80)) {
   Serial.println("--> Connect error www.ikomoto.com");
   delay(3000);
}
Serial.println("--> Connect www.ikomoto.com OK");


 String lnk;
 lnk = "POST /adiramef/api/generic.php?add_to_db&latitude=" + String(lati) + "&longitude=" + String(longi) + "&device_name=Frigorifero" +  " HTTP/1.1\r\n\r\n " ;
 char http_cmd[(lnk.length())+1];
 lnk.toCharArray(http_cmd,(lnk.length()+1));

 sim808.send(http_cmd, sizeof(http_cmd)-1);
 while (true) {
 int ret = sim808.recv(buffer, sizeof(buffer)-1);
  if (ret <= 0){
      Serial.println("  ");
      Serial.println("fetch over...");
      break; 
  }
  buffer[ret] = '\0';
  Serial.print("Recv: ");
  Serial.print(ret);
  Serial.print(" bytes: ");
  Serial.println(buffer);
  break;
  }

  delay(3000);
  
  Serial.println("   ");
  Serial.print("--> Ricomincio daccapo la lettura delle coordinate dal GPS");
  Serial.println("   ");
  
  }

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions