Last active
November 4, 2019 09:05
-
-
Save IdrisCytron/dc69f242ee171c56c3e45696796b75a2 to your computer and use it in GitHub Desktop.
Send Sensor's Data to ThingSpeak Using ESP32
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment