BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       👉 SKETCH BUILDER

If statement and virtual pin

Guys
I take absoloutely no credit the sketch below it was cut and pasted and altered to suit my needs what i need help with and I have googled the web before requesting help from forum members
I wish to use a virtual led to stay on if the temperature fall below a set figure
I have read and I believe understood the LED_Blink.ino but obviously I am getting it wrong dont want the code just guided in the wright directio
Thanks oldvolt

Preformatted text`
*/
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = " 0000000";

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

#define DHTPIN D4         // What digital pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11     // DHT 11
//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301
int alarmPin = D4;
int led1 = D2;
int led2 = 14;
DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;


void sendSensor(){
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Serial.println(t);
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);

  // SETUP the ALARM Trigger and Send EMAIL 
  // and PUSH Notification

 if(t > 28){
    Blynk.email("0000000", "ESP8266 Alert", "Temperature over 28C!");
    Blynk.notify("ESP8266 Alert - Temperature over 28C!");
  }
}
   



void setup(){
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  dht.begin();
  timer.setInterval(2500L, sendSensor);
}
void loop(){
  Blynk.run();
  timer.run();
}

I got the issue you are facing. But you question about the virtual LED is not very clear. I am unable to understand what do you actually mean by a virtual LED. What I understand is that you need to turn on/off an LED by relating it to certain figure.

1. If you want to turn on the led1 when t > 28

if(t > 28){
    Blynk.email("0000000", "ESP8266 Alert", "Temperature over 28C!");
    Blynk.notify("ESP8266 Alert - Temperature over 28C!");
    digitalWrite(led1, HIGH);
}

2. If you want to turn on led2 when h > 10

if(h > 10){
    digitalWrite(led2, HIGH);
}

I hope this has cleared your doubt on using if/else statements and performing an action on that basis. Let me know if you have any further doubts or questions :slightly_smiling_face:

Probably asking how to control this… http://docs.blynk.cc/#widgets-displays-led

@oldvolt While the LED Widget has “special” commands… the simplest way is the usual Blynk.virtualWrite(vPin, value) command with the value being 0 for off and 255 for ON… anything in between is just a variance of “intensity”… think PWM values.

Thus…

if(t > 28){
    Blynk.virtualWrite(vPin, 0);  // OFF
    } else {
    Blynk.virtualWrite(vPin, 255); // ON
    }

The other issue is that the code will try to send an email and Blynk notification every 2.5 seconds while the temperature remains over 28°C.
A flag is needed to track that these alerts have been sent.

Also, I’m a bit confused by @oldvolt’s original statement compared to the code:

If the LED wishes needs to be on when the temperature is below a certain level then a “less than” operator (<) needs to be used rather than a “greater than” operator (>).

Pete.

2 posts were split to a new topic: The difference between SimpleTimer and BlynkTimer

Vishal ,Gunner, Pete
Thanks for your help
Just a brief explanation of what I a trying to achieve , my nephew works in Dublin but live in the North West of Ireland and he asked me if there was away of checking the temperature of hi house in the winter
I have had to change the code previously posted as I was hitting brick walls so I went back to the start
The problem I am having with this code is I am not recieving emails but I am recieving Blynk Notifications once again I am asking for guidance
the reason for LED1 and LED8 is for a visual alert
Thanks oldvolt

#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#include <SimpleTimer.h>
#define DHTPIN D4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
int led1 = V1; // House Cold
int led8 = V8; // Frozen Pipes
SimpleTimer timer;
float humidity, temp_f; // Values read from sensor
unsigned int notified = 0;
char auth[] = "****************"; //insert here your token generated by Blynk
void setup()
{
Serial.begin(9600); // See the connection status in Serial Monitor
delay(10);
Blynk.begin(auth, "********", "*******"); //insert here your SSID and password
timer.setInterval(1000, sendData);
}
void sendData()
{
//Read the Temp and Humidity from DHT
float h = dht.readHumidity();
float t = dht.readTemperature();
//Write values to V04 and V05
Blynk.virtualWrite(4, h);
Blynk.virtualWrite(5, t);
if(t < 10){
  Blynk.virtualWrite(V1, 255); // House Cold
 } else
 Blynk.virtualWrite(V1, 0);
 if(t <= 5){
  Blynk.virtualWrite(V8, 255);Frozen Pipes
 } else
 Blynk.virtualWrite(V8, 0);

if (t <=5 && notified == 0){
notified = 1;
Blynk.email("******@*****","ALARM" ,"Pipes Freezing!");
timer.setTimeout(300000L, resetNotified); // 5 min between emails
notified = 0;
Blynk.notify("ESP8266 Alert - Pipes Freezing!");
}
}
void resetNotified(){
notified=0;
  

}
void loop()
{
Blynk.run();
timer.run();
}

I understand what is feels like when your code doesn’t works. But don’t worry, we won’t let your nephew’s house water pipes freeze :grin:

I have fully checked the code you have written. Actually, there were many unnecessary & unused statements in your script. Also, to send emails & notifications you don’t need to put all those if\else statements. Blynk has already solved the problem for us using a widget known as “Eventor”. But first, flash the code below on your board.

Before flashing, don’t forget to check the ledCold & ledFrozen pin numbers, blynkToken and lastly, your SSID & Password. For assigning pin, you can take help from this diagram.

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

int ledCold = 12; // LED to indicate house's Cold
int ledFrozen = 14; // LED to indicate pipe's frozen
int blynkPinH = 4; // Blynk virtual pin for humidity
int blynkPinT = 5; //Blynk virtual pin for temperature
char blynkToken[] = "****************"; //Blynk token

SimpleTimer timer;
DHT dht(D4, DHT11);

void setup(){
    Serial.begin(9600); // Initializing the serial monitor
    Serial.println("Starting up...");
    pinMode(ledCold, OUTPUT); //Sets the ledCold pin as output
    pinMode(ledFrozen, OUTPUT); //Sets the ledFrozen pin as output

    //Initializing Blynk
    Blynk.begin(blynkToken, "ssid", "password"); 
    Serial.println("Connection established with Blynk");

    //Calling sendData every five seconds
    timer.setInterval(5000, sendData);
}

void sendData(){
    //Getting the temperature and humidity from DHT
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    Serial.println("Temperature : " + String(t) + ", Humidity : " + String(h));

    //Updating sensor values on Blynk
    Blynk.virtualWrite(blynkPinH, h);
    Blynk.virtualWrite(blynkPinT, t);
    Serial.println("Updated temperature & humidity on Blynk");

    //Updating LEDs status according to temperature
    if (t < 5){
        //Its both cold and freezing
        digitalWrite(ledCold, HIGH);
        digitalWrite(ledFrozen, HIGH);
    }else if (t < 10){
        //Its cold, but not freezing
        digitalWrite(ledCold, HIGH);
        digitalWrite(ledFrozen, LOW);
    }else {
        //Its neither cold, nor freezing
        digitalWrite(ledCold, LOW);
        digitalWrite(ledFrozen, LOW);
    }

}

void loop(){
    Blynk.run();
    timer.run();
}

Now, to send email & notification when some event happens, you simply need to use the eventor widget. You can find more about it here. Here is a sample image of the eventor widget :point_down:

1 Like

Maybe set the DHT to read every 3 seconds instead of every second.

You’re assuming that @oldvolt and his nephew both use Android phones. Eventor isn’t available in the iOS version of the app and therefore is best avoided if you want a portable solution.

Pete.

:+1:

@PeteKnight Thanks for pointing that eventor issue. @oldvolt If you and your nephew have android phones, then this code is gonna work. If not, you can make some minor changes in few lines. Since the code is now already clean, you won’t be doing much hassle now. Even if it doesn’t works, do let me know. I will surely help you out :wink:

I’m surprised that this compiles. It should read:
Blynk.virtualWrite(V8, 255); //Frozen Pipes

Also, you have this line in your void sendData function:

This resets your notified flag to zero Immediately, instead of waiting until the timeout timer expires.

The structure if your “if statements is all wrong - you’re not closing the statements with curly brackets at the correct point, so they become one big if statement and I don’t think that’s what you want. It’s much easier to follow if you put your opening curly brackets on a separate line and use indents logically…

void sendData()
{
  //Read the Temp and Humidity from DHT
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  //Write values to V04 and V05
  Blynk.virtualWrite(4, h);
  Blynk.virtualWrite(5, t);

  if(t < 10)
  {
    Blynk.virtualWrite(V1, 255); // House Cold
  }
  else
  {
   Blynk.virtualWrite(V1, 0);
  }
 
  if(t <= 5)
  {
    Blynk.virtualWrite(V8, 255);Frozen Pipes
   }
  else
  {
   Blynk.virtualWrite(V8, 0);
  }

  if (t <=5 && notified == 0)
  {
    notified = 1;
    Blynk.email("******@*****","ALARM" ,"Pipes Freezing!");
    Blynk.notify("ESP8266 Alert - Pipes Freezing!");
    timer.setTimeout(300000L, resetNotified); // 5 min between emails
  }
}

Pete.

1 Like

Vishal,Pete,daveblynk
Thanks for all your advice and support I now have the sketch up and running I am hopeless when it comes to coding and without the support of the forum members I won’t be going around in circles
Once again Thanks
oldvolt

3 Likes

Glad to hear its running now :slight_smile:

Hello. Can anyone help me with this problem. I have a light that I want to turn on when a sensor is triggered, but also if a button from the app is pushed. Also to see in the app is the light is on or off and if it is on to get a notification. The button is V0, and V2 is the sensor. Now the light is turning on if the sensor is triggerd but if I push the button from the app the light goes fast on-off.
This is what I have so far.

int LightS=35;
int LightR=2;

BLYNK_WRITE(V0)
{
  int LightV = param.asInt();
  digitalWrite(LightR,LightV);
}

void sendSensor()
{
  int LightV=analogRead(LightS);
  Blynk.virtualWrite(V2,LightV);
  
  if (V0 == 0) {
    if (LightV < 1) {
    digitalWrite(LightR,HIGH);
    Blynk.virtualWrite(V0,1); 
    } else {
    digitalWrite(LightR,LOW);
    Blynk.virtualWrite(V0,0);
    }
  }
}
void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(2,OUTPUT);
  timer.setInterval(1000L, sendSensor);
  delay(100);
}

void loop() {
  Blynk.run();
  timer.run();
}

I’d suggest that you start by creating a new “Need help with my project” topic and provide ALL of the information that is requested when you do this, as well as your full sketch.

Pete.