I would like to simply have the blynk board send an email when the analog pin reads >200 (photo diode).
It keeps disconnecting my blynk board from the blynk server everytime I put the email code in. It seems as though I can only have the blynk.run() function in the void loop, anything else in the loop causes the blynk board to disconnect from the server.
Can anyone help point me in the right direction here?
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
const int Const = 1;
const int reset = 2;
int current = 0;
////////////////////
// Blynk Settings //
////////////////////
char BlynkAuth[] = "XXXXXXXXXXXXXXXXXX";
char WiFiNetwork[] = "XXXX";
char WiFiPassword[] = "XXXXXX";
void setup()
{
// Initialize hardware
Serial.begin(9600); // Serial
// Initialize Blynk
Blynk.begin(BlynkAuth, WiFiNetwork, WiFiPassword);
}
void loop()
{
// Execute Blynk.run() as often as possible during the loop
Blynk.run();
int sensorValue = analogRead(A0);
if(sensorValue>200)
{
current = 1;
}
if(sensorValue>500 && current==Const)
{
Blynk.email('XXXXXXX@gmail.com", "test subject", "test content");
current = reset;
}
while(sensorValue>200){
//do nothing
}
}
I should mention, my intent with the other parts of the code (cont, current, reset) was to ensure only one email was sent until the next time the analog voltage surpasses 200.
Hopefully that makes sense...
I was reading about "interrupts" but this only seems to apply to digital signals?
It's a good try, but you aren't doing it right. You need to keep track of the previous high/low state and compare it to the current high/low state to see if there has been a transition. You should also have hysteresis, meaning you don't switch at the same level.you might make the result HIGH when above 210, and make it LOW when below 190. When between the two values, leave it alone. This way, if you have a slow moving signal you don't get multiple triggers around the threshold.
If you're measuring ambient light, you may need to do additional low-pass filtering if you don't want it to be triggered by transient darkness like a shadow passing over the sensor.
Anyway, here's the way to do hysteresis with transition detection:
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 = 190;
void read_photodiode_sensor()
{
previous_hys_comparator_level = current_hys_comparator;
int current_analog = analogRead( );
if( current_analog > hys_comparator_high_thresh ) current_hys_comparator_level = HIGH;
if( current_analog > hys_comparator_low_thresh ) current_hys_comparator_level = LOW;
}
Run read_photodiode_sensor() once at the end of setup, and at the start of each loop. Then, if you want to test for a LOW -> HIGH transistion you do this:
Thanks for the tips, I will definitely implement this code.
However, my main issue is that It seems as though if I have any other logic in the void loop other than blynk.run(), the blynk board is disconnected from the blynk server. Serial monitor says "login timeout" repeatedly.
Maybe I should be posting this on the blynk website, but I thought it is worth a shot here too, as it is an arduino programming question.
Not entirely sure what you mean, but it does compile and upload to the blynk board. I have even managed to receive an email with this code, but only once before the server kicks me off for whatever reason.
My next plan is to hook up the photo diode to an Uno, then have the Uno send a high signal to the Blynk board, and use an interrupt function on the blynk board. But this is not ideal.
Single quotes and double quotes are different and mean different things. In the line that I showed, there are 5 (an odd number) of double quotes and one (also an odd number) of double quotes. Single quotes are usually used in a pair around a single character. Double quotes are usually used in a pair around zero or more characters.
Good catch, however this was an error I made when copying the code to the forum. I double checked my actual code, and I have all double quotations. Thanks for looking through my code though!
I am stumped here. When i bought this board, I thought it would be better than having an Uno with a Wifi shield (much, much smaller).
I figured it out with some help from the blynk forum, and the help I received earlier in this thread. Thanks for everyone's input! My project is now working perfectly.
mgale31:
I figured it out with some help from the blynk forum, and the help I received earlier in this thread. Thanks for everyone's input! My project is now working perfectly.
My code is functional, and attached:
#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";
How often does Blynk.run() have to be executed? It seems ridiculous that it wouldn't work unless you spun the analog stuff off onto a timer. You'r not doing anything differently in this code except reduce the frequency of reading the photosensor, and that isn't exactly a time-consuming operation.