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

Create a timer for the notifications

#1

Hi everyone, first of all sorry for my bad english but i’m italian and i’m learning it and Blynk :slight_smile:
I’m trying to create a simple project wich is an automated plant with arduino. I’m using 2 sensors, a moisture for the soil that controls a little pump and a water sensor that sends me a notification when the water level is too low. The problem is that the notification just keep going on and on on my Android and i want just a notification every 10 minutes or so. I’m not an expert so i hope you can give me some advices to code arduino and Blynk. Thank you so much!

LiquidCrystal lcd(2, 3, 4, 5, 6, 7);              // Dichiara Pin Display LCD

#define BLYNK_PRINT Serial


#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>

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

#define W5100_CS  10
#define SDCARD_CS 4

int livelloacqua = 0;
int umiditaterra = 0;
#define LEDV1 9
#define LEDV2 10
#define LEDR1 11
#define LEDR2 12

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", 80);
  //Blynk.begin(auth, IPAddress(192,168,1,100), 8080);
  // For more options, see Boards_Ethernet/Arduino_Ethernet_Manual example

  lcd.begin(16, 2);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  lcd.clear();
}

void loop() {

  Blynk.run();

  lcd.setCursor(0, 0);
  livelloacqua = analogRead(A0);
  if (livelloacqua < 160) {
    lcd.setCursor(0, 0);
    lcd.print("Riempire acqua      ");
    Blynk.notify("L'acqua è in esaurimento! Devi riempirla!");
  }
  if (livelloacqua >= 130) {
    lcd.setCursor(0, 0);
    lcd.print("Acqua OK            ");
  }
  umiditaterra = analogRead(A1);
  if (umiditaterra > 500) {
    lcd.setCursor(0, 1);
    lcd.print("Annaffiare          ");
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, HIGH);
    digitalWrite(12, HIGH);
  }
  if (umiditaterra < 500) {
    lcd.setCursor(0, 1);
    lcd.print("Pianta OKAY         ");
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
  }


}
0 Likes

#2

introduce a new bool e.g. bool sentMessage = false;
then change:

 if (livelloacqua < 160 && !sentMessage) {
    lcd.setCursor(0, 0);
    lcd.print("Riempire acqua      ");
    Blynk.notify("L'acqua è in esaurimento! Devi riempirla!");
    sentMessage  = true;
  }

when you refil you’ll need to reset this bool, so probably add sentMessage= false e.g. in :slight_smile:

if (livelloacqua >= 130) { 

this way you get the message only once. You can also add a timer that checks whether the bool = true and then sent a new message every 1000 * 60 * 10 ms. (=10 minutes).

1 Like

#3

I tried to change the program because i was using the loop and it’s not allowed no (?)
So now it’s like this:

#include <LiquidCrystal.h>                        // Includi libreria Display LCD
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);              // Dichiara Pin Display LCD

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "b92ca63401f34dd98ef80930c0899e4f";
BlynkTimer timer;
#define W5100_CS  10
#define SDCARD_CS 4

int livelloacqua = 0;
int umiditaterra = 0;
#define LEDV1 9
#define LEDV2 10
#define LEDR1 11
#define LEDR2 12

void programma() {
  lcd.setCursor(0, 0);
  livelloacqua = analogRead(A0);
  if (livelloacqua < 160) {
    lcd.setCursor(0, 0);
    lcd.print("Riempire acqua      ");
    Blynk.notify("L'acqua è in esaurimento! Devi riempirla!");
    Blynk.tweet("Parla Arduino");
  }
  if (livelloacqua >= 130) {
    lcd.setCursor(0, 0);
    lcd.print("Acqua OK            ");
  }

  umiditaterra = analogRead(A1);
  if (umiditaterra > 500) {
    lcd.setCursor(0, 1);
    lcd.print("Annaffiare          ");
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, HIGH);
    digitalWrite(12, HIGH);
  }
  if (umiditaterra < 500) {
    lcd.setCursor(0, 1);
    lcd.print("Pianta OKAY         ");
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
  }
}
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", 80);
  //Blynk.begin(auth, IPAddress(192,168,1,100), 8080);
  // For more options, see Boards_Ethernet/Arduino_Ethernet_Manual example

  lcd.begin(16, 2);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  lcd.clear();

   livelloacqua = analogRead(A0);
  Blynk.virtualWrite(V1, livelloacqua);
  umiditaterra = analogRead(A1);
  Blynk.virtualWrite(V2, umiditaterra);

  timer.setInterval(1000L, setup);
}

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

Now my prolem is that i can see the values of the sensors but i don’t receive nothing from the other void program, i can’t even turn on a led, like that part of the sketch dosen’t exist.

0 Likes

#4

Should be

timer.setInterval(1000L, programma); //run programma every 1 second
1 Like

#5

Remove this from your setup

and add it to your programma

1 Like

#6

ok, i’m trying

0 Likes

#7

Now it works but i receive the same problem of the gazzilion of notification. I want that if the water is not refilled a notification every…maybe 20 minutes

0 Likes

#8

Follow @wolph42 advise about using another variable as a check for the notification being sent.

1 Like

#9

Ok perfect, now the blynk part works just fine but the entire old sketch dosen’t work. I uploaded the old sketch without the blynk part and works like a charm but if i upload this sketch the dispaly and the leds are dead. The first sketch:

LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

int livelloacqua = 0;
int umiditaterra = 0;
#define LEDV1 9
#define LEDV2 10
#define LEDR1 11
#define LEDR2 12

void setup() {

  lcd.begin(16, 2);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  lcd.clear();
}

void loop() {

  lcd.setCursor(0, 0);
  livelloacqua = analogRead(A0);
  if (livelloacqua < 160) {
    lcd.setCursor(0, 0);
    lcd.print("Riempire acqua      ");
  }
  if (livelloacqua >= 130) {
    lcd.setCursor(0, 0);
    lcd.print("Acqua OK            ");
  }
  umiditaterra = analogRead(A1);
  if (umiditaterra > 500) {
    lcd.setCursor(0, 1);
    lcd.print("Annaffiare          ");
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, HIGH);
    digitalWrite(12, HIGH);
  }
  if (umiditaterra < 500) {
    lcd.setCursor(0, 1);
    lcd.print("Pianta OKAY         ");
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
  }
}


and the new sketch with blynk:

#include <LiquidCrystal.h>                        // Includi libreria Display LCD
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);              // Dichiara Pin Display LCD

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "b92ca63401f34dd98ef80930c0899e4f";
BlynkTimer timer;
#define W5100_CS  10
#define SDCARD_CS 4

int livelloacqua = 0;
int umiditaterra = 0;
int notifica = false;
#define LEDV1 9
#define LEDV2 10
#define LEDR1 11
#define LEDR2 12

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", 80);
  //Blynk.begin(auth, IPAddress(192,168,1,100), 8080);
  // For more options, see Boards_Ethernet/Arduino_Ethernet_Manual example

  lcd.begin(16, 2);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  lcd.clear();
  timer.setInterval(1000L, programma);
}

void programma() {
  livelloacqua = analogRead(A0);
  Blynk.virtualWrite(V1, livelloacqua);
  umiditaterra = analogRead(A1);
  Blynk.virtualWrite(V2, umiditaterra);
  lcd.setCursor(0, 0);
  livelloacqua = analogRead(A0);
  if ((livelloacqua < 160 && !notifica)) {
    lcd.setCursor(0, 0);
    lcd.print("Riempire acqua      ");
    Blynk.notify("L'acqua è in esaurimento! Devi riempirla!");
    notifica = true;
  }
  if ((livelloacqua >= 130) && (notifica = false)) {
    lcd.setCursor(0, 0);
    lcd.print("Acqua OK            ");
  }

  umiditaterra = analogRead(A1);
  if (umiditaterra > 500) {
    lcd.setCursor(0, 1);
    lcd.print("Annaffiare          ");
    digitalWrite(9, LOW);
    digitalWrite(10, LOW);
    digitalWrite(11, HIGH);
    digitalWrite(12, HIGH);
  }
  if (umiditaterra < 500) {
    lcd.setCursor(0, 1);
    lcd.print("Pianta OKAY         ");
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
  }
}


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

Where's the problem?

EDIT:
The entire program works just with the ethernet cable in...why?
0 Likes

#10

That is how you have it set up.

What hardware are you using? Arduino with ethernet shield? ESP8266?

1 Like

#11

Arduino mega with ethernet shield

0 Likes

#12

Change this

to

if ((livelloacqua >= 130) && (notifica = true)) {
   lcd.setCursor(0, 0);
   lcd.print("Acqua OK            ");
   notifica = flase;
 }
1 Like

#13

I think you mean:
if ((livelloacqua >= 130) && (notifica == true)) {

1 Like

#14

Yes, Sorry was just copying and pasting from the posted code

1 Like

#15

uh, yep i miss that.
But the connection problem? Do you have any solution?

0 Likes

#16

What is the connection problem? It wont connect to BLYNK, or that you must keep Ethernet cable plugged it?

1 Like

#17

from another thread with similar issue:

Blynk.begin() is a blocking routine… no connection no run.

Try Blynk.config() with prior WiFi connection and reconnection routines…

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

1 Like

#18

The problem is that i NEED an internet connection to let the circuit work. Even the sensors values on the display lcd doesn’t work without the connection. I would like a circuit that coul work fine even if the internet drops

0 Likes

#19

This was taken from @Gunner’s code examples found HERE

change your void loop() to this

void loop() {
  timer.run();
  if (Blynk.connected()) {  // If connected run as normal
    Blynk.run();
  } else if (ReCnctFlag == 0) {  // If NOT connected and not already trying to reconnect, set timer to try to reconnect in 30 seconds
    ReCnctFlag = 1;  // Set reconnection Flag
    Serial.println("Starting reconnection timer in 30 seconds...");
    timer.setTimeout(30000L, []() {  // Lambda Reconnection Timer Function
      ReCnctFlag = 0;  // Reset reconnection Flag
      ReCnctCount++;  // Increment reconnection Counter
      Serial.print("Attempting reconnection #");
      Serial.println(ReCnctCount);
      Blynk.connect();  // Try to reconnect to the server
    });  // END Timer Function
  }
}

and add this routine

BLYNK_CONNECTED() {
  Serial.println("Cconnected");
  ReCnctCount = 0;
}

and add these variables

int ReCnctFlag;  // Reconnection Flag
int ReCnctCount = 0;  // Reconnection counter
1 Like

#20

Ok, let’s try

0 Likes