Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending data via HTTP #35

Open
antmariani opened this issue Aug 17, 2022 · 13 comments
Open

Sending data via HTTP #35

antmariani opened this issue Aug 17, 2022 · 13 comments

Comments

@antmariani
Copy link

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("   ");
  
  }

}

@qsjhyy
Copy link
Contributor

qsjhyy commented Aug 17, 2022

Maybe you can debug the printed information to make the problem more specific. I don't have the corresponding module at present.

@hoerup
Copy link

hoerup commented Aug 17, 2022

3 questions for you:

  1. Are you sure you want to use a POST when you are adding the request parameters in GET style ??
  2. Why are you omitting the http Host: header while using http/1.1 ?
  3. In your read loop: Why are you breaking after first read - are you sure that you have received the entire response at that point ?

@antmariani
Copy link
Author

Hi Hoerup,

Thanks you for effort.
I just modified the code in according your suggestion, but I'not able to write the coordinates into db.... I don't understand why

This is the code:

#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 = "GET /adiramef/api/generic.php?add_to_db&latitude=" + String(lati) + "&longitude=" + String(longi) + "&device_name=Frigorifero" +  " HTTP/1.1\r\n\r\n " + " Host: " + "www.ikomoto.com" + "\r\n" + "Connection: close\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);
  }

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

}

This is the output by serial monitor:

--> Connect www.ikomoto.com OK
11:02:20.159 -> AT+CIPSEND=153
11:02:20.674 -> GET /adiramef/api/generic.php?add_to_db&latitude=40.95&longitude=14.64&device_name=Frigorifero HTTP/1.1
11:02:20.813 ->
11:02:20.813 -> Host: www.ikomoto.com
11:02:20.813 -> Connection: close
11:02:20.861 ->
11:02:21.280 -> �Recv: 255 bytes: $GPGGA,090312.000,4056.9999,N,01438.5621,E,1,3,3.27,344.4,M,42.3,M,,5B
11:03:11.857 -> $GPGLL,4056.9999,N,01438.5621,E,090312.000,A,A
59
11:03:11.903 -> $GPGSA,A,2,32,27,10,,,,,,,,,,3.42,3.27,1.0004
11:03:11.949 -> $GPGSV,3,1,11,08,71,298,19,27,65,147,27,10,48,049,40,21,47,296,18
7F
11:03:12.042 -> $GPGSV,3,2,11
11:03:12.226 -> Recv: 255 bytes: ,32,36,114,29,22,24,140,,01,20,280,,23,17,047,7D
11:03:12.319 -> $GPGSV,3,3,11
11:03:12.319 -> Recv: 255 bytes: $GPGGA,090312.000,4056.9999,N,01438.5621,E,1,3,3.27,344.4,M,42.3,M,,5B
$GPGLL,4056.9999,N,01438.5621,E,090312.000,A,A
59
$GPGSA,A,2,32,27,10,,,,,,,,,,3.42,3.27,1.00
04
$GP
11:03:12.691 -> Recv: 255 bytes: GSV,3,1,11,08,71,298,19,27,65,147,27,10,48,049,40,21,47,296,18*$GPGGA,090313.000,4056.9998,N,01438.5616,E,1,3,3.27,344.4,M,42.3,M,,5F
11:03:12.830 -> $GPGLL,4056.9998,N,01438.5616,E,090313.000,A,A
5D
11:03:12.922 -> $GPGSA,A,2,32,27,10,,,,,,,,,,3.42,3.27,1.0004
11:03:12.969 -> $GPGSV,3,1,11,08,71,
11:03:13.108 -> Recv: 255 bytes: 298,19,27,65,147,27,10,48,049,40,21,47,296,18
7F
11:03:13.154 -> $GPGSV,3,2,110.56,217.68,170822,,,A6D
11:03:13.200 -> $GPVTG,217.68,T,,M,0.56,N,1.04,K,A
31

Can you help me?
Thanks in advance.

@antmariani
Copy link
Author

Hi qsjhyy,

The output is in the upper part.

@hoerup
Copy link

hoerup commented Aug 17, 2022

So you're using the hardware serial both for arduion<->SIM808 - and for debug output - that makes it a bit more difficult to interpret then

Oh and you still have a double \r\n between the request line and first header - thats not correct !

@antmariani
Copy link
Author

I think the focus in on these lines, but I don't understand where is the problem:

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 = "GET /adiramef/api/generic.php?add_to_db&latitude=" + String(lati) + "&longitude=" + String(longi) + "&device_name=Frigorifero" +  " HTTP/1.0\r\n " + "Host: " + "www.ikomoto.com" + "\r\n" + "Connection: close\r\n ";;

 char http_cmd[(lnk.length())+1];
 lnk.toCharArray(http_cmd,(lnk.length()+1));

 Serial.println(http_cmd);
 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;
  }

@hoerup
Copy link

hoerup commented Aug 17, 2022

Looking at the source: it looks like theres a bug in detachGPS?

https://github.com/DFRobot/DFRobot_SIM808/blob/master/src/DFRobot_SIM808.cpp#L894

imho it should stop the GPS dataflow by sending "AT+CGNSTST=0\r\n"
@qsjhyy what to you think ??

@antmariani
Copy link
Author

the latest output is this, but the coordinates into db isn't writted......

11:55:11.685 -> --> Connect www.ikomoto.com OK
11:55:14.682 -> AT+CGNSPWR=1
11:55:14.730 -> AT+CGNSTST=1
11:55:14.776 -> --> Open the GPS power success
11:55:17.168 ->
11:55:17.168 -> Latitudine rilevata :40.9492111206
11:55:17.168 -> Longitudine rilevata :14.6444616317
11:55:17.215 ->
11:55:17.215 -> AT+CGNSPWR=0
11:55:22.178 -> AT
11:55:22.225 -> AT+CFUN=1
11:55:22.225 -> AT+CPIN?
11:55:22.414 -> --> IP Address is 10.50.191.97
11:55:11.685 -> --> Connect www.ikomoto.com OK
11:55:14.682 -> AT+CGNSPWR=1
11:55:14.730 -> AT+CGNSTST=1
11:55:14.776 -> --> Open the GPS power success
11:55:17.168 ->
11:55:17.168 -> Latitudine rilevata :40.9492111206
11:55:17.168 -> Longitudine rilevata :14.6444616317
11:55:17.215 ->
11:55:17.215 -> AT+CGNSPWR=0
11:55:22.178 -> AT
11:55:22.225 -> AT+CFUN=1
11:55:22.225 -> AT+CPIN?
11:55:22.414 -> --> IP Address is 10.50.191.97
11:55:43.759 -> AT+CIPSTART="TCP","ikomoto.com",80
11:55:43.852 -> --> Connect www.ikomoto.com OK
11:55:43.899 -> AT+CIPSEND=148
11:55:44.413 -> GET /adiramef/api/generic.php?add_to_db&latitude=40.95&longitude=14.64&device_name=Frigorifero HTTP/1.0
11:55:44.508 -> Host: www.ikomoto.com
11:55:44.554 -> Connection: close
11:55:44.974 -> �Recv: 255 bytes: $GPGGA,095547.000,,,,,0,5,,,M,,M,,47
11:55:46.613 -> $GPGLL,,,,,095547.000,V,N
70
11:55:46.659 -> $GPGSA,A,1,,,,,,,,,,,,,,,1E
11:55:46.706 -> $GPGSV,2,1,08,21,64,322,,32,44,084,32,22,43,120,27,27,39,158,38
75
11:55:46.752 -> $GPGSV,2,2,08,01,38,299,,10,26,050,24,03,23,236,14,14,13,313,17*7F
11:55:46.847 -> $GPRMC,095547.000,V,
11:55:49.792 ->
11:55:49.792 -> --> Ricomincio daccapo la lettura delle coordinate dal GPS
11:55:50.815 ->
11:55:50.815 -> Latitudine rilevata :40.9495239257
11:55:50.861 -> Longitudine rilevata :14.6437864303

11:55:43.759 -> AT+CIPSTART="TCP","ikomoto.com",80
11:55:43.852 -> --> Connect www.ikomoto.com OK
11:55:43.899 -> AT+CIPSEND=148
11:55:44.413 -> GET /adiramef/api/generic.php?add_to_db&latitude=40.95&longitude=14.64&device_name=Frigorifero HTTP/1.0
11:55:44.508 -> Host: www.ikomoto.com
11:55:44.554 -> Connection: close
11:55:44.974 -> �Recv: 255 bytes: $GPGGA,095547.000,,,,,0,5,,,M,,M,,47
11:55:46.613 -> $GPGLL,,,,,095547.000,V,N
70
11:55:46.659 -> $GPGSA,A,1,,,,,,,,,,,,,,,1E
11:55:46.706 -> $GPGSV,2,1,08,21,64,322,,32,44,084,32,22,43,120,27,27,39,158,38
75
11:55:46.752 -> $GPGSV,2,2,08,01,38,299,,10,26,050,24,03,23,236,14,14,13,313,17*7F
11:55:46.847 -> $GPRMC,095547.000,V,
11:55:49.792 ->
11:55:49.792 -> --> Ricomincio daccapo la lettura delle coordinate dal GPS
11:55:50.815 ->
11:55:50.815 -> Latitudine rilevata :40.9495239257
11:55:50.861 -> Longitudine rilevata :14.6437864303

@qsjhyy
Copy link
Contributor

qsjhyy commented Aug 18, 2022

Looking at the source: it looks like theres a bug in detachGPS?

https://github.com/DFRobot/DFRobot_SIM808/blob/master/src/DFRobot_SIM808.cpp#L894

imho it should stop the GPS dataflow by sending "AT+CGNSTST=0\r\n" @qsjhyy what to you think ??

That sounds reasonable. You can verify it first and then add a line

bool DFRobot_SIM808::detachGPS()
{
	 if(!sim808_check_with_cmd("AT+CGNSTST=0\r\n", "OK\r\n", CMD)) {
        return false;
    }
	 if(!sim808_check_with_cmd("AT+CGNSPWR=0\r\n", "OK\r\n", CMD)) {
        return false;
    }
	return true;
}

@hoerup
Copy link

hoerup commented Aug 18, 2022

@antmariani if you apply ☝️ it will probably be easier to debug whats going wrong with the http communication

@antmariani
Copy link
Author

sorry @hoerup what should i apply?

i think we need to debug the http connection, it seems that arduino does not send the correct string to the db..

@antmariani
Copy link
Author

any suggestion for me ?

@hoerup
Copy link

hoerup commented Sep 4, 2022

@antmariani
As i wrote earlier (but maybe not clear enough) the fact that you are using the same hardware serial port both for communication between arduino<->Sim808 AND for debug output, makes it very difficult to see what the different pieces of output are - especially when the GPS is in data streaming mode as it is now.

solution 1:
setup a seperate channel for debug output via a SoftSerial

solution 2:
Make the sim808 module less chatty by stopping the GPS dataflow. This could be accomplished by modifying your copy of DFRobot_SIM808.cpp with the changes suggested by @qsjhyy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants