Wi-Fi draw images from the web
What this example shows
Downloading images over Wi-Fi and drawing them on the display. The sketch draws three images from varipass.org: a 1-bit black and white BMP with display.image.draw(), a 24-bit color BMP that it fetches itself with HTTPClient and hands to drawBitmapFromWeb(), and a JPG with display.image.draw() again.
You need your Wi-Fi name (ssid) and password (password). No API key is needed.
Example code
Trimmed from Inkplate10_Show_Pictures_From_Web.ino: the file header comment is removed, the sketch is otherwise unchanged. ssid and password are placeholders that you replace.
// Next 3 lines are a precaution, you can ignore those, and the example would also work without them
#if !defined(ARDUINO_INKPLATE10) && !defined(ARDUINO_INKPLATE10V2)
#error "Wrong board selection for this example, please select e-radionica Inkplate10 or Soldered Inkplate10 in the boards menu."
#endif
#include "HTTPClient.h" //Include library for HTTPClient
#include "Inkplate.h" //Include Inkplate library to the sketch
#include "WiFi.h" //Include library for WiFi
Inkplate display(INKPLATE_1BIT); // Create an object on Inkplate library and also set library into 1 Bit mode (BW)
const char ssid[] = ""; // Your WiFi SSID
const char *password = ""; // Your WiFi password
void setup()
{
display.begin(); // Init Inkplate library (you should call this function ONLY ONCE)
display.clearDisplay(); // Clear frame buffer of display
display.display(); // Put clear image on display
display.print("Connecting to WiFi...");
display.partialUpdate();
// Connect to the WiFi network.
WiFi.mode(WIFI_MODE_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
display.print(".");
display.partialUpdate();
}
display.println("\nWiFi OK! Downloading...");
display.partialUpdate();
// Draw the first image from web.
// Monochromatic bitmap with 1 bit depth. Images like this load quickest.
// NOTE: Both drawImage methods allow for an optional fifth "invert" parameter. Setting this parameter to true
// will flip all colors on the image, making black white and white black. This may be necessary when exporting
// bitmaps from certain softwares. Forth parameter will dither the image. Photo taken by: Roberto Fernandez
if (!display.image.draw("https://varipass.org/neowise_mono.bmp", 0, 0, false, true))
{
// If is something failed (wrong filename or wrong bitmap format), write error message on the screen.
// REMEMBER! You can only use Windows Bitmap file with color depth of 1, 4, 8 or 24 bits with no compression!
display.println("Image open error");
display.display();
}
display.display();
// Draw the second image from web, this time using a HTTPClient to fetch the response manually.
// Full color 24 bit images are large and take a long time to load, will take around 20 secs.
HTTPClient http;
// Set parameters to speed up the download process.
http.getStream().setNoDelay(true);
http.getStream().setTimeout(1);
// Photo taken by: Roberto Fernandez
http.begin("https://varipass.org/neowise.bmp");
// Check response code.
int httpCode = http.GET();
if (httpCode == 200)
{
// Get the response length and make sure it is not 0.
int32_t len = http.getSize();
if (len > 0)
{
if (!display.image.drawBitmapFromWeb(http.getStreamPtr(), 0, 0, len))
{
// If is something failed (wrong filename or wrong bitmap format), write error message on the screen.
// REMEMBER! You can only use Windows Bitmap file with color depth of 1, 4, 8 or 24 bits with no
// compression!
display.println("Image open error");
display.display();
}
display.display();
}
else
{
display.println("Invalid response length");
display.display();
}
}
else
{
display.println("HTTP error");
display.display();
}
display.clearDisplay();
delay(3000);
// Try to load image and display it on e-paper at position X=0, Y=100
// NOTE: Both drawJpegFromWeb methods allow for an optional fifth "invert" parameter. Setting this parameter to
// true will flip all colors on the image, making black white and white black. forth parameter will dither the
// image.
if (!display.image.draw("https://varipass.org/destination.jpg", 0, 100, true, false))
{
// If is something failed (wrong filename or format), write error message on the screen.
display.println("Image open error");
display.display();
}
display.display();
http.end();
WiFi.mode(WIFI_OFF);
}
void loop()
{
// Nothing...
}
Functions used
Inkplate(uint8_t mode)returns NoneCreates the display object. The mode picks how pixels are stored: `INKPLATE_1BIT` is black and white, which is what the sketches on this page use.
ReturnsConstructor; no return value.
Parameters
| Type | Name | Description |
|---|---|---|
uint8_t | mode | Display mode, INKPLATE_1BIT here. |
display.begin()returns voidInitializes the Inkplate library and its peripherals and clears the frame buffer. Call it once in `setup()`; later calls are ignored.
ReturnsNothing.
display.clearDisplay()returns voidClears the frame buffer. The screen itself does not change until you call `display()` or `partialUpdate()`.
ReturnsNothing.
display.display(bool _leaveOn = 0)returns voidSends the frame buffer to the e-paper panel and refreshes it.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
bool | _leaveOn | Optional. If set to 1, the panel supply stays on after the refresh. Defaults to 0. |
display.print(value)returns size_tPrints text or a number at the cursor, like `Serial.print()`. The display class inherits it from the Arduino `Print` class (through Adafruit GFX). The text appears after the next `display()` or `partialUpdate()`.
ReturnsNumber of bytes written.
Parameters
| Type | Name | Description |
|---|---|---|
various | value | A string, character or number to print. |
display.partialUpdate(bool _forced = false, bool leaveOn = false)returns uint32_tRefreshes only the parts of the screen that changed, without a full refresh. Works in black and white mode only.
ReturnsNumber of pixels that changed from black to white.
Parameters
| Type | Name | Description |
|---|---|---|
bool | _forced | For advanced use with deep sleep. Defaults to false. |
bool | leaveOn | If true, the panel supply stays on after the update. Defaults to false. |
WiFi.mode(wifi_mode_t mode)returns boolSets the Wi-Fi mode. The sketch uses `WIFI_MODE_STA` (join a network), `WIFI_AP` (be an access point) and `WIFI_OFF` (radio off). Part of the ESP32 Arduino core.
Returns`true` if the mode was set.
Parameters
| Type | Name | Description |
|---|---|---|
wifi_mode_t | mode | The Wi-Fi mode. |
WiFi.begin(const char *ssid, const char *pass)returns wl_status_tStarts connecting to a Wi-Fi network. It returns right away; check `WiFi.status()` to see when the connection is up. Part of the ESP32 Arduino core.
ReturnsConnection status.
Parameters
| Type | Name | Description |
|---|---|---|
const char * | ssid | Name of the Wi-Fi network. |
const char * | pass | Password of the Wi-Fi network. |
WiFi.status()returns wl_status_tReturns the current Wi-Fi connection state. `WL_CONNECTED` means the connection is up. Part of the ESP32 Arduino core.
ReturnsThe connection status.
delay(unsigned long ms)returns voidPauses the sketch for the given number of milliseconds. Arduino core function.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
unsigned long | ms | Time to wait in milliseconds. |
display.println(value)returns size_tSame as `print()`, then moves the cursor to the start of the next line. Comes from the Arduino `Print` class.
ReturnsNumber of bytes written.
Parameters
| Type | Name | Description |
|---|---|---|
various | value | Optional. A string, character or number to print. |
display.image.draw(const char *path, int x, int y, bool dither = 1, bool invert = 0)returns boolDraws an image from a URL (`http://` or `https://`) or from the SD card, picking the decoder from the file extension: BMP/DIB, JPG/JPEG or PNG. Call `display()` afterwards to show it.
Returns`true` if the image was drawn, `false` if it was not.
Parameters
| Type | Name | Description |
|---|---|---|
const char * | path | URL or file path of the image. |
int | x | X position of the image top-left corner. |
int | y | Y position of the image top-left corner. |
bool | dither | If true, dithers the image. Defaults to true. |
bool | invert | If true, swaps black and white. Defaults to false. |
HTTPClient()returns NoneCreates an HTTP client object. Part of the ESP32 Arduino core.
ReturnsDeclaration; no return value.
http.getStream().setNoDelay(bool nodelay)returns voidTurns off Nagle buffering on the response stream. The example uses it to speed up the download.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
bool | nodelay | true to send data without delay. |
http.getStream().setTimeout(unsigned long timeout)returns voidSets how long the stream waits for data. The example sets 1 to speed up the download.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
unsigned long | timeout | Timeout value. |
http.begin(const char *url)returns boolPrepares an `HTTPClient` request to the given URL. Part of the ESP32 Arduino core `HTTPClient` class.
Returns`true` if the URL was accepted.
Parameters
| Type | Name | Description |
|---|---|---|
const char * | url | Address to request. |
http.GET()returns intSends the prepared HTTP GET request and waits for the response header.
ReturnsHTTP status code, or a negative number if the request failed.
http.getSize()returns intReturns the length of the response body, as reported by the server.
ReturnsLength in bytes.
http.getStreamPtr()returns WiFiClient *Returns a pointer to the stream that carries the response body.
ReturnsThe response stream.
display.image.drawBitmapFromWeb(WiFiClient *s, int x, int y, int32_t len, bool dither = 0, bool invert = 0)returns boolDraws a BMP image from a network stream you opened yourself, for example with `HTTPClient`. Only uncompressed Windows bitmaps with 1, 4, 8 or 24 bits of color depth work, according to the example.
Returns`true` if the image was drawn, `false` if not.
Parameters
| Type | Name | Description |
|---|---|---|
WiFiClient * | s | Stream that delivers the BMP data. |
int | x | X position of the image top-left corner. |
int | y | Y position of the image top-left corner. |
int32_t | len | Length of the image in bytes. |
bool | dither | If true, dithers the image. Defaults to false. |
bool | invert | If true, swaps black and white. Defaults to false. |
http.end()returns voidEnds the `HTTPClient` connection and frees its resources.
ReturnsNothing.
What to expect
The display prints "Connecting to WiFi...", adds a dot every half second, and then prints "WiFi OK! Downloading...". It then shows the black and white BMP, then the color BMP, which the example comments say can take around 20 seconds. Finally the frame buffer is cleared, the sketch waits 3 seconds and draws the JPG at x = 0, y = 100. If an image fails, you get "Image open error", "Invalid response length" or "HTTP error" on the screen.
The example comments say bitmaps must be Windows BMP files with a color depth of 1, 4, 8 or 24 bits and no compression. The fifth draw() argument inverts black and white, which the example says may be needed for bitmaps exported from some software. The Wi-Fi radio is turned off at the end.