Im currently trying to build a fridge thermostat. Im using a similar code that i used for an hvac controler.
The issue is that im saving my laterst settings to the eprom and works very well up until im using a negative value as my setpoint. the moment i use a negative value and restarts the esp8266 wemos d1 mini it returns the setpoint to 255 and so forth but if i use 0 or any other positive value it works just fine
Cant seem to work this out so i decided to ask for help.
Im using an wemos d1 mini board with a ds18b20 temp sensor and a relay module. my code follow:
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h>
#include <EEPROM.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ArduinoOTA.h>
#define UpdateFrequency (60 * 1L) //How often a new temperature will be read
#define RelayPin D1
#define ONE_WIRE_BUS D2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// Timer for temperature updates
BlynkTimer timer;
//WiFi and Blynk connection variables
char auth[] = "****************"; // Blynk token
//Thermostat variables
int TempDes = 2; //Desired temperature setting
int PreviousTempDes;
int TempAct = 70; //Actual temperature, as measured by the DS18B20 sensor
int LastRead = 2;
// Preference variables
int Hysteresis_C = 5;
int TempCorrection = 0; //Used to adjust readings, if the sensor needs calibration
// Current condition variables
boolean Cooling = true; //default is false
boolean FanState = 0; // is the fan on or off?
bool Connected2Blynk = false;
void Uptime()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V6, millis() / 1000);
}
void setup() {
Serial.begin(9600);
delay(10);
//Creates an AP if no wifi credentials are stored
WiFiManager wifi;
wifi.autoConnect("DindoGeladeira");
// delay(30);
Blynk.config(auth); // in place of Blynk.begin(auth, ssid, pass);
Blynk.connect(3333); // timeout set to 10 seconds and then continue without Blynk
while (Blynk.connect() == false) {}
// Wait until connected
Serial.println("Connected to Blynk server");
ArduinoOTA.setHostname("DindoGeladeira");
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
//Initialize the fan relay. Mine is "off" when the relay is set LOW.
pinMode(RelayPin,OUTPUT);
digitalWrite(RelayPin,LOW);
//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
timer.setInterval(UpdateFrequency, Uptime); // Update temp reading and relay state
}
//******************************************************************************************************************************************************
// Main loop
void loop() {
Blynk.run();
timer.run();
ArduinoOTA.handle();
}
//******************************************************************************************************************************************************
bool isFirstConnect = false; // Keep this flag not to re-sync on every reconnection
// This function will run every time Blynk connection is established
BLYNK_CONNECTED() {
if (isFirstConnect) {
// Request Blynk server to re-send latest values for all pins
Blynk.syncAll();
isFirstConnect = false;
}
}
void CheckConnection(){
Connected2Blynk = Blynk.connected();
if(!Connected2Blynk){
Serial.println("Not connected to Blynk server");
Blynk.connect(3333); // timeout set to 10 seconds and then continue without Blynk
}
else{
Serial.println("Connected to Blynk server");
}
}
// This is the decision algorithm for turning the HVAC on and off
void TempUpdate (){
{
OtherUpdates(); //Refeshes dashboard information
sensors.requestTemperatures(); // Polls the sensors.
TempAct = sensors.getTempCByIndex(0); // Stores temperature. Change to getTempCByIndex(0) for celcius.
Blynk.virtualWrite(V0, TempAct); // Send temperature to Blynk app virtual pin 0.
Serial.print(F("Actual temperature: "));
Serial.println(TempAct);
}
// Decision algorithm for running HVAC
if (Cooling)
//If I'm home, it's Summer, and the temp is too high, turn the relay ON
if (TempAct >= TempDes + (Hysteresis_C)){
FanState = 1;
Fan();
}
//Turn it off when the space is cooled to the desired temp - a few degrees
else if (TempAct <= TempDes && FanState){
FanState = 0;
Fan();
}
}
//Match temp gauge to slider in Blynk app
BLYNK_WRITE(V3){
TempDes = param.asInt();
Blynk.virtualWrite(V1,TempDes);
}
// Updates dashboard information on the Blynk app
void OtherUpdates(){
Blynk.virtualWrite(V1,TempDes); //Update desired temp on the dashboard
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;
}
}
// Turn the HVAC on or off
void Fan(){
digitalWrite(RelayPin,FanState); // Relay turns fan on with LOW input, off with HIGH
Blynk.virtualWrite(V7,FanState * 1023);// fan "ON" LED on dashboard
Blynk.virtualWrite(V8,HIGH);// fan "ON" Graph
Serial.print(F("Fan state: "));
Serial.println(FanState);
}
//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."));
}
Cooling = EEPROM.read(4);
Hysteresis_C = EEPROM.read(2);
if ((Hysteresis_C < 0) || (Hysteresis_C > 2)){
Hysteresis_C = 5;
}
if (Cooling){
Serial.println(F("Season setting: Cooling / heating"));
Serial.print(F("Cooling hysteresis: "));
Serial.print(Hysteresis_C);
Serial.println(F(" degrees."));
TempDes = EEPROM.read(3);
if ((TempDes < 0)){
TempDes = 0;
Serial.println(F("No saved temperature setting."));
}
else {
Serial.print(F("Desired temperature: "));
Serial.print(TempDes);
Serial.println(F(" degrees."));
}
Serial.println("");
}
}
}
Hope to find some light on this. Cant seem to find a workaround.