#define LDR_PIN 2
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
LiquidCrystal_I2C lcd(0x27, 20, 10);
/**************************************************/
/* ajouter les lignes de code afin d'afficher sur la ligne vide de l'afficheur LCD
le taux de change de devis (le résultat souhaité: https://ibb.co/BySF3Ys)
NB: vous devez récupérer les informations de taux de change par la méthode HTTP GET
*/
/**************************************************/
const char* server = "http://api.exchangerate.host/latest?base=TND&symbols=USD,EUR,CZK";
float USD,EUR;
const char* date;
void setup() {
pinMode(LDR_PIN, INPUT);
lcd.init();
lcd.backlight();
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("OK! IP=");
Serial.println(WiFi.localIP());
Serial.print("Fetching ");
Serial.print(server);
}
void loop() {
Tauxdechange(date,USD,EUR);
WiFiClient client;
HTTPClient http;
Serial.print("[HTTP] begin...\n");
if (http.begin(client, server)) {
Serial.print("[HTTP] GET...\n");
http.addHeader("Content-Type","application/json");
int httpResponseCode = http.GET();
if (httpResponseCode > 0) {
Serial.printf("[HTTP] GET... code: %d\n", httpResponseCode);
if (httpResponseCode == HTTP_CODE_OK || httpResponseCode == HTTP_CODE_MOVED_PERMANENTLY) {
String jsonfile= http.getString();
Serial.println(jsonfile);
DynamicJsonDocument doc(512);
DeserializationError error =deserializeJson(doc,jsonfile);
if (error) {
Serial.print("deserializeJson() failed");
Serial.println(error.c_str());
return;
}
date = doc["date"];
USD = doc["rates"]["USD"];
EUR = doc["rates"]["EUR"];
Serial.print("la date est ");
Serial.println(date);
Serial.print("les rates en USD est ");
Serial.println(USD);
Serial.print("les rates en EUR est ");
Serial.println(EUR);
Tauxdechange(date,USD,EUR);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpResponseCode).c_str());
}
http.end();
} else {
Serial.printf("[HTTP} Unable to connect\n");
}
delay(10000);
}
void Tauxdechange(const char* date,float USD, float EUR) {
lcd.setCursor(1, 0);
lcd.print("FST:Taux de change");
lcd.setCursor(2, 1);
lcd.print("date:");
lcd.print(date);
lcd.setCursor(2,2);
lcd.print("USD:");
lcd.print(USD);
lcd.setCursor(2, 3);
lcd.print("EUR:");
lcd.print(EUR);
delay(5000);
}