Reading API data correctly

Hello
I am looking for a solution to the problem of correct API reading with air quality data.
The first data download looks like this:

Connected to WiFi network with IP Address: 192.168.0.179
PM10 test: 0.00
SO2 test: 4.35
Test No2: 3.99
PM10 test: 10.40
SO2 test: 4.35
Test No2: 3.99

and it should look like:

Connected to WiFi network with IP Address: 192.168.0.179
PM10 test: 4.35
SO2 test: 3.99
Test No2: 10.40
PM10 test: 4.35
SO2 test: 3.99
Test No2: 10.40

How to fix it?

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>

const char *ssid = "SSID";
const char *password = "PASS";
// Serwer API
char serverPM[] = "http://api.gios.gov.pl/pjp-api/rest/data/getData/828"; // 828
char serverSO[] = "http://api.gios.gov.pl/pjp-api/rest/data/getData/831"; // 831
char serverNO[] = "http://api.gios.gov.pl/pjp-api/rest/data/getData/824"; // 824

float Pm10;
float So2;
float No2;

String payload;
WiFiClient client;
HTTPClient http;
StaticJsonDocument<200> docPM;
StaticJsonDocument<200> docSO;
StaticJsonDocument<200> docNO;
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());
}
// Podprogramy

void apiPM10()
{ //  Zapytanie do API z PM10 Inowrocław
 
 
  http.begin(client, serverPM);
  payload = http.getString();
 
  DeserializationError error = deserializeJson(docPM, payload);
  int httpResponseCode = http.GET();
 
    Pm10 = docPM["values"][1]["value"];
    Serial.print("Test PM10:");
    Serial.println(Pm10);

}
void apiSO2()
{ // Zapytanie do API z SO2 Inowrocław
  http.begin(client, serverSO);
  payload = http.getString();
  DeserializationError error = deserializeJson(docSO, payload);
  int httpResponseCodeSO = http.GET();

    So2 = docSO["values"][1]["value"];
    Serial.print("Test SO2:");
    Serial.println(So2);
   
}
void apiNO2()
{ // Zapytanie do API z NO2 Inowrocław
  http.begin(client, serverNO);
  payload = http.getString();
  DeserializationError error = deserializeJson(docNO, payload);
  int httpResponseCodeNO = http.GET();
 
    No2 = docNO["values"][1]["value"];
    Serial.print("Test No2:");
    Serial.println(No2);
 
}

void loop()
{
  apiPM10();
  delay(1000);
  apiSO2();
  delay(1000);
  apiNO2();
  delay(5000);
}

There are many ways it can be fixed depending on what it is. If it is a flat tire, then changing the tire could be a fix. If it is being too cold one may bundle up or turn up the thermostat. The issue comes down to a clear and sold definition of "IT", right?

Shouldn't you issue the GET request BEFORE you parse the returned payload?!? I'd look at an HTTP Client example to see how to fetch a page.

Thanks for your help, you only need to transfer GET before payload.

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