0

I have an Arduino UNO and sim808 EVB-V3.02.4. I want to send a GET request to my url but I always receive 400 Http bad request error.

#include <DFRobot_sim808.h>
#include <SoftwareSerial.h>
#define PIN_TX 7
#define PIN_RX 8
SoftwareSerial mySerial(PIN_TX,PIN_RX);
DFRobot_SIM808 sim808(&mySerial);//Connect RX,TX,PWR,
char http_cmd[] = "GET /index.php?lat=10&long=100 HTTP/1.0\r\n\r\n";
char buffer[512];
void setup() {
 mySerial.begin(9600);
 Serial.begin(9600);
}
void loop() {
 //*********** 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 ************
 while(!sim808.connect(TCP,"39c309ae.ngrok.io", 80)) {
 Serial.println("Connect error");
 }
 Serial.println("Connection 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();
 delay(5000); 
 }

My php code:

<?php
file_put_contents('test.txt', $_GET['lat'] . '---' . $_GET['lon']);
print '
<html>
<head>
<title>Good</title>
</head>
<body>
Was Good
</body>
</html>
';
?>

I want just open http://39c309ae.ngrok.io/index.php?lat=10&long=100 with arduino and sim808.

What is wrong in my codes?

Nouman
2174 silver badges13 bronze badges
asked Sep 2, 2019 at 13:55

1 Answer 1

1

You aren't sending the Host: header with your request. Without that the web server has no idea which website you want to get the /index.php page from.

char http_cmd[] = "GET /index.php?lat=10&long=100 HTTP/1.0\r\n"
 "Host: 39c309ae.ngrok.io\r\n"
 "\r\n";
answered Sep 2, 2019 at 13:59
1
  • nice worked! thanks a lot! becuse of low mine low reputation i can not flag your question for best answer! anyway , thanks Commented Sep 2, 2019 at 14:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.