/*************************
* Sketch to connect an IoT to ThinkSpeak IoT plataform
* 1) esp32 reads temperature fron DHT11 through a digital pim (GP14)
* 2) esp32 sends temperature reading to the inter-bult serial port to
* the host computer (115200)
* 3) esp32 sends the temperature reading to an attached OLED display
* (SSD1306) if avalaible, through the I2C bus:
* (SDA --> GP21, SDA --> GP22)
* 4) esp32 send the remperature readings to ThinkSpeak IoT Cloud Platform
*
* Please be sure that the proper libaries for handling sensors are
* inlcuded in Arduino IDE(check Ref-00-Starting-Guide-to-ESP32-Arduino.pdf):
* 1) Adafruit DTH sensor library
* 2) Adafruit_SSD1306 library
* 3) Adafruit_GFX library
* 4) ThhingSpeak library
*
* For any comments/questions contact: [email protected]
*
*************************/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <WiFi.h>
#include "ThingSpeak.h" // ThinkSpeak library
// Set up the particular type of sensor used (DHT11, DHT21, DHT22, ...)
#define DHTPIN 4 // Digital esp32 pin to receive data from DHT
#define DHTTYPE DHT11 // DHT 11 is used
DHT dht(DHTPIN, DHTTYPE);
// Variables to hold temperature (t) and humidity (h)
float t;
float h;
// Set up an SSD1306 display connected to I2C (SDA, SCL)
#define SCREEN_WIDTH 128 // OLED display width (pixels)
#define SCREEN_HEIGHT 64 // OLED display height (pixels)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Wifi credentials and client setup (context dependent)
// SSID, the network name provider
// PASSWORD, password to access the network
const char* ssid = "Wokwi-GUEST";
const char* password = "NULL";
WiFiClient client;
// ThingSpeak credentials. The following values can be used, but you
// should include your own once you have created an account in
// thingspeak.con
unsigned long myChannelNumber = 2300718;
const char * myWriteAPIKey = "DVX9S6MJMZI219JO";
// Some time variables to control DHT11 reading
unsigned long lastTime = 0;
unsigned long timerDelay = 15000;
void setup() {
Serial.begin(115200); // Setup the transmission rate to the serial port
dht.begin(); // Initialize the DHT sensor
// Initialize the OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay();
display.setTextColor(WHITE);
// Establishing WiFi network mode
WiFi.mode(WIFI_STA);
// Initialize ThingSpeak
ThingSpeak.begin(client);
}
void loop() {
// ThinkSpeak communication handling
/***********************/
if ((millis() - lastTime) > timerDelay) {
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect");
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid);
delay(5000);
}
Serial.println("\nConnected.");
}
// Get a new temperature reading
// Sensors readings: temperature and humidity
t = dht.readTemperature();
h = dht.readHumidity();
if ( isnan(t) || isnan(h)) {
Serial.println("Failed to read from DHT sensor!");
}
Serial.print(F("Temperature: "));
Serial.print(t);
Serial.print(" ºC ");
Serial.print(F(" Humidity: "));
Serial.print(h);
Serial.println(" % ");
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
// pieces of information in a channel. Here, we write to field 1.
ThingSpeak.setField(1, t);
ThingSpeak.setField(2, h);
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
lastTime = millis();
}
/***********************/
// DISPLAY handling
/***********************/
// clear display
display.clearDisplay();
// display temperature
display.setTextSize(1);
display.setCursor(0,0);
display.print("Temperature: ");
display.setTextSize(2);
display.setCursor(0,10);
display.print(t);
display.print(" ");
display.setTextSize(1);
display.cp437(true);
display.write(167);
display.setTextSize(2);
display.print("C");
// display humidity
display.setTextSize(1);
display.setCursor(0, 35);
display.print("Humidity: ");
display.setTextSize(2);
display.setCursor(0, 45);
display.print(h);
display.print(" %");
display.display();
}