Code Explanation:
should someone press the buzzer, phone user gets notification.
should phone user press in-app button, a lock opens, and notifications disabled until in-app button is released.
Code:
#define BLYNK_PRINT Serial
#include <SoftwareSerial.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
const int EspReset = 7;
const int Lock = 5; // Output pin to lock
const int Buzzer = A5; // Input pin from buzzer
const int BuzzerThreshold = 922;
bool AppButton;
char auth[] = "XXXX";
char ssid[] = "XXXX";
char pass[] = "XXXX";
char ip[] = "XXXX";
SoftwareSerial EspSerial(3,2); // //ESP TX to 3, RX to 2.
ESP8266 wifi(&EspSerial);
WidgetLED led1(V1);
BlynkTimer ServerTimer;
void BlinkLedWidget()
{
if (led1.getValue()){
led1.off();
Serial.println("LED on V1: off");
} else {
led1.on();
Serial.println("LED on V1: on");
}
} //"Breathing" Widget
bool BuzzerIsPressed(){
if (analogRead(Buzzer) < BuzzerThreshold)
{return true;}
else {return false;}
}
void NotifyMe()
{
Blynk.notify("Knock knock...");
Serial.println("Knock knock...");
}
BLYNK_WRITE(V1){
if(V1 == 1){
digitalWrite(Lock, HIGH);
AppButton = true;
} else {
digitalWrite(Lock, LOW);
AppButton = false;
}
} //In-app button on V1 controls door lock
//Also disables notification when pressed
void setup()
{
pinMode(Lock, OUTPUT);
pinMode(Buzzer, INPUT);
pinMode(EspReset, OUTPUT);
digitalWrite(EspReset, LOW);
delay(10);
digitalWrite(EspReset, HIGH);
delay(10);
//Serial communications
Serial.begin(9600);
delay(10);
EspSerial.begin(9600); // ESP8266-01s Baud Rate is 9600
delay(10);
Blynk.begin(auth, wifi, ssid, pass, ip, 80);
//Blynk.begin(auth, wifi, ssid, pass, server, port);
ServerTimer.setInterval(1000L, BlinkLedWidget);
// define "timer" to call function every second
// (also: "L" is for long int)
}
void loop()
{
if(Blynk.connected()){Blynk.run();}
else{Blynk.disconnect();Blynk.connect();}
if(BuzzerIsPressed() && !AppButton) {NotifyMe();}
ServerTimer.run();
}
Correct. Blynk is IoT, not just oT… But can mostly work without actual internet as long as the Server, Phones and Devices are all connected via the routers WiFi AP.
However, some features like notifications and email need the Server (and possibly the phone) to have actual internet access to Google’s services to make them work.
How would they work offline? That is like expecting your phone to ring if turned off.
You device code sends a notification request to the server, the server sends it to whatever Google stuff the developer mentioned above, that then works through said Google stuff to your phone… all this via the internet.
Now… if your Phone is offline, then it of course will not get the notification until it goes back on line (I think the Google stuff queues it up) .
But it seems the Server needs to be online at the time of the processed notification, else, well, nada…
yeah, im now looking through some websites to see if it is possible to send notifications, all gave similar answers.
i guess i kind of understand why, notifications work even though the app is not operating in the backround, which means there has to be something not dependent on the app, a server.
I am not sure if the App itself needs to be in background, I think it depends on the priority levels of the Notification widget. But as already alluded to, notifications use something in the Android’s OS as well… thus it is not the App per se that needs to be running as much as the phone needs to be on and online.
But even if the App is totally off, once it starts up, the notifications from the past start popping up … just like voicemail from missed calls when your phone is off.
And online connectivity with the Server is a key factor… as it is, well, an IoT server If it is NOT online, then nothing online related (email, notifications, webhook, API, etc.) will ever work properly.
Trains won’t work without track, planes won’t fly without sky, IoT won’t work without Internet…