0

I'm doing an IoT project where the temperature and humidity values from a DHT11 sensor is sent to a PHP server using HTTP post. When I run the Arduino, the Serial Monitor says "Bad request". The sensor values are not appearing on the webpage. Please, help me figure out the problem. I am using an Arduino Uno with and an ESP-01.

// Code to use SoftwareSerial
//The final sketch
#include "SoftwareSerial.h"
#include <dht.h>

#define dht_dpin 8

String data;
String ssid = "TRAK family";

String password = "rukman12345";

SoftwareSerial espSerial(6, 7); // RX, TX

dht DHT;

float t;
float h;

String server = "svce-ece-b.000webhostapp.com";

String uri = "esp.php";

boolean DEBUG = true;

void showResponse(int waitTime)
{
    long t = millis();
    char c;
    while (t + waitTime > millis()) {
        if (espSerial.available()) {
            c = espSerial.read();
            if (DEBUG)
                Serial.print(c);
        }
    }
}

void setup()
{
    DEBUG = true;

    Serial.begin(115200);
    delay(500);

    espSerial.begin(115200);

    espSerial.println("AT+RST");
    showResponse(1000);

    // espSerial.println("CIOBAUD=9600");
    // showResponse(1000);

    espSerial.println("AT+CWMODE=1");
    showResponse(1000);

    espSerial.println("AT+CWJAP=\"" + ssid + "\",\"" + password + "\"");
    showResponse(5000);

    if (DEBUG)
        Serial.println("Setup completed");
}

void httppost()
{
    espSerial.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");

    if (espSerial.find("OK")) {
        Serial.println("TCP connection ready");
    }
    delay(1000);

    String postRequest =
        "POST " + uri + " HTTP/1.0\r\n" +
        "Host: " + server + "\r\n" +
        "Accept: *" + "/" + "*\r\n" +
        "Content-Length: " + data.length() + "\r\n" +
        "Content-Type: application/x-www-form-urlencoded\r\n" +
        "\r\n" + data;

    String sendCmd = "AT+CIPSEND=";
    espSerial.print(sendCmd);
    espSerial.println(postRequest.length());

    delay(500);

    if (espSerial.find(">")) {
        Serial.println("Sending..");
        espSerial.print(postRequest);

        if (espSerial.find("SEND OK")) {
            Serial.println("Packet sent");
            while (espSerial.available()) {
                String tmpResp = espSerial.readString();
                Serial.println(tmpResp);
            }

            // close the connection
            espSerial.println("AT+CIPCLOSE");
        }
    }
}

void loop()
{
    // Read sensor values
    DHT.read11(dht_dpin);
    float t = DHT.temperature;
    float h = DHT.humidity;
    if (isnan(t) || isnan(h)) {
        if (DEBUG)
            Serial.println("Failed to read from DHT");
    }
    else {
        if (DEBUG)
            Serial.println("Temp=" + String(t) + " *C");
        if (DEBUG)
            Serial.println("Humidity=" + String(h) + " %");

        data = "temperature=" + String(t) + "&humidity=" + String(h);

        httppost();
    }

    delay(1000);
}

This is my PHP code:

<?php
    global $Temp;
    global $Humidity;
    if(isset($_POST['temperature'])){
        $Temp=$_POST["temperature"];
    }
    if(isset($_POST['humidity'])){
        $Humidity=$_POST["humidity"];
    }

    $Write="<p>Temperature : " .$Temp . " Celcius </p>" . "<p>Humidity : " .       $Humidity . " % </p>";
    file_put_contents('sensor.html',$Write);
?>
4
  • the serial monitor says bad request ... the code that you posted does not contain the text bad request ... the message does not come from the code that you posted
    – jsotola
    Commented Mar 15, 2018 at 6:01
  • @jsotola That's probably the response from the server.
    – gre_gor
    Commented Mar 15, 2018 at 6:08
  • uri should start with /.
    – gre_gor
    Commented Mar 15, 2018 at 6:09
  • @gre_gor thanks brother. it resolved my issure Commented Mar 15, 2018 at 9:02

1 Answer 1

1

The URI path in the HTTP request must start with a /.

It should be:

String uri = "/esp.php";

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.