Arduino library · Network

ThingSpeak: log sensor data to a channel from an ESP32 or ESP8266

ThingSpeak is MathWorks’ IoT service: a channel holds up to eight numbered fields, each a time series, and the website charts them. The ThingSpeak library turns each of its calls into one HTTP request to api.thingspeak.com, so a sketch needs only a channel number, a write API key and a WiFiClient to log readings.

Built in on Mokxi: #include <ThingSpeak.h> and it builds, with nothing to install. Free, and no account needed.

What ThingSpeak does

There are two ways to write. writeField() sends one value to one field. For several readings at once, setField() each value first and then call writeFields() once: the channel gets one entry with all of them, which matters because a free account accepts one write every 15 seconds.

Every call returns a code, 200 when the server took it. Anything else tells you what went wrong, from -301 for a connection that failed to -401 for a write the server refused, usually for coming too soon after the last one.

Library Manager nameThingSpeak
AuthorMathWorks
Include#include <ThingSpeak.h>
BoardsESP32-C3, ESP32-C6, Seeed XIAO ESP32-C3, ESP32-C3 SuperMini, Seeed XIAO ESP32-C6, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32, ESP8266 NodeMCU, Raspberry Pi Pico W, Arduino Uno R4 WiFi
In the simulatorRuns, with a gap the note below names.

Install it

It builds on ESP32-C3, ESP32-C6, Seeed XIAO ESP32-C3, ESP32-C3 SuperMini, Seeed XIAO ESP32-C6, ESP32 DevKit, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, ESP32-S3-DevKitC-1, Seeed XIAO ESP32-S3, Arduino Nano ESP32, ESP8266 NodeMCU, Raspberry Pi Pico W, Arduino Uno R4 WiFi.

In Mokxi it is built in: #include <ThingSpeak.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "ThingSpeak" by MathWorks from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add ThingSpeak to lib_deps in PlatformIO. Mokxi follows the calls of release 2.1.1, so a sketch written here builds there too.

What to know in the simulator: api.thingspeak.com is outside the simulated network, so calls return -301 there. #define THINGSPEAK_URL "api.mokxi" before the include sends them to the Cloud panel’s API lab instead.

ESP32-C3 · ESP32-C6 · Seeed XIAO ESP32-C3 · ESP32-C3 SuperMini · Seeed XIAO ESP32-C6 · ESP32 DevKit · ESP32-DevKitC V4 · LOLIN32 Lite · Wemos D1 R32 · Cheap Yellow Display · TTGO T-Display · ESP32-S3-DevKitC-1 · Seeed XIAO ESP32-S3 · Arduino Nano ESP32 · ESP8266 NodeMCU · Raspberry Pi Pico W · Arduino Uno R4 WiFi

The calls you will use

In the order a sketch meets them.

ThingSpeak.begin(client)

Gives the library the network client to send through, normally a WiFiClient declared at the top of the sketch. Call it once in setup().

ThingSpeak.writeField(myChannelNumber, 1, temperature, myWriteAPIKey)

Writes one value to field 1 and returns 200 on success. The value may be an int, a long, a float or a String.

int x = ThingSpeak.writeField(myChannelNumber, 1, temperature, myWriteAPIKey);
if (x != 200) Serial.println("HTTP error " + String(x));

ThingSpeak.setField(2, humidity)

Stages a value for field 2 without sending it. Stage as many fields as you have, then send them together.

ThingSpeak.setStatus("door open")

Stages the entry’s status text, shown with the entry on the channel page.

ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey)

Sends every staged field as one channel entry, and clears them for the next write.

ThingSpeak.readFloatField(myChannelNumber, 1, myReadAPIKey)

Reads the newest value of a field. Leave out the read key for a public channel. readIntField() and readStringField() read other types.

ThingSpeak.getLastReadStatus()

The code of the last read, 200 when it worked. A read returns 0 on failure, so check this to tell a real zero from an error.

Example: A ThingSpeak write, and why it fails here

On the ESP32-C3-DevKitM-1. It runs in the browser exactly as printed.

sketch.ino · ESP32-C3-DevKitM-1
#include <WiFi.h>
#include <ThingSpeak.h>

WiFiClient client;
unsigned long myChannelNumber = 123456;
const char *myWriteAPIKey = "XXXXXXXXXXXXXXXX";

void setup() {
  Serial.begin(115200);
  WiFi.begin("Mokxi-Lab", "breadboard");
  while (WiFi.status() != WL_CONNECTED) delay(250);
  ThingSpeak.begin(client);
  int x = ThingSpeak.writeField(myChannelNumber, 1, 42, myWriteAPIKey);
  if (x == 200) {
    Serial.println("Channel update successful.");
  } else {
    Serial.println("Problem updating channel. HTTP error code " + String(x));
  }
  Serial.print("field 9: ");
  Serial.println(ThingSpeak.setField(9, 1));
}

void loop() {}

Open it in the editor, wired and ready to run

Common errors and how to fix them

Problem updating channel. HTTP error code -401

The server did not insert the point, almost always because writes came faster than once every 15 seconds on a free account. Wait at least 15 seconds between writes, and combine readings into one writeFields() call.

HTTP error code -301

The board could not connect to the server: no WiFi, a wrong network client, or a WiFiClientSecure the library cannot use for plain HTTP. In Mokxi, add #define THINGSPEAK_URL "api.mokxi" above the include to send the calls to the Cloud panel’s API lab.

HTTP error code 400 or -210

400 means the write API key is wrong, often the read key pasted by mistake. -210 means writeFields() was called with nothing staged: call setField() for at least one field first.

Try ThingSpeak without installing anything

Open the example, change the sketch, and keep your version in a free account.