Friday, January 28, 2022

Blynk part 5 sending notifications to Android

 For an index to all my stories click this text

This time I present you a simple tutorial for using with Blynk. Apparently some of the readers of this blog did not know how to get a notification on their phone when an event in Blynk takes place. So I am going to show you how it is done.

For those in the dark: a notification means that if something happens at the microcontroller side a message is send to your phone. Something happening might be a button pressed, a PIR that notices movement, a door or window that opens, a temperature rising above a critical level etc etc etc.

For this tutorial I assume that you have already made some projects with Blynk. If not I urge you to read the previous stories on this weblog which you can find here:
http://lucstechblog.blogspot.com/2021/03/blynk-part-1-starting-with-blynk.html
http://lucstechblog.blogspot.com/2021/04/blynk-part-2-sending-data-to-blynk.html
http://lucstechblog.blogspot.com/2021/04/blynk-part-3-your-own-blynk-server.html
http://lucstechblog.blogspot.com/2021/06/blynk-part-4-using-blynk-with-multiple.html

http://lucstechblog.blogspot.com/2021/07/blynk-sad-story.html

So beware that the tutorial presented here is only working with the OLD Blynk.
For this tutorial start a new project.



The project is called (how surprising) Notification and the microcontroller to be used is the ESP8266.



The first thing to add to the project is a led. I chose as color red and attached it to virtual pin V1.
Next resize it to about half the screen.



Then search in the Widget Box (by pressing the + at the top of the screen) for the Notifications group and add a Notification widget.



The project is done !!
That really is all.

The ESP8266 breadboard layout


It can not get more simpler as this.



Just an ESP8266 with a pushbutton. The pushbutton is attached to D1. For simplicity I even did not add a pull-up resistor but did the pull-up in the software.

The ESP8266 notification program.

As usual I start with the complete program and then explain some details.

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

char auth[] = "Blynk-Authentification-token";

char ssid[] = "YOUR-ROUTERS-NAME";
char pass[] = "PASSWORD";

const int button = D1;

WidgetLED led1(V1);

BlynkTimer timer;

void buttonLedWidget()
{
    Blynk.run();
    if (digitalRead(button) == LOW) 
    {
      led1.on();
      Blynk.notify("Alert:The button was pressed.");
    } else 
    {
      led1.off();
    }
}

void setup()
{
  Blynk.begin(auth, ssid, pass, IPAddress(XXXX.XXXX.XXXX.XXXX), 8080);

  pinMode(button, INPUT_PULLUP);

  timer.setInterval(500L, buttonLedWidget);
}

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

A look at the details.

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

Naturally we need to include the Wifi library to contact the Blynk server and the Blynk library.

char auth[] = "Blynk-Authentification-token";

Fill in your Blynk Authentification token for this project. There are two ways to get the authentification token. I will show them later on in this story.

char ssid[] = "YOUR-ROUTERS-NAME";
char pass[] = "PASSWORD";


Fill in your own routers name and password. If you don't the program can not connect to the internet and to the Blynk server.

WidgetLED led1(V1);

BlynkTimer timer;

led1 is defined as a Blynk widget and attached to Blynks virtual pin V1
timer is defined as a Blynk timer and that is used to frequently check on the button.

void buttonLedWidget()
{
    Blynk.run();
    if (digitalRead(button) == LOW) 
    {
      led1.on();
      Blynk.notify("Alert:The button was pressed.");
    } else 
    {
      led1.off();
    }
}

This routine is frequently visited from the loop().
It tests wether the button has been pressed. And if it is the virtual led called led1 in the Blynk app is set ON and (there it is) a notification is send to the Blynk app.

In the setup there are two important lines.

Blynk.begin(auth, ssid, pass, IPAddress(XXX.XXX.XXX.XXX), 8080);

This line connects the ESP8266 to the Blynk server. In this case the XXX.XXX.XXX.XXX is the IP number of my own Blynk server. If you are using the Blynk cloud server replace that line by:

Blynk.begin(auth, ssid, pass);

The next line:

timer.setInterval(500L, buttonLedWidget);

uses the Blynk timer and calls every 500 microseconds (half a second) the buttonLedWidget routine that checks if the button has been pressed.

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


In Blynk programs the loop should always be as short as possibel. So here it just constantly runs the Blynk library and the blink timer.

And that's it

Where to get the authentification token.

There are several ways to get your authentification token from Blynk.

The first method is when you are using Blynks cloud server. As soon as you start making your project the authentification token is send to your email adres and you can copy it from there. You can re-read hoiw this works in the first story in this series: http://lucstechblog.blogspot.com/2021/03/blynk-part-1-starting-with-blynk.html

If you are using your own Blynk server you can find it in the users section of the server as described in the third story in this series : http://lucstechblog.blogspot.com/2021/04/blynk-part-3-your-own-blynk-server.html

The easiest way is the following.
On your computer download an Android emulator. I use LDplayer which I described in this story : http://lucstechblog.blogspot.com/2021/03/ldplayer-android-emulator.html
With this emulator I build all my Blynk apps and test them before I test them on my phone. I love the big screen on my PC, the ability to make screendumps for this blog without having to transfer them and the ease of copy and paste between Blynk and the Arduino IDE.



In the Blynk app in the emulator press on the little nut on the top of the page. 


 

Halfway the page there is a text saying Copy All. Press on that and the authentification code is copied Now you can paste it into your ESP's program.

Does it work ???

Of course it does.



Here is a screenshot from my faithfull Nokia 5 phone that got a notification from Blynk when I pressed the button on the ESP8266.

Now add this to the ESP32-Cam doorbell and you will get a notification when someone is at the door.

Till next time.
Have fun

Luc Volders