Im trying out a project whereby if i toggle the first switch, the first LED will light up as well as getting an email notification. Same thing goes to the 2nd switch, the 2nd LED will light up and a different email notification is send to my email.
I’m able to do all these staff. However, I notice that when I toggle the switch, lets say 1hr, I’ll be getting tons of notification. In my case, I will need to get only 1 notification instead of flooding notification. Can I get any help from you guys on this matter?
You’ll need to modify your sketch so that you use a flag which indicates if an email has already been sent.
When the first email has been sent the flag is set to “true”.
Before another email is sent the flag is checked. If it’s already “true” then do nothing, else send an email.
When the switch is toggled back to the off state, the flag is reset to “false”.
If you have 2 switches and 2 different email routines then you’ll need 2 flags.
You’d need to post your full code for this to make any sense. Details of what Blynk widgets, what type lol board you’re using and what physical connections you have to the board would also be handy.
Hi, I try my best to understand the timer but couldn’t get what it mean. I wish to get the notification once in a day instead of flooding. Is there any thing I need to adjust or to add on?
Thanks.
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "d3f3b7a9f81644b1ab572a88348cdc22";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "ASUS-Wang";
char pass[] = "8224088h";
const int switch1=14;
const int switch2=16;
const int led=13;
const int led2=12;
int switchStatus = 0;
int switchStatus2 = 0;
unsigned int notified = 0;
BlynkTimer timer;
void myTimerEvent()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V5, millis() / 1000);
}
void setup()
{
// Debug console
Serial.begin(9600);
pinMode(switch1,INPUT);
pinMode(switch2,INPUT);
pinMode(led,OUTPUT);
pinMode(led2,OUTPUT);
Blynk.begin(auth, ssid, pass);
}
void loop()
{
switchStatus = digitalRead(switch1);
switchStatus2 = digitalRead(switch2);
if (switchStatus == HIGH && switchStatus2 == LOW) {
FirstNotification();
}
if (switchStatus == LOW && switchStatus2 == HIGH) {
SecondNotification();
}
if (switchStatus == HIGH && switchStatus2 == HIGH) {
digitalWrite(led2,HIGH);
digitalWrite(led,HIGH);
}
else {
digitalWrite(led2,LOW);
digitalWrite(led,LOW);
}
Blynk.run();
timer.run();
}
void FirstNotification() {
digitalWrite(led2,HIGH);
digitalWrite(led,LOW);
Blynk.email("raccon82@hotmail.com","Medication Status","Morning medication has been taken");
timer.setTimeout(1000000L, myTimerEvent);
}
void SecondNotification() {
digitalWrite(led2,LOW);
digitalWrite(led, HIGH);
Blynk.email("raccon82@hotmail.com","Medication Status","Evening medication has been taken");
timer.setTimeout(1000000L, myTimerEvent);
}
void resetNotified(){
notified = 0;
}
I think that before we can advise on how to solve this, we need more information on the physical setup, which you aren’t provided yet.
It appears that you’re wanting confirmation that medication has been taken. Is this simply a case of the person who needs to take the medication operating a physical switch to say that they’ve taken it, or is the switch connected to medicine container and operated automatically?
Is the physical switch momentary, or latching?
If it’s momentary then how long is it likely to be operated for? - is this however long it takes the user to remove their pills from the container, or a shorter period that a push button will be pressed for?
Is the LED there to give visual feedback to the person taking the medication that the button press has been acknowledged by the system?
Once we know this, we’ll be able to advise what type of timer to use. If we’re looking to detect the press of a physical pushbutton that may last half a second or less then we need to be running a timer at a high frequency to look for that button press. If it’s a toggle switch that stays activated then we could check the button status much less frequently.
I was intending to make this as a mini project. By putting the medicine case onto a small board so that I will be notify when my Mum took her pills.
It basically work like this, once the pill is taken, the user will have to toggle the switch. The LED is for visual feedback. If I did not receive any notification, I will straight away know that the pills are not taken on time.
Actually I’m suppose to do 3 interval. Pills before breakfast, lunch and dinner. I’m just trying out doing 2 interval. I’m expecting only to receive 1 notification for each interval.
Okay, start by moving all of the stuff out of void loop() except for:
Blynk.run();
timer.run();
Put the code you take out into it’s own function and call that function with a timer once every half a second. You could call it less frequently, but then there would be a delay between the switch being operated and the corresponding LED coming on. Half a second maximum delay won’t cause an issue.
I’m not sure why you’re checking if switch 1 and switch 2 have been operated here:
Surely it would be better to know when the second set of medication has been taken, regardless of whether the first has been taken?
Here’s a sample of what I think your new function should look like:
void function_to_check_switches()
{
switchStatus = digitalRead(switch1);
switchStatus2 = digitalRead(switch2);
if (switchStatus == HIGH)
{
// We do this stuff if switch1 is ON
if (!First_Notification_Flag)
{
// We only do this if notification email hasn't been sent yet (First_Notification_Flag == false)
First_Notification_Flag=true; // Now set the notification flag to true
digitalWrite(led,HIGH); // Turn the corresponding LED ON
FirstNotification(); // Send the first notification emmail
}
}
else
{
// We do this stuff if switch is OFF
digitalWrite(led,LOW);
if (First_Notification_Flag)
{
// We get here if switch1 was ON, but is now OFF, and a notification email has been sent
// You might want to set the First_Notification_Flag back to false
// And you might want to send a different email
// Of course, whne you refill the medication and switch the switches back to OFF then you'd get that notification email
}
}
// Repeat the logic above for Switch2 and 3
} // End of function_to_check_switches
You’ll need a variable, preferably a Boolean, called “First_Notification_Flag” declared at the top of your code so that it’s a Global variable. You’ll need equivalent ones for the other notifications.
I’ve not compiled this code, so ther may be typos or other errors!