Skip to main content

Mapbox map

What this example shows​

Connect to Wi-Fi, download a static map image from the Mapbox Static Images service for a rectangle of coordinates you choose, draw it on the screen and go to deep sleep for 5 minutes. After waking, the board runs the sketch again and refreshes the map.

Before you upload:

  1. Write your network name and password between the quotes of ssid and pass.
  2. Get an API key from mapbox.com and write it between the quotes of API_KEY.
  3. Set LAT1, LON1, LAT2 and LON2 to the corners of the area you want to draw. The sketch's comment suggests bboxfinder.com for finding coordinates.

This example also uses the ESP32 deep sleep functions (arduino-esp32), which are not part of the Inkplate library and come with the ESP32 board package.

Example code​

Select Board
Inkplate7SPECTRA_Mapbox_API.ino
#include "Inkplate.h"

#define ssid "" // Name of the WiFi network (SSID) that you want to connect Inkplate to
#define pass "" // Password of that WiFi network

// Fill in these using api key from https://www.mapbox.com/ and coordinates you want to draw
// http://bboxfinder.com/ might help you :)
#define API_KEY ""
#define LAT1 18.679247
#define LON1 45.543870
#define LAT2 18.715210
#define LON2 45.562021

// Refresh time
#define DELAY_MS 5 * 60 * 1000

Inkplate inkplate;

void setup()
{
Serial.begin(115200); // Initialize serial
inkplate.begin(); // Initialize display

// Connect to WiFi
while (!inkplate.connectWiFi(ssid, pass))
{
Serial.println("Connecting to wifi");
}

// Format URL string
char URL[256];
sprintf(URL, "https://api.mapbox.com/styles/v1/mapbox/navigation-day-v1/static/[%lf,%lf,%lf,%lf]/600x448?access_token=%s", LAT1, LON1, LAT2, LON2, API_KEY);

Serial.println("Fetching:");
Serial.println(URL);

// Fetch and display image
inkplate.image.draw(URL, 0, 0, true, false);
inkplate.display();

// Go to sleep for DELAY_MS
esp_sleep_enable_timer_wakeup(1000L * DELAY_MS);

// Start deep sleep (this function does not return). Program stops here.
esp_deep_sleep_start();
}

void loop()
{
// Never here! If you are using deep sleep, the whole program should be in setup() because the board restarts each
// time. loop() must be empty!
}

Functions used​

Inkplate()returns None

Creates the Inkplate object. Inkplate 7SPECTRA has a single six-color mode, so the constructor takes no mode argument.

ReturnsConstructor; no return value.

inkplate.begin()returns void

Initializes the library, the peripherals and the frame buffer. Call it once in setup().

ReturnsNothing.

inkplate.connectWiFi(const char *ssid, const char *pass, int timeout = 23, bool printToSerial = false)returns bool

Connects to the Wi-Fi network in station mode. It checks the connection once a second and gives up after the timeout, 23 seconds by default. With printToSerial set to true it prints its progress to the Serial Monitor, which needs Serial.begin() first. The sketch repeats the call until it succeeds.

Returnstrue if connected, false if it timed out.

inkplate.image.draw(const char *path, int x, int y, bool dither = 1, bool invert = 0)returns bool

Draws an image at x, y. When the address starts with http:// or https://, the library downloads the file and picks the decoder from the file extension (bmp, dib, jpg, jpeg or png). The sketch gives it the Mapbox address, position (0, 0), dithering on and invert off.

Returnstrue if the image was drawn, false if it could not be opened or decoded.

inkplate.display(bool _leaveOn = 0)returns void

Sends the whole frame buffer to the panel and refreshes it.

ReturnsNothing.

esp_sleep_enable_timer_wakeup(uint64_t time_in_us)returns esp_err_t

ESP32 board package: wakes the chip from deep sleep after the given time in microseconds. The sketch passes 1000 × DELAY_MS, which is 5 minutes.

ReturnsESP_OK on success.

esp_deep_sleep_start()returns void

ESP32 board package: enters deep sleep. It does not return; the next wake-up starts the sketch from the beginning, which is why loop() stays empty.

ReturnsDoes not return.

What to expect​

The Serial Monitor at 115200 baud prints Connecting to wifi each time a connection attempt fails, then Fetching: followed by the full address it requested. The screen then refreshes and shows the downloaded map at the top-left corner. After 5 minutes the board wakes up and repeats this.

🔗Open the complete example

examples/Inkplate7SPECTRA/Diagnostics/Inkplate7SPECTRA_Mapbox_API

→