Home IoT & Hardware Hands-On Internet of Things with Blynk

Hands-On Internet of Things with Blynk

By Pradeeka Seneviratne
books-svg-icon Book
eBook $29.99 $20.98
Print $38.99
Subscription $15.99 $10 p/m for three months
$10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
BUY NOW $10 p/m for first 3 months. $15.99 p/m after that. Cancel Anytime!
eBook $29.99 $20.98
Print $38.99
Subscription $15.99 $10 p/m for three months
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats, plus a monthly download credit
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with video?
Stream this video
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
What do you get with Exam Trainer?
Flashcards, Mock exams, Exam Tips, Practice Questions
Access these resources with our interactive certification platform
Mobile compatible-Practice whenever, wherever, however you want
About 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.
Publication date:
May 2018
Publisher
Packt
Pages
271
ISBN
9781788995061

 

Setting Up a Development Environment

Blynk is known as the most user-friendly IoT platform consisting of an app builder that can be run on iOS and Android operating systems, and a set of libraries to build amazing IoT applications in minutes with your favorite hardware platform. It allows you to quickly build interfaces by simply dragging and dropping widgets to control and monitor your hardware projects from your iOS and Android device.

In this chapter, you will learn about:

  • Hardware platforms, connection types, architecture, ecosystem, and online resources
  • Installing Blynk app builder on Android
  • Creating a user account with the Blynk app builder to log in to Blynk Cloud
  • Creating a new project with Blynk
  • Preparing Raspberry Pi by adding SD card, Ethernet cable, Wi-Fi dongle, and power supply
  • Writing Raspbian Stretch Lite on Raspberry Pi by flashing image to SD card with Etcher
  • Configuring SSH on Raspberry Pi
  • Installing prerequisite packages such as git-core and WiringPi on Raspberry Pi
  • Using PuTTY to connect with Raspberry Pi with SSH protocol
  • Configuring wireless connection on Raspberry Pi for Raspbian Stretch Lite
  • Connecting Raspberry Pi with Blynk Cloud by running the sample C++ source file
 

What is Blynk?

Blynk was born as a Kickstarter project created by Pasha Baiborodin, and backed by 2,321 supporters who pledged $49,235 to help bring it to life. This is more than the finally announced stretched goal that is $26,000. Blynk is one of the successfully completed projects listed on Kickstarter (https://www.kickstarter.com/):

Kickstarter campaign page for Blynk

Hardware platforms

Blynk supports hardware platforms such as Arduino, Raspberry Pi, and similar microcontroller boards to build hardware for your projects. The following is a list of some microcontroller boards that can be coupled with Blynk:

  • Espressif (ESP8266, ESP32, NodeMCU, WeMos D1, Adafruit HUZZAH, SparkFun Blynk Board, SparkFun ESP8266 Thing)
  • Linux (C++) (Raspberry Pi, Ubuntu)
  • Arduino (Arduino UNO, Arduino MKR1000, Arduino MKRZero, Arduino Yun, Arduino 101, Arduino Zero, Arduino M0, Arduino M0 Pro, Arduino Nano, Arduino Leonardo, Arduino Due, Arduino Mega 2560, Arduino Mega 1280, Arduino Mega ADK, Arduino Micro, Arduino Pro Micro, Arduino Mini, Arduino Pro Mini, Arduino Fio, Arduino Decimilia, Arduino Duemilanove, Arduino Pro, Arduino Ethernet, Arduino Leonardo ETH, Arduino Industrial 101)
  • Particle (particle core, particle photon, particle electron)

You can read the list of up-to-date hardware that you can use with Blynk at https://github.com/blynkkk/blynkkk.github.io/blob/master/SupportedHardware.md.

Connection types

Blynk supports the following connection types to connect your microcontroller board (hardware) with the Blynk Cloud and Blynk's personal server:

  • Ethernet
  • Wi-Fi
  • Bluetooth
  • Cellular
  • Serial

However, throughout this book, you will only focus on Wi-Fi and Ethernet connection types to connect with Blynk Cloud and Blynk's personal server.

Blynk architecture

The Blynk platform includes the following components:

  • Blynk app builder: Allows to you build apps for your projects using various widgets. It is available for Android and iOS platforms.
  • Blynk server: Responsible for all the communications between your mobile device that's running the Blynk app and the hardware. You can use the Blynk Cloud or run your private Blynk server locally. It's open source, could easily handle thousands of devices, and can even be launched on a Raspberry Pi.
  • Blynk libraries: Enables communication with the server and processes all the incoming and outcoming commands from your Blynk app and the hardware. They are available for all the popular hardware platforms.

All the aforementioned components communicate with each other to build a fully functional IoT application that can be controlled from anywhere through a preconfigured connectivity type. You can control your hardware from the Blynk app running on your mobile device through the Blynk Cloud or Blynk's personal server. It works the same in the opposite direction by sending rows of processed data from hardware to your Blynk app.

Blynk ecosystem

The Blynk ecosystem consists of the following partners. They can cover anything from electronic components, to manufacturing and data plans:

  • Intel IoT Solutions Alliance
  • SparkFun Electronics
  • Espressif
  • Arduino
  • Texas Instruments
  • Proximus
  • Deutshe Telekom
  • Particle
  • Samsung
  • littleBits
  • Hologram
  • ThingSpeak.com
  • Electric Imp
  • Punch Through
  • Codebender
  • RedBearLab
  • Wicked device
  • TinyCircuits

Online resources

Blynk provides the following online resources through their website:

  • Getting started: Guides you how to connect your Arduino with Blynk Cloud by writing sketch with Arduino's Integrated Development Environment (IDE) (http://www.blynk.cc/getting-started).
  • Documentation: Provides a comprehensive documentation about how to work with the Blynk app builder, Blynk libraries, Blynk Cloud, and Blynk's personal server. This guide mainly focuses on the Arduino development environment (http://docs.blynk.cc/).
  • Forum: There are thousands of people around the globe who are willing to help your Blynk projects through this conversation board. You can register with the Blynk forum by creating a new account with it, posting your questions, discussing with experts, helping others, and sharing your work with others (http://community.blynk.cc/).
 

The Blynk app builder

The Blynk app builder provides an easy way to build IoT apps that can be run on smartphones and tablets. It provides a predefined set of draggable and droppable modules known as widgets, and allows the users to make further configuration on widgets with an easy-to-use user interface. Using the app builder, you can build apps for your personal use or business purposes, as shown in the following screenshot.

If you want to build an app with Blynk for business purposes, visit http://www.blynk.io/business/ for more information:

Blynk widget Box

Installing the Blynk app builder

The Blynk app builder is currently available for Android and iOS platforms. The following steps explain how to install the Blynk app builder on your Android smartphone or tablet:

  1. Tap the apps icon in the lower-right corner of the home screen (consider that this is the default location).
  2. Swipe left and right until you find the Play Store icon.
  3. Tap the Play Store icon.
  4. The first time you open the Play Store, you may be prompted to sign in with your Google credentials. If you still don't have a Google account, you can create a free account with Google.
  5. Once you sign in with Google Play Store, you can search for a specific app.
  6. Tap the magnifying glass in the upper-right corner, type in the name blynk, and tap the magnifying glass on the keyboard to execute the search. You will get a result on the screen as follows:
Search results for Blynk
  1. Tap INSTALL to start the installation process. Also, you can find more information about the app by tapping the MORE INFO button:
Installing Blynk app
  1. Tap ACCEPT to allow the Blynk app to access some resources on your mobile device:
Permitting access to resources in your mobile device
  1. The installation process will take some time depending on the bandwidth of your internet connection.
  2. After completing the installation, you can open the Blynk app by tapping on the OPEN button with Google Play. This will help you to open the Blynk app builder for the first time without moving to the home screen of your smartphone or tablet:
Once installed, opening Blynk app builder from Google Play
  1. After installation, an application icon associated with the Blynk app builder can be found on the home screen of your smartphone or tablet:
Opening Blynk app builder from Android home screen

Creating a Blynk account

Before you start to create applications with the Blynk app builder, you should create a user account with Blynk Cloud. The following steps will guide you through how to do that:

  1. Open the Blynk app builder on your mobile device by tapping on the application icon labeled Blynk.
  2. Tap Create New Account.
  3. Alternatively, you can log into the Blynk app builder with your Facebook account by tapping on the Log In with Facebook button:
Creating new account—step 1
  1. On the Create New Account page, type your email address for the username, and a password.
  2. Then, tap the Sign Up button:
Creating new account—step 2
  1. A user account will be created, and you will automatically be logged in to the Blynk app builder (Blynk Cloud).
  2. First, you will be notified on how energy works. Tap on the Cool! Got it. button:
Notification about how energy works
Each widget needs energy to operate. When you delete a widget, energy is returned.
  1. At any time, you can log out from the app builder by tapping the log out icon in the toolbar. When prompted, tap LOG OUT, or tap CANCEL if you want to stay logged in.
At the time of writing, Blynk doesn't send any email message to your email account confirming the creation of the new user account.
  1. When you log into the Blynk app builder again, type in your email address associated with the Blynk account and the password, then tap Log In.
  2. You can reset your password by tapping Reset Password at any time.

Creating a new project

Now that you have created a new account with Blynk Cloud and logged in to your Blynk app builder running on your smartphone or tablet, you can follow the following steps to create a new project with the Blynk app builder:

  1. To start building a project, tap New Project. This will bring you to the create New Project wizard:
Creating a new project
  1. Type in demo in the Project Name textbox:
Creating new project—project name
  1. Tap the CHOOSE DEVICE dropdown box to see the available hardware models (the default is ESP8266). Then, tap Raspberry Pi3 B from the Select your hardware list. You may find other versions of the Raspberry Pi as well. Following are the available options for Raspberry Pi versions:
    • Raspberry Pi 2/A+/B+
    • Raspberry Pi 3 B
    • Raspberry Pi A/B (Rev2)
    • Raspberry Pi B (Rev 1)
  2. After choosing the hardware model, tap OK:
Creating new project—Selecting hardware
  1. Tap on the CONNECTION TYPE dropdown list to see the available connection types. Then select WiFi from the CONNECTION TYPE list, followed by OK.
  2. Tap the Create button to create the project workspace:
Creating new project
  1. After successfully creating the project, the app builder will send an auth
    token to confirm your project
    . Every new project you create will have its
    own auth token. The auth token is a unique identifier that consists of alphanumeric characters, and authorizes you to access the Blynk Cloud,
    located at https://www.blynk.cc/:
Notification about the auth token
  1. At the same time, you'll have an auth token automatically sent to the email address you provided.
If you have lost your auth token, you can still find it with your project in Project Settings (the nut icon).
  1. Tap the nut icon in the toolbar to get to the Project Settings page. Then, scroll down to find the DEVICES section. Tap demo Raspberry Pi 3 B (WiFi).
  2. Alternatively, you can tap Email all under AUTH TOKENS to get all the Auth tokens on your email provided for each project in your Blynk account. You can also tap Copy all to copy all the Auth tokens to your mobile device's clipboard:
Finding Auth token—step 10
  1. On the My Devices page, tap demo Raspberry Pi 3 B(WiFi) under My Devices:
My Devices page
  1. You can find the Auth token under the AUTH TOKEN section, but it only shows the last four alphanumeric characters. You can tap the E-mail button to send it to the email account again:
Finding Auth token—step 12

You will learn how to use the Auth token with your C++ code later in this chapter.

 

Getting parts

Blynk supports over 400 hardware platforms and major connectivity types such as Ethernet, Wi-Fi, Bluetooth, Bluetooth Low Energy, and USB; however, it is very difficult to explain how to build devices for Blynk using all the supporting hardware platforms in a single book. Therefore, this book will be using Raspberry Pi as the hardware platform to demonstrate the ability and power of Blynk.

Raspberry Pi

Raspberry Pi is a small, programmable device. It is a mixture between a very small computer and a programmable embedded board. You can connect the Raspberry Pi to the internet through Wi-Fi or Ethernet. You will be using Raspberry Pi 3, the latest model of the Raspberry Pi family, as the hardware platform; however, you can use other versions of Raspberry Pi, such as Raspberry Pi 2/A+/B+ and Raspberry Pi A/B (revision 2) to work with projects that we will be discussing throughout the book.

The sample code provided with this book is not tested with Raspberry Pi Zero W or Raspberry Pi Zero.

Here is a list of the Raspberry Pi boards available in reverse chronological order:

  • Raspberry Pi 3 Model B (third-generation single-board computer)
  • Raspberry Pi 2 Model B (the Raspberry Pi 2 Model B is the second-generation Raspberry Pi)
  • Raspberry Pi 1 Model B+ (the model B+ is the final revision of the original Raspberry Pi)
  • Raspberry Pi 1 Model A+ (the model A+ is the low-cost variant of Raspberry Pi)
  • Raspberry Pi Zero W (single-board computer with wireless and Bluetooth connectivity)
  • Raspberry Pi Zero (lowest-cost single-board computer)
Raspberry Pi 3 Model B, which is one of the latest models of the Raspberry Pi family, can be found here along with the other models: https://www.raspberrypi.org/products/raspberry-pi-3-model-b/.

Refer to the following screenshot:

Raspberry Pi 3 Model B, a single-board computer with wireless LAN and Bluetooth connectivity

Following is the list of important specifications of Raspberry Pi Model 3:

  • Quad Core 1.2 GHz Broadcom BCM2837 64-bit CPU
  • 1 GB RAM
  • BCM43438 wireless LAN and Bluetooth Low Energy (BLE) on board
  • 40 extended GPIO pins
  • 4 USB 2 ports
  • 4 Pole stereo output and composite video port
  • Full-size HDMI
  • CSI camera port for connecting a Raspberry Pi camera
  • DSI display port for connecting a Raspberry Pi touchscreen display
  • Micro SD port for loading your operating system and storing data
  • Upgraded switched Micro USB power source up to 2.5 A

You will learn more technical details about Raspberry Pi in Chapter 2, Building Your First Blynk Application.

Raspberry Pi can be plugged in to a computer monitor or TV, and uses a standard keyboard and mouse. If you want, you can use the Raspberry Pi without attaching a monitor/TV, keyboard, or mouse.

You will need the following things to prepare your Raspberry Pi before installing Raspbian:

  • SD card: 8 GB class 4 SD card is recommended; however, you can use one with higher capacity like 16 GB, 32 GB, or 64 GB. But having a class 10 micro SD card in your Raspberry Pi is very much recommended. Don't use SD cards lower than class 4, because they provide very low writing speed. The following screenshot shows an 8 GB Kingston class 4 micro SD card. The SD card works as the hard drive of your Raspberry Pi:
Kingston 8 GB class 4 micro SD card
  • Power supply: Raspberry Pi is powered by a micro USB power supply. You'll need a good-quality power supply that can supply at least 2 A at 5V for the Raspberry Pi 3 Model B, or 700 mA at 5V for the earlier, lower-powered models. The following screenshot shows a micro USB power supply for Raspberry Pi:
A micro USB power supply for Raspberry Pi
  • A Wi-Fi dongle (required for earlier, lower-powered models): Allows you to connect your Raspberry Pi to a wireless network. The following screenshot shows the official and recommended universal USB Wi-Fi dongle for Raspberry Pi.
  • Ethernet (network) cable [Model B/B+/2/3 only]: An Ethernet cable is used to connect your Pi to a local network and the internet. The following screenshot shows an Ethernet cable:
SparkFunCAT 6 Cable—3ft (image is CC BY 2.0—https://creativecommons.org/licenses/by/2.0/)

Setting up Raspberry Pi

Let's see how to set up Raspberry Pi:

  1. Place your SD card into the SD card slot on the Raspberry Pi. It will only fit one way
  2. Plug your keyboard and mouse in to the USB ports on the Raspberry Pi
  3. Connect your HDMI cable from your Raspberry Pi to your monitor or TV
  4. Connect a Wi-Fi dongle to one of the USB ports (unless you have a Raspberry Pi 3)
  5. The Raspberry Pi is powered by the micro USB power supply
 

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 https://www.raspberrypi.org/downloads/ 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:
Raspbian
  1. You will navigate to the https://www.raspberrypi.org/downloads/raspbian/ page. Click on RASPBIAN STRETCH LITE, which is a minimal image based on Debian Stretch:
RASPBIAN STRETCH LITE
  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 https://etcher.io/:

  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):
C:>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 192.168.1.5. 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 http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html.

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 192.18.1.5
    • 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
pi@192.168.1.5'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:
network={
ssid="testing"
psk="testingPassword"
}
  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:
network={
ssid="testing"
key_mgmt=NONE
}
  1. Alternatively, you can use the wpa_passphrase utility to generate an encrypted PSK:
network={
ssid="testing"
#psk="testingPassword"
psk=131e1e221f6e06e3911a2d11ff2fac9182665c004de85300f9cac208a6a80531
}
  1. If you are using a hidden network, an extra option in the wpa_supplicant file, scan_ssid, may help connection:
network={
ssid="yourHiddenSSID"
scan_ssid=1
psk="Your_wifi_password"
}
  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.
 

Installing prerequisite software on Raspbian

Now you're ready to install all the required software on your Raspberry Pi to prepare it to work with Blynk Cloud:

  • Update and upgrade packages
  • git core
  • WiringPi
  • Blynk libraries

Updating and upgrading Raspbian

Let's see how to update and upgrade Raspbian:

  1. First, update your system's package list by entering the following command:
pi@raspberrypi:~ $sudo apt-get update

Refer to the following screenshot:

Updating Raspbian
  1. Then, upgrade all your installed packages to their latest versions with the following command:
pi@raspberrypi:~ $sudo apt-get dist-upgrade
  1. When prompted, press Y to continue:
Upgrading Raspbian

Installing git

You will need to install git-core on your Raspberry Pi to work with git repositories for cloning packages like WiringPi, and Blynk libraries.

  1. With the SSH to your Raspberry Pi with PuTTY, run the following command:
pi@raspberrypi:~ $sudo apt-get install git-core
Installing git-core
  1. This will install git on your Raspberry Pi. After installation is complete, verify the version of git by using the following command:
pi@raspberrypi:~ $ git -version

Refer to the following screenshot:

Verifying git version

Installing WiringPi

WiringPi is a PIN-based GPIO access library that can be used with all versions of Raspberry Pi:

  1. First, run the following command to clone the WiringPi repository:
pi@raspberrypi:~ $ git clone git://git.drogon.net/wiringPi

Refer to the following screenshot:

Cloning WiringPi
  1. Then move to the wiringPi directory:
pi@raspberrypi:~ $ cd wiringPi
  1. Run the following command to build the WiringPi:
pi@raspberrypi:~/wiringPi $ ./build

Refer to the following screenshot:

Building WiringPi

Deploying Blynk libraries

Let's look at how to deploy Blynk libraries:

  1. You can deploy the Blynk libraries on Raspberry Pi by running the following commands:
pi@raspberrypi:~/wiringPi $ cd
pi@raspberrypi:~ $ git clone https://github.com/blynkkk/blynk-library.git

Refer to the following screenshot:

Cloning Blynk libraries
  1. After cloning the git repository, move to the linux directory:
pi@raspberrypi:~ $ cd blynk-library/linux
  1. Then build the library from source by targeting Raspberry Pi:
pi@raspberrypi:~/blynk-library/linux $make clean all target=raspberry
  1. This command will output the Blynk file that will be used to connect to the Blynk server:
Building Blynk libraries
 

Connecting Raspberry Pi with Blynk Cloud

After completing the building process, you can run the sample C++ code to connect your Raspberry Pi with the Blynk Cloud through Wi-Fi:

  1. Run the default source code file, which is main.cpp, with the following command:
pi@raspberrypi:~/blynk-library/linux $sudo ./blynk --token=da999e8ef4ac42148f9f e8427d482451
  1. When you run the command sudo ./blynk --token=da999e8ef4ac42148f9fe8427d482451, the main.cpp file residing in the blynk-library/linux directory gets executed. The sample code only connects to the Blynk server at https://www.blynk.cc/. The output should look something like this:
Output for default file main.cpp
  1. You can find the sample code for main.cpp in the blynk-library/linux directory. You can read the content of the file with the following command:
pi@raspberrypi:~/blynk-library/linux $ cat main.cpp
  1. The output should look something similar to what is on this link: https://github.com/PacktPublishing/Hands-On-Internet-of-Things-with-Blynk/blob/master/Chapter%201/Listing%201-1/main.cpp.
  2. Go through the code and see whether you can understand the key functions. Don't worry, you will learn how to write C++ code for the Blynk application in Chapter 3, Using Controller Widgets.
  3. Now exit from the nano editor by pressing Ctrl + X with your keyboard. The editor will prompt you asking if you want to save the file; type Y in response.
 

Summary

Now you have to prepared your Raspberry Pi by equipping it with hardware and installing Raspbian OS and required software like git-core, WiringPi, and Blynk libraries. Finally, you ran the default C++ source file to connect your Raspberry Pi to the Blynk Cloud server.

Chapter 2, Building Your First Blynk Application, will present how to build your first application with Blynk. You will learn how to use the Blynk app builder to build an app to turn an LED on and off, and write C++ source code to allow your Raspberry Pi to connect, transfer, and manipulate data with Blynk Cloud.

About the Author
  • Pradeeka Seneviratne

    Pradeeka Seneviratne is a software engineer with over 10 years' experience in computer programming and systems design. He is an expert in the development of Arduino- and Raspberry Pi-based embedded systems. Pradeeka is currently a full-time embedded software engineer who works with embedded systems and highly scalable technologies. Previously, he worked as a software engineer for several IT infrastructure and technology servicing companies. He collaborated with the Outernet project as a volunteer hardware and software tester for Lighthouse- and Raspberry Pi-based DIY Outernet receivers based on Ku band satellite frequencies. He is also the author of five books: Internet of Things with Arduino Blueprints [Packt Publishing] IoT: Building Arduino-Based Projects [Packt Publishing] Building Arduino PLCs [Apress] Raspberry Pi 3 Projects for Java Programmers [Packt Publishing] Beginning BBC micro:bit [Apress]

    Browse publications by this author
Latest Reviews (1 reviews total)
Toch nog meer leren van Blynk.
Hands-On Internet of Things with Blynk
Unlock this book and the full library FREE for 7 days
Start now