(Solved) Help with coding notification delays

Im new to Blynk and think its awesome what they are doing to make things in the IoT World simpler as Im new to Coding but I agree with soundmaster I am getting tons of notifications when my temp is right at the threshold like 1 every 10 sec. Im wanting to get a notification when my greenhouse gets above 95 degrees so i can open a door to cool it off if the fan its self can’t cool it down. Like I said Im a beginner in coding so not sure if there is something i can write in my code to have it send me 1 notification like every 30min to an hour. If so I can post my code.

Help would be appreciated
Brian

Yes, there is, timers… covered quite frequently and with many examples all throughout this forum.

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

One option is to use the principal of hysteresis, which is used quite often on heating control circuits.
This builds a bit of tolerance into the system.

If your target temp is 95° (Fahrenheit I hope!) then send one message once this target temperature is met and use a variable as a flag, which is set to ‘true’.
If the flag is true then don’t send any more ‘open the windows’ messages.
Once your temperature drops below say 93° you can set your flag to false and send another message saying that you should close your greenhouse windows. Once again, only sent this close message once while the flag is false.
When the temperature hits 95° again and the flag is false then you send the ‘open the windows’ message again.

When you get bored with opening and closing the windows yourself the fit some motorised openers and open/close them automatically :grinning:

Pete.

1 Like

Pete could you give an example code like I said Im new to coding and not sure how to write what your saying

Hi @bcomer. You wont get people to write your code for you, but we will help you by pointing you in the right direction and giving feedback on your progress to date.
However, if you post a question then don’t come back and respond to the answers it’s not much of an incentive for community members to lend a hand.

It also doesn’t help that you’ve also created another thread about the same sort of subject today.
I’d suggest that you delete your new thread, then post your question on this thread, along with your FULL code. The code you posted in the other topic doesn’t have any lines that would update the Blynk switch widget, so we need to see it all if we’re going to be able to help.

Pete,

Pete

Like I said I am new at this I created a new thread concerning a button I cant get to work! I figured I would get an email or something stating someone replied to my old post which I never received so I figured I would add it to the new post I can remove that part from the new post sorry.

I had a similar question as you, and I was helped by this post

so I have moved everything to its own function and my question is do I use millis or a library timer?

Either will work, but a timer requires less coding.

Pete.

SimpleTimer (aka BlynkTimer) makes using the millis() timer simple.

1 Like

next question is are the timer calls placed in setup, loop, or in the functions I created?

All 3 and in definitions. See PUSH_DATA example for the elements of SimpleTimer.

Many users put the setInterval in setup() but it can go anywhere except loop() that should be kept to a maximum of 2 lines of code.

Depends on the type of timer, what you want it to do and whether it is a continuously running or temporarily running timer… checkout #12 here for some timer examples

Ok I have the sketch working with timers but my next question is will a delay within a function halt the sketch? I have a LCDdisplay function displaying Name then switching to Greenhouse Temp and then Time with a delay of 3 seconds between screens. If delay does halt sketch what is the best way to get around this? I went with the BlynkTimer

   void LCDdisplay()
{
// set up the LCD's number of columns and rows:
  // Print a message to the LCD.
    lcd.clear ();
    lcd.setCursor(0, 0);
    lcd.print ("Windy Ridge Farm");
    delay(3000);

  lcd.clear ();
  lcd.print("Greenhouse Temp:" );
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  lcd.print(GreenhouseTemp);
  lcd.print((char)223);
  lcd.print ("F");
  delay (3000);


DateTime now = rtc.now();

   char dateBuffer[12];

      //Print Second Line
    lcd.clear ();
    lcd.setCursor(0, 0);
    lcd.print ("Eastern Time:");
    lcd.setCursor(0, 1);
    sprintf(dateBuffer,"%02u:%02u ",now.hour(),now.minute());
    lcd.print(dateBuffer);
  
   delay (3000);

}

Yes, it halts everything

Fancy use of timers… look at the link I sent already in previous post (links are in green… at least on a PC)

Gunner Thanks for all your help!! I think I finally got it figured out I will post the updated code with no delay to get your thoughts on anything else i could do to make it better. The sketch compiles and everything is working as intended



/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************

  You’ll need:
   - Blynk App (download from AppStore or Google Play)
   - Arduino Ethernet board
   - Decide how to connect to Blynk
     (USB, Ethernet, Wi-Fi, Bluetooth, ...)

  There is a bunch of great example sketches included to show you how to get
  started. Think of them as LEGO bricks  and combine them as you wish.
  For example, take the Ethernet Shield sketch and combine it with the
  Servo example, or choose a USB sketch and add a code from SendData
  example.
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display
#define ONE_WIRE_BUS 7          //Arduino Mega Digital Pin 7
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
BlynkTimer timer;

RTC_DS3231 rtc;

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

#define W5100_CS  10
#define SDCARD_CS 4

/*-----( Declare Constants )-----*/
#define RELAY_ON 0
#define RELAY_OFF 1
/*-----( Declare objects )-----*/
/*-----( Declare Variables )-----*/
#define Relay_1  40  // Arduino Digital I/O pin number
#define Relay_2  41
#define Relay_3  42
#define Relay_4  43

int displayState = 0; 



int GreenhouseTemp;            // Greenhouse temperature in F

unsigned long previousMillis1 = 0;        // will store last time Display was updated
long interval = 3000; // m

//int timeState = LOW;

unsigned long previousMillis2 = 0;       // will store last time Time was updated
long OnTime = 100;           // milliseconds of on-time
long OffTime = 200;          // milliseconds of off-time

int sensorPin = 0;    // select the input pin for the potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor



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

  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card

  //Blynk.begin(auth);
  // You can also specify server:
  Blynk.begin(auth, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, IPAddress(192,168,10,222), 8442);

  // Send e-mail when your hardware gets connected to Blynk Server
  // Just put the recepient's "e-mail address", "Subject" and the "message body"
  Blynk.email("***********************", "Subject", "Greenhouse is online.");
  

     lcd.init();      // initialize the lcd 
     lcd.backlight(); // Turn on LCD Backlight
     
   // Uncomment to set time to PC time
  //rtc.begin();
  //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    //Serial.println("RTC lost power, lets set the time!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust (DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // Adjust Time on line Below 
    //rtc.adjust(DateTime(2017, 8, 23, 10, 36, 0));
  }

  Serial.begin(9600);

      //---( THEN set pins as outputs )----  
  pinMode(Relay_1, OUTPUT);   
  pinMode(Relay_2, OUTPUT);  
  pinMode(Relay_3, OUTPUT);  
  pinMode(Relay_4, OUTPUT);    
  //delay(1000); //Check that all relays are inactive at Reset
    
    
    //-------( Initialize Pins so relays are inactive at reset)----
  digitalWrite(Relay_1, RELAY_OFF);
  digitalWrite(Relay_2, RELAY_OFF);
  digitalWrite(Relay_3, RELAY_OFF);
  digitalWrite(Relay_4, RELAY_OFF); 



 timer.setInterval(1000L, Date_Time );
 timer.setInterval(1000L, sendTemps );
 timer.setInterval(3000L, LCDdisplay );
 timer.setInterval(1000L, Temp );  
 timer.setInterval(1000L, sendTime );
}


void loop()
{
    Blynk.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!

    timer.run();
}
  
   
   void sendTemps()
{
  sensors.requestTemperatures();                  // Polls the sensors.
  GreenhouseTemp = sensors.getTempFByIndex(0);   // Stores temperature. Change to getTempCByIndex(0) for celcius.
  Blynk.virtualWrite(V5, GreenhouseTemp);         // Send temperature to Blynk app virtual pin 1.
}

   void sendTime()
 {
  DateTime now = rtc.now();

   char dateBuffer[12];

   sprintf(dateBuffer,"%02u:%02u ",now.hour(),now.minute());
   Serial.println(dateBuffer);

   Blynk.virtualWrite(V1,dateBuffer);   

 }
   
   void LCDdisplay()
{

 // check to see if it's time to change the state of the LED
  unsigned long currentMillis = millis();
 
  if((displayState  == 0) && (currentMillis - previousMillis1 >= interval))
  {
    displayState = 1;  // Turn it off
    // set up the LCD's number of columns and rows:
  // Print a message to the LCD.
    lcd.clear ();
    lcd.setCursor(0, 0);
    lcd.print ("Windy Ridge Farm");
  }
  
     else if ((displayState == 1) && (currentMillis - previousMillis1 >= interval))
  {
    displayState = 2;  // turn it on
    previousMillis1 = currentMillis;   // Remember the time
    lcd.clear ();
    lcd.print("Greenhouse Temp:" );
    // set the cursor to column 0, line 1
    // (note: line 1 is the second row, since counting begins with 0):
    lcd.setCursor(0, 1);
    lcd.print(GreenhouseTemp);
    lcd.print((char)223);
    lcd.print ("F");
  }
  
  else if((displayState  == 2) && (currentMillis - previousMillis1 >= interval))
  {
    displayState = 0;  // Turn it off
     DateTime now = rtc.now();
      char dateBuffer[12];
      
      //Print Second Line
    lcd.clear ();
    lcd.setCursor(0, 0);
    lcd.print ("Eastern Time:");
    lcd.setCursor(0, 1);
    sprintf(dateBuffer,"%02u:%02u ",now.hour(),now.minute());
    lcd.print(dateBuffer);
    
  }

}


   void Date_Time ()
{
  DateTime now = rtc.now();

   char dateBuffer[12];

   sprintf(dateBuffer,"%02u-%02u-%04u ",now.month(),now.day(),now.year());
   Serial.print(dateBuffer);

   sprintf(dateBuffer,"%02u:%02u:%02u ",now.hour(),now.minute(),now.second());
   Serial.println(dateBuffer);
  

      
      // ********Watering Schedule***********

  int SchOn1 = (now.hour() == 8 & now.minute() == 30 & now.second() <= 10);
  int SchOff1= (now.hour() == 8 & now.minute() == 33 & now.second() <= 10);     
  int SchOn2 = (now.hour() == 17 & now.minute() == 30 & now.second() <= 10);
  int SchOff2= (now.hour() == 17 & now.minute() == 33 & now.second() <= 10);   
         
         
         
     unsigned long currentMillis = millis();
 
    if((SchOn1) && (currentMillis - previousMillis2 >= OnTime))
  {
    Blynk.notify("Plants Just Watered!");
    Blynk.email("***********************", "Subject: Greenhouse", "Plants Just Watered!");
    digitalWrite(Relay_2, RELAY_ON);// Turn the Watering Relay ON
  }
    
    else if ((SchOff1) && (currentMillis - previousMillis2 >= OffTime))
  {
    previousMillis2 = currentMillis;   // Remember the time
    digitalWrite(Relay_2, RELAY_OFF);// Turn the Watering Relay OFF
  }

    if((SchOn2) && (currentMillis - previousMillis2 >= OnTime))
  {
    Blynk.notify("Plants Just Watered!");
    Blynk.email("***********************", "Subject: Greenhouse", "Plants Just Watered!");
    digitalWrite(Relay_2, RELAY_ON);// Turn the Watering Relay ON
  }
  
   else if ((SchOff2) && (currentMillis - previousMillis1 >= OffTime))
  {
    previousMillis2 = currentMillis;   // Remember the time
    digitalWrite(Relay_2, RELAY_OFF);// Turn the Watering Relay OFF
  }
  
 }

 
 


void Temp()
{
     //***********Cooling Code***************

  if (GreenhouseTemp >= 85)
     { digitalWrite (Relay_1, RELAY_ON);}//Greenhouse Temp Control Fan ON Above 85 Degrees
   
  else if  ((GreenhouseTemp > 73)  && (GreenhouseTemp< 75))

     { digitalWrite (Relay_1, RELAY_OFF);}  //Greenhouse Temp Control  coolingFan OFF Below 75 Degrees
   
    bool tempflag = true;  
   
   if ((GreenhouseTemp >= 95)  && (tempflag == true))
  {
   Blynk.notify("GREENHOUSE OVERHEATING!!!");
   tempflag = false;
  }

if (GreenhouseTemp < 95)
{
  tempflag = true;
}



    //*************Heating Code****************
    
  if  ((GreenhouseTemp > 55)  && (GreenhouseTemp< 57))  
  { digitalWrite (Relay_3, RELAY_OFF);}    //Greenhouse Temp Control Heater Fan OFF Above 55 Degrees F

  if (GreenhouseTemp  <= 45.00) 
  { digitalWrite (Relay_3, RELAY_ON);}  //Greenhouse Temp Control Heater Fan ON Below 45 Degrees F


}


yes bl**dy backticks.

is that better?

Nope
3 backticks
sketch
3 backticks

i don’t understand what your talking about what is a backtick