Wi-Fi HTTP POST request
What this example shows
Sending a small HTTP POST request over Wi-Fi every 20 seconds to webhook.site, a free service that shows the requests it receives. The body is a form-encoded value= followed by a random number from 0 to 39, which you would replace with a sensor reading.
You need to supply three things: your Wi-Fi name (ssid), your Wi-Fi password (pass), and your own webhook.site address. Open webhook.site, copy your unique URL and paste only the path part, for example /abcd-1234-efgh, into WEBHOOK_PATH. No API key is needed.
Example code
Trimmed from Inkplate10_HTTP_POST_Request.ino: the file header comment is removed, the sketch is otherwise unchanged. ssid, pass and WEBHOOK_PATH are placeholders that you replace.
// Board check
#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 needed libraries
#include "Inkplate.h"
#include "WiFi.h"
// Create objects
Inkplate display(INKPLATE_1BIT);
WiFiClient client;
// Interval between POST requests (seconds)
#define POSTING_INTERVAL_IN_SECS 20
// WiFi credentials
const char *ssid = "";
const char *pass = "";
// Webhook.site settings
const char *server = "webhook.site";
const char *WEBHOOK_PATH = "/YOUR-UNIQUE-WEBHOOK-ID"; // e.g. "/abcd-1234-efgh"
// Last connection time
unsigned long lastConnectionTime = 0;
void setup()
{
Serial.begin(115200);
// Init Inkplate
display.begin();
display.clearDisplay();
display.setTextColor(BLACK, WHITE);
display.setTextSize(6);
display.printf("HTTP POST example\n\n");
display.printf("Using webhook.site\n\n");
display.printf("Open Serial Monitor\nat 115200 baud");
display.display();
// Connect to WiFi
WiFi.mode(WIFI_MODE_STA);
WiFi.begin(ssid, pass);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
}
void loop()
{
if ((unsigned long)(millis() - lastConnectionTime) > POSTING_INTERVAL_IN_SECS * 1000UL)
{
display.clearDisplay();
if (!client.connect(server, 80))
{
Serial.println("Connection failed");
lastConnectionTime = millis();
client.stop();
return;
}
// Example data (replace with sensor readings if needed)
int value = random(40);
// URL-encoded POST body
String data = "value=" + String(value);
// Send HTTP POST request
client.print(String("POST ") + WEBHOOK_PATH + " HTTP/1.1\r\n");
client.print(String("Host: ") + server + "\r\n");
client.println("Connection: close");
client.println("User-Agent: Inkplate-ESP32");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.print(data);
Serial.print("POST sent: ");
Serial.println(data);
lastConnectionTime = millis();
delay(250);
client.stop();
}
}
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. |
WiFiClient()returns NoneCreates a client for plain TCP connections. Part of the ESP32 Arduino core.
ReturnsDeclaration; no return value.
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.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.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.printf(const char *format, ...)returns size_tPrints formatted text at the cursor, using the usual `printf` format syntax. Comes from the Arduino `Print` class.
ReturnsNumber of bytes written.
Parameters
| Type | Name | Description |
|---|---|---|
const char * | format | Format string, for example with \n for new lines. |
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. |
Serial.begin(unsigned long baud)returns voidStarts serial communication at the given speed. The sketch uses 115200.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
unsigned long | baud | Baud rate. |
Serial.print(value)returns size_tPrints text or a number to the Serial Monitor without a line break.
ReturnsNumber of bytes written.
Parameters
| Type | Name | Description |
|---|---|---|
various | value | A string or number to print. |
Serial.println(value)returns size_tPrints text or a number to the Serial Monitor followed by a line break.
ReturnsNumber of bytes written.
Parameters
| Type | Name | Description |
|---|---|---|
various | value | A string or number to print. |
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.
WiFi.localIP()returns IPAddressReturns the IP address Inkplate got from the network. Part of the ESP32 Arduino core.
ReturnsThe local IP address.
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. |
millis()returns unsigned longReturns the number of milliseconds since the board started. Arduino core function.
ReturnsMilliseconds since start.
client.connect(const char *host, uint16_t port)returns intOpens a TCP connection to the host and port. Part of `WiFiClient` in the ESP32 Arduino core.
ReturnsNon-zero if connected, 0 if the connection failed.
Parameters
| Type | Name | Description |
|---|---|---|
const char * | host | Server name. |
uint16_t | port | TCP port. The sketch uses 80. |
client.print(value)returns size_tWrites text to the open connection. The sketch uses it to write the HTTP request lines.
ReturnsNumber of bytes written.
Parameters
| Type | Name | Description |
|---|---|---|
various | value | A string or number. |
client.println(value)returns size_tWrites text to the connection followed by `\r\n`. Called with no argument it writes an empty line, which ends the HTTP header.
ReturnsNumber of bytes written.
Parameters
| Type | Name | Description |
|---|---|---|
various | value | Optional. A string or number. |
client.stop()returns voidCloses the connection.
ReturnsNothing.
random(long max)returns longReturns a pseudo-random number from 0 up to, but not including, `max`. Arduino core function.
ReturnsThe random number.
Parameters
| Type | Name | Description |
|---|---|---|
long | max | Upper limit, exclusive. |
What to expect
The display shows "HTTP POST example", "Using webhook.site" and "Open Serial Monitor at 115200 baud". The progress is in the Serial Monitor:
Connecting to WiFi.....
Connected, IP address: <IP address>
POST sent: value=<number>
<IP address> is the address your router assigns to the board, and <number> is the random value from 0 to 39 that random(40) returns.
A new POST sent: line appears about every 20 seconds, and each request shows up on your webhook.site page. If the connection to the server cannot be opened, the sketch prints "Connection failed" and tries again after the next interval.