Adafruit sensor library:
Hi, Iām trying to āportā your amazing code to Wemos d1 mini Lite, oled shield, relay shield and sht30 shield v2.0.0 ā¦
this is your modified code:
/****************************************************************************
* HVAC control for a "2-pipe" radiator system.
*
* Compares readings from a DHT11 temperature sensor with desired temperature
* from the Blynk application, and runs the fan unit as necessary to heat or
* cool. Hysteresis levels for both Summer and Winter are independently
* adjustable from 2 to 6 degrees. The temperature sensor readings can be
* offset up or down by up to 5 degrees. All settings are saved to EEPROM, and
* automatically reloaded on startup.
*
* "Home" setting is triggered by IFTTT iOS location channel, and results in an
* action on the Maker channel. The Maker channel parameters are as follows:
* URL: http://blynk-cloud.com:8080/YOUR_TOKEN/pin/V31 // no http to post on forum
* Method: PUT
* Content Type: application/json
* Body: ["1"]
* "Away" mode requires an identical IFTTT recipe, but with
* Body: ["0"]
*
* Added a press-and-hold requirement to enter the settings menu, as well as
* a Menu timeout and reset after a period of inactivity.
*
* Added a manual override to turn on the system, independent of other factors
*
* WiFi connection is now simplified with Tapzu's WiFiManager. Wifi automatically
* reconnects to last working credentials. If the last SSID is unavailable, it
* creates an access point ("BlynkAutoConnect"). Connect any wifi device to the
* access point, and a captive portal pop up to receive new wifi credentials.
*
* The hardware is minimal: an ESP-01, a single relay on GPIO 0, and a DHT11
* temperature sensor on GPIO 2.
*
*****************************************************************************
*Thermostat tab
Desired Temperature gauge: Pin V1, range 50 - 80 (I'm in the US, so using Farenheit),
Push update frequency
Actual Temperature gauge: Pin V0, range 50 - 80, 2 second update frequency
Set Temperature slider: Pin V3, range 50 - 80, send-on-release set to OFF
Settings tab
Unnamed labeled value display: Pin V10, default range of 0-1023, default label of /pin/ ,
update frequency 500ms
Settings button: Pin V4, PUSH mode, blank on and off labels
Modify button: Pin V5, PUSH mode, blank on and off labels
On LED: pin V7
Home LED pin v29
For aesthetic reasons, many of the widget labels have been either blanked out,
by entering blank spaces in the label name field, or center justified, by adding
leading blank spaces before typing the name.
Linking the temperature slider to the desired temperature gauge is not done in the app.
It's done in the code. It's this section:
//Match temp gauge to slider in Blynk app
BLYNK_WRITE(V3){
TempDes = param.asInt();
Blynk.virtualWrite(V1,TempDes);
}
The BLYNK_WRITE function receives the new desired temperature value from the slider
(on pin V3). We transfer that value to the variable TempDes, so that we can continue to use
it in our code. Then, we immediately send that value back to the Desired Temperature gauge
(on pin V1), with the Blynk.virtualWrite command.
*/
#include <Wire.h>
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <SimpleTimer.h>
//#include "DHT.h"
#include <WEMOS_SHT3X.h>
#include <EEPROM.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SPI.h>
//#include <math.h>
#define OLED_RESET 0 // GPIO0
Adafruit_SSD1306 display(OLED_RESET);
#define UpdateFrequency 6000 //How often a new temperature will be read
#define MenuTimeOut 15000
#define RelayPin D5
SHT3X sht30(0x45);
//DHT dht(0,DHT11); //Initialize the sensor. Use pin 0. Sensor type is DHT11.
// Timer for temperature updates
SimpleTimer timer;
SimpleTimer quickTimer;
//WiFi and Blynk connection variables
char auth[] = "YourAuthToken"; // Blynk token "YourAuthToken"
//Thermostat variables
//int TempDes = 70; //Desired temperature setting
//int PreviousTempDes;
//int TempAct = 70; //Actual temperature, as measured by the DHT11 sensor
//int BadRead = 0; //Counts consecutive failed readings of the DHT11 sensor
//int LastRead = 70;
int TempDes = 21; //Desired temperature setting
int PreviousTempDes;
int TempAct = 22; //Actual temperature, as measured by the DHT11 sensor
int BadRead = 0; //Counts consecutive failed readings of the DHT11 sensor
int LastRead = 22;
// Preference variables
int Hysteresis_W = 2; //Summer and Winter hysteresis levels
int Hysteresis_S = 2;
int TempCorrection = 0; //Used to adjust readings, if the sensor needs calibration
// Current condition variables
boolean Winter = true;
boolean Home = true;
boolean ManualRun = false; // used for manual override of thermostat algorithm
int MenuItem = 0; //Settings menu selection variable
long buttonRelease; //time button was released
long buttonPress; // time button was last pressed
boolean ButtonDown = false; //Settings button state (pressed = true)
boolean FanState = 0; // is the fan on or off?
float ReadC;
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
Serial.begin(115200);
delay(10);
//Creates an AP if no wifi credentials are stored
WiFiManager wifi;
wifi.autoConnect("ThermoX");
Blynk.config(auth);
// dht.begin(); //Start temperature sensor
//delay(1500);
//Initialize the fan relay. Mine is "off" when the relay is set LOW.
pinMode(RelayPin,OUTPUT);
digitalWrite(RelayPin,HIGH);
//Load any saved settings from the EEPROM
EEPROM.begin(20);
Serial.println(F("STARTUP : LOADING SETTINGS FROM MEMORY"));
Serial.println(F(""));
GetPresets();
PreviousTempDes = TempDes;
MenuReset();
timer.setInterval(UpdateFrequency, TempUpdate); // Update temp reading and relay state
quickTimer.setInterval(100, ButtonCheck);
}
// Main loop
void loop() {
Blynk.run();
timer.run();
quickTimer.run();
}
// Checks for long press condition on SETTINGS button
void ButtonCheck(){
if (ButtonDown){
if (millis() - buttonPress > 1000){ // Was it a long press?
if (MenuItem == 0){
NextMenuItem(); // Enter the SETTINGS menu
}
else MenuReset(); // Exit the SETTINGS menu
ButtonDown = false; // Prevent repeat triggering
}
}
}
// This is the decision algorithm for turning the HVAC on and off
void TempUpdate (){
OtherUpdates(); //Refeshes dashboard information
delay(500);
//float ReadF = dht.readTemperature(true); //Get a new reading from the temp sensor
//float ReadF = SHT3X sht30(0x45);
//float ReadC = (sht30.cTemp);
int ReadC = (sht30.cTemp);
if (isnan(ReadC)) {
Serial.println(F("Failed to read from SHT sensor!"));
BadRead++;
return;
} else {
// Clear the buffer.
display.clearDisplay();
display.setTextSize(1);
display.setCursor(35, 10); //X,Y
display.setTextColor(WHITE);
display.setTextSize(3);
display.setCursor(55, 10); //X,Y
// display.println(sht30.cTemp);
display.println(ReadC);
Serial.print("cTemp is ");
Serial.println(sht30.cTemp);
Serial.print("ReadC is ");
Serial.println(ReadC);
}
/*
//To compensate for some instability in the SHT30, the corrected temperature is
//averaged with previous read, and any change is limited to 1 degree at a time.
else {
int TempAvg = (int((ReadF + LastRead + (2 * TempCorrection))/2));
if (TempAvg >= TempAct + 1){
TempAct = TempAct + 1;
}
if (TempAvg <= TempAct - 1){
TempAct = TempAct -1;
}
LastRead = int(ReadF + .5);
BadRead = 0;
}
*/
// Blynk.virtualWrite(V0,TempAct); //Report the corrected temperature in app
// Serial.print(F("Actual temperature: "));
// Serial.println(TempAct);
Blynk.virtualWrite(V0,ReadC); //Report the corrected temperature in app
Serial.print(F("Actual temperature: "));
Serial.println(ReadC);
// Decision algorithm for running HVAC
if (!ManualRun){
if (Home){
if (Winter){
//If I'm home, it's Winter, and the temp is too low, turn the relay ON
if (ReadC < TempDes){
FanState = 1;
Fan();
}
//Turn it off when the space is heated to the desired temp + a few degrees
else if (ReadC >= (TempDes + Hysteresis_W) && FanState) {
FanState = 0;
Fan();
}
}
else if (!Winter){
//If I'm home, it's Summer, and the temp is too high, turn the relay ON
if (ReadC > TempDes){
FanState = 1;
Fan();
}
//Turn it off when the space is cooled to the desired temp - a few degrees
else if (ReadC <= (TempDes - Hysteresis_S) && FanState){
FanState = 0;
Fan();
}
}
}
//If I'm not home, turn the relay OFF
else {
FanState = 0;
Fan();
}
}
}
//Match temp gauge to slider in Blynk app
BLYNK_WRITE(V3){
TempDes = param.asInt();
Blynk.virtualWrite(V1,TempDes);
}
//Get location (home or away) from the IFTTT iOS location and Maker channels
BLYNK_WRITE(V31)
{
if (param.asInt()){
Home = true;
Blynk.virtualWrite(V29,1023);
}
else{
Home = false;
Blynk.virtualWrite(V29,0);
}
}
// Dashboard SETTINGS button. Press-and-hold to enter menu. Short press for next item.
BLYNK_WRITE(V4) {
// Check for a button press
if (param.asInt()){
buttonPress = millis();
ButtonDown = true;
}
// check for button release
else {
buttonRelease = millis();
ButtonDown = false;
if (buttonRelease - buttonPress < 1000){ // It was a short press.
if (MenuItem == 0){
MenuReset(); // Remind user to hold 2 seconds to enter menu
}
else NextMenuItem(); // Advance to next menu item
}
}
}
//Cycles through the Settings Menu in the Labeled Value widget
void NextMenuItem(){
String Response = "";
MenuItem += 1;
if (MenuItem > 7){
MenuItem = 1;
}
switch(MenuItem){
case 1:
if (ManualRun){
Response += "END MANUAL RUN?";
}
else{
Response += "RUN MANUALLY?";
}
break;
case 2:
if (Home){
Response += "LOCATION : HOME";
}
else Response += "LOCATION : AWAY";
break;
case 3:
if (Winter){
Response += "MODE : WINTER";
}
else Response += "MODE : SUMMER";
break;
case 4:
if (Winter){
Response += "WINTER HYSTERESIS: ";
Response += Hysteresis_W;
Response += " DEGREES";
}
else{
Response += "SUMMER HYSTERESIS: ";
Response += Hysteresis_S;
Response += " DEGREES";
}
break;
case 5:
Response += "TEMP CORRECTION: ";
Response += TempCorrection;
Response += " DEGREES";
break;
case 6:
Response += "CLEAR WIFI SETTINGS?";
break;
case 7:
Response += "RESET ALL DEFAULTS?";
break;
}
Blynk.virtualWrite(V10,Response);
}
//Dashboard MODIFY button. Executes change of selected menu item
BLYNK_WRITE(V5){
String Response = "";
buttonRelease = millis(); //Resets menu timeout for inactivity
if ((MenuItem > 0) && (param.asInt())){
switch(MenuItem){
//Forced on
case 1:
if (ManualRun){
ManualRun = false;
FanState = 0;
Fan();
Response += "MANUAL RUNNING: OFF";
}
else{
ManualRun = true;
FanState = 1;
Fan();
Response += "MANUAL RUNNING: ON";
}
break;
//Change location manually
case 2:
if (Home){
Home = false;
Response += "LOCATION : AWAY";
}
else {
Home = true;
Response += "LOCATION : HOME";
}
break;
//Change season
case 3:
if (Winter){
Response += "MODE : SUMMER";
Winter = false;
EEPROM.write(4,0);
EEPROM.commit();
}
else {
Response += "MODE : WINTER";
Winter = true;
EEPROM.write(4,1);
EEPROM.commit();
}
break;
//Change hysteresis level of currently selected season
case 4:
if (Winter){
Hysteresis_W += 1;
if (Hysteresis_W > 6){
Hysteresis_W = 1;
}
EEPROM.write(1,(Hysteresis_W));
EEPROM.commit();
Response += "WINTER HYSTERESIS: ";
Response += Hysteresis_W;
Response += " DEGREES";
}
else{
Hysteresis_S += 1;
if (Hysteresis_S > 6){
Hysteresis_S = 1;
}
EEPROM.write(2,(Hysteresis_S));
EEPROM.commit();
Response += "SUMMER HYSTERESIS: ";
Response += Hysteresis_S;
Response += " DEGREES";
}
break;
case 5:
TempCorrection +=1;
if (TempCorrection > 5){
TempCorrection = -5;
}
EEPROM.write(0,(TempCorrection + 5));
EEPROM.commit();
Response += "TEMPERATURE CORRECTION: ";
Response += TempCorrection;
Response += " DEGREES";
break;
//Clear stored SSID and password
case 6:
Response += "Erasing WiFi credentials and restarting!";
WiFi.begin("FakeSSID","FakePW"); //replace current WiFi credentials with fake ones
delay(1000);
ESP.restart();
break;
//Clear current temperature settings
case 7:
Response += "All settings reset to default!";
Winter = true;
Hysteresis_W = 2;
Hysteresis_S = 2;
break;
}
Blynk.virtualWrite(V10, Response);
}
}
// Turn the HVAC on or off
void Fan(){
digitalWrite(RelayPin,!FanState);
Blynk.virtualWrite(V7,FanState * 1023);// fan "ON" LED on dashboard
Serial.print(F("Fan state: "));
Serial.println(FanState);
}
// Reset the Menu at startup or after timing out from inactivity
void MenuReset(){
MenuItem = 0;
Blynk.virtualWrite(V10, String("*****************************"));
Blynk.virtualWrite(V10, String("HOLD 2 SEC TO ENTER/EXIT MENU"));
}
// Updates dashboard information on the Blynk app
void OtherUpdates(){
Blynk.virtualWrite(V1,TempDes); //Update desired temp on the dashboard
Blynk.virtualWrite(V29,Home * 1023); // Update "home" LED on dashboard
//Reset the Settings Menu if there's been no activity for a while
if (MenuItem > 0 && (millis() - buttonRelease > MenuTimeOut)){
MenuReset();
}
// Notify when the temperature sensor fails repeatedly, and turn off the fan.
if (BadRead > 10){
Blynk.virtualWrite(V10, String("<<< SENSOR MALFUNCTION >>>"));
BadRead = 0;
if (!ManualRun){ //Manual mode supersedes a malfunction condition
FanState = 0;
Fan();
}
}
if (TempDes != PreviousTempDes){ //update the EEPROM if desired temperature had changed.
EEPROM.write(3,TempDes);
EEPROM.commit();
Serial.print(F("New desired temperature saved: "));
Serial.println(TempDes);
PreviousTempDes = TempDes;
}
}
//Retrieves saved values from EEPROM
void GetPresets(){
TempCorrection = EEPROM.read(0);
if ((TempCorrection < 0) || (TempCorrection > 10)){
TempCorrection = 0;
Serial.println(F("No saved temperature correction."));
}
else{
TempCorrection -= 5; // 5 was added at EEPROM save to account for negative values
Serial.print(F("Temperature correction: "));
Serial.print(TempCorrection);
Serial.println(F(" degrees."));
}
Winter = EEPROM.read(4);
Hysteresis_W = EEPROM.read(1);
Hysteresis_S = EEPROM.read(2);
if ((Hysteresis_W < 2) || (Hysteresis_W > 6)){
Hysteresis_W = 2;
}
if ((Hysteresis_S < 2) || (Hysteresis_S > 6)){
Hysteresis_S = 2;
}
if (Winter){
Serial.println(F("Season setting: Winter / heating"));
Serial.print(F("Winter hysteresis: "));
Serial.print(Hysteresis_W);
Serial.println(F(" degrees."));
}
else {
Serial.println(F("Season setting: Summer / cooling"));
Serial.print(F("Summer hysteresis: "));
Serial.print(Hysteresis_S);
Serial.println(F(" degrees."));
}
TempDes = EEPROM.read(3);
if ((TempDes < 10) || (TempDes > 27)){
TempDes = 21;
Serial.println(F("No saved temperature setting."));
}
else {
Serial.print(F("Desired temperature: "));
Serial.print(TempDes);
Serial.println(F(" degrees."));
}
Serial.println("");
}
but temperature value is 0
also I canāt display nothing to oled local displayā¦
ideas?
best regards
@cepics Aside from simply posting a bunch of āmodifiedā codeā¦ apparently without reading the Welcome Topic and forum policies (I have fixed part of that at least )ā¦
Perhaps you can say exactly WHAT you modified, how you wired it up and to what?.. AKA more detailsā¦ in order to assist someone in magically knowing what you might have done to break it
Are you getting āFailed to read from SHT sensor!ā messages, if so, are you certain that your SHT30 is at bus location 0x45 ?
Have you confirmed this with an I2C scanner sketch?
You seem to be missing a display.display();
command from the end of this block of code:
display.clearDisplay();
display.setTextSize(1);
display.setCursor(35, 10); //X,Y
display.setTextColor(WHITE);
display.setTextSize(3);
display.setCursor(55, 10); //X,Y
// display.println(sht30.cTemp);
display.println(ReadC);
Pete.
Hello.
Iām a beginner and I want to ask what changes to make in the code. My setup is esp-wroom 32s with dht22 and a single relay board. Thanks a lot.
This project has been adapted to just about every ESP8266 board, but Iāve not heard of anyone using it on an ESP32. As a self-described ābeginner,ā Iād suggest starting with proven hardware.
Imagine for a moment that you posted instructions for changing the alternator in a Honda Accord to an automotive forum. A reader follows your post with ācan you tell me how to do that on a BMW 5-series?ā Whatās your response? In theory, itās the same process, but the differences are not trivial.
Youāre right, Iām a little excited and tired. I will buy esp8266. Comgratulations for your beautiful project. Thanks a lot.
Feature Updates!
My ever-evolving ESP8266 thermostat has a few new tricks up its sleeve:
-
True Alexa Integration: You can now use natural language to ask Alexa to do things like āset (DeviceName) to 70 degrees,ā or āturn (DeviceName) UP or DOWN,ā which augments the setting by 2 degrees. āAlexa, turn (DeviceName) ONā will activate a pulse cycle, and āAlexa, turn (DeviceName) OFFā manually halts operation. Because Blynk GPS triggering is still not implemented for iOS, I do still use IFTTT, but only for geolocation. However, if you have an Android device, Iād encourage you to modify the code for IFTTT-free operation.
-
Minimum / Maximum temperature limits: I realized that when Iām away from home, there are limits to the temperature extremes that plants and pets will tolerate, so I added minimum and maximum settings. Now, even if youāre not home, the HVAC will keep things within a reasonable range.
/****************************************************************************
* ThermoX v0.20.0 HVAC Thermostat
*
* Compares ambient and desired temperatures, and runs heat or A/C as needed.
* Hysteresis levels for both Summer and Winter are independently adjustable.
* Sensor can be corrected +/- 5 degrees. All user preferences are saved, and
* reloaded on startup.
*
* HOME setting can be triggered in IFTTT by your cell phone location channel,
* causing an action on the Webhooks channel. Webhooks parameters are as follows:
* URL: http://blynk-cloud.com:8080/YOUR_TOKEN/pin/V31
* Method: PUT
* Content Type: application/json
* Body: ["1"]
* Make an identical IFTTT recipe for AWAY but use ["0"] for the body parmeter.
*
* Color coded DESIRED TEMPERATURE widget: red/blue/green for heat/cool/off
* modes, respectively.
*
* PERCEIVED TEMPERATURE mode augments actual temperature when Summer humidity is high.
*
* Added 15 minutes ON "pulse" mode.
*
* New minimum and maximum temperature settings override "away" mode (my plants were
* dying). Now, even in AWAY mode, HVAC will come on if min/max limits are exceded.
*
* Added native Alexa control (HUE emulation).
* - "Turn ON" activates deactivates system halt, and runs pulse mode.
* - "Turn OFF" activates cancels pulse, or activates system halt if no pulse was running.
* - "Turn UP/DOWN" chages temperature 2 degrees.
* - "Set ThermoX to ##" sets a new desired temperature
*
* An independent "Alexa, set temperature to ##" changes desired temperature setting.
*
* Automatically reconnects to last working wifi. If unavailable, it creates an access
* point ("ThermoX") to which you can connect and share locl wifi credentials.
*
* Use any ESP8266, a relay on GPIO 0, and DHT11 temperature sensor on GPIO 2.
* OTA updates are only possible on devices with >512K memory.
*
*****************************************************************************
*/
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <BlynkSimpleEsp8266.h> //https://github.com/blynkkk/blynk-library
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include "DHT.h" //https://github.com/adafruit/DHT-sensor-library
#include <WiFiUdp.h>
#include <EEPROM.h>
#include <ArduinoOTA.h>
#include <Espalexa.h> //https://github.com/Aircoookie/Espalexa
#define UpdateFrequency 8000 //How often a new temperature will be read
#define MenuTimeOut 10000 //Menu timeout from inactivity
#define LongPress 650 //How long SETTINGS button needs to be pressed to enter menu
#define RelayPin 2
#define OFF 0 // These just make the code easier to read
#define ON 1
//WiFi and Blynk connection variables
String myHostname = "ThermoX";
char auth[] = "Your_Blynk_Token"; // Blynk token "YourAuthToken"
//Set up as a native Alexa device (Hue emulation)
char Device1[] = "ThermoX"; // ON/OFF switch name in the Alexa app
EspalexaDevice* espalexaPointer;
Espalexa espalexa;
// Blynk color palette
const String BLYNK_BLUE = "#04C0F8";
const String BLYNK_RED = "#D3435C";
const String BLYNK_GREEN = "#23C48E";
DHT dht(0,DHT11); //Initialize the sensor. Use pin 0. Sensor type is DHT11.
// Timer for temperature updates
BlynkTimer timer;
//Thermostat variables
int TempDes = 70; //Desired temperature setting
int PreviousTempDes;
int TempAct = 70; //Actual temperature, as measured by the DHT11 sensor
int BadRead = 0; //Counts consecutive failed readings of the DHT11 sensor
float LastRead = 70; // Previous temperature reading
int Humidity = 50;
int TempMin = 58; // Minimum allowable temperature, even if in "away" mode
int TempMax = 90; // Maximum allowable temperature, even if in "away" mode
// Preference variables
int Hysteresis_W = 2; //Summer and Winter hysteresis levels
int Hysteresis_S = 2;
int TempCorrection = 0; //Used to adjust readings, if the sensor needs calibration
boolean UsePerceivedTemp = false; // Use humidity-adjusted perceived temperature, instead of actual temperature
long PulseTime = 15 * 60 * 1000; // Amount of time for a "pulse" manual run of the system (15 minutes)
// Current condition variables
boolean Winter = true;
boolean Home = true;
boolean ManualRun = false; // used to run fan, overriding thermostat algorithm
boolean ManualStop = false; // used to stop fan, overriding thermostat algorithm
int MenuItem = 0; // Settings menu selection variable
boolean ButtonPressed = false;// Settings button state
boolean LongHold = false; // Flag showoing a long hold detected on the SETTINGS button
int ButtonTimer; // Timer for detecting long press of Settings button
String Response = ""; // Text output to SETTINGS value widget
boolean FanState = OFF; // Is the fan on or off?
int MenuTimer; // Timer for resetting SETTINGS menu after a timeout has elapsed
void setup() {
// Create an access point if no wifi credentials are stored
WiFi.hostname(myHostname);
WiFiManager wifi;
wifi.autoConnect("ThermoX");
Blynk.config(auth);
dht.begin(); //Start temperature sensor and wait for initialization
delay(1500);
//Initialize the fan relay. Mine is "off" when the relay is set HIGH.
pinMode(RelayPin,OUTPUT);
digitalWrite(RelayPin,HIGH);
Serial.begin(115200);
//Load any saved settings from the EEPROM
EEPROM.begin(20);
GetPresets();
PreviousTempDes = TempDes;
MenuReset();
ArduinoOTA.begin();
// Espalexa initialization. Parameters: (device name, callback function, device type, initial value)
espalexaPointer = new EspalexaDevice(Device1, AlexaCommands, EspalexaDeviceType::dimmable, TempDes * 2.55);
espalexa.addDevice(espalexaPointer);
espalexa.begin();
timer.setInterval(UpdateFrequency, TempUpdate); // Update temp reading and relay state
timer.setInterval(30000L, OtherUpdates); // Refreshes non-urgent dashboard info
}
void loop() {
Blynk.run();
timer.run();
ArduinoOTA.handle();
espalexa.loop();
}
//*********************** Thermostat Functions **********************************
// This is the decision algorithm for turning the HVAC on and off
void TempUpdate (){
float ReadF = dht.readTemperature(true); //Get a new reading from the temp sensor
if (isnan(ReadF)) {
BadRead++;
return;
}
// Use perceived temperature instead of actual temperature for Summer cooling
if(UsePerceivedTemp == true && !Winter && ReadF > 70){
// Because perceived temp swings can be large, augment by only a fraction of
// a degree per read. Changes are slowed, and more samples inform the average.
if(ReadF > LastRead + 0.5){
ReadF = LastRead + 0.5;
}
else if(ReadF < LastRead - 0.5){
ReadF = LastRead - 0.5;
}
// Simplified "feels like" temperature formula
ReadF = ((Humidity * .02 * (ReadF - 70)) + ReadF);
}
//To compensate for the DH11's inaccuracy, the temperature is averaged
//withprevious read, and any change is limited to 1 degree at a time.
int TempAvg = int((ReadF + LastRead + (2 * TempCorrection))/2);
if (TempAvg > TempAct){
TempAct += 1;
}
else if (TempAvg < TempAct){
TempAct -= 1;
}
LastRead = ReadF;
BadRead = 0; // Reset counter for failed sensor reads
Blynk.virtualWrite(V0,TempAct); //Report the corrected temperature in app
// Decision algorithm for running HVAC
if (!ManualRun && !ManualStop){ // Make sure it's not in one of the manual modes
// If I'm home, run the algorithm
if (Home){
if (Winter){
//If I'm home, it's Winter, and the temp is too low, turn the relay ON
if (TempAct < TempDes){
Fan(ON);
}
//Turn it off when the space is heated to the desired temp + a few degrees
else if (TempAct >= (TempDes + Hysteresis_W)) {
Fan(OFF);
}
}
else if (!Winter){
//If I'm home, it's Summer, and the temp is too high, turn the relay ON
if (TempAct > TempDes){
Fan(ON);
}
//Turn it off when the space is cooled to the desired temp - a few degrees
else if (TempAct <= (TempDes - Hysteresis_S)){
Fan(OFF);
}
}
}
// If I'm not home...
else {
// Turn on the HVAC if the temperature outside of seasonal the minimum / maximum limits
if((Winter && TempAct < TempMin) || (!Winter && TempAct > TempMax)){
Fan(ON);
}
// Otherwise, turn it off
else{
Fan(OFF);
}
}
}
}
// Turn the HVAC ON or OFF
void Fan(boolean RunFan){
FanState = RunFan;
// Set the proper color for the Desired Temp gauge and ON/OFF LED
//(red = heating, blue = cooling, fan off = normal widget color
if (Winter && FanState){
Blynk.setProperty(V0, "color", BLYNK_RED);
}
else if (!Winter && FanState){
Blynk.setProperty(V0, "color", BLYNK_BLUE);
}
else{
// Return widgets to their "off" state color, depending on theme
Blynk.setProperty(V0, "color", BLYNK_GREEN);
}
digitalWrite(RelayPin,!FanState); // Relay turns fan on with LOW input, off with HIGH
}
// Ends manual pulse mode
void KillManual(){
Fan(OFF);
ManualRun = false;
}
//Temperature slider. Make the desired temperature gauge in Blynk reflect slider changes.
BLYNK_WRITE(V3){
TempDes = param.asInt();
Blynk.virtualWrite(V1,TempDes);
ManualStop = false; //New temperature setting ends any manual stop
if(espalexaPointer != nullptr){ //Update espalexa "brightness" value
espalexaPointer->setPercent(TempDes);
}
}
// Updates dashboard information on the Blynk app
void OtherUpdates(){
Blynk.virtualWrite(V29,Home * 1023); // Update "home" LED on dashboard
Blynk.virtualWrite(V1,TempDes); //Update desired temp on the dashboard
// Notify when the temperature sensor fails repeatedly, and turn off the fan.
if(MenuItem == 0 && !ButtonPressed){
if (BadRead > 10){
Blynk.virtualWrite(V10, String("<<< SENSOR MALFUNCTION >>>"));
BadRead = 0;
if (!ManualRun){ //Manual mode supersedes a malfunction condition
Fan(OFF);
}
}
// Clear notification when sensor reads correctly again
else{
MenuReset();
}
}
if (TempDes != PreviousTempDes){ //update the EEPROM if desired temperature had changed.
EEPROM.write(3,TempDes);
EEPROM.commit();
PreviousTempDes = TempDes;
}
// To stabilize perceived temperature calculation, only update humidity readings between fan cycles
if(FanState == OFF){
float ReadH = dht.readHumidity(); // Read humidity (percent)
// Only update humidity if it's a good read from the sensor. To mitigate any
// instability, average with previous reading, change by only 1% per reading
if(!(isnan(ReadH))){
int HumidityAvg = (ReadH + Humidity) / 2;
if (HumidityAvg > Humidity){
Humidity += 1;
}
if (HumidityAvg < Humidity){
Humidity -=1;
}
}
Blynk.virtualWrite(V2, Humidity);
}
}
//************************ External Changes (Alexa, IFTTT) ************************************
// Alexa native device (shows up in Alexa app as a Hue device)
void AlexaCommands(EspalexaDevice* espalexaPointer) {
if(espalexaPointer == nullptr) return;
//Retrieve numeric value, and show in Blynk settings tab
int AlexaPercent = espalexaPointer->getPercent();
Response = "Alexa temp: ";
Response += AlexaPercent;
Blynk.virtualWrite(V10,Response);
MenuTimer = timer.setTimeout(MenuTimeOut, MenuReset);
// "Alexa, Turn OFF" ends manual run, or applies Manual stop if not running
if(AlexaPercent == OFF){
Fan(OFF);
if(ManualRun){
ManualRun = false;
}
else{
ManualStop = true;
}
}
// "Alexa, turn ON," set level (temperature), or augment
else{
// If the fan is already ON, use imcomming level for temperature setting
if(FanState == ON){
//"Alexa, turn UP..." triggers an unusually big change. Incremnent 2 degrees.
if(AlexaPercent > TempDes + 10){
TempDes += 2;
}
//"Alexa, turn DOWN..." triggers an unusually big change. Decrement 2 degrees.
else if(AlexaPercent < TempDes - 10){
TempDes -= 2;
}
//"Alexa, set ThermoX to ##" triggers a reasonable change. Use as desired temperature.
else if(AlexaPercent >= TempMin && AlexaPercent <= TempMax){
TempDes = AlexaPercent;
}
Blynk.virtualWrite(V1, TempDes);
Blynk.virtualWrite(V3, TempDes);
if(espalexaPointer != nullptr){ //Update espalexa "brightness" value
espalexaPointer->setPercent(TempDes);
}
}
// Otherwise, it was a "Turn ON" command, so run a pulse cycle
else{
ManualRun = true;
Fan(ON);
timer.setTimeout(PulseTime, KillManual);
}
}
}
//Get location (home or away) from the IFTTT iOS location and Maker channels
BLYNK_WRITE(V31)
{
Home = param.asInt();
if (Home){ //Turn the HOME LED widget on or off
Blynk.virtualWrite(V29,1023);
}
else Blynk.virtualWrite(V29,0);
}
//************************** Settings Menu Functions *******************************
// Dashboard SETTINGS button. Press-and-hold to enter menu. Short press for next item.
BLYNK_WRITE(V4) {
// When the SETTINGS button is pressed, start a timer to check for a long press
if(param.asInt()){
ButtonTimer = timer.setTimeout(750, LongHoldDetect);
ButtonPressed = true;
}
// Button has been released
else {
timer.deleteTimer(ButtonTimer); // Kill current long button hold detection
ButtonPressed = false; // Reset the button press flag
// If the long hold function wasn't just called, it's a short press. Avance the menu.
if (!LongHold && MenuItem != 0){
NextMenuItem(); // Advance to next menu item
}
// Reset the long press flag
LongHold = false;
}
}
// Checks for long press condition on SETTINGS button
void LongHoldDetect(){
// If the button is still depressed, it's a long hold
if (ButtonPressed && LongHold == false){
// Enter or exit the SETTINGS menu, if it was a long press
LongHold = true; // Flag prevents repeated tripping of long hold
if (MenuItem == 0){
MenuTimer = timer.setTimeout(MenuTimeOut, MenuReset);
NextMenuItem(); // Enter the SETTINGS menu
}
else{
MenuReset(); // Exit the SETTINGS menu
}
}
}
//Cycles through the Settings Menu in the Labeled Value widget
void NextMenuItem(){
timer.restartTimer(MenuTimer);
MenuItem += 1;
if (MenuItem > 8){
MenuItem = 1;
}
switch(MenuItem){
case 1:
if (ManualRun){
Response = "CANCEL PULSE?";
}
else{
Response = "15 MIN PULSE?";
}
break;
case 2:
if (UsePerceivedTemp){
Response = "USE ACTUAL TEMP?";
}
else Response = "USE PERCEIVED TEMP?";
break;
case 3:
if (ManualStop){
Response = "END SYSTEM HALT?";
}
else{
Response = "HALT SYSTEM?";
}
break;
case 4:
if (Home){
Response = "LOCATION: HOME";
}
else Response = "LOCATION: AWAY";
break;
case 5:
if (Winter){
Response = "MODE : WINTER";
}
else Response = "MODE : SUMMER";
break;
case 6:
if (Winter){
Response = "HYSTERESIS: ";
Response += Hysteresis_W;
Response += " DEG";
}
else{
Response = "HYSTERESIS: ";
Response += Hysteresis_S;
Response += " DEG";
}
break;
case 7:
Response = "TEMP CORRECTION: ";
Response += TempCorrection;
Response += " DEGREES";
break;
case 8:
if(Winter){
if(TempMin < 50 || TempMin > 90){
Response = "SET MINIMUM TEMP?";
}
else{
Response = "MINIMUM TEMP: ";
Response += TempMin;
}
}
else{
if(TempMin < 50 || TempMin > 90){
Response = "SET MAXIMUM TEMP?";
}
else{
Response = "MAXIMUM TEMP: ";
Response += TempMax;
}
}
break;
}
Blynk.virtualWrite(V10,Response);
}
//Dashboard MODIFY button. Executes change of selected menu item
BLYNK_WRITE(V5){
if (MenuItem > 0 && param.asInt()){
timer.restartTimer(MenuTimer);
switch(MenuItem){
//Forced 15 minute run
case 1:
if (ManualRun){
ManualRun = false;
Response = "15 MIN PULSE?";
}
else{
ManualRun = true;
ManualStop = false;
Fan(ON);
Response = "PULSE: ON";
timer.setTimeout(PulseTime, KillManual);
}
break;
//User perceived temperature instead of actual
case 2:
if (UsePerceivedTemp){
Response = "ACTUAL TEMP MODE";
UsePerceivedTemp = false;
EEPROM.write(5,0);
}
else {
Response = "PERCEIVED TEMP MODE";
UsePerceivedTemp = true;
EEPROM.write(5,1);
}
if(UsePerceivedTemp){
Blynk.setProperty(V0, "label", " Perceived Temperature");
}
else{
Blynk.setProperty(V0, "label", " Actual Temperature");
}
break;
//Turn system off
case 3:
if (ManualStop){
ManualStop = false;
Response = "HALT SYSTEM?";
}
else {
ManualStop = true;
ManualRun = false;
Fan(0);
Response = "SYSTEM HALTED";
}
break;
//Change location manually
case 4:
if (Home){
Home = false;
Response = "LOCATION : AWAY";
}
else {
Home = true;
Response = "LOCATION : HOME";
}
break;
//Change season
case 5:
if (Winter){
Response = "MODE : SUMMER";
Winter = false;
EEPROM.write(4,0);
}
else {
Response = "MODE : WINTER";
Winter = true;
EEPROM.write(4,1);
}
break;
//Change hysteresis level of currently selected season
case 6:
if (Winter){
Hysteresis_W += 1;
if (Hysteresis_W > 6){
Hysteresis_W = 1;
}
EEPROM.write(1,(Hysteresis_W));
Response = "WINTER HYSTERESIS: ";
Response += Hysteresis_W;
Response += " DEG";
}
else{
Hysteresis_S += 1;
if (Hysteresis_S > 6){
Hysteresis_S = 1;
}
EEPROM.write(2,(Hysteresis_S));
Response = "SUMMER HYSTERESIS: ";
Response += Hysteresis_S;
Response += " DEG";
}
break;
// Correct faulty DHT11 readings
case 7:
TempCorrection +=1;
if (TempCorrection > 5){
TempCorrection = -5;
}
EEPROM.write(0, TempCorrection);
Response = "TEMP CORRECTION: ";
Response += TempCorrection;
Response += " DEG";
break;
//Change minimum Winter temperature or maximum Summer temperature
case 8:
if(Winter){ // Winter minimum temperature
TempMin += 2;
if(TempMin > 68){
TempMin = 58;
}
Response = "MINIMUM TEMP: ";
Response += TempMin;
EEPROM.write(7,(TempMin));
}
else{ // Summer maximum temperature
TempMin += 2;
if(TempMax > 90){
TempMax = 78;
}
Response = "MAXIMUM TEMP: ";
Response += TempMax;
EEPROM.write(8,(TempMax));
}
}
EEPROM.commit();
Blynk.virtualWrite(V10, Response);
}
}
// Reset the Menu at startup or after timing out from inactivity
void MenuReset(){
MenuItem = 0;
Blynk.virtualWrite(V10, String("HOLD 2 SEC FOR MENU"));
}
//**************************** Miscellaneous *********************************
//Retrieves saved values from EEPROM
void GetPresets(){
TempCorrection = EEPROM.read(0);
if ((TempCorrection < -5) || (TempCorrection > 5)){
TempCorrection = 0;
EEPROM.write(0, 0);
}
UsePerceivedTemp = EEPROM.read(5);
if(UsePerceivedTemp){
Blynk.setProperty(V0, "label", " Perceived Temperature");
}
else{
Blynk.setProperty(V0, "label", " Actual Temperature");
}
Winter = EEPROM.read(4);
Hysteresis_W = EEPROM.read(1);
Hysteresis_S = EEPROM.read(2);
if (!(Hysteresis_W >= 1) && !(Hysteresis_W <= 6)){
Hysteresis_W = 2;
EEPROM.write(1, Hysteresis_W);
}
if (!(Hysteresis_W >= 1) && !(Hysteresis_W <= 6)){
Hysteresis_S = 2;
EEPROM.write(2, Hysteresis_S);
}
TempDes = EEPROM.read(3);
if (!(TempDes >= 50) && !(TempDes <= 90)){
TempDes = 70;
EEPROM.write(3, 70);
}
if(espalexaPointer != nullptr){
espalexaPointer->setPercent(TempDes);
}
TempMin = EEPROM.read(7);
if(!(TempMin >= 50 && TempMin <= 90)){
TempMin = 58;
EEPROM.write(7, TempMin);
}
TempMax = EEPROM.read(8);
if(!(TempMax >= 50 && TempMax <= 90)){
TempMax = 90;
EEPROM.write(8, TempMax);
}
EEPROM.commit();
}
Enjoy!
Hello,
you did a very good job.
but I couldnāt see the code of the relay side.
one I did not see
also if you share the project on the github, it would be more comfortable to follow the updates.
Thank you.
@kgulle Iām not 100% sure what you mean by āthe code of the relay side,ā but Iāll try to clarify all the pertinent code.
The relay is on pin number 2.
#define RelayPin 2
It is initiated in the setup function withā¦
pinMode(RelayPin,OUTPUT);
digitalWrite(RelayPin,HIGH);
ā¦and is toggled on and off by the last command of the Fan function.
digitalWrite(RelayPin,!FanState);
My relay happens to toggle on when the input pin is held LOW, which is why itās initially set to HIGH (relay off), and is subsequently synchronized with !Fanstate.
Youāre right that I should start using GitHub. Soon.
those added features look pretty nice, congrats!
Gustavo.
Hi Dear .
Can you share the code?
Thanks
Thanks for sharing. Iām following you carefully. Do you share the Blynk QR code?
Thanks.
@baser Sure, here it is:
Note that I used no text on the tabs. In order to maintain a clean aesthetic, there are simple underscore lines. Touch on these to move between tab the main and settings tabs.
Thanks.
Which of the latest code should I use?
@baser Use the code from April 15. Also, hereās a screen shot with an arrow pointing to the tab switching ābutton.ā
Thank you very much. I appreciate your work. I ran the Code. Next Iām going to connect and test the thermostat.
First I enjoy this post very much, I did some searching for a DIY version of Nest and this is one I found. I was disappointed it was really only for a boiler/heater and not a normal HVAC system. I do plan to take your code and adapt it to a traditional HVAC system. To do so I will need to add 2 more relays at a minimum. I also want to design this around the wemos d1 mini and their modules to make it easier for people to use this. I already added a OLED display.
// change summer/winter naming to Heat/Cool
// add motion detector control also manually home/away
// add 2nd and 3rd relay to manually do fan and switch between heat and air conditioner
// rename fan in sketch to Cool for air conditioner, as fan is a different thing
// add manual circulate fan every X mins and menu to set this, to circulate air in house to even temps out.
// add status of Heat/Cool and fan on/off to both LCD and blynk
// add google assistant
@Jas, a few thoughts/comments - feel free to take them onboard or not
I live the Wemos D1 Mini and itās expansion possibilities. However, like all ESP8266 based MCVUs it has a limited number of GPIO pins. When you start adding more relays, motion sensors etc then you may run out of appropriate pins.
See this for more infoā¦
The simple solution is to add more D1 Minis, which would mean using Blynk bridge to communicate between them. This is do-able, but can be a challenge.
Or, you could move to an ESP32 instead.
One way to do motion sensors is to use 433MHz PIR detectors that are battery operated. Each detector will transmit a unique code when it detects movement, so you could have a 433MHz receiver attached to a D1 Mini or ESP32 to receive and process these movement signals. This has the advantage of allowing autonomous battery operated PIR sensors to be placed in convenient locations, and having your control unit tucked away in an appropriate location next to your HVAC system.
My preference for a control system like this is to use Node-Red as the heart of the system, and use this to interface with Blynk, google assistant, amazon alexa, external weather services etc etc.
Communication to/from the MCUs is then done via MQTT messages, which gives a robust platform that can be expanded easily in future.
I also like to use Nextion touch screen displays rather than the little oled ones, as they are much nicer looking and quite easy to use.
More on my climate control system, built using this architecture, is here:
Since I wrote this Iāve had some new aircon inverter units installed that can be operated by an app as well as an IR remote, and the next thing on my list is to sniff the HTTP protocols used by these and establish a 2-way control/feedback system for them, but thatās a little way in the future yet.
Pete.