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

Blynk email notifications

I’m just using the example sketch provided by blynk. It works, but only once, after that the arduino loses connection. (I’m using a lan shield) I think the answer is here Email when physical button is pressed But I’m not sure how to implement this into the example sketch. I’m not proficient with writing code for arduino so please excuse my blatant ignorance. I’m trying to build a high water level alert for my sump pit, I will be using a float switch so in a high water event the circuit would remain closed until the water went back down.
Thanks in advance!!

{
  // *** WARNING: You are limited to send ONLY ONE E-MAIL PER 5 SECONDS! ***

  // Let's send an e-mail when you press the button
  // connected to digital pin 2 on your Arduino

  int isButtonPressed = !digitalRead(2); // Invert state, since button is "Active LOW"

  if (isButtonPressed) // You can write any condition to trigger e-mail sending
  {
    Serial.println("Button is pressed."); // This can be seen in the Serial Monitor

    count++;

    String body = String("You pushed the button ") + count + " times.";

    Blynk.email("kscolesaccount@gmail.com", "Subject: Button Logger", body);

    // Or, if you want to use the email specified in the App (like for App Export):
    //Blynk.email("Subject: Button Logger", "You just pushed the button...");
  }
}
void setup()
{
pinMode (3, OUTPUT);
digitalWrite (3, HIGH);
  // 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,1,100), 8442);
  // For more options, see Boards_Ethernet/Arduino_Ethernet_Manual example
  pinMode(2, INPUT_PULLUP);
  // Attach pin 2 interrupt to our handler
  attachInterrupt(digitalPinToInterrupt(2), emailOnButtonPress, CHANGE);

}

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

Before creating the topic

  1. Search forum for similar topics
  2. Check http://docs.blynk.cc and http://help.blynk.cc/
  3. Add details :
    • Hardware model + communication type. For example: Arduino UNO with Ethernet Shield
    • Smartphone OS (iOS or Android) + version
    • Blynk server or local server
    • Blynk Library version
    • Add your sketch code. :point_up:Code should be formatted as example below.

Simply paste your code between ``` If you don’t format your code, your topic can be deleted by moderators.

```cpp

void loop()

```

I tried to add my sketch code but it just gives me an error and says new users are only allowed 1 link per post. :man_shrugging:

The message about the number of hyperlinks occurs because you aren’t surrounding your code with triple backticks.

@Dave1829 has provided the information about how to do this, and it was in the information that you deleted from your post before making your initial post - which you seem to have ignored.

There are dozens of examples of people doing the same thing. Searching and reading will help, as members of the community aren’t going to build your project for you.

You will probably find that your choice of hardware will cause you problems, as the Ethernet shields are notorious for being unreliable. You may be lucky and get a good one, but for every reliable shield there seem to be dozens of unreliable ones. This isn’t a Blynk thing, it’s just a problem with the design of the shield and the electrical and timing tolerances.
The issue is usually around getting the shield to boot-up correctly when the power is re-applied, but the shields are also very picky about the type of Ethernet switch they can be connected to.
Read this for more info about hardware choices…

Pete.

Ah looks like I simply tried to use the wrong symbol. With that out of the way, my shield has worked reliably on several other projects. I’m open to different hardware if needs be. Also I’m not asking anyone else to build my project. Just wanted some assistance, which I though I would find here. Hopefully I’m not wrong.

OK, try asking some questions.

Your op only has statements.

The link I posted says that example sketch spams the notification command and causes blynk to disconnect. How can I fix this?

Can you tell us what your serial output is?

large pieces of your code appear to be missing.

Pete.

Ok some of the code on my first post got cut out. Here is the whole thing.

  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
    Social networks:            http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

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

 *************************************************************
  This example shows how to use Arduino Ethernet shield (W5100)
  to connect your project to Blynk.

  NOTE: Pins 10, 11, 12 and 13 are reserved for Ethernet module.
        DON'T use them in your sketch directly!

  WARNING: If you have an SD card, you may need to disable it
        by setting pin 4 to HIGH. Read more here:
        https://www.arduino.cc/en/Main/ArduinoEthernetShield

  Feel free to apply it to any other example. It's simple!
 *************************************************************/

/* Comment this out to disable prints and save space */
#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[] = "tYMhbkrhYLXBeH5jACdbmppzUKh45Lhh";

#define W5100_CS  10
#define SDCARD_CS 4
unsigned count = 0;

void emailOnButtonPress()
{
  // *** WARNING: You are limited to send ONLY ONE E-MAIL PER 5 SECONDS! ***

  // Let's send an e-mail when you press the button
  // connected to digital pin 2 on your Arduino

  int isButtonPressed = !digitalRead(2); // Invert state, since button is "Active LOW"

  if (isButtonPressed) // You can write any condition to trigger e-mail sending
  {
    Serial.println("Button is pressed."); // This can be seen in the Serial Monitor

    count++;

    String body = String("You pushed the button ") + count + " times.";

    Blynk.email("kscolesaccount@gmail.com", "Subject: Button Logger", body);

    // Or, if you want to use the email specified in the App (like for App Export):
    //Blynk.email("Subject: Button Logger", "You just pushed the button...");
  }
}
void setup()
{
pinMode (3, OUTPUT);
digitalWrite (3, HIGH);
  // 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,1,100), 8442);
  // For more options, see Boards_Ethernet/Arduino_Ethernet_Manual example
  pinMode(2, INPUT_PULLUP);
  // Attach pin 2 interrupt to our handler
  attachInterrupt(digitalPinToInterrupt(2), emailOnButtonPress, CHANGE);

}

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

Serial monitor doesn’t show anything except my IP.

So you aren’t seeing the “Button is pressed.” message?

Pete.

I am seeing it now. Maybe I missed it before. This is what I’m seeing.

[5622] IP:192.168.0.107
[5623] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.0 on Arduino Uno

[5735] Connecting to blynk-cloud.com:8442
[37988] Connecting to blynk-cloud.com:8442
[38346] Ready (ping: 47ms).
Button is pressed.
Bu

If you think my ethernet shield is causing the problem, is there a different board you would recommend? I read through your article but didn’t see any options with an ethernet port.

You are using interrupt not correctly. ISR (Interrupt Service Routine) must be lean and mean, without using time-consuming functions such as Blynk.email(), Blynk.virtualWrite(), etc…
Please do some research about writing ISRs before using it to avoid unnecessary headache.

Anyway, try this example in BlynkEthernet_WM library, just written based on your posted sketch

The fixed code

/****************************************************************************************************************************
 * W5100_Blynk_Email.ino
 * For Mega/UNO/Nano boards
 *
 * BlynkEthernet_WM is a library for Mega/UNO/Nano AVR boards, with Ethernet W5X00 board,
 * to enable easy configuration/reconfiguration and autoconnect/autoreconnect of Ethernet/Blynk
 * 
 * Library forked from Blynk library v0.6.1 https://github.com/blynkkk/blynk-library/releases
 * Built by Khoi Hoang https://github.com/khoih-prog/Blynk_WM
 * Licensed under MIT license
 * Version: 1.0.4
 *
 * Original Blynk Library author:
 * @file       BlynkSimpleEsp8266.h
 * @author     Volodymyr Shymanskyy
 * @license    This project is released under the MIT License (MIT)
 * @copyright  Copyright (c) 2015 Volodymyr Shymanskyy
 * @date       Jan 2015
 * @brief
 *
 * Version Modified By   Date      Comments
 * ------- -----------  ---------- -----------
 *  1.0.4   K Hoang     12/01/2020 First release v1.0.4 in synch with Blynk_WM library v1.0.4
 *****************************************************************************************************************************/

#if defined(ESP8266) || defined(ESP32)
#error This code is designed to run on Arduino AVR (Nano, UNO, Mega, etc.) platform, not ESP8266 nor ESP32! Please check your Tools->Board setting.
#endif

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

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

// Start location in EEPROM to store config data. Default 0
// Config data Size currently is 128 bytes)
#define EEPROM_START     256

#define USE_SSL     false

#if USE_SSL
  // Need ArduinoECCX08 and ArduinoBearSSL libraries
  // Currently, error not enough memory for UNO, Mega2560. Don't use
  #include <BlynkSimpleEthernetSSL_WM.h>
#else
  #include <BlynkSimpleEthernet_WM.h>
#endif

#define USE_BLYNK_WM      true

#if !USE_BLYNK_WM
  #define USE_LOCAL_SERVER      true

  #if USE_LOCAL_SERVER
    char auth[] = "******";
    char server[] = "your_account.duckdns.org";
    //char server[] = "192.168.2.112";
  #else
    char auth[] = "******";
    char server[] = "blynk-cloud.com";
  #endif
  
  #define BLYNK_HARDWARE_PORT       8080
#endif

BlynkTimer timer;

#define W5100_CS        10
#define SDCARD_CS       4
#define BUTTON_PIN      2

volatile unsigned int count       = 0;
volatile bool isButtonPressed     = false;

void emailOnButtonPress()
{ 
  if ( !isButtonPressed && !digitalRead(BUTTON_PIN)) // You can write any condition to trigger e-mail sending
  {
    isButtonPressed = true;
    count++;
    Serial.println("Button pressed");
  }
  
}

void processButton(void)
{
  // *** WARNING: You are limited to send ONLY ONE E-MAIL PER 5 SECONDS! ***
  // Let's send an e-mail when you press the button
  // connected to digital pin BUTTON_PIN (2) on your Arduino
  static String body;
  
  if (isButtonPressed) // You can write any condition to trigger e-mail sending
  {
    body = String("You pushed the button ") + count + " times.";

    // This can be seen in the Serial Monitor
    Serial.println(body);

    Blynk.email("your_email@gmail.com", "Subject: Button Logger", body);

    isButtonPressed = false;
  }
  
}

void setup()
{
  // Debug console
  Serial.begin(115200);
  Serial.println(F("\nStart W5100_Blynk_Email"));

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

#if USE_BLYNK_WM
  Blynk.begin();
#else
  #if USE_LOCAL_SERVER
    Blynk.begin(auth, server, BLYNK_HARDWARE_PORT);
  #else
    Blynk.begin(auth);
    // You can also specify server:
    //Blynk.begin(auth, server, BLYNK_HARDWARE_PORT);
  #endif
#endif

  if (Blynk.connected())
  {
    Serial.print(F("Conn2Blynk: server = "));
    Serial.print(Blynk.getServerName());
    Serial.print(F(", port = "));
    Serial.println(Blynk.getHWPort());
    Serial.print(F("Token = "));
    Serial.print(Blynk.getToken());  
    Serial.print(F(", IP = "));
    Serial.println(Ethernet.localIP());
  }

  // Attach pin BUTTON_PIN (2) interrupt to our handler
  attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), emailOnButtonPress, FALLING /*CHANGE*/);

  timer.setInterval(30000L, processButton);
}

void heartBeatPrint(void)
{
  static int num = 1;

  if (Blynk.connected())
    Serial.print(F("B"));
  else
    Serial.print(F("F"));
  
  if (num == 80) 
  {
    Serial.println();
    num = 1;
  }
  else if (num++ % 10 == 0) 
  {
    Serial.print(F(" "));
  }
} 

void check_status()
{
  static unsigned long checkstatus_timeout = 0;

#define STATUS_CHECK_INTERVAL     60000L

  // Send status report every STATUS_REPORT_INTERVAL (60) seconds: we don't need to send updates frequently if there is no status change.
  if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0))
  {
    heartBeatPrint();
    checkstatus_timeout = millis() + STATUS_CHECK_INTERVAL;
  }
}

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

Hereafter is the terminal output showing it’s working OK


Start W5100_Blynk_Email
[0] EEPROM, sz:4096
[0] Hdr=W5X00,Auth=****
[3] Svr=your_account.duckdns.org,Port=8080
[6] SIP=192.168.2.79,BName=W5100_Blynk
[9] MAC: FE-F6-B5-F8-EC-EC
Ethernet begin: IP = 192.168.2.79, dns = 192.168.2.1, gateway = 192.168.2.1, subnet = 255.255.255.0
[1574] GetIP:
[1575] IP:192.168.2.79
[1575] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.6.1 on Arduino Mega

[1586] bg: E.con.Try B
[1903] Ready (ping: 5ms).
[1971] bg: EBconn'd
Conn2Blynk: server = your_account.duckdns.org, port = 8080
Token = ****, IP = 192.168.2.79
B
Button pressed
You pushed the button 1 times.
B
Button pressed
You pushed the button 2 times.
BBBBBBBB BBB

Your Blynk library is extremely out of date, the current version is 0.6.1

Pete.

Ok wasn’t aware of that. I’ll update it.