Récupérer JSON avec un ESP32

Bonjour,

j'essaie en vain de récupérer le JSON de cette adresse :

https://data.stib-mivb.be/api/explore/v2.1/catalog/datasets/waiting-time-rt-production/records?where=%22UZ-VUB%22&limit=100&refine=pointid%3A1755

Mon code :

#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>

const char* ssid = "XXXXX";
const char* password = "XXXXXXXXX";


// THE DEFAULT TIMER IS SET TO 10 SECONDS FOR TESTING PURPOSES
// For a final application, check the API call limits per hour/minute to avoid getting blocked/banned
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 10 seconds (10000)
unsigned long timerDelay = 10000;

String jsonBuffer;

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 10 seconds (timerDelay variable), it will take 10 seconds before publishing the first reading.");
}

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

      String serverPath = "https://data.stib-mivb.be/api/explore/v2.1/catalog/datasets/waiting-time-rt-production/records?where=%22UZ-VUB%22&limit=100&refine=pointid%3A1755";
      
      jsonBuffer = httpGETRequest(serverPath.c_str());

      Serial.println(jsonBuffer);

      Serial.println(jsonBuffer);
      JSONVar myObject = JSON.parse(jsonBuffer);
  
      // 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);

    }
    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);
  
  // 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;
}

Mais le code me renvoi :

<html>
<head><title>400 The plain HTTP request was sent to HTTPS port</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
<center>The plain HTTP request was sent to HTTPS port</center>
<hr><center>openresty</center>
</body>
</html>

Parsing input failed!

J'ai ensuite essayé avec WifiSecureClient mais sans succès.

Je souhaite simplement récupérer ce JSON pour le parser et ensuite l'afficher sur un écran RGB.

D'avance merci

Bonjour

Comment ? Avec quel code ?

Avec WiFISecureClient () as-tu tenté la méthode setInsecure() ?

ESP32 HTTPS Requests without Certificate

https://randomnerdtutorials.com/esp32-https-requests/

un truc comme cela sans doute pour récupérer le json

#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid = "SECRET_SSID";
const char* wpd = "SECRET_PASS";
const char * url = "https://data.stib-mivb.be/api/explore/v2.1/catalog/datasets/waiting-time-rt-production/records?where=%22UZ-VUB%22&limit=100&refine=pointid%3A1755";

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

  WiFi.begin(ssid, wpd);
  Serial.println("Connexion au WiFi...");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.write('.');
  }
  Serial.println("\nConnecté au WiFi");

  HTTPClient http;
  http.begin(url);

  int httpCode = http.GET();

  if (httpCode > 0) {
    String json = http.getString();
    Serial.println(json);
  } else {
    Serial.print("Erreur lors de la requête HTTP: ");
    Serial.println(httpCode);
  }

  http.end();
}

void loop() {}

Je te remercie !

Je n'ai plus qu'à trouver comment parser et l'envoyer à un écran.

quels champs voulez vous extraire ?

ArduinoJSON fait ça très bien pour vous

1 Like

Je vais essayer de trouver tout seul pour la suite.

Merci quand même :wink:

Oui :slight_smile:

il y a l'assistant qui est bien pratique

1 Like

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