BC Robotics

Blynk is a powerful Internet of Things (IoT) app for iOS and Android that allows you to control your Arduino, Raspberry Pi, or ESP8266 from your mobile phone. On your phone, the Blynk app allows you to build your own control interface without any complicated coding. It also runs on the target device (in our case, the Raspberry Pi) and handles all of the communication between the two over the internet. Building a complex IoT device has never been so easy! In this tutorial we are going to look at the basics of setting up Blynk for the Raspberry Pi. Unlike the Arduino platform, setting it up on the Raspberry Pi is a little more involved so we have made this handy tutorial to get you up and running in no time. There are a lot of different operating systems out there for the Raspberry Pi, so we are going to focus on the most popular: Raspbian. We are using the version dated: November 13, 2018. Deviating from this version may result in different / additional steps.

The Parts Needed:

start
This Raspberry Pi 3 tutorial will be requiring a few parts: [list type=”check”]

Step 1 – Get Everything Prepared

4
In this tutorial we are not going to go over the basics of setting up your Raspberry Pi – we are starting under the assumption that the Pi is already hooked up and running Raspbian dated: November 13, 2018 and your internet connection is set up. You will also want to get the Blynk App installed. Available free from the Android or iOS store. So, if you haven’t done any of that – best to go get it all set up before we get started.

Step 2 – How It Works

The Blynk system is rather clever in the sense that it takes all of the complicated parts of building a slick user interface on a phone out of the equation. It also handles the entire Internet portion of your Internet of Things device, meaning you don’t need to figure out any networking or communications stuff. So how does it work? Well, in the most basic sense, the Blynk project you design on your phone has a unique identifier assigned to it. When you start your internet connected device (the Raspberry Pi in this case) the Blynk program running on the device connects to the Blynk server using that same unique identifier. The server then lets the app on your phone know it is connected, and creates the connection. To perform the most basic operations (like turn a GPIO pin on or off) you don’t actually need to do any programming at all. However, sending data from the Pi back to your Phone does require a little bit of coding that we will explore in this tutorial as well.

Step 3 – Install Git Core

3
First we will start by installing everything we need on the Raspberry Pi. These commands can be typed directly into the terminal, and run by pressing enter. Be sure to enter these exactly as they appear. We are going to install git-core
sudo apt-get install git-core
With each of these commands, hit enter, and let it do its thing!

Step 4 – Clone Blynk to your Pi

4
Next we are going to use Git to clone the Blynk project to the Pi
git clone https://github.com/blynkkk/blynk-library.git

Step 5 – Navigate To New Folder

5
Next we will navigate to the newly created Blynk folder:
cd blynk-library/linux
You can list the contents of the folder to make sure everything is there using the command:
 ls

Step 6 – Make Clean

6
Now we use the make clean command to set it up for the Raspberry Pi:
make clean all target=raspberry

Step 7 – Build

5
We will now build our program so that it can be run.
./build.sh raspberry
That’s pretty much it for the Raspberry Pi – so we will jump over to the Phone side for the next few steps.

Step 8 – Create The Project

6
Before we can run the project, we need to create it on the phone side. We are using Android here so there may be subtle differences if you are using iOS. Within the Blynk app, click “New Project” and select “Raspberry Pi 3 B” from the list and select your method of internet connection. Once you create the project, an email will be sent with the authorization token for your project. This is the key that allows your phone to communicate with your Raspberry Pi by way of the Blynk server so keep that handy.

Step 9 – Test The Connection

5
We are now going to test the connection. Hit the Play button in the top right corner of the App. Don’t worry if you have no controls added. You should see a little device icon in the top bar, with a notice saying it isn’t connecting. Jump over to the Raspberry Pi and type the following into the terminal (be sure to put your token from the email, not our example!)
sudo ./blynk --token=2432a5113a448fcb45e664a1934215
You should see a connection message in the terminal, and almost immediately, the logo on your Phone should show it is now connected.

Step 10 – Design Your App

6
So now that they are both talking to one another, we should really use it to do something. As an example, we will be turning on an LED and reading a Pushbutton connected to the Raspberry Pi. On your phone app: Click the “Stop” button in the top right corner of the app. Now click anywhere in the workspace. This will show the “Widget Box” full of all the interesting bits that are available for your App. We will start by adding a basic button to turn on the LED we will connect to the Raspberry Pi. Next, Add the “LED” widget to the screen. We are going to use this to indicate whenever the button is pressed. Each widget can be resized and moved around – in this case we increased the LED to be the same size as the button.

Step 11 – Configure Your App Controls

5
Click on the button we have just added – this will open a configuration window for that specific button. We need to tell it what to do on the Raspberry Pi; so in this case we are going to trigger Raspberry Pi digital pin GPIO04. To do this we are going to change the “Pin” setting. Click “PIN” and use the boxes to select “Digital” and “gp4”; click OK when you are finished . You can change the type of button from “Momentary” to “On/Off” depending on your preference. Hit the back button on your device to return to the workspace. Now click on the LED – this will open a configuration window for that LED. We are going to change the “Pin” setting. Click “PIN” and use the boxes to select “Virtual” and “V0”; click OK when you are finished. Hit the back button on your device to return to the workspace.

Step 12 – Wire It Up

5
Now that we have it all set up on the App side – lets wire it all up on the breadboard. Power off the Pi before connecting everything. The LED should be connected to GPIO04 on the Pi as shown using a 330 ohm resistor. The button should be connected to pin GPIO17. Once it matches the attached image, boot the Pi up again.

Step 13 – Try Lighting The LED!

6
With everything wired up, we can now start the program on the Pi once again to remotely switch our breadboard LED on and off. Start by navigating back to the Blynk project folder in the terminal:
cd blynk-library/linux
and then start Blynk:
sudo ./blynk --token=2432a5113a448fcb45e664a1934215
Once everything connects, pressing the button on your App should light up the LED on your breadboard. To send information from the Raspberry Pi back to the phone requires a bit more work. If we want to light the indicator on the phone from the breadboard button, we will need to do a little coding.

Step 14 – Sending Back Data From The Pi

5
Adding custom code is a bit different when working with Blynk. We are actually using “Wiring Pi”, which is an environment that allows you to write Arduino code and run it on your Raspberry Pi. The basic “sketch” is already running whenever we run Blynk – so any changes require us to shutdown Blynk, modify the main.cpp file, and build it again. Sounds complicated – but it isn’t! In your terminal window where Blynk is currently running, press “Ctrl + C” to quit. We will then run the following command to open main.cpp and make our code addition:
sudo nano main.cpp
If you are at all familiar with programming an Arduino, the code that loads is going to look very familiar. There is a setup() and loop() just like any old Arduino sketch. There are also a few Blynk specific chunks as well and a bit of example code we will want to remove as well.

Step 15 – Clean Up

We are going to start by getting rid of the example code. Remove the following highlighted lines: [cpp highlight=”28-33, 38-40, 46,”] /** * @file main.cpp * @author Volodymyr Shymanskyy * @license This project is released under the MIT License (MIT) * @copyright Copyright (c) 2015 Volodymyr Shymanskyy * @date Mar 2015 * @brief */ //#define BLYNK_DEBUG #define BLYNK_PRINT stdout #ifdef RASPBERRY #include <BlynkApiWiringPi.h> #else #include <BlynkApiLinux.h> #endif #include <BlynkSocket.h> #include <BlynkOptionsParser.h> static BlynkTransportSocket _blynkTransport; BlynkSocket Blynk(_blynkTransport); static const char *auth, *serv; static uint16_t port; #include <BlynkWidgets.h> BlynkTimer tmr; BLYNK_WRITE(V1) { printf(“Got a value: %sn”, param[0].asStr()); } void setup() { Blynk.begin(auth, serv, port); tmr.setInterval(1000, [](){ Blynk.virtualWrite(V0, BlynkMillis()/1000); }); } void loop() { Blynk.run(); tmr.run(); } int main(int argc, char* argv[]) { parse_options(argc, argv, auth, serv, port); setup(); while(true) { loop(); } return 0; } [/cpp] We are now left with a clean slate to work from!

Step 16 – New Code

First we will start by setting up our variables. We will be reading the button connected to GPIO17 – so we will create a variable to store that pin number. We will also create a second variable to store the previous state of the button. This way we can write some basic code to only send an update to our Blynk app when there is a change detected with the button. Add the highlighted lines: [cpp highlight=”28, 29″] /** * @file main.cpp * @author Volodymyr Shymanskyy * @license This project is released under the MIT License (MIT) * @copyright Copyright (c) 2015 Volodymyr Shymanskyy * @date Mar 2015 * @brief */ //#define BLYNK_DEBUG #define BLYNK_PRINT stdout #ifdef RASPBERRY #include <BlynkApiWiringPi.h> #else #include <BlynkApiLinux.h> #endif #include <BlynkSocket.h> #include <BlynkOptionsParser.h> static BlynkTransportSocket _blynkTransport; BlynkSocket Blynk(_blynkTransport); static const char *auth, *serv; static uint16_t port; #include <BlynkWidgets.h> int buttonPin = 17; //GPIO17 Pin on the Pi bool buttonState = false; //Used to store the previous state of the button void setup() { Blynk.begin(auth, serv, port); } void loop() { Blynk.run(); } int main(int argc, char* argv[]) { parse_options(argc, argv, auth, serv, port); setup(); while(true) { loop(); } return 0; } [/cpp]

Step 17 – Configure The GPIO Pin

Next, we are going to configure the GPIO pin. We are going to set it to an input using the pinMode function and then enable the internal Pull-Up using pullUpDnControl. Internally this sets the GPIO pin 17 to float HIGH by way of an internal pull up resistor. As soon as we press the button we will connect it to ground and pull the pin LOW causing a detectable change. Add the following lines: [cpp highlight=”34, 35″] /** * @file main.cpp * @author Volodymyr Shymanskyy * @license This project is released under the MIT License (MIT) * @copyright Copyright (c) 2015 Volodymyr Shymanskyy * @date Mar 2015 * @brief */ //#define BLYNK_DEBUG #define BLYNK_PRINT stdout #ifdef RASPBERRY #include <BlynkApiWiringPi.h> #else #include <BlynkApiLinux.h> #endif #include <BlynkSocket.h> #include <BlynkOptionsParser.h> static BlynkTransportSocket _blynkTransport; BlynkSocket Blynk(_blynkTransport); static const char *auth, *serv; static uint16_t port; #include <BlynkWidgets.h> int buttonPin = 17; //GPIO17 Pin on the Pi bool buttonState = false; //Used to store the previous state of the button void setup() { Blynk.begin(auth, serv, port); pinMode(buttonPin, INPUT); //Set GPIO17 as input pullUpDnControl (buttonPin, PUD_UP); //Set GPIO17 internal pull up } void loop() { Blynk.run(); } int main(int argc, char* argv[]) { parse_options(argc, argv, auth, serv, port); setup(); while(true) { loop(); } return 0; } [/cpp]

Step 18 – Virtual Write

Finally, we are going to write a bit of code to check the button vs. its last known value. If there is a change we will update the Virtual Pin using the VirtualWrite function, which will automatically update the indicator in our app. We set our indicator light in the app to use Virtual Pin V0. When using Virtual Pins with the LED indicator widget, they can be set anywhere between 0 (full off) and 255 (full on). [cpp highlight=”42-53″] /** * @file main.cpp * @author Volodymyr Shymanskyy * @license This project is released under the MIT License (MIT) * @copyright Copyright (c) 2015 Volodymyr Shymanskyy * @date Mar 2015 * @brief */ //#define BLYNK_DEBUG #define BLYNK_PRINT stdout #ifdef RASPBERRY #include <BlynkApiWiringPi.h> #else #include <BlynkApiLinux.h> #endif #include <BlynkSocket.h> #include <BlynkOptionsParser.h> static BlynkTransportSocket _blynkTransport; BlynkSocket Blynk(_blynkTransport); static const char *auth, *serv; static uint16_t port; #include <BlynkWidgets.h> int buttonPin = 17; //GPIO17 Pin on the Pi bool buttonState = false; //Used to store the previous state of the button void setup() { Blynk.begin(auth, serv, port); pinMode(buttonPin, INPUT); //Set GPIO17 as input pullUpDnControl (buttonPin, PUD_UP); //Set GPIO17 internal pull up } void loop() { Blynk.run(); if(buttonState != digitalRead(buttonPin)) //check the button state against its last known value, if true: { if(digitalRead(buttonPin) == TRUE) //if true, set the Virtual Pin “V0” to a value of 0 (full off) { Blynk.virtualWrite(V0, 0); } else{ Blynk.virtualWrite(V0, 255); //Else we set the virtual pin “V0” to a value of 255 (full on) } } else {} //if last value = current value, we do nothing. buttonState = digitalRead(buttonPin); //update the button state. } int main(int argc, char* argv[]) { parse_options(argc, argv, auth, serv, port); setup(); while(true) { loop(); } return 0; } [/cpp] And that’s it for the code – now we can test it out! Exit and save the file by hitting “CTRL + X” and then “Y” to save.

Step 19 – Compile and Test

5
In the terminal we are going to re-run our build command:
./build.sh raspberry
and then start our program once again (remember to use your token!):
sudo ./blynk --token=2432a5113a448fcb45e664a1934215
Now, pressing the button on the breadboard will cause the indicator to light up. Letting go will cause it to shut off. And with that we have bi-directional communication between the Pi and your Phone!

Step 20 – Going Forwards

Blynk is a powerful IoT platform. With the simple code above and one of our Raspberry Pi Relay HATs or IoT Power Relays, you can quite easily control real world devices from your phone. But, we have barely scratched the surface of what it can do! We will look at additional widgets, functions, and capabilities in future tutorials. Keep an eye out for those!
[info]Have A Question? If you have any questions, or need further clarification please post in the comments section below; this way future users of this tutorial can see the questions and answers! [/info]
Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
  • Attributes
  • Custom attributes
  • Custom fields
Click outside to hide the comparison bar
Compare