/*
ESP32 HTTPClient Jokes API Example
https://wokwi.com/projects/342032431249883731
Copyright (C) 2022, Uri Shaked
*/
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
#define BTN_NEXT 5
#define BTN_PREV 16
#define TFT_DC 2
#define TFT_CS 15
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
const String url = "https://opendata.infrabel.be/api/explore/v2.1/catalog/datasets/maandelijks-papierverbruik/records?where=annee%3Ddate%272024%27&limit=20";
int mois = 0;
String getValue() {
HTTPClient http;
http.useHTTP10(true);
http.begin(url);
http.GET();
String result = http.getString();
DynamicJsonDocument doc(2048);
DeserializationError error = deserializeJson(doc, result);
// Test if parsing succeeds.
if (error) {
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
return "<error>";
}
Serial.begin(115200);
String feuilles = doc["results"][mois]["number_of_sheets_used"].as<String>();
Serial.println(feuilles);
String periode = doc["results"][mois]["aaaa_mm"].as<String>();
Serial.println(periode);
http.end();
if (periode != "null") {
return periode + " : " + feuilles;
}
else
{
return "Pas de donnees disponible";
}
}
void nextJoke() {
tft.setTextColor(ILI9341_WHITE);
tft.println("\nChargement");
String joke = getValue();
tft.setTextColor(ILI9341_GREEN);
tft.println(joke);
}
void setup() {
pinMode(BTN_NEXT, INPUT_PULLUP);
pinMode(BTN_PREV, INPUT_PULLUP);
WiFi.begin(ssid, password, 6);
tft.begin();
tft.setRotation(1);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(100);
tft.print(".");
}
tft.print("\nOK! IP=");
tft.println(WiFi.localIP());
nextJoke();
}
void loop() {
if (digitalRead(BTN_NEXT) == LOW) {
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 0);
mois++;
nextJoke();
}
if (digitalRead(BTN_PREV) == LOW) {
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 0);
mois--;
nextJoke();
}
delay(100);
}