I am trying to get data from a MYSQL server using an ESP8266 board. However, I am not doing it directly. I have used a PHP script that handles the http request send by the ESP board, whereby the PHP script performs the action to the server and returns back with an http respond. The code is shown below:
#include <ArduinoJson.h>
#include <ESP8266HTTPClient.h>
#include <Arduino_JSON.h>
#include <ESP8266WiFi.h>
const char* ssid = "KHAMIS";
const char* password = "khamis11";
const char* serverName = "http://192.168.86.51/Getting_Data.php?temperature";
void setup() {
Serial.begin(115200);
delay(500);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
WiFiClient client;
HTTPClient http;
http.begin(client, serverName);
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println("\nStatuscode: " + String(httpCode));
Serial.println(payload);
char Json[500];
payload.replace(" ", "");
payload.replace("\n", "");
payload.trim();
payload.remove(0,1);
payload.toCharArray(Json, 500);
StaticJsonDocument<200> doc;
deserializeJson(doc, Json);
int id = doc[id];
const char* scrol = doc[scrol];
Serial.println(String(id) + " - " + String(scrol) + "\n");
http.end();
} else {
Serial.println("Error on HTTP request");
}
} else {
Serial.println("Connection lost");
}
delay(10000);
}
As you can see I am using JSON to do that. I have received the data from the database in the server; however, the data I am receiving doesn't look to be a JSON object based on what is printed on the serial monitor. What I see in the monitor is this:
id: 1 - scrol: up - slider: 0
0 -
This might be reason that I can't select the data. Am I receiving a JSON object ? and if not how is the code retrieving the exact data from the database for me ? Please I need help it is for my university project.