Send Sensor’s Data to ThingSpeak Using ESP32

IoT with ThingSpeak, Arduino and ESP32.

Introduction

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.

Video

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
Board:
– ESP32 Dev Module (Node32 Lite)
https://my.cytron.io/p-node32-lite-wifi-and-bluetooth-development-kit
Sensor:
– MPL3115A2 I2C Barometric/Altitude/Temperature Sensor
https://my.cytron.io/p-mpl3115a2-i2c-barometric-altitude-temperature-sensor
Connection ESP32 | MPL3115A2
RAW(5V) – Vin
GND – GND
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.begin(115200);
Serial.println();
Serial.println("Send Sensor's Data to ThingSpeak Using ESP32");
Serial.println();
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop()
{
if (!baro.begin()) {
Serial.println("Couldn't find sensor");
return;
}
// Connect or reconnect to WiFi
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, password);
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
if (millis() – prevMillisSensor > intervalSensor) {
pascals = baro.getPressure() / 1000;
Serial.print(pascals);
Serial.print(" kPa\t");
altitude = baro.getAltitude();
Serial.print(altitude);
Serial.print(" meters\t");
celsius = baro.getTemperature();
Serial.print(celsius);
Serial.println("*C");
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

References:

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

Getting Started with Robotic Operating System (ROS) and JetRacer AI Kit
Barrier Gate with micro:bit and MakeCode
Play MP3 File on Maker Pi Pico Using CircuitPython
Remote Jetson Nano using SSH with Visual Studio Code
3.3V Serial LCD with Raspberry Pi Pico via SPI or I2C
Tutorials of Cytron Technologies Scroll to Top