I'm trying to send a temperature reading to a php file in my website. However, the temperature variable doesn't seem to be passed. Below I use the following Arduino code:
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#define SERVER_IP "XXX.XX.XXX.XX"
#ifndef STASSID
#define STASSID "XXX"
#define STAPSK "XXX"
#endif
void setup() {
Serial.begin(115200);
Serial.println(); Serial.println(); Serial.println();
WiFi.begin(STASSID, STAPSK);
while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }
Serial.println(""); Serial.print("Connected! IP address: "); Serial.println(WiFi.localIP());
}
void loop() {
if ((WiFi.status() == WL_CONNECTED)) {
WiFiClient client;
HTTPClient http;
Serial.print("[HTTP] begin...\n");
http.begin(client, "http://" SERVER_IP "/device.php"); //HTTP
http.addHeader("Content-Type", "application/json");
Serial.print("[HTTP] POST...\n");
int httpCode = http.POST("{\"temp\":\"15\"}");
if (httpCode > 0) {
Serial.printf("[HTTP] POST... code: %d\n", httpCode);
if (httpCode == HTTP_CODE_OK) {
const String& payload = http.getString();
Serial.println("received payload:\n<<");
Serial.println(payload);
Serial.println(">>");
}
} else {
Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
delay(100000);
}
with the following code in my device.php:
<?php header('Content-type: application/json'); header('Content-Type: text/html; charset=utf-8'); require 'config.php';
header("Access-Control-Allow-Origin: *");
$temp = $_POST["temp"];
$ins = mysqli_query($link,"INSERT INTO `test`(`id`,`test`)VALUES(NULL,'$temp')");
$responseArray = array('status' => $_POST);
$encoded = json_encode($responseArray); header('Content-Type: application/json'); echo $encoded;
?>
However, I end up with php error saying:
PHP Notice: Undefined index: temp in /var/www/html/device.php on line 6
I get empty entry in mysql and serial monitor shows empty payload:
[HTTP] begin...
[HTTP] POST...
[HTTP] POST... code: 200
received payload:
<<
{"status":[]}
>>