DFRobot / DFRobot_SIM808

SIM808
MIT License
69 stars 56 forks source link

Sending data via HTTP #7

Open ghost opened 7 years ago

ghost commented 7 years ago

Hi,

Fiirst off thanks for the library, the GPS portion works great and is accurate. However, I am having trouble setting up HTTP POST requests to send data to an API. the TCP version is not working for me. Any advise or example code?

hoerup commented 7 years ago

Have you seen this forum post ? https://www.dfrobot.com/forum/viewtopic.php?f=8&t=2921

jbm27 commented 7 years ago

Hi, Great library, thank you, but I'm also having some problems with sending data via HTTP. I've setup a page at chqrdflg.com/check.txt which is a clone of the mbed.org..../hello.txt page. I've modified the two relevant lines in the tutorial code to if(!sim808.connect(TCP,"chqrdflg.com", 80)) and char http_cmd[] = "GET /check.txt HTTP/1.0\r\n\r\n";. It connects to chqrdflg.com successfully but I get the following error "Recv: 424 bytes: HTTP/1.1 400 Bad Request". Anybody got any idea what might be going on?

hoerup commented 7 years ago

You might want to request as http 1.1 instead. Remember hat Host request header is mandatory in http/1.1

jbm27 commented 7 years ago

Thanks hoerup - it looks like that's the issue. D'you know how I should alter the code to include the host header? Apologies if it's obvious, I haven't been able to find anything online and I'm fairly new to all this.

ghost commented 7 years ago

LOL @hoerup I actually have the last comment for that forum. I will try the 1.1 and see if that helps the response I get back

ghost commented 7 years ago

This is the response that prints out to my console when changing to 1.1 and POST

AT AT+CFUN=1 AT+CPIN? AT+CGNSPWR=1 AT+CGNSTST=1 Open the GPS power success latitude : longitude : AT+CSTT="wholesale","","" AT+CIICR AT+CIFSR Sim808 join network error AT+CSTT="wholesale","","" AT+CIICR AT+CIFSR IP Address is 21.216.41.135 waiting to fetch... AT+CIPSEND=166 Recv: 544 bytes: 5,184.1,M,-34.1,M,,*6C $GPGLL,4229.1977,N,08317.6395,W,135054.000,A,A*4C $GPGSA,A,3,07,30,28,11,,,,,,,,,2.37,2.15,0.99*0C $GPGSV,2,1,08,30,71,232,30,11,63,090,27,28,59,314,28,07,47,170,29*7D $GPGSV,2,2,08,17,32,239,,08,27,054,,13,20,299,19,15,03,326,*78 $GPRMC,135054.000,A,4229.1977,N,08317.6395,W,0.13,125.72,140817,,,A*71 $GPVTG,125.72,T,,M,0.13,N,0.23,K,A*3D $GPGGA,135055.000,4229.1977,N,08317.6395,W,1,4,2.15,184.1,M,-34.1,M,,*6D $GPGLL,4229.1977,N,08317.6395,W,135055.000,A,A*4D $GPGSA,A,3,07,30ß?®§‡}íô[}ÿüm7³<ý4X)

hoerup commented 7 years ago

@jbm27 You might want to read https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Message_format

@britneygill 1) When pasting you might want to use https://gist.github.com/ or at least make sure github markup doesn't mess with your \r\n 2) how are we supposed to help when you don't show the corresponding code ?

hoerup commented 7 years ago

@britneygill If the problem is solved, you should close this ticket

harry1227 commented 5 years ago

can i get the code to send gps location to local server please as i am a beginner for this. thanks for the appreciation please if any one tried this project then send me the code to patelhardik861@gmail.com

danielkp1234 commented 5 years ago

can i get the code too im getting the same error this isnt working replaced my actual website with myserver.com its hosted by unoeuro GET /submit.php?x=%s&y=%s&code=tPsiv3d8FPgYoof6tkWgoSuth6DCMSqE User-Agent: myserver.com \r\nHost: myserver.com HTTP/1.1\r\n\r\n\0 @hoerup @ghost

bsantos09 commented 4 years ago

Did anyone manage to get the code? and if so any chance anyone can send it to me, please?

Mohammad-Moinul-Haq commented 2 years ago

Hello guys, I want to send my HTTP request to this API: http://104.91.285.212/ama/api/add_data?dev_id=myid&lat=70.6093748&lng=56.5514707&tem=25&wt=442&hum=42&pid=1

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

include

define PIN_TX 7

define PIN_RX 6

SoftwareSerial mySerial(PIN_TX,PIN_RX); DFRobot_SIM808 sim808(&mySerial);//Connect RX,TX,PWR,

//DFRobot_SIM808 sim808(&Serial);

// char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n"; char http_cmd[] = "GET /ama/api/add_data?dev_id=myid&lat=70.6093748&lng=56.5514707&tem=25&wt=442&hum=42&pid=1 HTTP/1.0\r\n\r\n"; char buffer[512];

void setup(){ pinMode(8, OUTPUT); digitalWrite(8, HIGH);

mySerial.begin(9600); Serial.begin(9600);

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

//* Attempt DHCP ***** while(!sim808.join(F("cmnet"))) { Serial.println("Sim808 join network error"); delay(2000); }

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

//*** Establish a TCP connection **** // if(!sim808.connect(TCP,"mbed.org", 80)) { if(!sim808.connect(TCP,"http://104.91.285.212", 80)) { Serial.println("Connect error"); }else{ Serial.println("Connect mbed.org success"); }

//* Send a GET request *** 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; }

//***** Close TCP or UDP connections ** sim808.close();

//* Disconnect wireless connection, Close Moving Scene ***** sim808.disconnect();
}

void loop(){

}

//"""""""""""""""""""""""""""

hoerup commented 2 years ago

if(!sim808.connect(TCP,"http://104.91.285.212", 80)) {

remove http:// from the host string

Mohammad-Moinul-Haq commented 2 years ago

Thanks, @hoerup, the issue is solved!

Mohammad-Moinul-Haq commented 2 years ago

Now came up with a new one! :( When I was trying to send my sensor data using HTTP get method, I am not receiving it in my server. Can anyone please say what am I missing here?

include

include

define PIN_TX 7

define PIN_RX 6

SoftwareSerial mySerial(PIN_TX,PIN_RX); DFRobot_SIM808 sim808(&mySerial);//Connect RX,TX,PWR,

//DFRobot_SIM808 sim808(&Serial);

//FUNCTION-SIM808-TCP void sendLastValue(float fltTemperature, float fltHumidity, float fltLatitude, float fltLongitude );

//============================== const size_t DATA_TEXT_LENGTH = 10; char* DEVICE_ID = "LOO1";

void setup(){ pinMode(8, OUTPUT); digitalWrite(8, HIGH);

mySerial.begin(9600); Serial.begin(9600);

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

void loop(){ sendLastValue(11.111111, 11.111111, 11.111111, 11.111111);
}

void sendLastValue(float fltTemperature, float fltHumidity, float fltLatitude, float fltLongitude ) { // CONVERT TEMP SENSOR DATA TO TEXT. char strTemperatureData[DATA_TEXT_LENGTH]; char strHumidityData[DATA_TEXT_LENGTH];

memset(strTemperatureData, 0, DATA_TEXT_LENGTH); memset(strHumidityData, 0, DATA_TEXT_LENGTH);

floatToText(fltTemperature, strTemperatureData, DATA_TEXT_LENGTH); floatToText(fltHumidity, strHumidityData, DATA_TEXT_LENGTH); // END OF CONVERTING TEMP SENSOR DATA.

// GPS DATA CONVERT TO TEXT. char strLatitudeData[DATA_TEXT_LENGTH]; char strLongitudeData[DATA_TEXT_LENGTH];

memset(strLatitudeData, 0, DATA_TEXT_LENGTH); memset(strLongitudeData, 0, DATA_TEXT_LENGTH);

floatToText(fltLatitude, strLatitudeData, DATA_TEXT_LENGTH); floatToText(fltLongitude, strLongitudeData, DATA_TEXT_LENGTH); // END OF CONVERTING GPS DATA.

char aux_str_local[150];

snprintf(aux_str_local, sizeof(aux_str_local), "GET /sms/api/add_data?dev_id=%s&lat=%s&lng=%s&tem=%s&pid=0 HTTP/1.0\r\n\r\n", DEVICE_ID, strLatitudeData, strLongitudeData, strTemperatureData);

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

//* Attempt DHCP ***** while(!sim808.join(F("cmnet"))) { Serial.println("Sim808 join network error"); delay(2000); } //

//*** Establish a TCP connection **** // if(!sim808.connect(TCP,"mbed.org", 80)) { if(!sim808.connect(TCP,"204.7.251.214", 80)) { Serial.println("Connect error"); }else{ Serial.println("Connect 204.7.251.214 success"); }

//* Send a GET request *** Serial.println("Waiting to fetch.."); // Serial.println( sim808.send(http_cmd, sizeof(http_cmd)-1) ); Serial.println( sim808.send(aux_str_local, sizeof(aux_str_local)-1) ); //delay(5000);

//***** Close TCP or UDP connections ** sim808.close();

//* Disconnect wireless connection, Close Moving Scene ***** sim808.disconnect(); }

bool floatToText(float flt, char* strData, size_t szDataLength) { if (strData == NULL || szDataLength == 0) { return false; } memset(strData, 0, szDataLength); //dtostrf(flt, 4, 2, strData); dtostrf(flt, 4, 6, strData); // value, width, digits after decimal, string to store

return true; }

hoerup commented 2 years ago

1) you have of course verified that aux_str_local is formatted correct after your snprintf ??

2) in Serial.println( sim808.send(aux_str_local, sizeof(aux_str_local)-1) );

why use sizeof() so youre sending the entire char buffer of 150 characters? including what comes after the null terminator?

i would just use a strlen instead

3) with dtostrf() you need to be 100% sure that you dont add any padding - otherwise it would mess up your query string

Mohammad-Moinul-Haq commented 2 years ago

@hoerup

Although my actual string should be like, "GET /sms/api/add_data?dev_id=LOO1&lat=66.7773738&lng=66.7774707&tem=111&pid=0 HTTP/1.0\r\n\r\n";

but, in the serial monitor, I am getting, GET /sms/api/add_data?dev_id=LOO1&lat=66.7773738&lng=66.7774707&tem=111&pid=0 HTTP/1.0

Why is it coming like this? Is this format okay?

hoerup commented 2 years ago

I can't see what the issue should be here ? please elaborate

Mohammad-Moinul-Haq commented 2 years ago

The code works, thanks all!

interstellarturk commented 1 year ago

Do not use ngrok. I just deployed my web project to the live servers. After configured subdomain. of my domain.com the problem solved. It might the TCP connection of SIM808 could stuck at firewall of ngrok. Or ngrok domains could very long. I don't know.