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???
I reworked your code to use char arrays instead of String class and changed the decoding logic. It might have flaws though, cause i could not test the logic for your script.If its not doing what you want, uncomment getDebug() and comment getData() upload and post result (starting with ==== DEBUG ====) as edit to your question.If you get a buffersize error double the amount of BUFFERSIZE till it runs through.
/*
Web Server
A simple webserver getting a value from a database.
by codebraker007
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPClient.h>
const uint16_t BUFFERSIZE = 256;
void sendReq();
void getData();
void getDebug();
char msgBuffer[BUFFERSIZE] = {'\0'};
int status = WL_IDLE_STATUS;
const char ssid[] = "MYWIFI";
const char pass[] = "123654MY";
// Server related vars
const char serverIp[] = "112.135.74.254";
const uint16_t httpPort = 80;
const char url[] = "/get.php";
float mililine;
WiFiClient client;
void setup() {
// For serial monitor
Serial.begin(9600);
while (!Serial) {
; // wait
}
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);
Serial.print("Gateway IP Address: ");
Serial.println(gateway);
if (!client.connect(serverIp, httpPort)) {
Serial.println("connection failed");
return;
}
while (client.connected()) {
sendReq();
getData();
//getDebug();
}
}
void loop() {
}
void sendReq() {
Serial.print("requesting URL: ");
Serial.println(url);
strcpy ( msgBuffer, "GET ");
strcat ( msgBuffer, url);
strcat ( msgBuffer, " HTTP/1.1\r\n" );
strcat ( msgBuffer, "Host: ");
strcat ( msgBuffer, serverIp);
strcat ( msgBuffer, "\r\n");
strcat ( msgBuffer, "User-Agent: BuildFailureDetectorESP8266\r\n");
strcat ( msgBuffer, "Connection: close\r\n\r\n");
client.print(msgBuffer);
Serial.println("request sent");
}
void getData() {
bool isHeader = true;
uint16_t i = 0;
msgBuffer[0] = '\0';
if (client.available()) {
if (i >= BUFFERSIZE) {
Serial.println("==== DATA READ ERROR ==== /n Enlarge buffer size");
return;
}
char c = client.read();
if (c != '\r') {
if (c == '\n' && isHeader ) {
Serial.println("header received ");
Serial.println("reply was:");
Serial.println("==========");
Serial.println(msgBuffer);
i = 0;
msgBuffer[i] = '\0';
isHeader = false;
}
else if (c == '\n' && !isHeader ) {
Serial.println(msgBuffer);
Serial.println("==========");
mililine = atof(msgBuffer) * 0.001;
Serial.println("Final value is - ");
Serial.println(mililine);
Serial.println("==========");
msgBuffer[i] = '\0';
i = 0;
}
else if ( c != '\n' ) {
msgBuffer[i] = c;
i++;
}
} // '\r'
} // client available
}
void getDebug() {
uint16_t i = 0;
msgBuffer[0] = '\0';
if (client.available()) {
if (i >= BUFFERSIZE) {
Serial.println("==== DEBUG ERROR ==== /n Enlarge buffer size");
return;
}
char c = client.read();
if (c == '\r') {
msgBuffer[i] = '/';
i++;
msgBuffer[i] = 'r';
i ++;
}
else if (c == '\n') {
msgBuffer[i] = '/';
i++;
msgBuffer[i] = 'n';
i ++;
}
else {
msgBuffer[i] = c;
i++;
}
} // client available
Serial.println("==== DEBUG ====");
Serial.println(msgBuffer);
Serial.println("==========");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.