Help with Blynk sending email

I purchased the Sparkfun Blynk Board, and I cannot seem to get my project to work. Any help is appreciated.

I simply want the Blynk board to send an email when a photodiode reaches a certain value.
I have the photo diode connected to the ADC (A0) pin, and it is functioning the way I would like, as I de-bugged using the serial monitor.

My issue, is that my current code kicks me off the server. The serial monitor shows this:
[292159] Login timeout
[295159] Connecting to blynk-cloud.com:8442
[297342] Login timeout

`#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

int current_hys_comparator_level = LOW;
int previous_hys_comparator_level = LOW;

const int hys_comparator_high_thresh = 210;
const int hys_comparator_low_thresh = 150;

////////////////////
// Blynk Settings //
////////////////////
char BlynkAuth[] = “XXXXXXXXXX”;
char WiFiNetwork[] = “XXXX”;
char WiFiPassword[] = “XXXXXX”;

void read_photodiode_sensor()
{
previous_hys_comparator_level = current_hys_comparator_level;

int current_analog = analogRead(A0);
if( current_analog > hys_comparator_high_thresh ) current_hys_comparator_level = HIGH;
if( current_analog > hys_comparator_low_thresh ) current_hys_comparator_level = LOW;
}

void setup()
{
// Initialize hardware
Serial.begin(9600); // Serial
// Initialize Blynk
Blynk.begin(BlynkAuth, WiFiNetwork, WiFiPassword);
read_photodiode_sensor();
}

void loop()
{
read_photodiode_sensor();
Blynk.run();
if( current_hys_comparator_level==HIGH && previous_hys_comparator_level==LOW )
{
Blynk.email("XXXXXXXX@gmail.com", “subject”, “content”);
}

}
`

@mgale31 with ioT sketches we don’t put any heavy lifting in the loop. We use timers that ensure the loop continues to run uninterrupted.

How often are the readings triggered per minute bearing in mind the restriction on the number of emails sent out per minute?

Thanks for the quick reply!
Ideally I would take a reading as often as possible, but I could live with a reading per 30 minutes.
Would that help?

I should also note that my intention is to only send an email when the reading is above a certain value. This would likely only occur a few times per day.

That is fine but is that what your sketch currently does?

you should create a new function to read the sensor, then in the loop you use a timer to run the function ever X seconds

like:

#include <SimpleTimer.h>

void read_photodiode_sensor()
{
previous_hys_comparator_level = current_hys_comparator_level;

int current_analog = analogRead(A0);
if( current_analog > hys_comparator_high_thresh ) current_hys_comparator_level = HIGH;
if( current_analog > hys_comparator_low_thresh ) current_hys_comparator_level = LOW;
if( current_hys_comparator_level==HIGH && previous_hys_comparator_level==LOW )
{
Blynk.email("XXXXXXXX@gmail.com", "subject", "content");
}
}

void setup()
{
// Initialize hardware
Serial.begin(9600); // Serial
// Initialize Blynk
Blynk.begin(BlynkAuth, WiFiNetwork, WiFiPassword);
  timer.setInterval(10L * 60000L, read_photodiode_sensor);

}

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

not sure if that is 100% correct, but that’s the idea :slight_smile:

i think your “if’s” are wrong…

My current sketch takes a sample once per loop. Any idea how I can get this sketch functional?

Thanks @Dave1829! I’ll try that out.

Just so I’m clear, if you use a timer function outside the loop, in theory I should stay connected to the Blynk server?

Thanks again for the quick help.

sample rate is determined by: timer.setInterval(10L * 60000L, read_photodiode_sensor);

in this case it is 10 x 1 minute = 10 minutes

Like @Dave1829 has provided but with an addtional line after:

#include <SimpleTimer.h>

of

SimpleTimer timer;
1 Like

exactly as @Costas said :slight_smile:

Solved! Big thanks to @Costas and @Dave1829 !

Attached is my code for anyone else who may want to do something similar:
#define BLYNK_PRINT Serial #include <ESP8266WiFi.h> #include <BlynkSimpleEsp8266.h> #include <SimpleTimer.h> SimpleTimer timer; int current_comparator_level = LOW; int previous_comparator_level = LOW; const int comparator_high_thresh = 210; const int comparator_low_thresh = 150; char BlynkAuth[] = "yourblynkauthkey"; char WiFiNetwork[] = "yournetwork"; char WiFiPassword[] = "yourpassword"; void read_photodiode_sensor() { int current_analog = analogRead(A0); Serial.println(current_analog); previous_comparator_level = current_comparator_level; if(current_analog > comparator_high_thresh) { current_comparator_level = HIGH; } if(current_analog < comparator_low_thresh) { current_comparator_level = LOW; } if( current_comparator_level==HIGH && previous_comparator_level==LOW ) { Blynk.email("your email", "subject", "email body"); } } void setup() { // Initialize hardware Serial.begin(9600); // Serial // Initialize Blynk Blynk.begin(BlynkAuth, WiFiNetwork, WiFiPassword); timer.setInterval(1L * 2500L, read_photodiode_sensor); } void loop() { Blynk.run(); timer.run(); }

2 Likes

Do the email working? I seem to have problem even with the sample program.

Works on my end. What problem are you seeing?

@mgale31 @victagayun we had some problems with emails recently, right now everything is fixed.

Tested this morning, works fine. Thanks for the reply @Dmitriy

Hello again.

Looking for some help troubleshooting a new sparkfun blynk board.

Earlier in this thread we figured out how to get the blynk board to send emails when our equipment goes down.

This is still working great, so I bought another blynk board for our second machine to clone the setup.

I am confident everything is the same, but this new blynk board will not send emails. It won’t even turn on the LED on pin 5 if I replace the email code with "digitalWrite(5, HIGH);

If you look at the code above: When I open the serial monitor, the “current analog” variable is responding properly, and it should therefore be triggering an email.

Essentially I just have a photo-resistor connected to the ADC pin on the blynk board that is changing with ambient light.

Any ideas for troubleshooting? Is the board just a lemon maybe?

Thanks!

Matt

Question one: does the photo resistor work OK without Blynk? E.g. can you do a very basic simple reading? Always trace your steps and remember, all things being equal, the most simple explanation tends to be the right one. But that is in contrast with: if all is excluded, whatever remains, however improbable, must be the truth.

I hate this debug stuff :wink: But as I said, start with basic troubleshooting. E.g does the Photoresistor work, does Blynk work on the specific board? etc.

1 Like

Thanks for the reply @Lichtsignaal. I figured it out.
The problem was that I did not go into the app and add an email widget, and run the project.

I didn’t think you actually had to program using Arduino, and also setup the app for this to work. I thought the arduino code took care of everything.

Thanks,
Matt

Remember also that if you don’t allow low security app on Gmail settings, Gmail in Blynk don’t work.