Downloading and displaying a BMP on 2.4" TFT

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
novis1
 
Posts: 1
Joined: Wed Oct 16, 2013 12:43 am

Downloading and displaying a BMP on 2.4" TFT

Post by novis1 »

I'm trying to make a program to download and display an image on the screen. With a lot of help from OpenAI ChatGPT I've gotten to the point where my code downloads and displays, but the display is digital static. I tried all four screen rotations with nothing but digital noise in all four rotations. Can anyone help me fix the following code so the image is properly displayed?

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);
}

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”