Set Up a NodeMCU to communicate with Thingspeak using MQTT

Charlotte Dorn
4 min readApr 23, 2020

--

Goal: create a network of sensors that can be used to get small scale, local IoT data

Thingspeak

Thingspeak is a publicly available software, powered by Mathworks that is commonly used to collect data from internet-connected sensors. Thingspeak hosts an MQTT Server with different channels for different users. You can create a unique channel, each of which can collect data for up to 8 fields and use this id to read or write from any device connected to the internet.

MQTT (Message Queuing Telemetry Transport) is a Machine-to-Machine connectivity protocol. This is fancy jargon which basically means its a way for devices to communicate It allows data to be published and subscribed to by clients through one central server on topic channels. The server can be as simple as a local computer or Raspberry Pi (using an IP address) or larger and public like mqtt.thingspeak.com. In our case, the NodeMCU sensors will be the clients.

ESP8266 and NodeMCU

An ESP8266 chip is most commonly used by hobbyists to connect small analog or digital sensors to the internet. However, the chip alone will require additional work because it needs a programmer and a 3.3V supply instead of the usual 5V for Arduinos and Pis.

Instead, you can use a NodeMCU which is one of many versions of boards that allow for easy interface with the ESP8266, ideal for hobby and prototyping. The best part is that the NodeMCU allows you to program the chip directly using the Arduino IDE, just by adding the boards.

Once you’ve downloaded the Arduino IDE, go to Tools > Board > Board Manager… and then search for and install esp8266.

\

The same should be done for PubSubClient, the library to be used for MQTT protocols.

Now when you want to program your NodeMCU, choose it in the list of available ESP8266 Boards. Then connect your board with a micro USB and choose the correct port to upload.

Note that some boards will require you to use a baud rate of 9600 instead of 115200. This can be changed in the function:

Serial.begin(9600);

This code was written as an example of the most basic Thingspeak sensor — it will just print a counting value.

Code Walkthrough

The initialized variables at the beginning need to be input by each user of this program, but should be self-explanatory. This includes an optional ssid (name of wifi network) and password, the channel id and API Key. In the case of my network (shown below), the Channel ID is 1041147 and the APIkey is S537TW8F21VHNXC3.

We then initialize the wifi and PubSub clients

WiFiClient client; // Initialize the Wi-Fi client library.
PubSubClient mqttClient( client ); // Initialize the PuBSubClient library.

We’ll be calling the following functions from our main loop:

// Generate a unique client ID and connect to MQTT broker.
void mqttConnect();
// Connect to a given Wi-Fi SSID
void connectWifi();
//Send data to the provided MQTT channel
void publishMQTT();

One thing to note is that the connectWifi() is set to try to connect to any available network. But there is a commented out section that can be uncommented if you wish to use a known/specific wifi network.

In loop(), we try to connect to the wifi and then our mqtt client. We then publish the MQTT value with the following function:

void publishMQTT() {    String dataString = "field1=" + String(counter);    Serial.println(dataString);

Serial.println(dataString);
mqttClient.loop(); // Call the loop to maintain connection to the server.

String topicString ="channels/" + String(writeChannelID) + "/publish/"+String(writeAPIKey);
mqttClient.publish( topicString.c_str(), dataString.c_str() );
Serial.println( "to channel " + String(writeChannelID ) );

}

Note: to add more fields, you can add the following to the dataString declaration.

+ “&field2= “ + String(data)

--

--