I’m using Esp8266 and XAMPP on macbook.
I can’t get string from PHP page. :’(
code esp
// Load Wi-Fi library
#include <ESP8266WiFi.h>
#include "Arduino.h"
#include "PCF8574.h"
#include <ESP8266HTTPClient.h>
#include <Wire.h>
//Access point credentials
const char* ssid = "Barney";
const char* pwd = "Stinson555";
const char* host = "http://192.168.64.2";
String get_host = "http://192.168.64.2";
WiFiServer server(80); // open port 80 for server connection
void setup()
{
Serial.begin(115200); //initialise the serial communication
delay(20);
WiFi.begin(ssid, pwd);
//starting the server
server.begin();
}
void loop()
{
get_device_status("home_light","Home Light");
get_device_status("shop_light","Shop Light");
}
void get_device_status(String device_name, String device_text)
{
WiFiClient client = server.available();
HTTPClient http;
String url = get_host+"/temperature/get_status.php?device_name="+device_name;
http.begin(url);
int httpCode = http.GET();
Serial.println(httpCode); //1
String payload = http.getString();
Serial.println(payload); //null
if(payload=="1")
{
Serial.println(device_text+" is ON");
}
else
{
Serial.println(device_text+" is Off");
}
http.end();
delay(1000);
}
run code
18:54:29.075 -> -1
18:54:29.075 ->
18:54:29.075 -> Home Light is Off
18:54:35.100 -> -1
18:54:35.100 ->
18:54:35.100 -> Shop Light is Off
PHP page
http://192.168.64.2/temperature/get_status.php?device_name=home_light
it shows ’ 1 '.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "device_status";
$conn = new mysqli($servername, $username, $password,$dbname);
$device_name = $_GET['device_name'];
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT device_status FROM devices_status WHERE device_name='$device_name' LIMIT 1";
$result = $conn->query($sql);
$sql2 = "SELECT device_status FROM devices_status WHERE device_name='home_light' LIMIT 1";
//$result2 = $conn->query($sql2);
//echo strval($result2);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc())
{
echo $row['device_status'];
}
} else {
echo "Error:" . $sql . "
" . $conn->error;
}
$conn->close();
?>
DB
CREATE TABLE `devices_status` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`device_name` varchar(50) DEFAULT NULL,
`device_status` varchar(1) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
INSERT INTO `devices_status` VALUES ('1', 'home_light', '1');
INSERT INTO `devices_status` VALUES ('2', 'shop_light', '0');