Maximizing the ESP32’s Potential with ThingSpeak and MQTT IoT Protocol

How to send data from your IoT project to the cloud? Is there some free solution for prototyping porpoise on which you can grow later if you decide? Maybe ThingSpeak is a good solution and can be a good way to send IoT data to Cloud. So let us begin.

In IoT (Internet of Things) we need a connection to the internet in order to send data and/or receive commands. For any device, the microprocessor is very easy to be connected to the internet, but that doesn’t make it IoT. We need to give meaning to the data that is been sent and we need some kind of server and/or service that can be on a local PC or in the cloud. In this example, I would like to show how can we start with a simple IoT project that will just periodically send data to an IoT service that is running in the cloud and how can we present that data to the end user. This can be converted to a bunch of fun projects in the future with all kinds of sensors, which will send data to ESP32 and then ESP32 will only forward that data to the Cloud service for processing.

And now, before I start let us check the rest of our system. For that, I will use an ESP32 NodeMCU development board with integrated WiFi so we have everything that we need for this project. AND FINALLY, What is ThingSpeak?

What is ThingSpeak?

If we check the official site for ThingSpeak we can see the definition:

ThingSpeak™ is an IoT analytics service that allows you to aggregate, visualize, and analyze live data streams in the cloud. ThingSpeak provides instant visualizations of data posted by your devices to ThingSpeak. With the ability to execute MATLAB® code in ThingSpeak, you can perform online analysis and process data as it comes in. ThingSpeak is often used for prototyping and proof-of-concept IoT systems that require analytics. 

official ThingSpeak site

With ThingSpeak we have a service that will receive our data that is based on MQTT protocol and that display that same data. Also, we can use MatLab functions in order to process and virtualize that data in order to get useful info.

Set up ThingSpeak

Go to ThingSpeak official site and click on Get Started for Free. YES, we can check it out for free. There are some limitations but we will cover them later.

ThingSpeak home page

Let us log in with a MathWorks account if you have one. If not, it is easy to create one.

Log in page

Answer the question. In my case, I’m using ThingSpeak as a Personal, non-commercial project.

Go to the Channels tab, and let us create New Channel.

In order to create a new channel we just need to add the Channel name, Description and assign how many fields we need. We can also give each field a more descriptive name.

So, my new channel is shown in the image below. I want to send three different data to the ThingSpeak cloud service.

And in addition to this, we need to check out our Channel API keys, Write and Read Key. API Keys can be seen in the API Keys tab of your TestESP32 channel.

And we have a good overview of our date in Tab Private View. Here we have three graphs with our data shown.

And that is basically it for server Cloud service with ThingSpeak.

Download Library

Now we will focus on the device side, on our ESP32 in this case. We need to provide the library that has everything that we need from ThingSpeak. If we would like to use Arduino IDE, it is a bit easier, and we just need to go to Library manager and search ThingSpeak, select Library and install. And that is it.

But I using VS code and Platform IO, so we need a library from GitHub. Check out the next link:

In order to get source code go to releases section and get the latest stable release, in this case v 2.0.0.

Extract the library in lib directory of your ESP32 project and we can begin with adding code to send IoT data to Cloud.

Adopting to code

In order to send IoT data to the cloud with ESP32 and ThingSpeak first we need to include two libraries. One for ThingSpeak and one for ESP32 Wifi connection.

#include "ThingSpeak.h"
#include <WiFi.h>

Specified your WiFi network name and password.

char ssid[] = "<wifi_name>";   // your network SSID (name) 
char pass[] = "<wifi_password>";   // your network password

then we need information about out created ThingSpeak channel as channel number and Write API key.

unsigned long myChannelNumber = 1914080;
const char * myWriteAPIKey = "0UI82F275J7K9ESB";

In Set up phase (in void setup() function), we need to begin WiFi communication and activate ThingSpeak client with next commands.

void setup() {
  // put your setup code here, to run once:
  WiFi.mode(WIFI_STA);        // Initialize WiFi
  ThingSpeak.begin(client);  // Initialize ThingSpeak

Now, in the loop() function as the first part, we need to check for WiFi connection and if it is non, we should start with WiFi.begin(ssid, pass)

void loop() {
  // Connect or reconnect to WiFi
  if(WiFi.status() != WL_CONNECTED){
    while(WiFi.status() != WL_CONNECTED){
      WiFi.begin(ssid, pass);  // Connect to WPA/WPA2 network. Change this line if using open or WEP network

And last we simply need to send data in our code when we want to with next commands

  ThingSpeak.writeField(myChannelNumber, 1, data_sec, myWriteAPIKey);  // data for Field 1 
  ThingSpeak.writeField(myChannelNumber, 2, data_min, myWriteAPIKey);  // data for Field 2 
  ThingSpeak.writeField(myChannelNumber, 3, data_hrs, myWriteAPIKey);  // data for Field 3 

Limitation of the free version of ThingSpeak

Here I noticed the first limitation that is related to the free version of ThingSpeak and that is the speed at which you can send data to the cloud service. Apparently, if the data is sent too fast some samples would be discarded. But what is too short? I noticed that if I send the sample only once in 5 seconds everything is fine.

Final words

And that is in, so basic but powerful. With this, you can turn any sensor to IoT data in under maybe 10 or 15 mins and send IoT data to Cloud using ESP32 and ThingSpeak. If you liked this topic please check out my other post on IoT at:

Leave a Comment