Introduction
ThingSpeak is a free IoT analytics platform by MathWorks that lets you collect, visualize, and analyze sensor data from your microcontrollers. In this tutorial, we connect an ESP8266 NodeMCU to ThingSpeak and create a live temperature and humidity dashboard visible from anywhere in the world.
Components Needed
- ESP8266 NodeMCU
- DHT11 or DHT22 sensor
- Free ThingSpeak account (thingspeak.com)
Step 1: Create ThingSpeak Channel
- Sign up at thingspeak.com.
- Click New Channel and create two fields: Field 1 = Temperature, Field 2 = Humidity.
- Save and note your Write API Key from the API Keys tab.
Install Libraries
Install ThingSpeak by MathWorks and DHT sensor library by Adafruit via the Library Manager.
The Code
#include <ESP8266WiFi.h>
#include <ThingSpeak.h>
#include <DHT.h>
#define DHTPIN D4
#define DHTTYPE DHT11
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
const char* apiKey = "YOUR_THINGSPEAK_WRITE_API_KEY";
unsigned long channelID = 1234567; // Your channel ID
WiFiClient client;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) delay(500);
ThingSpeak.begin(client);
Serial.println("Connected!");
}
void loop() {
float t = dht.readTemperature();
float h = dht.readHumidity();
if (!isnan(t) && !isnan(h)) {
ThingSpeak.setField(1, t);
ThingSpeak.setField(2, h);
int code = ThingSpeak.writeFields(channelID, apiKey);
Serial.println(code == 200 ? "Data sent!" : "Error: " + String(code));
}
delay(20000); // ThingSpeak requires min 15s between updates
}
Viewing Your Dashboard
Go to your ThingSpeak channel and click the Private View tab. You will see beautiful auto-updating charts for temperature and humidity!
Conclusion
ThingSpeak is one of the fastest ways to get an IoT cloud dashboard up and running. With its built-in MATLAB analytics, you can also set alerts, perform calculations, and export data for machine learning projects.