Send Sensor’s Data to ThingSpeak Using ESP32

IoT with ThingSpeak, Arduino and ESP32.


Previously I have shared on how to send sensor’s data to ThingSpeak IoT platform using Raspberry Pi. Now I want to try with ESP32. You may refer to this tutorial first to install ESP32 board package.


This video will show you how to send sensor’s data to ThingSpeak by using ESP32 and Arduino IDE.

Hardware Preparation

This is the list of items used in the video.

Sample Program

This is Arduino sample program to read sensor’s data from MPL3115A2 module for every 2 seconds, and send it to ThingSpeak for 20 seconds time interval.

Tutorial: Send Sensor's Data to ThingSpeak Using ESP32
– ESP32 Dev Module (Node32 Lite)
– MPL3115A2 I2C Barometric/Altitude/Temperature Sensor
Connection ESP32 | MPL3115A2
RAW(5V) – Vin
22 – SCL
21 – SDA
External libraries:
– Adafruit MPL3115A2 Library by Adafruit Version 1.2.2
– ThingSpeak by MathWorks Version 1.5.0
Created by:
4 Nov 2019 Idris Zainal Abidin, Cytron Technologies
#include <WiFi.h>
const char ssid[] = "Your WiFi SSID";
const char password[] = "Your WiFi Password";
WiFiClient client;
#include <ThingSpeak.h>
const long CHANNEL = Your ThingSpeak Channel ID;
const char *WRITE_API = "Your ThingSpeak Write API";
#include <Wire.h>
#include <Adafruit_MPL3115A2.h>
Adafruit_MPL3115A2 baro = Adafruit_MPL3115A2();
float pascals = 0.0;
float altitude = 0.0;
float celsius = 0.0;
long prevMillisSensor = 0;
int intervalSensor = 2000;
long prevMillisThingSpeak = 0;
int intervalThingSpeak = 20000; // Minimum ThingSpeak write interval is 15 seconds
void setup()
Serial.println("Send Sensor's Data to ThingSpeak Using ESP32");
ThingSpeak.begin(client); // Initialize ThingSpeak
void loop()
if (!baro.begin()) {
Serial.println("Couldn't find sensor");
// Connect or reconnect to WiFi
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, password);
if (millis() – prevMillisSensor > intervalSensor) {
pascals = baro.getPressure() / 1000;
Serial.print(" kPa\t");
altitude = baro.getAltitude();
Serial.print(" meters\t");
celsius = baro.getTemperature();
prevMillisSensor = millis();
if (millis() – prevMillisThingSpeak > intervalThingSpeak) {
// Set the fields with the values
ThingSpeak.setField(1, pascals);
ThingSpeak.setField(2, altitude);
ThingSpeak.setField(3, celsius);
// Write to the ThingSpeak channel
int x = ThingSpeak.writeFields(CHANNEL, WRITE_API);
if (x == 200) {
Serial.println("Channel update successful.");
else {
Serial.println("Problem updating channel. HTTP error code " + String(x));
prevMillisThingSpeak = millis();

Thank You


Thanks for reading this tutorial. If you have any technical inquiries, please post at Cytron Technical Forum.

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

DIY Interactive Robot Using REKA:BIT With Micro:bit
BLTouch Installation for Ender 3 with 32-bit V4.2.2 Board
Pick and Send Random Meal’s Option and Locations through Telegram Bot Using Grove WiFi 8266 on micro:bit
DIY Automated Vacuum Cleaner Using REKA:BIT With Micro:bit
Rainbow Spark in Mini House using Maker Uno.
Tutorials of Cytron Technologies Scroll to Top