Skip to main content

HTTP POST to ThingSpeak

What this example shows​

Connect to your Wi-Fi network and send a random number to a ThingSpeak channel with an HTTP POST request every 20 seconds. The screen shows a short message once, at the start; the progress is printed on the Serial Monitor.

Before you upload:

  1. Create a free account on ThingSpeak.com and open the Channels tab.
  2. Create a new channel with one field. The sketch sends field1, and the field must have this name.
  3. Open the channel, go to the API Keys tab and copy your Write API Key.
  4. Write your network name and password between the quotes of ssid and pass, and the key between the quotes of writeAPIKey.

The sketch's own comment states that a free ThingSpeak license allows a minimum of 15 seconds between requests; POSTING_INTERVAL_IN_SESCS is 20. When a request has been sent, open your channel to see the graph of the sent data.

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

Example code​

Select Board
Inkplate7SPECTRA_HTTP_POST_ThingSpeak.ino
#include "Inkplate.h"
#include "WiFi.h"

Inkplate display;
WiFiClient client;

#define POSTING_INTERVAL_IN_SESCS 20

const char *ssid = "";
const char *pass = "";

char *server = "api.thingspeak.com";
String writeAPIKey = ""; // Enter your Write API Key

unsigned long lastConnectionTime = 0;

void setup()
{
// Init serial communication
Serial.begin(115200);

// Init Inkplate library (you should call this function ONLY ONCE)
display.begin();

// Clear frame buffer of display
display.clearDisplay();

// Set text color and size
display.setTextColor(INKPLATE_BLACK);
display.setTextSize(3);

// Display a message
display.printf("HTTP POST request example\n\n");
display.printf("Open Serial Monitor at \n115200 baud rate to see \nwhat's happening.");
display.display();

// Connect to the WiFi network
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 to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
}


void loop()
{
// Every POSTING_INTERVAL_IN_SESCS seconds make the POST request
if ((unsigned long)(millis() - lastConnectionTime) > POSTING_INTERVAL_IN_SESCS * 1000LL)
{
// Clear frame buffer of display
display.clearDisplay();

// Connect the WiFi client to the server via port 80
if (!client.connect(server, 80))
{
// If it fails, print a message, remember time, stop the client and reset the loop
Serial.println("Connection failed");
lastConnectionTime = millis();
client.stop();
return;
}
else
{
// If you have any sensor or something else, here you have to put data to send instead of a random number
int field1Data = random(40);

// Create data string to send to ThingSpeak
String data = "field1=" + String(field1Data); // shows how to include additional field data in http post

// POST data to ThingSpeak
if (client.connect(server, 80))
{
client.println("POST /update HTTP/1.1");
client.println("Host: api.thingspeak.com");
client.println("Connection: close");
client.println("User-Agent: ESP32WiFi/1.1");
client.println("X-THINGSPEAKAPIKEY: " + writeAPIKey);
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.print(data.length());
client.print("\n\n");
client.print(data);

Serial.print("The POST request is done: ");
Serial.println(data);
lastConnectionTime = millis();
delay(250);
}
}
client.stop();
}
}

Functions used​

Inkplate()returns None

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

ReturnsConstructor; no return value.

display.begin()returns void

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

ReturnsNothing.

display.clearDisplay()returns void

Fills the frame buffer with white. It does not change the panel.

ReturnsNothing.

display.setTextColor(uint16_t c)returns void

Sets the text color, here INKPLATE_BLACK.

ReturnsNothing.

display.setTextSize(uint8_t s)returns void

Sets the text magnification, here 3.

ReturnsNothing.

display.printf(format, ...)returns size_t

Prints formatted text into the frame buffer, like Serial.printf(). The sketch uses it for the two-part message that tells you to open the Serial Monitor at 115200 baud.

ReturnsNumber of bytes printed.

display.display(bool _leaveOn = 0)returns void

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

ReturnsNothing.

WiFi.mode(wifi_mode_t mode)returns bool

ESP32 board package: sets the Wi-Fi mode. The sketch uses WIFI_MODE_STA, the mode for connecting to a network.

Returnstrue if the mode was set.

WiFi.begin(ssid, pass)returns wl_status_t

ESP32 board package: starts connecting to the network.

ReturnsConnection status.

WiFi.status()returns wl_status_t

ESP32 board package: the connection status; WL_CONNECTED once connected.

ReturnsConnection status.

WiFi.localIP()returns IPAddress

ESP32 board package: the IP address the board got on the network.

ReturnsThe board's IP address.

WiFiClient()returns None

ESP32 board package: a TCP client object. The sketch creates one as `client`.

ReturnsConstructor; no return value.

client.connect(host, port)returns int

WiFiClient: opens a connection to the server. The sketch connects to api.thingspeak.com on port 80.

Returns1 if the connection was made, 0 if not.

client.println(data)returns size_t

WiFiClient: sends the text followed by a line break. The sketch sends the request line and the HTTP headers with it, including the Write API Key in the X-THINGSPEAKAPIKEY header.

ReturnsNumber of bytes sent.

client.print(data)returns size_t

WiFiClient: sends the text as it is. The sketch sends the Content-Length header value, a blank line and the body `field1=<number>` with it.

ReturnsNumber of bytes sent.

client.stop()returns void

WiFiClient: closes the connection.

ReturnsNothing.

What to expect​

The screen shows HTTP POST request example and a line asking you to open the Serial Monitor at 115200 baud. In the Serial Monitor at 115200 baud you see Connecting to Wifi and a dot every half second until the board is connected, then Connected to WiFi network with IP Address: followed by the address the board got. After that, every 20 seconds the sketch prints The POST request is done: field1= followed by the random number from 0 to 39 it sent, or Connection failed when it cannot reach the server.

🔗Open the complete example

examples/Inkplate7SPECTRA/Advanced/WEB_WiFi/Inkplate7SPECTRA_HTTP_POST_ThingSpeak

→