// https://programmersqtcpp.blogspot.com/2022/07/termostato-con-dht22.html
#include <LiquidCrystal.h>
#include "dht.h"
#define DHTPIN 6
dht dht22;
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
const byte g_relayPin = 13;
int16_t g_temperature;
int16_t g_setpoint;
int16_t g_isteresi = 3 * 10;
uint32_t g_timer2sec;
bool checkThermostate(bool state) {
switch (state) {
case LOW:
if (g_temperature <= g_setpoint - g_isteresi) {
state = HIGH;
}
break;
case HIGH:
if (g_temperature > g_setpoint) {
state = LOW;
}
break;
} // end switch (thermostateState)
return state;
} // end checkThermostate()
void setup()
{
Serial.begin(115200);
lcd.begin(16, 2);
pinMode(g_relayPin, OUTPUT);
} // end void setup()
constexpr char *onoff[] = { "OFF", "ON " };
char temperatureBuffer[7];
// struttura di supporto
struct Setpoint {
uint16_t _old;
uint16_t _new = 1024; // new != old
};
Setpoint mySetpoint;
void loop() {
// Non possiamo leggere il sensore ad intervalli
// di tempo inferiori a 2 secondi.
if (millis() - g_timer2sec >= 2000) {
g_timer2sec = millis();
int chk = dht22.read22(DHTPIN);
if (chk == DHTLIB_OK) {
dtostrf(dht22.temperature, 6, 1, temperatureBuffer);
lcd.setCursor(0, 1);
lcd.print(temperatureBuffer);
// da floart x 10 a int16_t
g_temperature = dht22.temperature * 10;
// chiama la funzione termostato
bool thState = checkThermostate(digitalRead(g_relayPin));
digitalWrite(g_relayPin, thState);
// visualizza ON/OFF sul display
lcd.setCursor(10, 0);
lcd.print(onoff[thState]);
} else {
// qui la gestione degli errori relativi al DHT22
}
}
// usa struttura di supporto
mySetpoint._old = analogRead(A1);
if (mySetpoint._old != mySetpoint._new) {
mySetpoint._new = mySetpoint._old;
float stpf = mySetpoint._new / 13.3;
g_setpoint = stpf * 10; // da float x 10 a uint16_t
dtostrf(stpf, 6, 1, temperatureBuffer);
lcd.setCursor(0, 0);
lcd.print(temperatureBuffer);
}
} // end void loop()
nano:12
nano:11
nano:10
nano:9
nano:8
nano:7
nano:6
nano:5
nano:4
nano:3
nano:2
nano:GND.2
nano:RESET.2
nano:0
nano:1
nano:13
nano:3.3V
nano:AREF
nano:A0
nano:A1
nano:A2
nano:A3
nano:A4
nano:A5
nano:A6
nano:A7
nano:5V
nano:RESET
nano:GND.1
nano:VIN
nano:12.2
nano:5V.2
nano:13.2
nano:11.2
nano:RESET.3
nano:GND.3
lcd:VSS
lcd:VDD
lcd:V0
lcd:RS
lcd:RW
lcd:E
lcd:D0
lcd:D1
lcd:D2
lcd:D3
lcd:D4
lcd:D5
lcd:D6
lcd:D7
lcd:A
lcd:K
r1:1
r1:2
pot2:GND
pot2:SIG
pot2:VCC
relay1:NO2
relay1:NC2
relay1:P2
relay1:COIL2
relay1:NO1
relay1:NC1
relay1:P1
relay1:COIL1
led1:A
led1:C
dht1:VCC
dht1:SDA
dht1:NC
dht1:GND