Hi!
I`m creating a aplicattion with arduino, and I need to make a GET requisition in my server, and every time I try to do this i get a ERROR 400 BAD REQUEST. So my arduino is reading some data like temperature and humid and send this to my server that has a PHP file that will receive the two variables and put this in a data bank. So when I try to do this in the browser, in my case google chrome, and put this in the URL
XXXXXXXXXXXX/weather/insert_weather.php/?temp=25&humid=40
(Where X is the name of my web site)
The file are put in my data bank normaly, the way I want.
But in arduino I`m using this:
///////////////////////////////
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include "DHT.h"
#define DHTTYPE DHT22
const char* ssid = "XXXXXXXXX";
const char* password = "XXXXXXXXXX";
const int DHTPin = 2;
DHT dht(DHTPin, DHTTYPE);
static char celsiusTemp[7];
static char fahrenheitTemp[7];
static char humidityTemp[7];
const char http_site[] = "XXXXXXXXXXX";
const int http_port = 80;
WiFiClient client;
IPAddress server(xxx,xxx,xxx,xxx);
void setup() {
delay(2000); //Aguarda 2 segundos
Serial.begin(115200);
dht.begin();
Serial.println("NodeMCU - Putting data int DB Using GET");
Serial.print("Trying to connect to: ");
Serial.println(ssid);
Serial.println("Waiting for connection");
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED ) {
delay(1000);
Serial.print(".");
}
Serial.print("\nWI-FI Connection sucess: ");
Serial.println(ssid);
}
void loop() {
//Read of sensor DHT11
delay(3000);
float humid = dht.readHumidity();
// Read temperature as Celsius (the default)
float temp = dht.readTemperature();
if (isnan(humid) || isnan(temp)) {
Serial.println("Failed to read from DHT sensor!");
strcpy(celsiusTemp,"Error");
strcpy(fahrenheitTemp, "Error");
strcpy(humidityTemp, "Error");
return;
}
Serial.println("Puttin data on DB: ");
Serial.print((float)temp);
Serial.print(" *C, ");
Serial.print((float)humid);
Serial.println(" %");
if ( !getPage((float)temp,(float)humid) ) {
Serial.println("GET request failed");
}
}
bool getPage(float temp, float humid)
{
if ( !client.connect(server, http_port) )
{
Serial.println("Error in connection ");
return false;
}
String param = "/?temp=" + String(temp) + "&humid=" + String(humid);
Serial.println(param);
Serial.println("GET /weather/insert_weather.php" + param + " HTTP/1.1\r\n");
client.println("GET /weather/insert_weather.php" + param + " HTTP/1.1\r\n");
// client.println("GET /favicon.ico HTTP/1.1\r\n");
client.println(http_site);
client.println("Connection: close");
client.println();
client.println();
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
return true;
}
///////////////////////////////
Ive search a lot on internet and couldn
t find an anwser to this, please help me!
Some details:
My server is an APACHE
The port that im using is 80, I
ve tried using 8080 and 50000, and with both I cant connect I
m using a WIFI connection to arduino