void loop plese now Blynk.run otherwise there is stress with the program
with void loop only blynk.run (); otherwise do nothing clean the other remove otherwise thereās stress with blynk program can not work properly
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
Hi @Gunner Thanks for the help but I am unable to understand how timer will work can you please help me with my code.
@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ā¦
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();
}
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.
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.
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.
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.
@PeteKnight Thanks for the suggestion that worked like charmā¦saving my EEPROM
will now work on timer as @Gunner and @markop suggestedā¦will be posting updated code soonā¦
In Love With Blynk Communityā¦
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.
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
Hi can you please give me some example how to do soā¦
@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 ⦠but BlynkTimer is the better way regardless.
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();
}
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.
this issue has been resolved by using timerā¦