/**
ESP32 + DHT22 Example for Wokwi
https://wokwi.com/arduino/projects/322410731508073042
*/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "DHTesp.h"
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
const int rgbPins[] = { 11, 10, 9};
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define DHT22_PIN PB1
const int DHT_PIN = 15;
DHTesp dhtSensor;
void setup() {
Serial.begin(115200);
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.display();
delay(1000); // Pause for 2 seconds
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(WHITE); // Draw white text
display.cp437(true);
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("---");
// Clear the buffer
display.clearDisplay();
display.setCursor(1,1);
display.print("Temp: " + String(data.temperature, 2) + "C");
display.display();
display.setCursor(1,10);
display.print("Humidity: " + String(data.humidity, 1) + "%");
display.display();
display.setCursor(1,20);
display.print("---");
display.display();
// Text über Zeiger ausgebee
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(2, OUTPUT);
pinMode(4, OUTPUT);
pinMode(25, OUTPUT);
}
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
if(data.humidity < 40 || data.humidity > 50){
digitalWrite(25, HIGH); // turn the LED on (HIGH is the voltage level) // wait for a second
}
else{
digitalWrite(25, LOW);
}
if(data.temperature < 18 ){
digitalWrite(2, HIGH); // turn the LED on (HIGH is the voltage level) // wait for a second
}
else{
digitalWrite(2, LOW);
}
if(data.temperature > 22 ){
digitalWrite(4, HIGH); // turn the LED on (HIGH is the voltage level) // wait for a second
}
else{
digitalWrite(4, LOW);
}
unsigned long currentMillis = millis();
// The buttons do not need any debounce in this specific sketch.
// They can be pressed long or short or multiple times.
if( digitalRead(buttonPins[0]) == LOW) // Red button, active LOW
newStatus = 100.0;
if( digitalRead(buttonPins[1]) == LOW) // Green button, active LOW
newStatus = 50.0;
if( digitalRead(buttonPins[2]) == LOW) // Blue button, active LOW
newStatus = 0.0;
// Millis timer to set the update rate of the PWM for RGB led,
// and to provide a constant interval to calculate the pulse.
if( currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
// Low pass filter.
// Lower percentage for slower filter, higher percentage for faster filter
const float pct = 1.8; // percentage of new value added to filter
filteredStatus = (( 1.0 - (pct / 100.0)) * filteredStatus) + (pct / 100.0 * newStatus);
float r, g, b; // values 0...1
float f; // frequency of the pulse in Hz
// Convert status (0...100) to hue and frequency
// 0 = blue = 0.5 Hz
// 50 = green = 1.0 Hz
// 100 = red = 3.0 Hz
if( filteredStatus < 50.0)
{
r = 0.0;
g = filteredStatus / 50.0;
b = 1.0 - g;
f = 0.5 + (g / 2); // 0.5 to 1.0 for lower range
}
else
{
r = (filteredStatus - 50.0) / 50.0;
g = 1.0 - r;
b = 0.0;
f = 1.0 + (2 * r); // 1.0 to 3.0 for higher range
}
// The standard deviation is:
// y = e ^ ( -0.5 * x * x )
// This sketch uses:
// y = expf ( -s * squaref ( x ) ) // s = steepness, -50 is normal, -150 is steep
// The 'y' will be used for the amplitude of the r, g, b values.
float y = expf( -50.0 * squaref( x));
// Increment 'x' for the time axis.
// Keep 'x' between -0.5 and 0.5
x += f * float( interval) / 1000.0; // divide interval by 1000 because it is in milliseconds
if( x >= 0.5)
x -= 1.0;
// Multiply each color by the amplitude of the pulse.
// The minimal value is set to 1, because turning the led completely off
// didn't look good.
int pwmR = (int) round( 1.0 + (r * y * 254.0));
int pwmG = (int) round( 1.0 + (g * y * 254.0));
int pwmB = (int) round( 1.0 + (b * y * 254.0));
delay(10);
}