//////////////////////////////////////////////////////////////////
// DHT22 vs. BME280 Test program for the ESP32 TTGO-T Display //
// Jordan Rubin 2020 //
// More at https://www.youtube.com/c/jordanrubin6502 //
// http://technocoma.blogspot.com //
//////////////////////////////////////////////////////////////////
// Provides a graphical output on the built in display of the TTGO for both
// Temperature and humidity giving a comparison of these two sensors in real
// time for experimentation.
// Directly related to the video https://youtu.be/y2-55LDZYKU
#include <Arduino.h>
#include <DHT.h>
#include <Adafruit_BME280.h>
#include <TFT_eSPI.h>
#include <SPI.h>
#define DHTPIN 17
#define DHTTYPE DHT22
#define BUTTON1PIN 35
#define BUTTON2PIN 0
#define SEALEVELPRESSURE_HPA (1013.25)
DHT dht(DHTPIN, DHTTYPE);
Adafruit_BME280 bme;
TFT_eSPI tft = TFT_eSPI();
TaskHandle_t Task1;
bool useFahrenheit = true; // Default to Fahrenheit
bool showTemp = true; // Default to Temp / Humidity
long lastDebounceButton1 = 0; // Holds Button1 last debounce
long lastDebounceButton2 = 0; // Holds Button2 last debounce
long debounceDelay = 200; // 200ms between re-polling
unsigned long delayTime;
//////////////////////////////////////////////////////////////////////////
//-FUNCTION-[toggleButton1]----------------------------------------------]
//////////////////////////////////////////////////////////////////////////
void toggleButton1() {
if ((millis() - lastDebounceButton1) > debounceDelay) {
if (useFahrenheit){useFahrenheit = false;}
else {useFahrenheit = true;}
lastDebounceButton1 = millis();
}
}
//-----------------------------------------------------------------------]
//////////////////////////////////////////////////////////////////////////
//-FUNCTION-[toggleButton2]----------------------------------------------]
//////////////////////////////////////////////////////////////////////////
//Function to execute when Button 2 is Pushed
void toggleButton2() {
if ((millis() - lastDebounceButton2) > debounceDelay) {
if (showTemp){showTemp = false;}
else {showTemp = true;}
lastDebounceButton2 = millis();
}
}
//-----------------------------------------------------------------------]
//////////////////////////////////////////////////////////////////////////
//-FUNCTION-[showScrn1]--------------------------------------------------]
//////////////////////////////////////////////////////////////////////////
void showScrn1() {
float t = dht.readTemperature(useFahrenheit);
float bt = bme.readTemperature();
float h = dht.readHumidity();
float bh = bme.readHumidity();
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_GREEN, TFT_BLACK);
tft.setCursor(0, 20);
tft.setFreeFont(&Orbitron_Light_24);
tft.println("DHT22");
tft.drawLine(0, 25, 250, 25, TFT_BLUE);
tft.setCursor(0, 50);
tft.print(t);
if (useFahrenheit){tft.print(F("f"));}
else {tft.print(F("c"));}
tft.setCursor(130, 50);
tft.print(h);tft.print(F("%"));
tft.setTextColor(TFT_RED, TFT_BLACK);
tft.setCursor(0, 90);
tft.println("BME280");
tft.drawLine(0, 95, 250, 95, TFT_BLUE);
tft.setCursor(0, 120);
tft.print(bt);
Serial.print("Temperature BME: "); Serial.println(bt);
Serial.print("Pressure BME: ");Serial.print(bme.readPressure() / 100.0F); Serial.println(" hpa");
Serial.print("Altitude BME: ");Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA)); Serial.println("m");
Serial.println("------------------------------\n");
if (useFahrenheit){tft.print(F("f"));}
else {tft.print(F("c"));}
tft.setCursor(130, 120);
tft.print(bh);tft.print(F("%"));
}
//-----------------------------------------------------------------------]
//////////////////////////////////////////////////////////////////////////
//-FUNCTION-[showScrn2]--------------------------------------------------]
//////////////////////////////////////////////////////////////////////////
void showScrn2() {
float t = dht.readTemperature(useFahrenheit);
float h = dht.readHumidity();
float hi = dht.computeHeatIndex(t, h, useFahrenheit);
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_RED, TFT_BLACK);
tft.setCursor(50, 30);
tft.setFreeFont(&Orbitron_Light_24);
tft.println("Heat Index");
tft.drawLine(0, 35, 250, 35, TFT_BLUE);
tft.setFreeFont(&Orbitron_Light_32);
tft.setTextColor(TFT_GREEN, TFT_BLACK);
tft.setCursor(60, 100);
tft.print(hi);
if (useFahrenheit){tft.print(F("f"));}
else {tft.print(F("c"));}
}
//-----------------------------------------------------------------------]
//////////////////////////////////////////////////////////////////////////
//-SYSTEM-[Task1code]----------------------------------------------------]
//////////////////////////////////////////////////////////////////////////
void Task1code( void * pvParameters ){
Serial.print("Task1 running on core ");
Serial.println(xPortGetCoreID());
int button1State = 0;
int button2State = 0;
for(;;){
button1State = digitalRead(BUTTON1PIN);
button2State = digitalRead(BUTTON2PIN);
if (button1State == LOW) {
toggleButton1();
}
// if (button2State == LOW) {
// toggleButton2();
// }
delay(10);
}
}
//-----------------------------------------------------------------------]
//////////////////////////////////////////////////////////////////////////
//-SYSTEM-[setup]--------------------------------------------------------]
//////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(115200);
if (! bme.begin(0x76, &Wire)) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
Serial.println("-- Default Test --");
Serial.println("normal mode, 16x oversampling for all, filter off,");
Serial.println("0.5ms standby period");
delayTime = 5000;
pinMode(BUTTON1PIN, INPUT);
pinMode(BUTTON2PIN, INPUT);
dht.begin();
tft.begin();
tft.setRotation(1); //Landscape
xTaskCreatePinnedToCore(Task1code,"Task1", 10000, NULL, 2, &Task1, 0);
}
//-----------------------------------------------------------------------]
//////////////////////////////////////////////////////////////////////////
//-SYSTEM-[loop]---------------------------------------------------------]
//////////////////////////////////////////////////////////////////////////
void loop() {
delay(1000); //Required by this device to function properly
if (showTemp){showScrn1();} // Temp Humidity
else {showScrn2();} // Heat Index
}
//-----------------------------------------------------------------------]
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:ttgo-lora32-v1]
platform = espressif32
board = ttgo-lora32-v1
framework = arduino
monitor_speed = 115200
lib_deps =
DHT sensor library
TFT_eSPI
adafruit/Adafruit Unified Sensor@^1.1.4
adafruit/Adafruit BME280 Library@^2.1.1
build_flags =
-DUSER_SETUP_LOADED=1
-DST7789_DRIVER=1
-DTFT_WIDTH=135
-DTFT_HEIGHT=240
-DCGRAM_OFFSET=1
-DTFT_MOSI=19
-DTFT_SCLK=18
-DTFT_CS=5
-DTFT_DC=16
-DTFT_RST=23
-DTFT_BL=4
-DTFT_BACKLIGHT_ON=HIGH
-DLOAD_GLCD=1
-DLOAD_FONT2=1
-DLOAD_FONT4=1
-DLOAD_FONT6=1
-DLOAD_FONT7=1
-DLOAD_FONT8=1
-DLOAD_GFXFF=1
-DSMOOTH_FONT=1
-DSPI_FREQUENCY=40000000
-DSPI_READ_FREQUENCY=6000000
No comments:
Post a Comment