BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Slider Value to set relay ON time

esp8266

#2

void loop plese now Blynk.run otherwise there is stress with the program


#3

Hello @markop sorry i did not understand Blynk.run(); should be removed or keep.


#4

with void loop only blynk.run (); otherwise do nothing clean the other remove otherwise there’s stress with blynk program can not work properly


#5

Having this in your void loop() is no different then having the contents of that function there… they still try to run thousands of times a second. Use timed functions with BlynkTimer

http://docs.blynk.cc/#blynk-firmware-blynktimer


#6

Hi @Gunner Thanks for the help but I am unable to understand how timer will work can you please help me with my code.


#7

Blynk - RE-SEAR-CH

@chinusabya Don’t forget to search around this forum as well as reading all the related documentation… BlynkTimer is based off of the Arduino SimpleTimer Library.

And I have a few examples here…


#8

Dear @Gunner my node mcu is not keeping eeprom value after reboot can you please help me on that.

Here is the code.

#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <EEPROM.h> 
  
  
  // You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxxxxxxxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxxxx";


int Status = 12;
int sensor = 16;
int relay = 5;
long r;

   BLYNK_WRITE(V0) { 
    r = param.asInt()*60000;  // timer switch
    
}
void EEPROMWritelong(int address, long value)
{
   byte four = (value & 0xFF);
      byte three = ((value >> 8) & 0xFF);
      byte two = ((value >> 16) & 0xFF);
      byte one = ((value >> 24) & 0xFF);

      EEPROM.write(address, four);
      EEPROM.write(address + 1, three);
      EEPROM.write(address + 2, two);
      EEPROM.write(address + 3, one);


  
}

long EEPROMReadlong(long address)
{
   long four = EEPROM.read(address);
      long three = EEPROM.read(address + 1);
      long two = EEPROM.read(address + 2);
      long one = EEPROM.read(address + 3);
      return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);

}



void setup() {
    EEPROM.begin(512);  

  Serial.begin(115200);
  
   Blynk.begin(auth, ssid, pass);
 
  pinMode(sensor, INPUT); // declare sensor as input
  pinMode(Status, OUTPUT);  // declare LED as output
   pinMode(relay, OUTPUT);  // declare LED as output

  

}

void relays_onoff()
             {
         long address=0;
    EEPROMWritelong(address, r);
   address+=4;

     EEPROM.commit();
     yield();
  long state = digitalRead(sensor);
  
    if(state == HIGH){
      digitalWrite (Status, HIGH);
      digitalWrite (relay, LOW);
 
      Serial.println("Motion detected!");
      Serial.println(EEPROMReadlong(0));
      delay(EEPROMReadlong(0));
    }
    else {
      digitalWrite (Status, LOW);
       digitalWrite (relay, HIGH);
  
      Serial.println("Motion absent!");
      }
  
}

void loop(){

  
   Blynk.run();
   relays_onoff();

}

#9

I just had a quick skim through your code and you don’t appear to be retrieving the EEPROM value at startup. Shouldn’t you be calling the read function in your void setup?

Also, are you aware that each EEPROM memory location has a limited life in terms of the number of writes it can endure?
The manufacturers say that they’re good for at least 100,000 writes, but as you’re doing at least one write each time your void loop is processed then I’d expect you’ll “burn out” your chosen memory location within a matter of minutes.

Pete.


#10

Hello @PeteKnight thanks for going through my code i am really a beginner in this but trying my level best to understand how code works,yes i do understand that node mcu can handle only 100,000 write cycles but I can not find a way to avoid that.
I have to write value to eeprom through blynk slider only 2 or 3 times a year.
I dont understand what u mean by read function
Is it this one.

long EEPROMReadlong(long address)
{
   long four = EEPROM.read(address);
      long three = EEPROM.read(address + 1);
      long two = EEPROM.read(address + 2);
      long one = EEPROM.read(address + 3);
      return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);

}

Would love to hear further…
thanks.


#11
IF
newValue != currentValue
THEN
EEPROMWritelong 

better yet if you throw that into a timer, say you do that check every second, so if you ‘slide’ the slider you do not write each. and. every. slide. step. you. do.


#12

Yes, that’s the function that reads the EEPROM value, but it’s never called in your code, so the value is never retrieved from memory.

There’s actually no need to use EEPROM memory at all. The value of the slider is stored on the Blynk server, you just need to do a Blynk sync at startup to retrieve that value.

Pete.


#13

@PeteKnight Thanks for the suggestion that worked like charm…saving my EEPROM :grinning:
will now work on timer as @Gunner and @markop suggested…will be posting updated code soon…
In Love With Blynk Community…


#14

Here Is Working code

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>


char auth[] = "xxxxxxxxxxxxxxxxxxxxxx"; //Auth Token in the Blynk App.


// Set password to "" for open networks.
char ssid[] = "xxxxxxxxx"; // WiFi SSID.
char pass[] = "xxxxxxx";  // WiFi Password.


//Start Initialize PIN//

int Status = 12;
int sensor = 16;
int relay1 = 5;
int relay2 = 4;
int relay3 = 0;
int relay4 = 14;
int relay5 = 12;
int relay6 = 13;
int relay7 = 15;
int relay8 = 3;
long tme;

//End Initialize Pin//

BlynkTimer timer;
//get data stored in virtual pin V0 from server
BLYNK_CONNECTED() {

  Blynk.syncVirtual(V0);
}

BLYNK_WRITE(V0) {
  tme = param.asInt() * 60000; // timer switch

}



void setup() {


  Serial.begin(115200);

  Blynk.begin(auth, ssid, pass);

  pinMode(sensor, INPUT); // declare sensor as input
  pinMode(Status, OUTPUT);  // declare LED as output
  pinMode(relay1, OUTPUT);  // declare LED as output
  pinMode(relay2, OUTPUT);  // declare LED as output
  pinMode(relay3, OUTPUT);  // declare LED as output
  pinMode(relay4, OUTPUT);  // declare LED as output
  pinMode(relay5, OUTPUT);  // declare LED as output
  pinMode(relay6, OUTPUT);  // declare LED as output
  pinMode(relay7, OUTPUT);  // declare LED as output
  pinMode(relay8, OUTPUT);  // declare LED as output
  timer.setInterval(1000L, relays_onoff);

}

void relays_onoff()
{

  long state = digitalRead(sensor);

  if (state == HIGH) {

    digitalWrite (Status, HIGH);
    digitalWrite (relay1, LOW);
    digitalWrite (relay2, LOW);
    digitalWrite (relay3, LOW);
    digitalWrite (relay4, LOW);
    digitalWrite (relay5, LOW);
    digitalWrite (relay6, LOW);
    digitalWrite (relay7, LOW);
    digitalWrite (relay8, LOW);

    Serial.println("Motion detected!");
    Serial.println(tme);
    delay(tme);
  }
  else {


    digitalWrite (Status, LOW);
    digitalWrite (relay1, HIGH);
    digitalWrite (relay2, HIGH);
    digitalWrite (relay3, HIGH);
    digitalWrite (relay4, HIGH);
    digitalWrite (relay5, HIGH);
    digitalWrite (relay6, HIGH);
    digitalWrite (relay7, HIGH);
    digitalWrite (relay8, HIGH);


    Serial.println("Motion absent!");
  }

}



void loop() {


  Blynk.run();
  timer.run();

}

But got another problem unable to turn relay on off with blynk button as timer reset every second please help me on this issue.


#15

Try it that way it works without a timer

void checkTimer () <<< z.b.
your program

and under void loop
check timer ();
thats how it works


#16

Hi can you please give me some example how to do so…


#17

Hello

ok look LCD flickers


#18

@markop That ‘timing’ method is not well thought out…

@chinusabya keep using the BlynkTimer as recommended, but either use a 2nd timeout Timer to turn the Relay off. Or run a 2nd interval timer every 1/2 second. Adjust timers as wanted.

Actually it is hard to tell what you are trying to do :stuck_out_tongue: … but BlynkTimer is the better way regardless.


#19

SET Light On time- Slider to set delay for relay to be kept on after pir has detected motion.
Manual Mode- To turn on/off pir detecting motion.

LT1…LT2…- To turn the relay on/off through button.

Now the problem arises when

timer.setInterval(1000L, relay_onoff);

When this runs it turn the relay off which were turned on by blynk button.

latest code…

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#define vPIN_INFO               V5
char auth[] = "xxxxxxxxxxxxxxxxxxxxx"; //Auth Token in the Blynk App.


// Set password to "" for open networks.
char ssid[] = "xxxxxxxxxxx"; // WiFi SSID.
char pass[] = "xxxxxxxxxxxxxxx";  // WiFi Password.


//Start Initialize PIN//
int enableTimer = LOW;
int Status = 12;
int sensor = 16;
int relay1 = 5;
int relay2 = 4;
int relay3 = 0;
int relay4 = 14;
int relay5 = 12;
int relay6 = 13;
int relay7 = 15;
int relay8 = 3;
long tme;
int lt1;
//End Initialize Pin//

BlynkTimer timer;
//get data stored in virtual pin V0 from server
BLYNK_CONNECTED() {

  Blynk.syncVirtual(V0);
  Blynk.syncVirtual(V9);
}


BLYNK_WRITE(V9)  // Timer enable/disable for lights
{
  enableTimer = param.asInt();
  
}
BLYNK_WRITE(V0) {
  tme = param.asInt() * 60000; // timer switch

}

BLYNK_WRITE(V1) {
  lt1 = param.asInt();
   if((enableTimer == 1) && (lt1 == 1))
   digitalWrite (relay1, LOW);
   
  
  
   else
   digitalWrite (relay1, HIGH);
    
}

void setup() {


  Serial.begin(115200);

  Blynk.begin(auth, ssid, pass);

  pinMode(sensor, INPUT); // declare sensor as input
  pinMode(Status, OUTPUT);  // declare LED as output
  pinMode(relay1, OUTPUT);  // declare LED as output
  pinMode(relay2, OUTPUT);  // declare LED as output
  pinMode(relay3, OUTPUT);  // declare LED as output
  pinMode(relay4, OUTPUT);  // declare LED as output
  pinMode(relay5, OUTPUT);  // declare LED as output
  pinMode(relay6, OUTPUT);  // declare LED as output
  pinMode(relay7, OUTPUT);  // declare LED as output
  pinMode(relay8, OUTPUT);  // declare LED as output
  timer.setInterval(1000L, relay_onoff);
  
     Blynk.setProperty(vPIN_INFO, "label", String("WIFI: ") + String(map(WiFi.RSSI(), -105, -40, 0, 100)) + String("% (") + WiFi.RSSI() + String("dB)"));
}

void relay_onoff()
{

  long state = digitalRead(sensor);

 if((enableTimer == 0) && (state == HIGH)) 
 {

    digitalWrite (Status, HIGH);
    digitalWrite (relay1, LOW);
    digitalWrite (relay2, LOW);
    digitalWrite (relay3, LOW);
    digitalWrite (relay4, LOW);
    digitalWrite (relay5, LOW);
    digitalWrite (relay6, LOW);
    digitalWrite (relay7, LOW);
    digitalWrite (relay8, LOW);

    Serial.println("Motion detected!");
    Serial.println(tme);
   delay(tme);
  }
  else {


    digitalWrite (Status, LOW);
    digitalWrite (relay1, HIGH);
    digitalWrite (relay2, HIGH);
    digitalWrite (relay3, HIGH);
    digitalWrite (relay4, HIGH);
    digitalWrite (relay5, HIGH);
    digitalWrite (relay6, HIGH);
    digitalWrite (relay7, HIGH);
    digitalWrite (relay8, HIGH);


    Serial.println("Motion absent!");
  }

}



void loop() {

 
  Blynk.run();
  timer.run();
   
}

#20

I’m not a 100% sure that I’m following you, but I noticed that you use
if … else…
I would advise you to use
if…else if…
So you have better control over BOTH cases. Now you’re doing: IF this particular situation is met THEN turn it on ELSE in ALL other occasions turn if off. Which it seems is not something you want.


#21

this issue has been resolved by using timer…