Uploading Data to ThingSpeak With MQTT

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 mqtt.thingspeak.com:1883.

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 = "mqtt.thingspeak.com";

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

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

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

String clientName="ESP-Thingspeak";
  Serial.print("Connecting to ");
  Serial.print(server);
  Serial.print(" as ");
  Serial.println(clientName);
  
  if (client.connect((char*) clientName.c_str())) {
    Serial.println("Connected to MQTT broker");
    Serial.print("Topic is: ");
    Serial.println(topic);
    
    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...");
    abort();
  }
}

void loop() {
  static int counter = 0;
  String payload="field1=";
  payload+=micros();
  payload+="&field2=";
  payload+=counter;
  payload+="&status=MQTTPUBLISH";
  
  if (client.connected()){
    Serial.print("Sending payload: ");
    Serial.println(payload);
    
    if (client.publish(topic, (char*) payload.c_str())) {
      Serial.println("Publish ok");
    }
    else {
      Serial.println("Publish failed");
    }
  }
  ++counter;
  delay(20000);
}

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

Be the First to Share

    Recommendations

    • Tinkercad to Fusion 360 Challenge

      Tinkercad to Fusion 360 Challenge
    • Origami Speed Challenge

      Origami Speed Challenge
    • Home and Garden Contest

      Home and Garden Contest

    5 Comments

    0
    Mistguard
    Mistguard

    Question 2 years ago

    Hello, bravo for this work. I'm a student who start to learn IOT and I have a project where I want to send data via MQTT but I only have an Arduino with an ESP8266 WiFi module. Do you use an ESP8266 module only or is it connected to an Arduino ? If so what's are the pins connected between arduino and ESP ? Thanks again and have a nice day !

    0
    diy_bloke
    diy_bloke

    Answer 10 months ago

    Apologies i only now see your question. I just use esp8266. No need to connect it to an arduino

    0
    32shardul
    32shardul

    11 months ago

    I have copied the entire code and made changes to my channel ID and api write key. It works well for the first 2 instances and then it does not upload data for 3rd instance. Can anyone please help me?

    0
    diy_bloke
    diy_bloke

    Reply 10 months ago

    Apologies i see your question so late. Not sure if yoh still need help but if zo please tell me what you mean wigh 'instance'

    0
    sahmiimane
    sahmiimane

    2 years ago

    thanks for sharing this tuto, can you share with me the appropriate cabling of ESP8266 and arduino.