I have a table in MySQL database inside my local server and I managed to echo that value using a PHP script. Here is the PHP script (I have named it get.php and it is directly inside my htdocs folder in xammp).
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT reading FROM myread";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["reading"]."<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
and I also tried to get that data to esp8266 using the below code
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPClient.h>
int status = WL_IDLE_STATUS;
char server[] = "112.135.74.254";
char ssid[] = "MYWIFI";
char pass[] = "123654MY";
const int httpPort = 80;
float line;
float mililine;
WiFiClient client;
void setup() {
Serial.begin(9600);
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(10000);
}
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
IPAddress ip = WiFi.localIP();
IPAddress gateway = WiFi.gatewayIP();
Serial.print("IP Address: ");
Serial.println(ip);
if (!client.connect(server, httpPort)) {
Serial.println("connection failed");
return;
}
String url = "/get.php";
Serial.print("requesting URL: ");
Serial.println(url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + server + "\r\n" +
"User-Agent: BuildFailureDetectorESP8266\r\n" +
"Connection: close\r\n\r\n");
Serial.println("request sent");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
Serial.println("reply was:");
Serial.println("==========");
Serial.println(line);
Serial.println("==========");
Serial.println("closing connection");
mililine= line * 0.001;
Serial.println("Final value is - ");
Serial.println(mililine);
}
void loop() {
}
But the problem is even my database table has a value on it (the value is 55.25, I mean it has decimal places too) Serial monitor only display output as zero Here is the Arduino serial monitor output
⸮_⸮⸮=s⸮⸮Attempting to connect to Network named: MYWIFI
Attempting to connect to Network named: MYWIFI
Attempting to connect to Network named: MYWIFI
SSID: MYWIFI
IP Address: 192.168.1.102
requesting URL: /get.php
request sent
headers received
reply was:
==========
0.00
==========
closing connection
Final value is -
0.00
When I run get.php script in a browser it shows me the value PHP script shows the value in a browser. So I need to get this displaying value to the Arduino serial monitor. As I mentioned above it shows zero instead of this value.Any help, please ???