Skip to main content

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.

Select Board
Inkplate10_HTTP_POST_Request.ino
// 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 None

Creates 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

TypeNameDescription
uint8_tmodeDisplay mode, INKPLATE_1BIT here.
WiFiClient()returns None

Creates a client for plain TCP connections. Part of the ESP32 Arduino core.

ReturnsDeclaration; no return value.

display.begin()returns void

Initializes the Inkplate library and its peripherals and clears the frame buffer. Call it once in `setup()`; later calls are ignored.

ReturnsNothing.

display.clearDisplay()returns void

Clears 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 void

Sets the text color and the background color behind it. Comes from the Adafruit GFX base class.

ReturnsNothing.

Parameters

TypeNameDescription
uint16_tcText color.
uint16_tbgBackground color.
display.setTextSize(uint8_t s)returns void

Sets 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

TypeNameDescription
uint8_tsText scale factor.
display.printf(const char *format, ...)returns size_t

Prints formatted text at the cursor, using the usual `printf` format syntax. Comes from the Arduino `Print` class.

ReturnsNumber of bytes written.

Parameters

TypeNameDescription
const char *formatFormat string, for example with \n for new lines.
display.display(bool _leaveOn = 0)returns void

Sends the frame buffer to the e-paper panel and refreshes it.

ReturnsNothing.

Parameters

TypeNameDescription
bool_leaveOnOptional. If set to 1, the panel supply stays on after the refresh. Defaults to 0.
Serial.begin(unsigned long baud)returns void

Starts serial communication at the given speed. The sketch uses 115200.

ReturnsNothing.

Parameters

TypeNameDescription
unsigned longbaudBaud rate.
Serial.print(value)returns size_t

Prints text or a number to the Serial Monitor without a line break.

ReturnsNumber of bytes written.

Parameters

TypeNameDescription
variousvalueA string or number to print.
Serial.println(value)returns size_t

Prints text or a number to the Serial Monitor followed by a line break.

ReturnsNumber of bytes written.

Parameters

TypeNameDescription
variousvalueA string or number to print.
WiFi.mode(wifi_mode_t mode)returns bool

Sets 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

TypeNameDescription
wifi_mode_tmodeThe Wi-Fi mode.
WiFi.begin(const char *ssid, const char *pass)returns wl_status_t

Starts 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

TypeNameDescription
const char *ssidName of the Wi-Fi network.
const char *passPassword of the Wi-Fi network.
WiFi.status()returns wl_status_t

Returns 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 IPAddress

Returns the IP address Inkplate got from the network. Part of the ESP32 Arduino core.

ReturnsThe local IP address.

delay(unsigned long ms)returns void

Pauses the sketch for the given number of milliseconds. Arduino core function.

ReturnsNothing.

Parameters

TypeNameDescription
unsigned longmsTime to wait in milliseconds.
millis()returns unsigned long

Returns the number of milliseconds since the board started. Arduino core function.

ReturnsMilliseconds since start.

client.connect(const char *host, uint16_t port)returns int

Opens 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

TypeNameDescription
const char *hostServer name.
uint16_tportTCP port. The sketch uses 80.
client.print(value)returns size_t

Writes text to the open connection. The sketch uses it to write the HTTP request lines.

ReturnsNumber of bytes written.

Parameters

TypeNameDescription
variousvalueA string or number.
client.println(value)returns size_t

Writes 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

TypeNameDescription
variousvalueOptional. A string or number.
client.stop()returns void

Closes the connection.

ReturnsNothing.

random(long max)returns long

Returns a pseudo-random number from 0 up to, but not including, `max`. Arduino core function.

ReturnsThe random number.

Parameters

TypeNameDescription
longmaxUpper 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:

Serial MonitorAutoscrollShow timestampNewline115200 baud
Message (Enter to send message to the board)
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.