Personal IoT App with Blynk and Raspberry PI

4.3
(11)

Last Updated on 22nd March 2024 by peppe8o

Today I want to show you how to create a flexible IoT solution, fully customizable, with the open source Blynk platform and a Raspberry PI 3 model A+. The same procedure apply also to newer Raspberry PI computer boards.

IoT applications are spreading the world to give people control of their homes, garden, work and many other applications. IoT apps are often linked to products to buy and are strictly connected to product functions. Blynk, together with Raspberry PI, can make their access super simple and efficient.

What Is Blynk

Blynk_widgets

Blynk is an open-source IoT platform, hardware agnostic, which enables users to create their own app layout and connect it to its own IoT hardware. It has a really awesome App interface (available both from Google Play and Apple AppStore) with a widget-based building system. You can create your own layout by simply dragging and dropping available widgets.

It works with an accounting system based on Energy Point. The free version offers an account with 2000 Energy Points which you can spend by adding widgets to your project. Every time you remove a widget, its Energy Point will go back to your Energy Balance.

Blynk is based on a client-server-IoT HW architecture, with the Blynk server managing connection and authentication between App and your Hardware.

What We Need

raspberry pi 3 model A+

As usual, I suggest adding from now to your favourite e-commerce shopping cart all the needed hardware, so that at the end you will be able to evaluate overall costs and decide if to continue with the project or remove them from the shopping cart. So, hardware will be only:

Check hardware prices with the following links:

Amazon raspberry pi boards box
Amazon Micro SD box
Amazon Raspberry PI Power Supply box

Step-by-Step Procedure

Blynk supports many hardware languages. In this guide, I’ll use Python. This procedure will proceed in the following order:

  • Install the Blynk IoT app on your Smartphone
  • Configure the Blynk dashboards (both Web and Mobile)
  • Install Raspberry PI OS Lite on Raspberry PI
  • Run the first test

Install Blynk IoT App (NOT legacy) on your Smartphone

This step depends on your smartphone’s Operating System. Use your app store (Google Play for Android or AppStore for iOS) to locate and install the “Blynk IoT” app.

Create a Blynk Account

blynk-iot-app-install-01

After the Blynk app is installed, you will need to create a new Blynk app account. This account will require a mail to register. This mail will be used later in the next steps also to receive an authentication token to connect your hardware to your App.

So, open your Blynk IoT app and tap on “Sign Up” button

blynk-iot-app-install-02

In the next window, please read the Terms and Conditions and Privacy Policy. You will need to accept them by flagging the related option and fill your email address. Then tap the “Continue” button.

blynk-iot-app-install-03

The next window will confirm that the authentication email has been sent. You will need to check your email.

You’ll receive the Blynk email that will provide you with a link to view the following web page that will ask you to create a password:

blynk-create-password

The following page will ask for your first name:

blynk-create-username

Then a brief tour will show you the main features of Blynk:

blynk-login-tour

You can skip it in order to get to the dashboard.

Create a New Template

Blynk templates are a set of pre-defined parameters that are common to multiple devices. This helps when adding multiple devices of the same kind. Before adding a new device linked to our Raspberry PI, we’ll create its template.

At any time, you can go to the templates page by clicking the related icon at the left bar (highlighted in the following picture). The very first time, when no templates are available, you will be warned and linked with a “New Template” button on the first page. In both cases, identify and click the “New Template button”.

blynk-templates

Please fill the template details like name and description according to your preferences. Use the “Raspberry PI” option in “Hardware”. Then, click “Done”:

blynk-new-template-name

Once the Template is generated, you can set the default widgets to see on both web and mobile dashboards. Please remember that from your web console you can set the widget on Web Dashboard, while from Blynk IoT mobile app you can set the Mobile Dashboard widgets.

So, please select the Web Dashboard:

blynk-new-template-dashboards

On your Web Dashboard page, please scroll down the “Widget Box” and identify the “Label” widget:

blynk-new-template-web-dashboard-01

A double click on the Label widget will add this widget to your dashboard:

blynk-new-template-web-dashboard-02_2

By moving the mouse over the new Label widget, it will show more options such as creating a copy, setting and deleting:

blynk-new-template-web-dashboard-03

Please use the weel icon on it to enter the label widget settings window. Here you can define what will appear on top of the value we’ll update from our Raspberry PI. So, set the “Title” as you like and then press the “Create Datastream” button and select “Virtual Pin”:

blynk-new-template-web-dashboard-04

The DataStreams are data connections that allow the endpoint device to send its data to Blynk, by keeping a reference (for example, the Virtual Pin Datastream will be named “V0”, “V1”, “V2” and so on). Please set the Datastream to “V1” and the Datastream type to “String”, as in the following picture:

blynk-new-template-web-dashboard-05_2

You can now click the “Save” button on the bottom-right side of this window. You can also save the template with the following “Save” button:

blynk-new-template-web-dashboard-06

Create a New Device

It’s time to associate a device (our Raspberry PI) with the Blynk template.

Use the glass icon on the left menu to reach the device search and then use the resulting “+ New Device” button to add a new device:

blynk-new-device-01

Click “From Template” in order to create the new device from the just saved Template:

blynk-new-device-02

In the new device creation, please set as Template the one just created. Then you can give your preferred name to this device. The click create:

blynk-new-device-03

After the device is created, a window on the right side will show the authentication info to connect from your Raspberry PI to the Blynk dashboard. With this tutorial, what will be important is the “BLYNK_AUTH_TOKEN” value. Keep it noted as it will be required later:

blynk-new-device-04_2

Create Blynk IoT App Dashboard on your Smartphone

After finishing the Web Dashboard, it’s time to move to the mobile side. As said, the Blynk Mobile Dashboard can be set only from the Blynk IoT App. So, go back to your smartphone and use the left arrow icon to go to the App login:

blynk-iot-app-back-from-confirmation

Here, please fill-in your email address and password to log in and reach the mobile dashboard:

blynk-iot-app-device-setup-02

Please use the “hamburger” icon (the three horizontal lines on the top-rigt) to open the side menu:

blynk-iot-app-device-setup-03

From here, please tap the “Developer Mode” link (we already have the device as we created it from the Web Console):

blynk-iot-app-device-setup-04

Tap the Raspberry PI Template (or whatever you created as your Raspberry PI template). You will enter the Template in Development mode. But this development mode only sets the mobile presentation of your project. At the start it will be empty:

blynk-iot-app-device-setup-05

Use the “+” button on top-right side of the page to open the list of Widgets that we can add. Scroll down untill you find the “Labeled Value”

blynk-iot-app-device-setup-06

Tap on it to add to the Dashboard.

blynk-iot-app-device-setup-07

As for the unhappy choice of colors, you may not clearly see it added. Paying more attention, you will find it in the form of a white square on top-left side of the dashboard:

blynk-iot-app-device-setup-07_2

Tap the label widget to enter its setting up:

blynk-iot-app-device-setup-08

Now, tap the “Choose datastream” button to enter the datastream configuration page:

blynk-iot-app-device-setup-09

Create a Virtual Pin Datastream with the related button and configure the Virtual Pin with the “V1” index. Also configure the Data Type as “String”, to get a configuration like the one shown in the following picture. Also set the Name and Alias to your favourite (“V1 string” in my case). Then click the “Done” button on top-right side of the page:

blynk-iot-app-device-setup-10_2

The following page will result in your label settings overview and you can click the “X” button on the top-left side of the page to go back at the app dashboard:

blynk-iot-app-device-setup-11

Our label is now created and configured:

blynk-iot-app-device-setup-12

With the same process, please add a Button associated to a “V2” Virtual Pin datastream, with Datatype “Integer” and max value to “1”:

blynk-iot-app-device-setup-13

Back to the Button configuration overview, please set the Mode to “Switch”:

blynk-iot-app-device-setup-14

Exit from the Dashboard and from the Developer mode to reach the Blynk IoT main page. It will show the button (by default, it is off). The label, even if present, will not be shown until no data are received

blynk-iot-app-device-setup-15

Configuration summary

Finally, just to resume what we’ve configured, we have:

  • on Web Dashboard a Label widget set to V1 datastream
  • on Mobile Dashboard a Label widget set to V1 datastream and a Button set to V2 datastream

So, the V1 Virtual Pin will be uploaded from our Raspberry PI to both our dashboards (Web and Mobile). The V2 Virtual Pin will be set from our Mobile dashboard via the button and will be read from our Raspberry PI.

These 2 Pins will show how to use both uplink and downlink communication between Raspberry PI and Blynk.

Prepare your Raspberry PI for Blynk

Please refer to my Raspberry PI OS Lite tutorial for OS installation. We’ll configure Raspberry PI from remote with an SSH terminal shell.

Be sure your OS is up to date. From terminal:

sudo apt update -y && sudo apt upgrade -y

Python is pre-installed in Raspberry PI OS Lite, as you can verify by typing:

pi@raspberrypi:~ $ python --version
Python 3.9.2

We’ll need the pip package to install the Python requests module, as this will allow us to use REST communication. The following two terminal commands will do the job:

sudo apt install python3-pip
python3 -m pip install requests

You can now get a copy of my Blynk script directly in your Raspberry PI computer with the following terminal command:

wget https://peppe8o.com/download/python/blynk/restblynk.py

The following will explain my code.

At the first line, we import the requests module:

import requests

Then we associate the token to a variable. Please fill there with your token get at the device creating time:

token="Add-Your-Device-Token-Here"

Here I prefer to manage the write (upload) and read (download) process by using custom functions so that the usage will be highly simplified as we’ll see later.

The first custom is the write() function, which allows the Raspberry PI to write a value to any virtual Pin on Blynk. It gets as input the token value, the Virtual Pin number and the value to write.

We’ll compose the API URL according to the Blynk REST documentation. The Blynk cloud service answers with only 2 possible results: the code “200” means that everything worked correctly, while the code “400” means that something went wrong. So, my custom function is the following:

def write(token,pin,value):
        api_url = "https://blynk.cloud/external/api/update?token="+token+"&"+pin+"="+value
        response = requests.get(api_url)
        if "200" in str(response):
                print("Value successfully updated")
        else:
                print("Could not find the device token or wrong pin format")

The read function is similar to the previous one, but this reads a Widget value and returns it to our Python program. Of course, it requires only the token and the Pin value:

def read(token,pin):
        api_url = "https://blynk.cloud/external/api/get?token="+token+"&"+pin
        response = requests.get(api_url)
        return response.content.decode()

With these functions, writing a virtual Pin will be as simple as the following line:

write(token,"v1","150")

And reading a Virtual Pin will result as the following:

button=read(token,"v2")
print(button)

You can find the official Blynk REST documentation at https://docs.blynk.io/en/blynk.cloud/device-https-api.

Run the restblynk.py Script

Finally, execute this script. From

python3 restblynk.py

The result will be the following:

pi@raspberrypi:~ $ python3 restblynk.py
Value successfully updated
0

The first line means that it has successfully uploaded the “150” value to our labels and the “0” is the value read from the button (OFF=”0″ and “ON”=1),

From our web dashboard, the result will be the following:

raspberry-pi-blynk-test-web-dashboard

While on your Mobile Dashboard you will have the following:

raspberry-pi-blynk-test-mobile-dashboard

Of course, on the Mobile Dashboard, if you tap the button switching it to ON, running again the script will result in the following terminal result:

pi@raspberrypi:~ $ python3 restblynk.py
Value successfully updated
1

Enjoy!

How useful was this post?

Click on a star to rate it anonymously!

Average rating 4.3 / 5. Vote count: 11

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?