Introduction: Uploading Data to ThingSpeak With MQTT

About: I am a physician by trade. After a career in the pharmeceutical world I decided to take it a bit slower and do things I like. Other than my hobbies that involves grassroots medicine in S.E.&P Asia. I have buil…

In the past I published various programs to upload values to Thingspeak via the Thingspeak API. There is another way as well: through MQTT. Thingspeak has recently added an MQTT broker for this at

There are two topics one can use: To upload more than 1 field in one go use: channels/<channelID/publish/<channelAPI>

To upload an individual channel use: channels/<channelID>/publish/fields/field1/<channelAPI> (just using field1 as example)

In the first case, the payload string is as follows: field1=<value1>&field2=<value2>&status=MQTTPUBLISH

In the second case the payload string is just <value1>

In the program below I am using the PubSubClient from Knolleary. The "credentials.h" file is a file that defines my WiFi credentials, you can either create such a file yourself or just insert your wificredentials.

I am using an ESP8266 to make the connection but ofcourse it is also possible to use an Arduino with Ethernet connection when you make the proper changes in this file in order to connect to Ethernet.

To avoid using again a DHT11 as an example, I show uploading variables by using micros() and a counter
#include "PubSubClient.h" //Knolleary
#include  <ESP8266WiFi.h> //ESP8266WiFi.h
#include   <credentials.h> //This is a personal file containing web credentials

const char* ssid = WAN_SSID;// this constant is defined in my credentials file
const char* password = WAN_PW;// ditto
//char* topic="channels/<channelID/publish/<channelAPI>
char* topic = "channels/123456/publish/T8I9IO457BAJE386"; 
char* server = "";

WiFiClient wifiClient;
PubSubClient client(server, 1883, wifiClient);

void callback(char* topic, byte* payload, unsigned int length) {
  // handle message arrived

void setup() {
  Serial.print("Connecting to ");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");

String clientName="ESP-Thingspeak";
  Serial.print("Connecting to ");
  Serial.print(" as ");
  if (client.connect((char*) clientName.c_str())) {
    Serial.println("Connected to MQTT broker");
    Serial.print("Topic is: ");
    if (client.publish(topic, "hello from ESP8266")) {
      Serial.println("Publish ok");
    else {
      Serial.println("Publish failed");
  else {
    Serial.println("MQTT connect failed");
    Serial.println("Will reset and try again...");

void loop() {
  static int counter = 0;
  String payload="field1=";
  if (client.connected()){
    Serial.print("Sending payload: ");
    if (client.publish(topic, (char*) payload.c_str())) {
      Serial.println("Publish ok");
    else {
      Serial.println("Publish failed");

The file is available for download here. Whether this is a better method than with the api remains to be seen