Thermostat X Blynk Wemos D1 mini lite, relay shield, oled shield, SHT30 shield

“port” for wemos D1 mini lite oled shield relay shield of Thermostat X

    /****************************************************************************
 * THERMOSTAX
 * https://community.blynk.cc/t/esp8266-hvac-control/2586/93
 * 
 * 
 * FUNZIONA CON ARDUINO IDE 1.6.5
ATTENZIONE IL RELAY SHIELD VA MODIFICATO PERCHE
NON PUO LAVORARE SUL D1 DATO CHE QUESTO PIN 
è UTILIZZATO DAL BUS I2C, NEL CODICE UTILIZZO IL
D5 E SUL RELAY HO PONTICELLATO IL D1 AL D5
 * LA LIBRERIA SPI.h, QUANDO SI LAVORA CON IL WEMOS 
VA SOSTITUITA CON QUELLA OPPORTUNA (QUELLA DI NATIVA
ARDUINO NON VA BENE)

libraries esp8266 ver 2.4.2

Scheda "Wemos D1 mini Lite"
Flash Size: "1M (no SPIFFS)"
Debug port: "Disabled"
Debug Level: "None"
IwIP Variant: "v2 Lower Memory"
VTables: "flash"
CPU Frequency: "80MHz"
Upload Speed: "115200"
Erase Flash "sketch + wifi settings"
Porta: quella che è


 * 
 * 
 * 
*  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
*       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 <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.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 <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[] = "BLYNK TOKEN"; // 



//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() {


  Serial.begin(115200);
  delay(10);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.display();
  delay(2000);

  // Clear the buffer.
  display.clearDisplay();



  //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
  sht30.get();
  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);
    display.println(sht30.cTemp);
    display.display();


    //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("");
}

thanks a lot

1 Like

Again with the unformatted post… :frowning:

Blynk%20-%20FTFC

Go edit your post to see how I fixed it … again… next time it will just be deleted :stuck_out_tongue_winking_eye: