Wi-Fi and HTTP request
What this example shows
Scan for Wi-Fi networks and list them on the screen, connect to your network, then download a web page and print its raw HTML on the screen. The sketch does not parse the HTML; it shows it as text.
Before you upload, write your network name and password between the quotes of the ssid and pass defines. To fetch another page, change the address the sketch passes to http.begin().
This example also uses WiFi and HTTPClient (arduino-esp32), which are not part of the Inkplate library and come with the ESP32 board package.
Example code
#include "Inkplate.h" //Include Inkplate library to the sketch
#include <HTTPClient.h> //Include HTTP library to this sketch
#include <WiFi.h> //Include ESP32 WiFi library to our sketch
#define ssid "" // Name of the WiFi network (SSID) that you want to connect Inkplate to
#define pass "" // Password of that WiFi network
Inkplate inkplate; // Create an object on Inkplate library and also set library into 1 Bit mode (BW)
void setup()
{
Serial.begin(115200); // Begin Serial for debugging
inkplate.begin(); // Init Inkplate library (you should call this function ONLY ONCE)
inkplate.clearDisplay(); // Clear frame buffer of display
inkplate.display(); // Put clear image on display
inkplate.setTextSize(3); // Set text scaling to three (text will be three times bigger)
inkplate.setCursor(0, 0); // Set print position
inkplate.setTextColor(INKPLATE_BLACK); // Set text color to black and background color to white
Serial.println("Scanning for WiFi networks..."); // Write text
int n =
WiFi.scanNetworks(); // Start searching WiFi networks and put the nubmer of found WiFi networks in variable n
if (n == 0)
{ // If you did not find any network, show the message and stop the program.
Serial.print("No WiFi networks found!");
while (true);
}
else
{
if (n > 10)
n = 10; // If you did find, print name (SSID), encryption and signal strength of first 10 networks
for (int i = 0; i < n; i++)
{
inkplate.print(WiFi.SSID(i));
inkplate.print((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? 'O' : '*');
inkplate.print('\n');
inkplate.print(WiFi.RSSI(i), DEC);
}
inkplate.display();
}
inkplate.clearDisplay(); // Clear everything in frame buffer
inkplate.setCursor(0, 0); // Set print cursor to new position
Serial.print("Connecting to "); // Print the name of WiFi network
Serial.print(ssid);
WiFi.begin(ssid, pass); // Try to connect to WiFi network
while (WiFi.status() != WL_CONNECTED)
{
delay(1000); // While it is connecting to network, display dot every second, just to know that Inkplate is
// alive.
Serial.print('.');
}
Serial.print("connected"); // If it's connected, notify user
HTTPClient http;
if (http.begin("http://example.com/index.html"))
{ // Now try to connect to some web page (in this example www.example.com. And yes, this is a valid Web page :))
if (http.GET() > 0)
{ // If connection was successful, try to read content of the Web page and display it on screen
String htmlText;
htmlText = http.getString();
inkplate.setTextSize(3);
inkplate.clearDisplay();
inkplate.setCursor(0, 0);
inkplate.print(htmlText);
inkplate.display();
}
}
}
void loop()
{
// Nothing
}
Functions used
Inkplate()returns NoneCreates the Inkplate object. Inkplate 7SPECTRA has a single six-color mode, so the constructor takes no mode argument (the comment about 1-bit mode comes from other Inkplates).
ReturnsConstructor; no return value.
inkplate.begin()returns voidInitializes the library, the peripherals and the frame buffer. Call it once in setup().
ReturnsNothing.
inkplate.clearDisplay()returns voidFills the frame buffer with white. It does not change the panel.
ReturnsNothing.
inkplate.display(bool _leaveOn = 0)returns voidSends the whole frame buffer to the panel and refreshes it.
ReturnsNothing.
inkplate.setTextSize(uint8_t s)returns voidSets the text magnification, here 3.
ReturnsNothing.
inkplate.setCursor(int16_t x, int16_t y)returns voidSets where the next printed text starts, here the top-left corner.
ReturnsNothing.
inkplate.setTextColor(uint16_t c)returns voidSets the text color, here INKPLATE_BLACK.
ReturnsNothing.
inkplate.print(value, format)returns size_tPrints text, a character or a number into the frame buffer, like Serial.print(). DEC prints a number in decimal.
ReturnsNumber of bytes printed.
WiFi.scanNetworks()returns int16_tESP32 board package: scans for Wi-Fi networks and waits for the result.
ReturnsNumber of networks found.
WiFi.SSID(uint8_t i)returns StringESP32 board package: the name of network i from the last scan.
ReturnsNetwork name.
WiFi.encryptionType(uint8_t i)returns wifi_auth_mode_tESP32 board package: the encryption of network i; WIFI_AUTH_OPEN means no password. The sketch prints O for open networks and * for the rest.
ReturnsEncryption type.
WiFi.RSSI(uint8_t i)returns int32_tESP32 board package: the signal strength of network i, in dBm.
ReturnsSignal strength.
WiFi.begin(ssid, pass)returns wl_status_tESP32 board package: starts connecting to the network.
ReturnsConnection status.
WiFi.status()returns wl_status_tESP32 board package: the connection status; WL_CONNECTED once connected.
ReturnsConnection status.
HTTPClient()returns NoneHTTPClient class, ESP32 board package: the HTTP client object. The sketch creates one as `http`.
ReturnsConstructor; no return value.
http.begin(url)returns boolHTTPClient, ESP32 board package: prepares a request to the URL.
Returnstrue if the URL could be parsed.
http.GET()returns intHTTPClient: sends the GET request.
ReturnsThe HTTP status code, or a negative error code.
http.getString()returns StringHTTPClient: reads the whole response body.
ReturnsThe response body.
What to expect
The screen first lists up to 10 networks it found, each with its name, O (open) or * (password protected) and its signal strength. Once the board is on your network, the screen refreshes again and shows the HTML of the page as text. The Serial Monitor at 115200 baud shows Scanning for WiFi networks..., then Connecting to with your network name, a dot every second while it connects, and connected.
🔗Open the complete example
examples/Inkplate7SPECTRA/Advanced/WEB_WiFi/Inkplate7SPECTRA_HTTP_Request