EEPROM and Blynk Slider

Hi guys, I’m relatively new to arduino and programming, I’m trying to develop some sort of relay-based thermostat with blynk. It’s been a couple of days I am dealing with this issue: I’m trying to memorize on the EEPROM the value of the slider, the same value is printed on a value-display (widget).

the code I’ve been using is this one:

BLYNK_WRITE(V16) // Slider control 
{
 int destemp = param.asInt(); // assigning incoming value from pin V16 to a variable
 Blynk.virtualWrite(V17, destemp); // print on value-display
 EEPROM.write(0, highByte(destemp)); //write the first half on eeprom slot0
 EEPROM.write(1, lowByte(destemp)); //write the second half on eeprom slot1
}

The void function “bot” do all the job:

void bot(boolean vacexp, boolean heatexp, int margh)
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  byte high = EEPROM.read(0); //read the first half
  byte low = EEPROM.read(1); //read the second half
  int destemp = high + low; // desired temperature

heatxp is a boolean variable connected to a button to manually activate the heater.

if(destemp > t && heatexp==false && digitalRead(D4) != LOW)
  {
      digitalWrite(D4, LOW);
  }
  else if(destemp < t + margh && heatexp==false && digitalRead(D4) != HIGH)
  {
     digitalWrite(D4, HIGH);
  }
}

The function “bot” is then called in void loop.

The problem is that if I set the temperature, the relay doesn’t turn on, it seems like destemp value is not being read from the eeprom. any advices?

Sections of code do not give us enough idea of the program flow. Post the whole sketch please.

sure, here you go !

#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
#include <EEPROM.h>


char auth[] = "";


char ssid[] = "Informasistemi";
char pass[] = "";

#define DHTPIN D2       
#define DHTTYPE DHT11     // DHT 11



DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;
boolean vacexp;
boolean heatexp;
int margh = 2;
int addr = 1;

BLYNK_WRITE(V16) //slider control, it send the value from the slider to the value display
{
 int destemp = param.asInt(); // assigning incoming value from pin V16 to a variable
 Blynk.virtualWrite(V17, destemp);
 EEPROM.write(0, highByte(destemp)); //write the first half
 EEPROM.write(1, lowByte(destemp)); //write the second half
}

 
BLYNK_WRITE(V8) // Manually turn on the vacuum
{
 int vacum = param.asInt();
 Blynk.virtualWrite(V8, vacum);
    if (vacum==1) 
    {
        digitalWrite(D4, HIGH);
        vacexp = false;
    }
    else 
    {
        digitalWrite(D4, LOW);
        vacexp = true;
    }
}

BLYNK_WRITE(V9) // Manually turn on the heater
{
 int heater = param.asInt();
 Blynk.virtualWrite(V9, heater);
    if (heater==1) 
    {
        digitalWrite(D4, HIGH);
        heatexp = false;
    }
    else 
    {
        digitalWrite(D4, LOW);
        heatexp = true;
    }
}

void bot(boolean vacexp, boolean heatexp, int margh) 
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  byte high = EEPROM.read(0); //read the first half
  byte low = EEPROM.read(1); //read the second half
  int destemp = high + low;
 

  if (isnan(h) || isnan(t)) 
  {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
 
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
  
           // Vacuum fan
           
  if(vacexp==false && h>70 && digitalRead(D4) != LOW)
  {
    digitalWrite(D4, LOW);
  }
  else if(vacexp==false && h<70 && digitalRead(D4) != HIGH)
  {
    digitalWrite(D4, HIGH);
  }
  
  

          // TEMPERTURE CONTROL

 if(destemp > h && heatexp==false && digitalRead(D5) != LOW)
  {
      digitalWrite(D5, LOW);
  }
  else if(destemp < h + margh && heatexp==false && digitalRead(D5) != HIGH)
  {
     digitalWrite(D5, HIGH);
  }
}


void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

  dht.begin();

  pinMode(D4, OUTPUT);
  pinMode(D5, OUTPUT);
}

void loop()
{
  Blynk.run();
  bot(vacexp, heatexp, margh);
}
1 Like

Glancing at your sketch, it seems you have a good grasp on Blynk commands, so aside from the eprom issue, which is not specific to Blynk and I don’t think it would be affected by it, have you tried just reading the value directly to confirm the rest of your code works?

Also something to be aware of, the DHT11 is slow, recommended minimum of 5-10 seconds between readings, so get a timer running… and really no sense in anything less than a minute or so for a basic thermostat. The DHT11 is also inaccurate at ±2 degrees.

Finally, the function you seem to be calling in the main loop, sendSensor() doesn’t make sense to me… I recognise the variables referenced as also being in the void bot, but how they work together doesn’t compute for me (amateur programmer :wink: ), so I might not be able to offer much more help, sorry.

@alexy91 The EEPROM library for ESP’s is slightly different to the Arduino version.

See details for the main ESP libraries, including EEPROM, at http://esp8266.github.io/Arduino/versions/2.0.0/doc/libraries.html

You are missing EEPROM.commit().

You should try the EEPROM examples for ESP and then incorporate into your sketch.

It could be that you decide you don’t need EEPROM at all as the Blynk server can store any variables you need to retrieve. See sync for details.

1 Like

I’m only looking at this code on a tablet, so I might be missing something, but I can’t see how this could compile without errors. You’re calling a function called sendSensor() from void loop, but no such function exists in your code. You’re never calling the bot() function, so if assume you’ve done some manual editing of the code before uploading, and you were previously calling bot() function from your void loop?

The Arduino EEPROM has a finite number of write functions for each memory location before you “burn out” that location. Each location is supposed to be good for >100k write/erase functions, so with the way you’ve structured your code this shouldn’t be an issue, but as @Costas says, using the Blynk server to store this data makes more sense.

Pete.

yeah, sorry, “sendsensor” is indeed the"bot" function, I’ve uploaded an old version of the code, but it’s more or less the same, I’ve just correct it.

how can i use the blynk server to store my “destemp” value?

OK, so I wasn’t seeing some newfangle coding methods… I sometimes get gun shy when I think I understand some strange looking code, then some uber coder comes along and proves me wrong :wink: … so I erred on the side of caution… and got proven wrong anyhow :stuck_out_tongue_winking_eye: Go fig, but all is good :+1:

Blynk server already has the latest virtual pin data and that can be recalled if needed. e.g. via Blynk.syncVirtual(vPin)

Also, as recommended before, replace your bot() call in the main loop with a timer call.

timer.setInterval(60000L, bot(vacexp, heatexp, margh));  // Run this loop every minute
void loop()
{
  Blynk.run();
  timer.run();
}

yeah, got it, adding a timer would be a good idea to avoid the imprecision of the DHT11.

the point I’m missing is how can i store and recall that value.
For example, if I add Blynk.syncVirtual(V17) ( the printed value display from the slider ) in the “slider control” BLYNK_WRITE(V16)
how can I recall the destemp value, stored in the blynk server in my “bot” function?

I’m sure it’s easy than i think, but i can’t get it.

The only reason to “recall” it is if after a disconnect or device reboot, otherwise the variable destemp already contains the integer value from the slider… just use it.

Although I think you will need to assign int destemp as a global variable, as you do for int margh and int addr and then change this:

int destemp = param.asInt();

to this:

destemp = param.asInt();

And get rid of this:

int destemp = high + low;
1 Like

it worked !! thanks !!!

thanks