// This project implements the simulation of the reading a DHT sensor using an ESP32 with Wifi and Thingspeak
// The source code is available on my git repo at : https://github.com/Bamamou/DHT11_ESP32.git
// the only difference is the sensor, Here; we use a DHT11 while in Platform io we use a DHT22
#include <DHT.h>
#include <LiquidCrystal_I2C.h> // LCD librairy.h>
#include <WiFi.h>
#include "ThingSpeak.h"
//===============================================================================
// Set up the DHT sensor and LCD
const int DHT_Pin =4; // The DHT pin
float temperature ; // Sensor temperature
float humidity; // Sensor Humidity
int counter; // Data counter
int totalColumns = 16; // number of colums pixels on the LCD
int totalRows = 2; // number of rows pixels on the LCD
const int delayTime = 500; // delay time on between each iteration
const int scrolling_time = 100; // Scrolling time of the test
DHT dht(DHT_Pin, DHT22); // If you are using the DHT22, you just need to change the value 11 to 22
LiquidCrystal_I2C lcd(0x27, totalColumns, totalRows); // Define the lcd object from the liduidCrystal class
String staticMessage = "DHT Tutorial";
String scrollingMessage = "Welcome to ESP32 with DHT11 !";
const char* ssid = "xiangyu801"; // your network SSID (name)
const char* password = "xiangyu801"; // your network password
unsigned long myChannelNumber = 1; // Your chanel number in Thingspeak
const char * myWriteAPIKey = "XLCANR5167WQ0B83"; // YOur channel write Key
// Timer variables
unsigned long lastTime = 0;
unsigned long timerDelay = 15000;
WiFiClient client; // The client
//=============================================================================
// The below function allow us to scroll a test and it is used to scroll the ouput of data of the sensor
void scrollMessage(int row, String message, int delayTime, int totalColumns) {
for (int i=0; i < totalColumns; i++) {
message = " " + message;
}
message = message + " ";
for (int position = 0; position < message.length(); position++) {
lcd.setCursor(0, row);
lcd.print(message.substring(position, position + totalColumns));
delay(delayTime);
}
}
//=================================================================================
void setup(){
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Hello, ESP32!");
WiFi.mode(WIFI_STA); // Wifi mode
ThingSpeak.begin(client); // Initialize ThingSpeak
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the back light
lcd.clear(); // clear everything from the screen
lcd.setCursor(2, 0); // set the position of the test
lcd.print(staticMessage); // print the static message
scrollMessage(1, scrollingMessage, scrolling_time, totalColumns);
}
void loop() {
// put your main code here, to run repeatedly:
//=================================================================
if ((millis() - lastTime) > timerDelay) {
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
Serial.print("Connecting to WiFi");
while(WiFi.status() != WL_CONNECTED){
WiFi.begin("Wokwi-GUEST", "", 6); // This funtion is used only for Wokwi
// WiFi.begin(ssid, password); //call this function when you are using your own network
delay(5000);
Serial.print(".");
}
Serial.println("\nConnected.");
}
}
//===============================================================
temperature = dht.readTemperature(); // To store the values of tempreature
humidity = dht.readHumidity(); // To store the values of Humidity
//===============================================================
// set the fields with the values
ThingSpeak.setField(1, temperature); // Give the temperature
ThingSpeak.setField(2, humidity); // Give the humidity
ThingSpeak.setField(3, dht.computeHeatIndex(temperature, humidity, false)); // Give the Heat Index
ThingSpeak.setField(4, dht.readTemperature(false)); // Give the temperature in Fahrenheit
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey); // Write them in their respective fields
//uncomment if you want to get temperature in Fahrenheit
//int x = ThingSpeak.writeField(myChannelNumber, 1, temperatureF, myWriteAPIKey);
if(x == 200){
Serial.println("Channel update successful.");
}
else{
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
lastTime = millis();
//===========================================================================
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Data: "+ String(counter)); // Print the data index
// Print the values of Humidity
lcd.setCursor(0,1);
String scrollingMessageHumidity = "Humidity: "+String(humidity);
scrollMessage(1, scrollingMessageHumidity, scrolling_time, totalColumns);
// Print he values of Temperature in Celsus
String scrollingMessageTemperature = "Temperature: "+String(dht.readTemperature(false)) +"C";
scrollMessage(1, scrollingMessageTemperature, scrolling_time, totalColumns);
// Print the values of temperature in Fahrenheit
String scrollingMessageTemperatureFahrenheit = "Temperature: "+String(dht.readTemperature(true)) +"F";
scrollMessage(1, scrollingMessageTemperatureFahrenheit, scrolling_time, totalColumns);
// Print the values of the heat Index for both Units
String scrollingMessageHeatIndexCelsus = "Heat Index In Celsus: "+String(dht.computeHeatIndex(temperature, humidity, false)) +"%";
scrollMessage(1, scrollingMessageHeatIndexCelsus, scrolling_time, totalColumns);
String scrollingMessageHeatIndexFahrenheit = "Heat Index In Fahrenheit: "+String(dht.computeHeatIndex(temperature, humidity, true)) +"%";
scrollMessage(1, scrollingMessageHeatIndexFahrenheit, scrolling_time, totalColumns);
delay(delayTime);
counter++; // update the counbter
}