Wi-Fi web server
What this example shows
Turning Inkplate 10 into its own Wi-Fi access point with a small web page. Connect your phone or computer to the Inkplate network, open the address shown on the screen, type some text into the form and press the button. The text then appears on the e-paper display.
The sketch does not join your home network, so you do not need to enter Wi-Fi credentials. The access point is called "Inkplate 10" with the password "Soldered", set by the two #define lines at the top, and you can change them.
Example code
Trimmed from Inkplate10_HTTP_Web_Server.ino: the file header comment is removed, the sketch is otherwise unchanged. The page that the browser receives is stored in htmlCode.h, which sits in the same folder and is shown second. Keep both files in one sketch folder.
// 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 "Inkplate.h" //Include Inkplate library to the sketch
#include "htmlCode.h" //Include .h file where we stored out html code of our web page
#include <WebServer.h> //Include ESP32 library for Web server
#include <WiFi.h> //Include ESP32 WiFi library
#include <WiFiClient.h> //Include ESP32 WiFi library for AP
#include <uri/UriBraces.h>
#define ssid "Inkplate 10"
#define pass "Soldered"
Inkplate display(INKPLATE_1BIT); // Create an object on Inkplate library and also set library into 1 Bit mode (BW)
WebServer server(80); // Create Web server on port 80 (HTTP port number)
IPAddress serverIP;
String txt;
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.setTextSize(3); // Scale text to be two times bigger then original (5x7 px)
display.setTextColor(BLACK, WHITE); // Set text color to black and background color to white
display.setTextWrap(true); // If text does not fit on screen, send it to new line
WiFi.begin(); // Init. WiFi library
WiFi.mode(WIFI_AP); // Set WiFi to Access point mode
WiFi.softAP(ssid, pass); // Set SSID (WiFi name) and password for Access point
serverIP = WiFi.softAPIP(); // Get the server IP address
server.on("/", handleRoot); // If you open homepage, go to handle root function
server.on(UriBraces("/string/{}"),
handleString); // If you send some text to Inkplate, go to handleString function. Note that {} brackets at
// the end of address. That means that web address has some arguments (our text!).
server.begin(); // Start the web server
updatePaper();
}
void loop()
{
server.handleClient(); // You have to constantly read if there is any new client connected to web server
}
void updateHTML()
{ // This function will send response to client and send HTML code of our web page
server.send(200, "text/html", s);
}
void handleRoot()
{ // This function will send response to client if client open a root (homepage) of our web page
updateHTML();
}
void handleString()
{ // This function will send response to client, send HTML code of web page, get the text from argument sent in web page
// address and refresh screen with new text
txt = server.arg(0);
updateHTML();
updatePaper();
}
void updatePaper()
{ // This function updates screen with new data (text)
display.clearDisplay(); // Clear everything from epaper frame buffer
display.setCursor(20, 40); // Print out instruction on how to connect to Inkplate WiFi and how to open a web page
display.print("Connect to ");
display.print(ssid);
display.println(" WiFi with pass: ");
display.setCursor(240, 100);
display.println(pass);
display.setCursor(100, 150);
display.print("Open Your web browser and open");
display.setCursor(240, 210);
display.print("http://");
display.print(serverIP);
display.println('/');
display.println();
display.fillRect(10, 240, 780, 4, BLACK);
display.println("User text:"); // Print out what user typed in web page
display.print(txt);
display.display(); // Send everything to screen (refresh the screen)
}
String s = "<head>"
"<title>Soldered Inkplate e-paper display</title>"
"<style type='text/css'>"
"textarea {"
"width: 300px;"
"height: 5em;"
"}"
"textarea {"
" font-size: 150%;"
"}"
"t1 {"
" font-size: 150%;"
" font-weight: bold;"
"}"
"</style>"
"</head>"
"<body><form action='/string/' method='get' target='_self'><t1>Insert string that you want to display:</t1><br><textarea autofocus maxlength = '100' rows = '7' cols = '30' name = 'input1'>Write some text!</textarea><br>"
"<input type='submit' value='Send to display!'></form></body></html>";
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. |
WebServer(uint16_t port)returns NoneCreates a web server on the given port. 80 is the standard HTTP port. Part of the ESP32 Arduino core.
ReturnsDeclaration; no return value.
Parameters
| Type | Name | Description |
|---|---|---|
int | port | TCP port to listen on. |
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.setTextSize(uint8_t s)returns voidSets the text scale. 1 is the base size, 2 is twice as big, and so on. The function comes from the Adafruit GFX base class the library builds on.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
uint8_t | s | Text scale factor. |
display.setTextColor(uint16_t c, uint16_t bg)returns voidSets the text color and the background color behind it. Comes from the Adafruit GFX base class.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
uint16_t | c | Text color. |
uint16_t | bg | Background color. |
display.setTextWrap(bool w)returns voidChooses whether text that is too long for the screen width wraps to the next line (`true`) or is clipped (`false`). Comes from the Adafruit GFX base class.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
bool | w | true to wrap, false to clip. |
WiFi.begin()returns wl_status_tInitializes the Wi-Fi library. With no arguments the example only uses it to start the Wi-Fi stack before switching to access point mode. Part of the ESP32 Arduino core.
ReturnsConnection status.
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.softAP(const char *ssid, const char *pass)returns boolStarts Inkplate as a Wi-Fi access point with the given name and password. Part of the ESP32 Arduino core.
Returns`true` if the access point started.
Parameters
| Type | Name | Description |
|---|---|---|
const char * | ssid | Name of the access point. |
const char * | pass | Password of the access point. |
WiFi.softAPIP()returns IPAddressReturns the IP address of Inkplate while it runs as an access point. Part of the ESP32 Arduino core.
ReturnsThe access point IP address.
server.on(const Uri &uri, handler)returns voidRegisters a function that runs when a browser requests the given path. `UriBraces` with a path that ends in a braces placeholder matches a path with one argument after `/string/`.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
const Uri & | uri | The path to handle, as a string or UriBraces. |
function | handler | The function to call. |
UriBraces(const String &uri)returns NoneBuilds a path matcher where `{}` stands for a part of the address that can change. Included with `uri/UriBraces.h`.
ReturnsConstructor; no return value.
Parameters
| Type | Name | Description |
|---|---|---|
const String & | uri | Path pattern such as /string/{}. |
server.begin()returns voidStarts the web server.
ReturnsNothing.
server.handleClient()returns voidChecks for and serves waiting browser requests. Call it often, which the sketch does in `loop()`.
ReturnsNothing.
server.send(int code, const char *content_type, const String &content)returns voidSends a response to the browser.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
int | code | HTTP status code, 200 here. |
const char * | content_type | Content type, text/html here. |
const String & | content | The response body, here the page in htmlCode.h. |
server.arg(int i)returns StringReturns the value of the request argument at index `i`.
ReturnsThe argument value.
Parameters
| Type | Name | Description |
|---|---|---|
int | i | Index of the argument. |
display.setCursor(int16_t x, int16_t y)returns voidSets where the next printed text starts, in pixels from the top-left corner. Comes from the Adafruit GFX base class.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
int16_t | x | X position in pixels. |
int16_t | y | Y position in pixels. |
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.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.fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)returns voidDraws a filled rectangle. Comes from the Adafruit GFX base class.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
int16_t | x | X position of the top-left corner. |
int16_t | y | Y position of the top-left corner. |
int16_t | w | Width in pixels. |
int16_t | h | Height in pixels. |
uint16_t | color | Fill color, for example BLACK. |
IPAddress()returns NoneHolds an IP address. The sketch stores the access point address in an `IPAddress` variable and prints it on the display. Part of the ESP32 Arduino core.
ReturnsConstructor; no return value.
What to expect
The display shows instructions: connect to the "Inkplate 10" Wi-Fi with the password "Soldered", then open http:// followed by the access point IP address in your browser. Below a black line, "User text:" is followed by whatever you sent.
In the browser you get a form with a text box (100 characters at most) and a "Send to display!" button. Each time you send, the screen refreshes with your new text.