ESP32 | Cloud Firestore - Ep 0. Showing temp/humidity from DHT11 on TTGO display [Upgraded Version]

This commit is contained in:
Eric
2021-08-08 21:54:26 -07:00
parent 2ddce7f16c
commit 9a111786e0
3 changed files with 246 additions and 0 deletions

View File

@@ -0,0 +1,197 @@
/////////////////////////////////////////////////////////////////
/*
ESP32 | Cloud Firestore - Ep 0. Showing temp/humidity from DHT11 on TTGO display [Upgraded Version]
Video Tutorial: https://youtu.be/aAUIlCWs_bc
Created by Eric N. (ThatProject)
*/
/////////////////////////////////////////////////////////////////
#include "DHTesp.h" // Click here to get the library: http://librarymanager/All#DHTesp
#include <Ticker.h>
#include "Display.h"
#ifndef ESP32
#pragma message(THIS EXAMPLE IS FOR ESP32 ONLY !)
#error Select ESP32 board.
#endif
/**************************************************************/
/* Example how to read DHT sensors from an ESP32 using multi- */
/* tasking. */
/* This example depends on the Ticker library to wake up */
/* the task every 20 seconds */
/**************************************************************/
Display *display;
DHTesp dht;
void tempTask(void *pvParameters);
bool getTemperature();
void triggerGetTemp();
/** Task handle for the light value read task */
TaskHandle_t tempTaskHandle = NULL;
/** Ticker for temperature reading */
Ticker tempTicker;
/** Comfort profile */
ComfortState cf;
/** Flag if task should run */
bool tasksEnabled = false;
/** Pin number for DHT11 data pin */
int dhtPin = 21;
/**
* initTemp
* Setup DHT library
* Setup task and timer for repeated measurement
* @return bool
* true if task and timer are started
* false if task or timer couldn't be started
*/
bool initTemp() {
byte resultValue = 0;
// Initialize temperature sensor
dht.setup(dhtPin, DHTesp::DHT11);
Serial.println("DHT initiated");
// Start task to get temperature
xTaskCreatePinnedToCore(
tempTask, /* Function to implement the task */
"tempTask ", /* Name of the task */
4000, /* Stack size in words */
NULL, /* Task input parameter */
5, /* Priority of the task */
&tempTaskHandle, /* Task handle. */
1); /* Core where the task should run */
if (tempTaskHandle == NULL) {
Serial.println("Failed to start task for temperature update");
return false;
} else {
// Start update of environment data every 20 seconds
tempTicker.attach(20, triggerGetTemp);
}
return true;
}
/**
* triggerGetTemp
* Sets flag dhtUpdated to true for handling in loop()
* called by Ticker getTempTimer
*/
void triggerGetTemp() {
if (tempTaskHandle != NULL) {
xTaskResumeFromISR(tempTaskHandle);
}
}
/**
* Task to reads temperature from DHT11 sensor
* @param pvParameters
* pointer to task parameters
*/
void tempTask(void *pvParameters) {
Serial.println("tempTask loop started");
while (1) // tempTask loop
{
if (tasksEnabled) {
// Get temperature values
getTemperature();
}
// Got sleep again
vTaskSuspend(NULL);
}
}
/**
* getTemperature
* Reads temperature from DHT11 sensor
* @return bool
* true if temperature could be aquired
* false if aquisition failed
*/
bool getTemperature() {
// Reading temperature for humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)
TempAndHumidity newValues = dht.getTempAndHumidity();
// Check if any reads failed and exit early (to try again).
if (dht.getStatus() != 0) {
Serial.println("DHT11 error status: " + String(dht.getStatusString()));
return false;
}
float heatIndex = dht.computeHeatIndex(newValues.temperature, newValues.humidity);
float dewPoint = dht.computeDewPoint(newValues.temperature, newValues.humidity);
float cr = dht.getComfortRatio(cf, newValues.temperature, newValues.humidity);
String comfortStatus;
switch (cf) {
case Comfort_OK:
comfortStatus = "OK";
break;
case Comfort_TooHot:
comfortStatus = "TooHot";
break;
case Comfort_TooCold:
comfortStatus = "TooCold";
break;
case Comfort_TooDry:
comfortStatus = "TooDry";
break;
case Comfort_TooHumid:
comfortStatus = "TooHumid";
break;
case Comfort_HotAndHumid:
comfortStatus = "Hot&Humid";
break;
case Comfort_HotAndDry:
comfortStatus = "Hot&Dry";
break;
case Comfort_ColdAndHumid:
comfortStatus = "Cold&Humid";
break;
case Comfort_ColdAndDry:
comfortStatus = "Cold&Dry";
break;
default:
comfortStatus = "Unknown";
break;
};
Serial.println(" T:" + String(newValues.temperature) + " H:" + String(newValues.humidity) + " I:" + String(heatIndex) + " D:" + String(dewPoint) + " " + comfortStatus);
display->tempUpdates("Temp " + String(newValues.temperature, 1) + "'C",
"Humidity " + String(newValues.humidity, 0) + "%",
comfortStatus);
return true;
}
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println("DHT ESP32 example with tasks");
initDisplay();
initTemp();
// Signal end of setup() to tasks
tasksEnabled = true;
}
void loop() {
if (!tasksEnabled) {
// Wait 2 seconds to let system settle down
delay(2000);
// Enable task that will read values from the DHT sensor
tasksEnabled = true;
if (tempTaskHandle != NULL) {
vTaskResume(tempTaskHandle);
}
}
yield();
}
void initDisplay() {
display = new Display();
display->initTFT();
display->centerMsg("System Init...");
}

View File

@@ -0,0 +1,29 @@
#include "Display.h"
Display::Display() {
tft = new TFT_eSPI();
}
Display::~Display() {
delete tft;
}
void Display::initTFT() {
tft->init();
tft->fillScreen(TFT_BLACK);
tft->setRotation(1);
tft->setTextColor(TFT_WHITE, TFT_BLACK);
tft->setTextDatum(MC_DATUM);
tft->setFreeFont(&Orbitron_Light_24);
}
void Display::centerMsg(String text) {
tft->drawString(text, tft->width() / 2, 60);
}
void Display::tempUpdates(String temp, String hum, String status) {
tft->setTextPadding(tft->width());
tft->drawString(temp, tft->width() / 2, 40);
tft->drawString(hum, tft->width() / 2, 70);
tft->drawString(status, tft->width() / 2, 110);
}

View File

@@ -0,0 +1,20 @@
#ifndef Display_H_
#define Display_H_
#include <TFT_eSPI.h>
class Display {
private:
TFT_eSPI* tft;
public:
Display();
~Display();
void initTFT();
void centerMsg(String text);
void tempUpdates(String temp, String hum, String status);
};
#endif