-->
Page 1 of 1

Problems with http POST

PostPosted: Tue Apr 16, 2019 8:27 am
by cerjones
Been banging my head against this for a few hours and cant figure out whats wrong. Trying to get temperature / humidity readings uploaded to my website. Just it doesnt seem to pass the parameters to the server script...

Code: Select all#include "DHT.h"
#include "ESP8266WiFi.h"
#include "ESP8266HTTPClient.h"

DHT dht;
WiFiClient client;

const char* MY_SSID = "********";
const char* MY_PWD =  "********";

void setup()
{
  Serial.begin(9600);
  dht.setup(4);
}

void loop()
{
  delay(10*1000);

  Serial.print(dht.getHumidity());
  Serial.print("\t");
  Serial.print(dht.getTemperature());
  Serial.print("\n");

  WiFi.begin(MY_SSID, MY_PWD);
  Serial.print("connecting to wifi...");

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

  HTTPClient http;

  String url = "http://mywebsite.co.uk/set_temp.php";
  String data = "sid=1&temp=" + (String) dht.getTemperature() + "&hum=" + (String) dht.getHumidity();

  http.begin(url);
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  int httpCode = http.POST(data);
  String payload = http.getString();
 
  Serial.println(httpCode);
  Serial.println(payload);

  http.end();
 
  WiFi.disconnect();
}


server script...

Code: Select all<?php

    $sid = $_GET['sid'];
    $temp = $_GET['temp'];
    $hum = $_GET['hum'];
   
    $stats = $_SERVER['DOCUMENT_ROOT'] . '/stats.txt';
           
    $handle = fopen($stats, 'a');
    fwrite($handle, "sid: $sid \t temp: $temp \t humidity: $hum \n");

    fwrite($handle, $_SERVER['REQUEST_URI']."\n");

    fclose($handle);

    exit;
?>


If i do it via Chrome with...

http://mywebsite.co.uk/set_temp.php?sid ... =20&hum=50

it correctly adds

sid: 1 temp: 20 humidity: 50
/set_temp.php?sid=1&temp=20&hum=50

to the stats.txt file.

When done via the ESP8266 it just does this...

sid: temp: humidity:
/set_temp.php?

If I append the "data" to the "url" directly and pass that in http.begin it works, but only on my sever on my local network. On my website it timesout and then blocks my ip.