ESP8266 sending an HTTP request

Hi, I am having an IOT project where I want to read data from a MYSQL server with ESP8266. I am using XAMPP to work with MYSQL and PHP. I want to send an HTTP request to the webserver which will handled by the PHP script, whereby it will go to the MYSQL server, perform action and responds back with an HTTP. I successfully could insert value to the database by HTTP request but how can I send an HTTP request that will get values from there ?
I followed this tutorial https://esp32io.com/tutorials/esp32-mysql
and I have used the following code which uses JSON Object:

/*
  Rui Santos
  Complete project details at Complete project details at https://RandomNerdTutorials.com/esp32-http-get-post-arduino/

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files.

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <Arduino_JSON.h>

const char* ssid = "KHAMIS";
const char* password = "khamis11";

//Your Domain name with URL path or IP address with path
const char* serverName = "http://192.168.86.51/Getting_Data.php?temperature=30.5";

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;

String sensorReadings;
float sensorReadingsArr[3];

void setup() {
  Serial.begin(115200);

  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());

  Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}

void loop() {
  //Send an HTTP POST request every 10 minutes
  if ((millis() - lastTime) > timerDelay) {
    //Check WiFi connection status
    if (WiFi.status() == WL_CONNECTED) {

      sensorReadings = httpGETRequest(serverName);
      Serial.println(sensorReadings);
      JSONVar myObject = JSON.parse(sensorReadings);

      // JSON.typeof(jsonVar) can be used to get the type of the var
      if (JSON.typeof(myObject) == "undefined") {
        Serial.println("Parsing input failed!");
        return;
      }

      Serial.print("JSON object = ");
      Serial.println(myObject);

      // myObject.keys() can be used to get an array of all the keys in the object
      JSONVar keys = myObject.keys();

      for (int i = 0; i < keys.length(); i++) {
        JSONVar value = myObject[keys[i]];
        Serial.print(keys[i]);
        Serial.print(" = ");
        Serial.println(value);
        sensorReadingsArr[i] = double(value);
      }
      Serial.print("1 = ");
      Serial.println(sensorReadingsArr[0]);
      Serial.print("2 = ");
      Serial.println(sensorReadingsArr[1]);
      Serial.print("3 = ");
      Serial.println(sensorReadingsArr[2]);
    }
    else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}

String httpGETRequest(const char* serverName) {
  WiFiClient client;
  HTTPClient http;

  // Your Domain name with URL path or IP address with path
  http.begin(client, serverName);

  // If you need Node-RED/server authentication, insert user and password below
  //http.setAuthorization("REPLACE_WITH_SERVER_USERNAME", "REPLACE_WITH_SERVER_PASSWORD");

  // Send HTTP POST request
  int httpResponseCode = http.GET();

  String payload = "{}";

  if (httpResponseCode > 0) {
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
    payload = http.getString();
  }
  else {
    Serial.print("Error code: ");
    Serial.println(httpResponseCode);
  }
  // Free resources
  http.end();

  return payload;
}

Which I took it from a website. However, when I upload it and run it, the serial monitor shows some not understandable symbols any help please ?

is the baud rate set in Serial Monitor to 115200 baud
if your esp8266 board has the Ch340 USB chip, add delay(500); after Serial.begin

Yeah it works thank you

Do you know how can I take specific data from there to use it for something (such as turning an LED ON). I saw Arduino Jason library which helps you to deserialize Jason, but I still can't understand is what I am getting from the web and printing it on the serial monitor is a deserialize Jason or a serialize Jason ?

If you are sure that your data is coming out of the SQL server in JSON format, then you need to deserialize it to use it. Take a look at this example. You are missing a few key elements to getting it to work. First of all, your sensor readings are a string. You need a JSON object to get it to work.

I am now using a different code but it is showing the same result in the serial monitor. 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);
}

But I have seen in the video that his JSON object looks different such as texts enclosed in quotation marks. However, what I am receiving doesn't look so 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 data for me ? Please I need help it is for my university project.

Did you even look at the JSON Deparser example that I linked to? Your code doesn't include any of the necessary elements to deserialize a JSON message. I suggest you look at that example as it walks your through all the steps.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.