Blynk + Email (Arduino UNO with Adafruit WINC1500 WiFi Shield)

I’m having problem with my project. Everything works until I try to send email using
What happens is, my counter is set to 0, when you trigger the sensor first time, it counts 1 and plays Audio1, the second time you hit it, it counts 2 and plays Audio2. After about 5 seconds it stops and resets counter.
Everything works to this point as planned.


When I add"", “Subject”, “Alarm Triggered!”); to one of the if() statements
I still receive email but the counter stops counting and gets stuck in 1. It never counts up after you hit the sensor.

Here is my code:

#define Audio1 2 // Primary Warning
#define Audio2 3 // Secondary Warning

#define BLYNK_PRINT Serial // Blynk
#include <SPI.h> // Blynk
#include <WiFi101.h> // Blynk
#include <BlynkSimpleWiFiShield101.h> // Blynk

int VibrationSensor1 = 10; // Sensor #1
int VibrationSensor2 = 11; // Sensor #2

unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 1000;
const unsigned long ALARM_DURATION = 5000; // 5 Seconds Timeout
const unsigned long HIT_RESET_TIME = 900000; // 15 Minutes Reset Time

char auth[] = “AUTH TOKEN”; // Blynk
char ssid[] = “SSID”; // Blynk
char pass[] = “PASSWORD”; // Blynk

void setup() {

pinMode(Audio2, OUTPUT); // Audio2 OUTPUT
digitalWrite(Audio2, HIGH); // Audio2 Standby
pinMode(Audio1, OUTPUT); // Audio1 OUTPUT
digitalWrite(Audio1, HIGH); // Audio1 Standby
pinMode(VibrationSensor1, INPUT); // set VibrationSensor1 input for measurment
pinMode(VibrationSensor2, INPUT); // set VibrationSensor2 input for measurment
Serial.begin(9600); // init serial 9600
startMillis = millis(); // initial start time
Serial.println(“System Ready!”);
Blynk.begin(auth, ssid, pass);

void loop() {

static int hitCount;
static unsigned long alarmStartMs; // when alarm was turned on
static boolean alarmOn; // is alarm turned on?
static unsigned long firstHitMs; // when the first hit happened

long measurement1 = TP_init1();
long measurement2 = TP_init2();
Serial.print(“HitCount = “);

if (alarmOn) {
if (millis() - alarmStartMs >= ALARM_DURATION) {

  Serial.println("Turn Alarm Off");
  //reset variables
  alarmOn = false;
  hitCount = 0;
  //turn off
  digitalWrite(Audio2, HIGH);      // Turn OFF Audio2


if (hitCount > 0 && millis() - firstHitMs >= HIT_RESET_TIME) {
hitCount = 0;

if ((measurement1 >= 1000) || (measurement2 >= 500)) { //Sensitivity of Sensor 1 and Sensor 2
hitCount += 1;

if (hitCount <= 1) {
  //First hit
  Serial.println("First Hit...");
  digitalWrite(Audio1, LOW);       // Turn ON Audio1
  digitalWrite(Audio1, HIGH);      // Turn OFF Audio1"", "Subject", "Alarm Triggered!");    // HERE IS MY PROBLEM, ONCE THIS LINE IS ACTIVE COUNTER STOPS WORKING
else if (hitCount >= 2 && !alarmOn) {
  //Second hit
  Serial.println("Second hit - Turn alarm on!");
  digitalWrite(Audio2, LOW);            // Turn ON Audio2
  alarmOn = true;
  alarmStartMs = millis();


long TP_init1() {
long measurement1 = pulseIn (VibrationSensor1, HIGH); //wait for the pin to get HIGH and returns measurement //Sensor 1
return measurement1;
long TP_init2() {
long measurement2 = pulseIn (VibrationSensor2, HIGH); //wait for the pin to get HIGH and returns measurement //Sensor 2
return measurement2;

Thank you!

You didn’t format your posted code as required in the Welcome Topic and on the topic instructions, that you cleared when posting this… I have fixed your post (twice). Please follow the directions in the future. Thank you.

Blynk - FTFC

As for your issue, we are not code mechanics here, we try to assist with learning about Blynk. And on that note, you shouldn’t have all that code in your void loop() when running Blynk. Use timed functions instead.

The void loop() runs thousands of times a second and you are probably flooding something when initiating an email and causing either a Blynk server disconnection or system block due to too many emails.

See Avoiding the void in this document…

Hey Andrei, I have a similar problem with my Shock Sensor. Whenever I try to use it with Blynk app, it causes them not to work properly. Anyway you could share your fix with me? Maybe it will help me understand how blynk works with our code.

the issue is due to the void loop, no more problem !

I have Relay Shield (with 4 relays) and Wifi Shield attached to my Arduino UNO and relay uses PINS 4-7
When I upload this code to my Arduino, I get PINS triggered on and off and PIN 3 stays ON
I get my email notifications delivered but whats up with PINS?
If I run other functions, everything gets stuck. What is going on?

Here is my CODE:

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <WiFi101.h>
#include <BlynkSimpleWiFiShield101.h>
#define BlueLED 3

char auth[] = "TOKEN";
char ssid[] = "SSID";
char pass[] = "PASS";

void setup()
  pinMode(BlueLED, OUTPUT);
  digitalWrite(BlueLED, HIGH);
  Blynk.begin(auth, ssid, pass);"", "Subject", "My Blynk project is online.");

void loop()

No in the void loop?


no, no code run

there is no code to run !
where are relay code ?

if there is no code to run, why wouild it set PIN 2 and 3 to HIGH?
I applied to code to keep PINS LOW with:

pinMode(3, OUTPUT);
digitalWrite(3, LOW);

I did that for all 4 PINs, 4-7 and it still triggers them anyway.

Relay Shield is sitting on top of WIFI Shield 101 which is sitting on TOP of the Arduino UNO.
Relay Shield has only 4 PINS that are actually used if anything, every other header is pass through.

I just hooked up another Arduino UNO, a brand new from a box and i still get PINS 2 and 3 on HIGH
which would trigger relay if I had something connected to it.!

this is my code for 4 relay board

/*************** Relays *********************/
int relay1 = 5;
int relay2 = 4;
int relay3 = 16;
int relay4 = 0;
int ON = 0;
int OFF = 1;
int RelayBTN1 = 0;
int RelayBTN2 = 0;
int RelayBTN3 = 0;
int RelayBTN4 = 0;
int RelayAllOff = 0;

/*************** LEDS *********************/
WidgetLED led1(10); //virtual led
WidgetLED led2(11); //virtual led
WidgetLED led3(12); //virtual led
WidgetLED led4(13); //virtual
WidgetLED led5(14); //virtual
WidgetLED led6(15); //virtual
WidgetLED led7(16); //virtual
WidgetLED led8(17); //virtual
WidgetLED led9(5); //virtual
const int checkInterval = 1000;

/*************** Setup *********************/
void setup()
  Blynk.begin(auth, ssid, pass, IPAddress(192, 168, 0, 1), 8088);
  /************************ OTA ***************/
  ArduinoOTA.setHostname("Nodemcu02"); //OTA Set the name of the network port
  ArduinoOTA.setPassword((const char *)"0000"); //OTA Set access password for remote firmware
  ArduinoOTA.begin(); //Initialize OTA
  Serial.print("IP address: ");

  /********** relay init *************/
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);


  //Blynk.syncVirtual(RelayBTN1, RelayBTN2, RelayBTN3, RelayBTN4);

  timer.setInterval(checkInterval, blynkCheck);
  timer.setInterval(200, worktime);

/*************** buttons *********************/
  RelayBTN1 = param.asInt();
  if (RelayBTN1 == true) {
    digitalWrite(relay1, ON);
    Serial.println("relay1 on");

  else {
    digitalWrite(relay1, OFF);
    Serial.println("relay1 off");;

  RelayBTN2 = param.asInt();
  if (RelayBTN2 == true) {
    digitalWrite(relay2, ON);
    Serial.println("relay2 on");

  else {
    digitalWrite(relay2, OFF);
    Serial.println("relay2 off");;

  RelayBTN3 = param.asInt();
  if (RelayBTN3 == true) {
    digitalWrite(relay3, ON);
    Serial.println("relay3 on");

  else {
    digitalWrite(relay3, OFF);
    Serial.println("relay3 off");;

  RelayBTN4 = param.asInt();
  if (RelayBTN4 == true) {
    digitalWrite(relay4, ON);
    Serial.println("relay4 on");
  else {
    digitalWrite(relay4, OFF);
    Serial.println("relay4 off");;

  RelayAllOff = param.asInt();


void blynkCheck() {
  Serial.println(WiFi.status() );
  if (WiFi.status() == 3) {
    if (!Blynk.connected()) {
      Serial.println("WiFi OK, trying to connect to the server...");
  if (WiFi.status() == 1) {
    Serial.println("No WiFi connection, going offline.");

void worktime() {

  if (led9.getValue()) {;
  } else {

void AllOff() {
  digitalWrite(relay1, OFF);
  digitalWrite(relay2, OFF);
  digitalWrite(relay3, OFF);
  digitalWrite(relay4, OFF);;

  Blynk.virtualWrite(V1, LOW);
  Blynk.virtualWrite(V2, LOW);
  Blynk.virtualWrite(V3, LOW);
  Blynk.virtualWrite(V4, LOW);

void loop() {
  ArduinoOTA.handle();  // For OTA;;

okay but you see, I’m trying to achieve something else. I’m trying to build alarm.
When a Shock Sensor get triggered, I want lets Say PIN 10 to get HIGH and
in if Statement, a few things happen and than I want to send me the email as a notiication
and than, everything gets messed up.

My code works perfectly without Blynk.
Blynk was supposed to be an addon to send me an email in case of an event.
It does but its clogging my other functions

something like this:

if ((measurement1 >= 1000) || (measurement2 >= 500)) {
digitalWrite (3, HIGH); // It will light up LED"", "Alert", "Sensor Triggered");

and if I have another if statement, everything gets stuck after Blynk,email.

Please post the full code here else we can’t help you

blynk runs in a loop hence the;
in the loop.
You’re code will only run if its part of that same loop, its bad practice to dump it in straight there, so create a function around it and call that function on a timer:
say every 500 (= 2x per second) you can shorter if you want.
One thing you have to keep in mind though that as long as that if statement remains true: you get an email 2x per second, so you need to add an additional boolean to make sure you receive the message only once. You could reset it as soon as the tremor stops, but that again means you get it with every distinct tremor…so you can consider to put that in a timer as well.
Overall take a look at some basic scripts of blynk to get a feeling of how they operate.

You have you own topic (EDIT - actually you had two - merged them all together as well), no need to spread your issue into others topics. I moved your post back here.

Blynk is an extensive library, not a simple add-on.

Due to how Blynk works - providing a constant interconnected IoT based GUI interface, you need to understand some new ways of running arduino code in timed functions instead of dumping everything into a single void loop().

Spend some time learning the basics of how Blynk works, and how timed functions work (BlynkTimer) then you will better see how to merge Blynk in with your own code.

Now if ALL you wanted Blynk for was the email notification… well, then you are just trying to place a thumbnail with a sledgehammer :stuck_out_tongue_winking_eye: I think you can Google around for alternative email options in that case.



Ha!!.. was supposed to be thumbtack… but the end result would probably be the same :stuck_out_tongue:

1 Like


new issue ? :smile:

OP self sabotage… I reverted the posts.

1 Like