Book Image

Hands-On Internet of Things with Blynk

By : Pradeeka Seneviratne
Book Image

Hands-On Internet of Things with Blynk

By: Pradeeka Seneviratne

Overview of this book

Blynk, known as the most user-friendly IoT platform, provides a way to build mobile applications in minutes. With the Blynk drag-n-drop mobile app builder, anyone can build amazing IoT applications with minimal resources and effort, on hardware ranging from prototyping platforms such as Arduino and Raspberry Pi 3 to industrial-grade ESP8266, Intel, Sierra Wireless, Particle, Texas Instruments, and a few others. This book uses Raspberry Pi as the main hardware platform and C/C++ to write sketches to build projects. The first part of this book shows how to set up a development environment with various hardware combinations and required software. Then you will build your first IoT application with Blynk using various hardware combinations and connectivity types such as Ethernet and Wi-Fi. Then you'll use and configure various widgets (control, display, notification, interface, time input, and some advanced widgets) with Blynk App Builder to build applications. Towards the end, you will learn how to connect with and use built-in sensors on Android and iOS mobile devices. Finally you will learn how to build a robot that can be controlled with a Blynk app through the Blynk cloud and personal server. By the end of this book, you will have hands-on experience building IoT applications using Blynk.
Table of Contents (10 chapters)

Setting up software on Raspberry Pi

The SD card works as the hard drive of your Raspberry Pi. As you know, every computer should have an operating system to manage.

The official operating system for Raspberry Pi is known as Raspbian. The Raspbian OS can be installed on the SD card.

Installing Raspbian

Raspbian is the officially supported operating system for Raspberry Pi by the Raspberry Pi Foundation. Raspbian can be installed on Raspberry Pi through NOOBS, or as a standalone image:

  1. Browse the page on the Raspberry Pi website. You can find two options at the top of the page:
    • New Out Of Box Software (NOOBS): An easy operating system installer for beginners
    • Raspbian: The officially supported Raspberry Pi operating system based on Debian
  2. Click Raspbian:
  1. You will navigate to the page. Click on RASPBIAN STRETCH LITE, which is a minimal image based on Debian Stretch:
  1. You can directly download the image file as a Zip file, or you can download it through the torrent. I found that downloading through the torrent is the fastest and most convenient way, but you will need a torrent client installed on your computer. uTorrent and BitTorrent are a couple of the popular clients. You can find plenty of resources on the internet on how to install and use torrent software.
  2. Extract the downloaded compressed image file using 7-Zip (Windows), or the Unarchiver (macOS). You will get an image file like 2017-11-29-raspbian-stretch-lite.img. The first part of the filename indicates the date on which you downloaded the file.

Writing Raspbian Stretch Lite image on SD card

You will need to use an image writing tool to install the image you have downloaded on your SD card.

Etcher is a graphical SD card writing tool that works on macOS, Linux,
and Windows, and is the easiest option for most users. Etcher also supports
writing images directly from the ZIP file, without any unzipping required.
You can download Etcher at

  1. From the drop down-list, choose the operating system you are going to use to run Etcher (I chose Etcher for Windows x64):
Choosing the operating system
  1. The executable file will start downloading, and will be saved to your computer's Downloads folder. You can use Etcher on your computer without installing it. Just run the executable file (Etcher-Portable-1.2.1-x64.exe) to launch the application:
Etcher application window
  1. Connect the micro SD card to your computer. You can use an SD card reader to connect it to your computer. Once connected, Windows will recognize it as a removable drive.
  2. Click the Select Image button, browse the Raspbian image file (2017-11-29-raspbian-stretch-lite.img), and click the Open button. The following screenshot shows the resultant window after selecting the image file:
Selecting image
  1. Click the Select Drive button and choose the SD card. The following screenshot shows the resultant window after selecting the drive for the SD card:
Selecting SD card as the drive
  1. Click the Flash! button to write the image on your micro SD card. The following screenshot shows the window during flashing. When you get a message from Windows User Account Control, click the Yes button to proceed:
Image flashing to SD card in progress

Creating a configuration file for SSH

The Raspbian Stretch Lite doesn't have any configuration settings enabled to work with the SSH protocol for secure remote login from another computer. Therefore, first you should create a configuration file to enable the use of the SSH protocol:

  1. With the SD card connected to your computer, open Command Prompt and change to your Pi's boot partition (let's assume the boot partition of the SD card is i):
  1. Then type the following command to create an empty file named .ssh on the root of the boot partition of the SD card:
i:>echo .ssh
  1. Remove the SD card from your computer, and place it into the SD card slot on the Raspberry Pi. It will only fit one way.
  2. Connect the USB Wi-Fi dongle to one of the USB ports (unless you have a Raspberry Pi 3).
  3. Connect the Ethernet cable between your Raspberry Pi and the router.
  4. Finally, connect the power supply to the micro USB port of the Raspberry Pi, and apply power. You will see a steady red LED and intermittently flashing green LED. Wait until the green LED turns off.
  5. Log into your router's portal and find the dynamic IP address assigned with your Raspberry Pi:
Raspberry Pi can be found under Device List
  1. You will notice that the allocated IP address for my Raspberry Pi is You may get a different IP address based on your network topology.

Connecting with Raspberry Pi with SSH

SSH allows you to secularly log into the Raspberry Pi from your computer. PuTTY can be used to establish the connection between your computer and Raspberry Pi, and provides a terminal to run commands on Raspberry Pi. You can download PuTTY at

The following steps will show you how to use PuTTY on Windows:

  1. Open PuTTY by running the downloaded executable file (putty.exe).
  2. In the PuTTY Configuration window, type in your connection settings:
    • Host Name (or IP address): IP address assigned by your router like
    • Port: 22
    • Connection type: SSH
  3. Then click the Open button to start the SSH session:
PuTTY configuration
  1. If this is your first time connecting to Raspberry Pi from your computer, you will see the PuTTY security alert. Accept the connection by clicking the Yes button:
PuTTY Security Alert
  1. Once the SSH connection is open, you should see a terminal. Type in the following default credentials to log into your Raspberry Pi. Press Enter following each input:
login as: pi
[email protected]'s password: raspberry
  1. You are now logged in to your Raspberry Pi with SSH.
  2. When typing your password, you will not see the cursor moving, or any characters typed on your terminal screen:
  1. Log into Raspberry Pi using your default username and password.

Configuring a wireless connection on Raspberry Pi

Now you can configure the wireless connection to allow your Raspberry Pi to connect with your home or school Wi-Fi network. The following steps will guide you through how to configure the connection settings:

  1. Open the wpa_supplicant configuration file in nano using the following command:
pi@raspberrypi:~ $sudonano /etc/wpa_supplicant/wpa_supplicant.conf
  1. Go to the bottom of the file and add the following:
  1. If the network you are connecting to does not use a password, the wpa_supplicant entry for the network will need to include the correct key_mgmt entry:
  1. Alternatively, you can use the wpa_passphrase utility to generate an encrypted PSK:
  1. If you are using a hidden network, an extra option in the wpa_supplicant file, scan_ssid, may help connection:
  1. Now save the file by pressing Ctrl + X, then Y, then finally press Enter.
  2. Reboot your Raspberry Pi for the new configurations to take effect by issuing the following command:
pi@raspberrypi:~ $sudo reboot
  1. You can verify if it has successfully connected by using ifconfig wlan0. If the inetaddr field has an address beside it, the Raspberry Pi has connected to the network. If not, check that your ssid and psk are correct:
Verifying the wireless connection
  1. Otherwise, you can verify the wireless connection between your Raspberry Pi and router by logging into your router's configuration page.
You may get a different user interface depending on your router's brand.

Refer to the following screenshot:

Device list at router configuration page
  1. Now you have successfully connected your Raspberry Pi to the home router's wireless network.