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 Blynk.email()
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.

but!

When I add Blynk.email("user@email.com", “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
delay(5000);
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();
delay(2);
Serial.print(“HitCount = “);
Serial.print(hitCount);
Serial.println(””);

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
  delay(200);
  digitalWrite(Audio1, HIGH);      // Turn OFF Audio1
  Blynk.email("user@email.com", "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();
}

}

Blynk.run();
}

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…

http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/how-to-display-any-sensor-data-in-blynk-app

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 !
:wink:

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()
{
  Serial.begin(9600);
  delay(5000);
  pinMode(BlueLED, OUTPUT);
  digitalWrite(BlueLED, HIGH);
  Blynk.begin(auth, ssid, pass);
  Blynk.email("user@email.com", "Subject", "My Blynk project is online.");
}

void loop()
{
}

No Blynk.run in the void loop?

Pete.

no Blynk.run, no code run
:wink:

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()
{
  Serial.begin(115200);
  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.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

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

  AllOff();


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

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

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

  }
  else {
    digitalWrite(relay1, OFF);
    Serial.println("relay1 off");
    led1.off();
    led2.on();
  }
}

BLYNK_WRITE(V2)
{
  RelayBTN2 = param.asInt();
  if (RelayBTN2 == true) {
    digitalWrite(relay2, ON);
    Serial.println("relay2 on");
    led3.on();
    led4.off();

  }
  else {
    digitalWrite(relay2, OFF);
    Serial.println("relay2 off");
    led3.off();
    led4.on();
  }
}

BLYNK_WRITE(V3)
{
  RelayBTN3 = param.asInt();
  if (RelayBTN3 == true) {
    digitalWrite(relay3, ON);
    Serial.println("relay3 on");
    led5.on();
    led6.off();

  }
  else {
    digitalWrite(relay3, OFF);
    Serial.println("relay3 off");
    led5.off();
    led6.on();
  }
}

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

BLYNK_WRITE(V6)
{
  RelayAllOff = param.asInt();
  AllOff();

}

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

void worktime() {

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

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

  led1.off();
  led2.on();
  led3.off();
  led4.on();
  led5.off();
  led6.on();
  led7.off();
  led8.on();

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

void loop() {
  ArduinoOTA.handle();  // For OTA
  Blynk.run();
  timer.run();
  ESP.wdtFeed();
}

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 Blink.email 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
Blynk.email("email@email.com", "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
Blynk.run();
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: http://docs.blynk.cc/#blynk-firmware-blynktimer
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.

:joy::joy::joy::joy::joy:

27-021133

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

1 Like

@Gunner

new issue ? :smile:

OP self sabotage… I reverted the posts.

1 Like