Last Updated on
IoT application are spreading the world to give people control of their home, garden, work and many other applications. IoT apps are ofter linked to products to buy and are strictly connected to products functions.
Today I want to show you how to create a flexible IoT solution, fully customizable, with open source Blynk platform and a Raspberry PI Zero W. The same procedure apply also to newer Raspberry PI boards.
What Is Blynk
Blynk is an open source IoT platform, hardware agnostic, which enables user 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 widget based building system. You can create your own layout by simply dragging and dropping available widgets.
It works with and accounting system based on Energy Point. The free version offers an account with 2000 Energy Point which you can spend by adding widgets in your project. Every time you remove a widget, its Energy Point will go back to your Enery Balance.
Blynk is based on a client-server-IoT HW architecture, with Blynk server managing connection and authentication between App and your Hardware.
What We Need
As usual, I suggest adding from now to your favourite ecommerce shopping chart all needed hardware, so that at the end you will be able to evaluate overall costs and decide if continuing with the project or removing them from shopping chart. So, hardware will be only:
- Raspberry PI Zero W (or newer board), with proper Power Supply
- microSD card (suggested at least 16GB class 10 microSD)
- Smartphone with Android or iOS (in my test I will use a Samsung Galaxy Note 5)
Blynk supports many hardware languages. In this guide I’ll use Python. This procedure will proceed with following order:
- Install Blynk app on Smartphone
- Install Raspbian Buster Lite on Raspberry PI
- Install Blynk Python Library on Raspberry PI
- Configure and run first test
Install Blynk App On Smartphone
This part of procedure follows the official guide available on Blynk docs website.
Install Blynk App in your Smartphone
This step depends on your smartphone Operating System. Use you app store (Google Play for Android or AppStore for iOS) to locate and install Blynk app.
Create a Blynk Account
After 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 next steps also to receive authentication token to connect your hardware to your App.
Create a New Project
Once successfully logged in, create a new project.
Choose Generic Device Hardware
Use Generic Hardware in next screen. Raspberry Pi is supported by Blynk, but only models A/B are listed. If you are using one of these, of course, use the proper model. You will be warned that your authentication token connected to this project has been sent to registration mail. This code will be used later in our Python code. After confirmaton, screen will switch to a new empty project:
Insert Your First Widget
Tap the button with cross on top-left. Your widget box will show a list of available widgets with their Energy costs. Your Energy Balance will be showed too. Tap on “Value Display” widget to have it inserted in you layout.
PS: You can remove at any time this widget and Energy Points will be given back to your Energy Balance.
Configure Your Widget
Tap on your widget, so that you will go on widget setting configuration. For our test, we’ll use as INPUT a Virtual PIN number 2, with Reading rate 1 second. So, tap on “PIN” and select “Virtual” -> “V2” -> “Ok”. Then, inder “Reading Rate” substitute default “Push” with “1 sec”.
New Project Configuration Completed
Value Display widget can also be enlarged by keeping it pressed and releasing. This way will enable users to see larger info inside the widget, like we’ll see in following example.
Our New Project configuration on Blynk app ended. Now we’ll move on Raspberry PI
Install Raspbian Buster Lite on Raspberry PI
Please refer to Install Raspbian Buster Lite in your Raspberry PI – Guide for OS installation. We’ll configure Raspberry PI from remote with SSH terminal shell.
Be sure your Raspbian Buster Lite is up to date. From terminal:
sudo apt update && sudo apt upgrade
Install Blynk Python Library on Raspberry PI
Python is pre-installed in Raspbian Buster Lite, as you can verify by typing:
pi@raspberrypi:~ $ python --version Python 2.7.16
We’ll need pip package to install blynk. Use the following command:
sudo apt install python-pip
Install blynk library for Python:
pip install blynk-library-python
Write your first Blink test code. Create a new file:
Insert the following code, taking care on using the token you received by mail from Blynk when:
import BlynkLib from datetime import datetime # Initialize Blynk blynk = BlynkLib.Blynk('YOUR-TOKEN') # Register Virtual Pin @blynk.VIRTUAL_READ(2) def my_read_handler(): currentTime = datetime.now() blynk.virtual_write(2, currentTime.strftime("%d/%m/%Y %H:%M:%S")) print("started!") while True: blynk.run()
Finally execute this script:
Swith back to Blynk app in your SmartPhone and press the Play button:
You should now see in your widget current date and time.