Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By barti5001
#51613 Hmm..
I added your code to my esp code.
Code: Select all  String h = String(dht.readHumidity());
  String t = String(dht.readTemperature());

String postStr = "humi=" + h + "&temp=" + t;
    client.print(String("POST ") + "/esp/dht.php HTTP/1.1\r\n" +
               "Host: localhost\r\n" +
               "Content-Type: application/x-www-form-urlencoded\r\n" +
               "Connection: keep-alive\r\n" +
               "Content-Length: " + postStr.length() + "\r\n\r\n" + postStr);


I had to make a string of h and t.
And I placed php file in xampp>htdocs>esp>dht.php, is it right?

My dht.php code:
Code: Select all<?php

   require_once "connect.php";
      
      if (isset($_POST['temp']))
      {
      
      $polaczenie = @new mysqli($host,$db_user,$db_password,$db_name);
         
         if($polaczenie->connect_errno!=0)
         {
            echo "Error:".$polaczenie->connect_errno;
         }
         else
         {
         $date1 = date("y-m-d");
         $temp = $_POST['temp'];
         $humi = $_POST['humi'];
         
         $polaczenie->query("insert into dht values (NULL,'$date1','$temp','$humi'");
         
         $polaczenie->close();
         }
      }

?>


connect.php have the db_name, db_password etc.

But it still doesn't work. What may I do wrong?
Should esp be in servermode or client?
User avatar
By martinayotte
#51645 Your ESP client connection has wrong host header where you provided "localhost" instead of the PHP server IP.
Did you check the server log to see if the request is properly received ?
You can add debug prints in the PHP to make sure it is actually processed properly.
User avatar
By villTech
#51688 or your can use WifiClient from arduino Examples folder.
it will serial out information about the status of your request.
from connection to server to server replies.

and as martinayotte pointed out, you need to correct your host header.

might as well check the status of your mysql query.
User avatar
By barti5001
#51725 Ok, thank you, it works when I send data to my website's server. :)
But when instead of 'xyz123.com' I enter localhost or 127.0.0.1 then it doesn't connect and 'if (client.connect(server,80))' doesn't return true.
Why?
Code: Select all#include <DHT.h>
#include <ESP8266WiFi.h>

const char* ssid = "xxx";
const char* password = "xxx";

const char* server = "xyz123.com";
#define DHTPIN 14

DHT dht(DHTPIN, DHT22,15);
WiFiClient client;

void setup() {
Serial.begin(115200);
delay(10);
dht.begin();

WiFi.begin(ssid, password);

Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");

}

void loop() {

float h = dht.readHumidity();
float t = dht.readTemperature();
Serial.println(t);
Serial.println(h);
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

if (client.connect(server,80)) {

Serial.println("It works!");

String postStr = "humi=";
postStr += String(h);
postStr +="&temp=";
postStr += String(t);
postStr += "\r\n\r\n";

client.print("POST /esp/dht.php HTTP/1.1\n");
client.print("Host: xyz123.com\n");
client.print("Connection: close\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);

Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" degrees Celcius Humidity: ");
Serial.print(h);
Serial.println("% send to server");
}
client.stop();

Serial.println("Waiting…");
delay(10000);
}