Go Down

Topic: Help With Blynk Board Code (Read 482 times) previous topic - next topic

mgale31

I purchased a Sparkfun Blynk Board: Blynk Board

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?

Code: [Select]
#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
  }
}

mgale31

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?

Thanks in advance!

Jiggy-Ninja

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:
Code: [Select]

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:
Code: [Select]

if( current_hys_comparator_level==HIGH && previous_hys_comparator_level==LOW )
{

}
Hackaday: https://hackaday.io/MarkRD
Advanced C++ Techniques: https://forum.arduino.cc/index.php?topic=493075.0

mgale31

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.

Thanks!

vaj4088

Code: [Select]

  Blynk.email('XXXXXXX@gmail.com", "test subject", "test content");


How does this even compile?

mgale31

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.

vaj4088

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.

mgale31

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).

Thanks for everyone's input.


mgale31

#8
Sep 14, 2016, 02:54 pm Last Edit: Sep 14, 2016, 03:00 pm by mgale31
My revised Code (still doesn't work):
Code: [Select]
#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[] = "XXXXXXX";
char WiFiNetwork[] = "XXXXX";
char WiFiPassword[] = "XXXX";

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("XXXXX@gmail.com", "XXXXX", "XXXXX");
}

}


The serial Monitor says:
[292159] Login timeout
[295159] Connecting to blynk-cloud.com:8442
[297342] Login timeout
[300342] Connecting to blynk-cloud.com:8442
[302532] Login timeout
[305532] Connecting to blynk-cloud.com:8442

But if I remove everything from the void loop except "Blynk.run();" the Blynk board will stay connected to the server...

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:
Code: [Select]

    #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();
    }

Jiggy-Ninja

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:
Code: [Select]

    #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();
    }

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.
Hackaday: https://hackaday.io/MarkRD
Advanced C++ Techniques: https://forum.arduino.cc/index.php?topic=493075.0

mgale31

Quote
How often does Blynk.run() have to be executed?
I'm not sure, but if you try and put anything else into the loop other than the timer and blynk.run, it will kick you off the server.

This was the only way I could get it to function.

Go Up