#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <time.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
#define TFT_DC 2
#define TFT_CS 15
// Variables pour la résolution de l'écran
const int screenWidth = 240;
const int screenHeight = 320;
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
const String aqicnToken = "76b9e4609731bf7e44c1f1e6cf464cd3a62fdb1b"; // Votre jeton API AQICN
const String weatherApiKey = "08f16d1798b4b97b4520ce302a7d9d1a"; // Remplacez par votre clé API OpenWeatherMap
const String weatherUrl = "http://api.openweathermap.org/data/2.5/weather?q=Hanoi,VN&appid=" + weatherApiKey + "&units=metric&lang=fr";
const String pollutionUrl = "http://api.waqi.info/feed/hanoi/?token=" + aqicnToken;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
tft.begin();
tft.fillScreen(ILI9341_BLACK);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(0, 0);
tft.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(100);
tft.print(".");
}
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(0, 0);
tft.print("WiFi connected!");
tft.setCursor(0, 30);
tft.print("IP: ");
tft.println(WiFi.localIP());
// Configuration du serveur NTP pour l'heure
configTime(25200, 0, "pool.ntp.org", "time.nist.gov"); // UTC+7 pour Hanoï
}
int getTextWidth(const String& text, int textSize) {
return text.length() * 6 * textSize; // 6 pixels de largeur par caractère pour la police par défaut
}
void drawCenteredText(const String& text, int y, int textSize, uint16_t color) {
int x = (screenWidth - getTextWidth(text, textSize)) / 2;
tft.setCursor(x, y);
tft.setTextColor(color);
tft.setTextSize(textSize);
tft.println(utf8ToExtendedAscii(text)); // Utilisez utf8ToExtendedAscii pour convertir les caractères spéciaux
}
String utf8ToExtendedAscii(String s) {
String r = "";
char c;
unsigned char n;
for (unsigned int i = 0; i < s.length(); i++) {
c = s.charAt(i);
if (c >= 0 && c <= 127) {
r += c;
} else {
n = (unsigned char)c;
switch (n) {
case 0xC3:
c = s.charAt(++i);
if (c == 0xA9) r += (char)0x82; // é
else if (c == 0xA8) r += (char)0x8A; // è
else if (c == 0xAA) r += (char)0x88; // ê
else if (c == 0xA0) r += (char)0x85; // à
else if (c == 0xA7) r += (char)0x87; // ç
else if (c == 0xAF) r += (char)0x8F; // ï
break;
default:
r += '?';
break;
}
}
}
return r;
}
void loop() {
// Récupération de l'heure
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Échec de l'obtention de l'heure");
return;
}
char dateString[64];
char timeString[64];
strftime(dateString, sizeof(dateString), "%d-%m-%Y", &timeinfo); // Format jour-mois-année
strftime(timeString, sizeof(timeString), "%H:%M", &timeinfo); // Format heure:minute sans les secondes
// Récupération des données météo
HTTPClient http;
http.begin(weatherUrl);
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
DynamicJsonDocument doc(2048);
deserializeJson(doc, payload);
float temperature = doc["main"]["temp"].as<float>();
String weather = doc["weather"][0]["description"].as<String>();
int humidity = doc["main"]["humidity"].as<int>();
// Récupération des données de pollution de l'air
http.begin(pollutionUrl);
httpCode = http.GET();
if (httpCode > 0) {
payload = http.getString();
Serial.println(payload); // Affiche la réponse brute de l'API
DynamicJsonDocument pollutionDoc(2048);
deserializeJson(pollutionDoc, payload);
int aqi = pollutionDoc["data"]["aqi"].as<int>();
// Affichage sur l'écran
tft.fillScreen(ILI9341_BLACK);
drawCenteredText("Hanoi", 10, 3, ILI9341_CYAN); // Utilisez les conversions spécifiques
drawCenteredText(dateString, 50, 2, ILI9341_YELLOW); // Centre la date
drawCenteredText(timeString, 70, 5, ILI9341_YELLOW); // Centre et agrandit l'heure
drawCenteredText("Condition:", 120, 2, ILI9341_WHITE);
drawCenteredText(weather, 140, 2, ILI9341_GREEN);
drawCenteredText("Température:", 170, 2, ILI9341_WHITE);
drawCenteredText(String(temperature) + " C", 190, 2, ILI9341_RED);
drawCenteredText("Humidité:", 220, 2, ILI9341_WHITE);
drawCenteredText(String(humidity) + " %", 240, 2, ILI9341_BLUE);
drawCenteredText("Qualité de l'air:", 270, 2, ILI9341_WHITE);
drawCenteredText("AQI: " + String(aqi), 290, 2, ILI9341_MAGENTA);
} else {
Serial.println("Erreur de requête HTTP pour la pollution de l'air");
}
http.end();
} else {
Serial.println("Erreur de requête HTTP pour la météo");
}
http.end();
delay(60000); // Mise à jour toutes les 60 secondes
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4