Code: Select all
#include <WiFi.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <XPT2046_Touchscreen.h>
// Your WiFi network name and password
const char* ssid = "MYWIFISSID";
const char* password = "MYWIFIPASSWORD";
#define TFT_CS 14 //for D32 Pro
#define TFT_DC 27 //for D32 Pro
#define TFT_RST 33 //for D32 Pro
#define TS_CS 12 //for D32 Pro
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
XPT2046_Touchscreen ts(TS_CS);
// URL and filename for the bitmap image
const char* imageURL = "https://cdn-learn.adafruit.com/assets/assets/000/037/204/medium640/feather_purple.bmp";
// Declare the bitmap image array
const unsigned char feather_bmp[] PROGMEM = {
// Bitmap image data goes here
};
void setup() {
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi!");
// Initialize TFT display
tft.begin();
tft.setRotation(1);
// Download image from URL into program memory (progmem)
downloadImage();
// Display the image on the TFT display
displayImage();
}
void loop() {
// Your main code goes here (if needed)
}
void downloadImage() {
// Connect to the server
WiFiClient client;
if (!client.connect("cdn-learn.adafruit.com", 443)) {
Serial.println("Connection failed");
return;
}
// Send a GET request for the image
client.println(String("GET /assets/assets/000/037/204/medium640/feather_purple.bmp HTTP/1.1"));
client.println("Host: cdn-learn.adafruit.com");
client.println("Connection: close");
client.println();
// Skip headers
while (client.connected() && client.read() != '\n') {}
// Save the image to program memory
size_t imageSize = 0;
const size_t bufferSize = 1024;
uint8_t buffer[bufferSize];
bool imageStarted = false;
while (client.connected()) {
size_t bytesRead = client.readBytes(buffer, bufferSize);
if (bytesRead > 0) {
if (!imageStarted) {
// Find the start of the image data
const char* imageStart = strstr((const char*)buffer, "\r\n\r\n");
if (imageStart != nullptr) {
imageStarted = true;
size_t headerSize = (imageStart + 4) - (const char*)buffer;
bytesRead -= headerSize;
memmove(buffer, imageStart + 4, bytesRead);
}
}
if (imageStarted) {
// Save the image data to program memory (progmem)
memcpy_P((void*)&feather_bmp[imageSize], buffer, bytesRead);
imageSize += bytesRead;
}
} else {
break;
}
}
Serial.println("Image downloaded successfully!");
Serial.print("Image size: ");
Serial.println(imageSize);
// Close the connection
client.stop();
}
void displayImage() {
tft.fillScreen(ILI9341_BLACK);
tft.drawBitmap(0, 0, (unsigned char*)feather_bmp, tft.width(), tft.height(), ILI9341_WHITE);
}