#define LDR_PIN 2
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
/**************************************************/
/* 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";
const char* ssid = "Wokwi-GUEST";
const char* password = "";
String payload;
float USD,EUR;
const char* date;
void setup() {
Serial.begin(115200);
// pin setup
pinMode(LDR_PIN, INPUT);
// lcd setup
lcd.init();
lcd.backlight();
// wifi setup
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
}
void loop() {
WiFiClient client;
HTTPClient http;
if (http.begin(client, server)) {
int httpResponseCode = http.GET();
if(httpResponseCode>0){
Serial.printf("HTTP GET code : %d",httpResponseCode);
if(httpResponseCode == HTTP_CODE_OK || httpResponseCode == HTTP_CODE_MOVED_PERMANENTLY){
String payload = http.getString();
Serial.println(payload);
StaticJsonDocument<1024> doc;
DeserializationError error =deserializeJson(doc,payload);
if (error) {
Serial.print("deserializeJson() failed");
Serial.println(error.c_str());
return;
}
Serial.println(payload);
Serial.println("....................................................");
const char* date = doc["date"];
float usd = doc["rates"]["USD"];
float eur = doc["rates"]["EUR"];
Serial.printf("date : %s \n",date);
Serial.printf("USD= %.2f \n",usd);
Serial.printf("EUR= %.2f \n",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(2000);
}
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,5);
lcd.setCursor(2, 3);
lcd.print("EUR:");
lcd.print(EUR,5);
delay(5000);
}