BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Help with project

android

#1

Hello, i’m having a problem with my code idk why please help, the code functions perfectly but it does not send an email and i don’t know what to do
Im on android version
last library version
i’m using the Wemos D1 version of arduino which is a arduino board with ESP8266 Wifi shield together

#define BLYNK_PRINT Serial
/* Set this to a bigger number, to enable sending longer messages */
//#define BLYNK_MAX_SENDBYTES 128

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "********";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "*****";
char pass[] = "*******";

void emailOnButtonPress()
{
 //  *** WARNING: You are limited to send ONLY ONE E-MAIL PER 15 SECONDS! ***

//   Let's send an e-mail when you press the button
 //  connected to digital pin 2 on your Arduino

  int isButtonPressed = 0; // Invert state, since button is "Active LOW"

int x=analogRead (A0);
  if (x>100) // You can write any condition to trigger e-mail sending
  {
    Serial.println("Button is pressed."); // This can be seen in the Serial Monitor
    Blynk.email("********@gmail.com", "Subject: Button Logger", "ITEM STOLEN");

    // Or, if you want to use the email specified in the App (like for App Export):
    //Blynk.email("Subject: Button Logger", "You just pushed the button...");
  }
  else
    {
       Serial.println("Button is NOT pressed.");
    }
    delay (200);
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  // Send e-mail when your hardware gets connected to Blynk Server
  // Just put the recepient's "e-mail address", "Subject" and the "message body"
  Blynk.email("**********@gmail.com", "Subject", "My Blynk project is online.");

  // Setting the button
  pinMode(A0, INPUT_PULLUP);
  // Attach pin A0 interrupt to our handler
  attachInterrupt(digitalPinToInterrupt(A0), emailOnButtonPress, CHANGE);
}

void loop()
{
  int isButtonPressed = 0; // Invert state, since button is "Active LOW"

int x=analogRead (A0);
  if (x>100) // You can write any condition to trigger e-mail sending
  {
    Serial.println("Button is pressed."); // This can be seen in the Serial Monitor
    Blynk.email("**********@gmail.com", "Subject: Button Logger", "ITEM STOLEN");

    // Or, if you want to use the email specified in the App (like for App Export):
    //Blynk.email("Subject: Button Logger", "You just pushed the button...");
  }
  else
    {
       Serial.println("Button is NOT pressed.");
    }
  Blynk.run();
  delay (200);
}

#2

You have the same lines of code in both your emailonbuttonpress function and your void loop.
The code needs to come out of your void loop, leaving just the Blynk.run line.

You also have an interrupt attached to an analogue pin (A0) rather than a digital pin.

Pete.


#3

is there any chance you can change the wrong part about the code and resend it?


#4

also we’re using a force sensor that requires anaglog pins


#5

You can start with removing all delay(millis) calls - they are breaking Blynk.run


#6
BlynkTimer timer;

void emailOnButtonPress()
{
 //  *** WARNING: You are limited to send ONLY ONE E-MAIL PER 15 SECONDS! ***
 //  ***AND 100 emails per day on blynk-cloud.com Server!!

//   Let's send an e-mail when Analog value is >100

  int isButtonPressed = 0; // Invert state, since button is "Active LOW"

int x=analogRead (A0);
  if (x>100) // You can write any condition to trigger e-mail sending
  {
    Serial.println("Button is pressed."); // This can be seen in the Serial Monitor
    Blynk.email("********@gmail.com", "Subject: Button Logger", "ITEM STOLEN");

    // Or, if you want to use the email specified in the App (like for App Export):
    //Blynk.email("Subject: Button Logger", "You just pushed the button...");
  }
  else
    {
       Serial.println("Button is NOT pressed.");
    }
}


void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  // Send e-mail when your hardware gets connected to Blynk Server
  // Just put the recepient's "e-mail address", "Subject" and the "message body"
  Blynk.email("**********@gmail.com", "Subject", "My Blynk project is online.");

  // Setting the button
  pinMode(A0, INPUT_PULLUP);

// Setup a function to be called every 15 seconds (see warning in function above)
  timer.setInterval(15000L,emailOnButtonPress);
}

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

#7

that code did not work for some reason


#8

Did you add BlynkTimer timer;?

I think you need to do a little homework, and learn about the simple timer library, which the BlynkTimer is based on. Especially if you plan on using Blynk. It is the most important concept to understand for well performing Blynk code.


#9

Ok, so the code worked but for some reason the force sensor was always on max value of 1024 (without me touching it) and did not work like with the first code i sent maybe it’s got something to do with the connections, although last time it worked normally anyone knows how to solve this? image


#10

image


#11

image


#12

wiggle the wires, check resistor values, etc.? We really cant help in that department. The code you are running does not effect the value being read.


#13

ok, so we added a buzzer and a LED to the code for our project but the thing is we cannot use void loop because of blynk.run and we need the LED and the Buzzer (simultaneously) to keep blinking while the code is active how do we do that? (we don’t want to just write the same thing 20 times)

/* Set this to a bigger number, to enable sending longer messages */
//#define BLYNK_MAX_SENDBYTES 128

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "******************************";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "***********";
char pass[] = "*************";
BlynkTimer timer;

void emailOnButtonPress()
{
 //  *** WARNING: You are limited to send ONLY ONE E-MAIL PER 15 SECONDS! ***

//   Let's send an e-mail when you press the button
 //  connected to digital pin 2 on your Arduino

  int isButtonPressed = 0; // Invert state, since button is "Active LOW"
  const int buzzer = D8;
  pinMode(D7, OUTPUT);

int x=analogRead (A0);
  if (x>100) // You can write any condition to trigger e-mail sending
  {
    Serial.println("Button is pressed."); // This can be seen in the Serial Monitor
    Blynk.email("**************@gmail.com", "Subject: Button Logger", "ITEM STOLEN");
    pinMode(buzzer, OUTPUT);
  tone(buzzer, 10000); // Send 1KHz sound signal...
  digitalWrite(D7, HIGH);

    // Or, if you want to use the email specified in the App (like for App Export):
    //Blynk.email("Subject: Button Logger", "You just pushed the button...");
  }
  else
    {
       Serial.println("Button is NOT pressed.");
  noTone(buzzer);     // Stop sound...
  digitalWrite(D7, LOW);
    }
}
void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(***,***,*,***), ****);

  // Send e-mail when your hardware gets connected to Blynk Server
  // Just put the recepient's "e-mail address", "Subject" and the "message body"
  Blynk.email("**************@gmail.com", "Subject", "My Blynk project is online.");

  // Setting the button
  pinMode(A0, INPUT_PULLUP);
  

// Setup a function to be called every 15 seconds (see warning in function above)
  timer.setInterval(15000L,emailOnButtonPress);
}

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

#14

i thought i could open another { } in the loop but didn’t have the chance to check it out yet so i’m not sure


#15

also big thanks to the people that told me to remove everything from the loop because i was stuck on that code for over a 2 weeks


#16

You could use a Blynk timeout timer, or start a regular timer then delete it, but that might be a bit too difficult with your current coding skills.

The other option is to use a library like “Ticker”. If you google it you’ll find information about how to install the library and how to start and stop a ticker that will flash your LED and sound your buzzer.
Take a look at this bit of code where I use Ticker to flash an LED when required:

Pete.


#17

i had a little time and i did some tests and i found a way to make it work like that :
tell me what you think if it’s ok

/* Set this to a bigger number, to enable sending longer messages */
//#define BLYNK_MAX_SENDBYTES 128

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "*********************";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "***********";
char pass[] = "*****************";
BlynkTimer timer;

void emailOnButtonPress()
{
 //  *** WARNING: You are limited to send ONLY ONE E-MAIL PER 15 SECONDS! ***

//   Let's send an e-mail when you press the button
 //  connected to digital pin 2 on your Arduino

  int isButtonPressed = 0; // Invert state, since button is "Active LOW"

int x=analogRead (A0);
const int buzzer = D8;
  pinMode(D7, OUTPUT);
  pinMode(buzzer, OUTPUT);
  if (x>100) // You can write any condition to trigger e-mail sending
  {
    Serial.println("Button is pressed."); // This can be seen in the Serial Monitor
    Blynk.email("**********@gmail.com", "Subject: Button Logger", "ITEM STOLEN");

    // Or, if you want to use the email specified in the App (like for App Export):
    //Blynk.email("Subject: Button Logger", "You just pushed the button...");
  }
  else
    {
       Serial.println("Button is NOT pressed.");
  noTone(buzzer);     // Stop sound...
  digitalWrite(D7, LOW);
    }
}
void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  // Send e-mail when your hardware gets connected to Blynk Server
  // Just put the recepient's "e-mail address", "Subject" and the "message body"
  Blynk.email("*************@gmail.com", "Subject", "My Blynk project is online.");

  // Setting the button
  pinMode(A0, INPUT_PULLUP);
  

// Setup a function to be called every 15 seconds (see warning in function above)
  timer.setInterval(15000L,emailOnButtonPress);
}

void loop()
{
  int x=analogRead (A0);
  const int buzzer = D8;
  pinMode(D7, OUTPUT);
  pinMode(buzzer, OUTPUT);
  if (x>100) // You can write any condition to trigger e-mail sending
  {
   tone(buzzer, 10000); // Send 1KHz sound signal...
  digitalWrite(D7, HIGH); 
  delay(100);
  noTone(buzzer);     // Stop sound...
  digitalWrite(D7, LOW);
  delay(100);

    // Or, if you want to use the email specified in the App (like for App Export):
    //Blynk.email("Subject: Button Logger", "You just pushed the button...");
  }
 
  Blynk.run();
  timer.run();
}

#18

Your solution to your newest problem is to ignore the advice given, and put lots of junk back in your void loop!

What do you think to that approach?

Pete.


#19

i mean it works and i didn’t ignore the advice given i just didn’t understand it so i found another way


#20

If you aren’t able to work out how the Ticker library from the code example I linked and by searching the internet for Ticker examples the you’d be better to come back here with a question about that, explaining what it is that you’ve tried and what hasn’t worked.

If you don’t want people to tell you what they think of your code then don’t ask the question!"

I’ll mark this topic as “Solved” as you’ve clearly solved your original issue and seem happy with your solution to the follow-up problem.

Pete.