/**
ESP32 + DHT22 Example for Wokwi
https://wokwi.com/arduino/projects/322410731508073042
*/
#include "DHTesp.h"
#define ISITICI 2
#define ISITICI_ONOFF 4
const int DHT_PIN = 15;
bool bHeaterContAuto = true;
int potpin = 33; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
// the number of the LED pin
const int ledPin = 5; // 16 corresponds to GPIO16
// setting PWM properties
const int freq = 5000;
const int ledChannel = 0;
const int resolution = 8;
int potPinDimmer = 32; // analog pin used to connect the potentiometer
int valDimmer; // variable to read the value from the analog pin
DHTesp dhtSensor;
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(ISITICI, OUTPUT);
pinMode(ISITICI_ONOFF, INPUT_PULLUP);
// configure LED PWM functionalitites
ledcSetup(ledChannel, freq, resolution);
// attach the channel to the GPIO to be controlled
ledcAttachPin(ledPin, ledChannel);
}
void heaterControl(TempAndHumidity data)
{
if(bHeaterContAuto)
{
if(data.temperature < 20.0)
{
digitalWrite(ISITICI, HIGH);
}
else if(data.temperature > 24.0)
{
digitalWrite(ISITICI, LOW);
}
}
else
{
if(digitalRead(ISITICI_ONOFF) == LOW)
{
if(digitalRead(ISITICI) == LOW)
{
digitalWrite(ISITICI, HIGH);
delay(1000);
}
else
{
digitalWrite(ISITICI, LOW);
delay(1000);
}
}
}
}
void analogControl()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)4095
Serial.println("Analog : " + String(val));
val = map(val, 0, 4095, 0, 180); // scale it to use it with the servo (value between 0 and 180)
Serial.println("Analog map : " + String(val));
}
void dimmerLedControl()
{
// increase the LED brightness
/*for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){
// changing the LED brightness with PWM
ledcWrite(ledChannel, dutyCycle);
delay(15);
}
// decrease the LED brightness
for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){
// changing the LED brightness with PWM
ledcWrite(ledChannel, dutyCycle);
delay(15);
}*/
valDimmer = analogRead(potPinDimmer); // reads the value of the potentiometer (value between 0 and 1023)4095
Serial.println("Analog Dimmer : " + String(valDimmer));
valDimmer = map(valDimmer, 0, 4095, 0, 255); // scale it to use it with the servo (value between 0 and 180)
Serial.println("Analog map Dimmer : " + String(valDimmer));
// changing the LED brightness with PWM
ledcWrite(ledChannel, valDimmer);
}
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
//Serial.println("Temp: " + String(data.temperature, 2) + "°C");
//Serial.println("Humidity: " + String(data.humidity, 1) + "%");
//Serial.println("---");
heaterControl(data);
analogControl();
dimmerLedControl();
delay(100);
}