#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <WiFi.h>
#include <HTTPClient.h>
#include "base64.h"
#define TFT_DC 2
#define TFT_CS 15
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// Define your Wi-Fi credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Define your image URL
const char* imageURL = "http://static.vecteezy.com/system/resources/thumbnails/022/024/202/small/tom-and-jerry-cartoon-free-vector.jpg";
void setup() {
// Initialize Serial for debugging
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Initialize display
tft.begin();
}
void loop() {
// Fetch the image from the web
String base64Image = getImageFromWeb(imageURL);
// Display the image on the TFT display
if (!base64Image.isEmpty()) {
displayBase64Image(base64Image);
}
delay(5000); // Fetch the image every 5 seconds
}
void connectWiFi() {
Serial.println("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
}
String getImageFromWeb(const char* url) {
HTTPClient http;
String base64Image;
if (http.begin(url)) {
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
base64Image = http.getString();
}
http.end();
}
return base64Image;
}
void displayBase64Image(String base64Image) {
// Decode base64 string to byte array
int decodedSize = base64_dec_len(base64Image.c_str(), base64Image.length());
uint8_t decodedData[decodedSize];
base64_decode(decodedData, base64Image.c_str(), base64Image.length());
// Display the image
tft.fillScreen(ILI9341_BLACK);
tft.drawRGBBitmap(0, 0, decodedData, tft.width(), tft.height());
}