//Libraries
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
//Define LCD
LiquidCrystal_I2C lcd(0x27,16,2);
// Declare HTTP client object
HTTPClient http;
//Define API endpoint
const char* apiEndpoint = "https://642be1a0208dfe254721ec1c.mockapi.io/api/v1/Tables/1";
//Declare deserialiseJSON function
void deserialiseJSON (String jsonString);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
//Screen functions
lcd.init(); // LCD driver initialization
lcd.backlight(); // Open the backlight
//Message functions
lcd.setCursor(0,0); // Move the cursor to row 0, column 0
lcd.print("Booting..."); // The print content is displayed on the LCD
//WiFi connection
lcd.setCursor(0,1);
lcd.print("WIFI CONN.");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
lcd.setCursor(0,1);
lcd.print(" Connected!");
}
void loop() {
// Make HTTP request
http.begin(apiEndpoint);
int httpCode = http.GET();
// Check for successful response
if (httpCode == HTTP_CODE_OK) {
Serial.println("HTTP OK");
String payload = http.getString();
deserialiseJSON(payload);
}
delay(5000); // this speeds up the simulation
}
void deserialiseJSON (String jsonString) {
// Create a JSON object
StaticJsonDocument<200> jsonDoc;
DeserializationError error = deserializeJson(jsonDoc, jsonString);
// Check for any errors during deserialization
if (error) {
Serial.println("Failed to parse JSON");
return;
}
// Extract the balance value
const char* balance = jsonDoc["balance"];
// Print the balance value
Serial.println(balance);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Balance:");
lcd.setCursor(0,1);
lcd.print(balance);
}