Blynk is a very popular open source project. The development of Blynk was supported by a participatory Kickstarter fundraising campaign in 2015. Blynk consists of libraries (Arduino, ESP8266, Nodejs for Raspberry Pi …) and a mobile application on iOS and Android. It is very easy to create connected objects or to control objects remotely (lighting, servo-motor …) from the internet in a few lines of code. We will start this new series of articles by installing a private server on a local computer.
The Blynk server was developed in Java. It is therefore possible to install it on all platforms. For a private use, Blynk will adapt very well to a Raspberry Pi 3. In an upcoming tutorial we will test Blynk on Orange Pi + 2e, more powerful and cheaper than Raspberry Pi.
Install a local Blynk server on Raspberry Pi 3
The diagram below shows how the Blynk system works. A server can connect a connected object developed using an Arduino or a computer (Raspberry Pi, Orange Pi …) to the mobile application Blynk running on a smartphone or a tablet (iOS or Android) . Here we will install our own server in place of the official server on a Raspberry Pi 3.
The installation is very well detailed on the official page on GitHub.
The first thing to do is to check that Java 8 is installed on your distribution
pi@raspberrypi:~ $ java -version java version "1.8.0_65" Java(TM) SE Runtime Environment (build 1.8.0_65-b17) Java HotSpot(TM) Client VM (build 25.65-b01, mixed mode)
If this is not the case, run the following command to install Java 8
sudo apt-get install oracle-java8-jdk
Place yourself in the directory where you want to install Blynk then download the server
wget "https://github.com/blynkkk/blynk-server/releases/download/v0.23.0/server-0.23.0.jar"
It’s over !
Configuring the local server
Before starting the server, here are some useful settings to make. The server is ready to run out of the box, as soon as the installation is complete. Here are some adjustments that I advise you to do because you will need them quickly, you will understand why. Configuration is done using files. The next two are particularly important
- server.properties: allows you to configure all server settings
- mail.properties: allows you to configure the server to send emails
In an upcoming tutorial, we’ll see how to add a certificate to secure communication with the Blynk server.
Configuring the server (server.properties file)
The first configuration file to create is server.properties. Get a basic file on GitHub here. In addition to the destination files of the files, you will be able to modify the communication ports. This is especially important if you have installed a home automation server (or other software) that uses the same ports as the local Blynk server. For example, by default the port http (and web socket) is the 8080. If you use Domoticz (for example), it will get stuck. You can use port 8082 instead.
#hardware mqtt port hardware.mqtt.port=8440 #hardware ssl port hardware.ssl.port=8441 #hardware plain tcp/ip port hardware.default.port=8442 #http and web sockets port http.port=8080 #https and web sockets port https.port=9443 #application ssl port app.ssl.port=8443
Blynk has a management interface accessible from a web browser. By default this interface is accessible only from the computer the raspberry Pi on which the server is installed. If you want to be able to access it from any computer on the local network, change the allowed.administrator.ips parameter at the end of the file like this
allowed.administrator.ips = 0.0.0.0/0
Save the server.properties file in the same folder as the server.
Configuring the email account (mail.properties file)
You will need tocken for your Arduino code. It is a (long) identifier consisting of numbers and letters. Rather than copying it, which is tedious and error-prone, Blynk sends an email containing the token as soon as a new Project (a new Arduino / ESP8266 board) is created. It is also possible to return a Token at any time by email from the mobile application. Create a new configuration file named mail.properties and save it to the same folder as the server. Here is a sample configuration file for gmail.
mail.smtp.auth=true mail.smtp.starttls.enable=true mail.smtp.host=smtp.gmail.com mail.smtp.port=587 mail.smtp.username=EMAIL mail.smtp.password=PWD
Starting Blynk server
How to start the server manually
To start the server, run the following command
java -jar server-0.23.0.jar -dataFolder /home/pi/Blynk
The path to the data directory (-dataFolder) is passed as a parameter. The server starts in about ten seconds.
Blynk Server successfully started.
All server output is stored in current folder in 'logs/blynk.log' file.
How to start the Blynk server at system startup
To start the server automatically at startup, just run the start command by adding & at the end of the command. This gives
java -jar server-0.23.0.jar -dataFolder /home/pi/Blynk &
If it does not work, edit the crontab
pi@raspberrypi:~ $ crontab -e no crontab for pi - using an empty one Select an editor. To change later, run 'select-editor'. 1. /bin/ed 2. /bin/nano <---- easiest 3. /usr/bin/vim.tiny Choose 1-3 [2]: 2
Go to the end of the file and add the following command. Do not forget to modify the path to match your installation directory.
@reboot java -jar /home/pi/server-0.23.0.jar -dataFolder /home/pi/Blynk &
Logs files
Several logs are created in the logs directory:
- blynk.log: The server runtime log
- postgres.log: log if you have enabled data logging in the database. Attention, it is consumer in space.
- stats.log: statistics about health of the server, queries …
- worker.log: query log to external internet services (in a future tutorial)
Connect to a local Blynk server from the iOS or Android app
Go to the App Store or Google Play on your smartphone and install the Blynk app. Launch the application.
Create a new user account
You arrive on a login screen. Click Create New Account
On the screen that appears, click the server selection button (shown in red in the screenshot below). Click the button to activate the Custom mode. Specify the IP address of the local Blynk server and the port. By default, it is 8443 (unless you have modified the latter in the server.properties settings file). Confirm with OK. Enter a username and password. You are now connected to your local Blynk server with a valid user account.
Connecting with an existing account on a local server
Once you have created an account, choose Login to reconnect. You will find the same button to change server.
Indicate the IP address of the local server as before, then your username. If you have configured the email server, you will be able to reset your password in case of loss.
Administration interface: Add energy
Blynk works with an energy system. Each Widget (gauge, display, notification, email, button …) consumes energy. We start with a capital of 2000 points. We recharge energy by purchasing it via an in-app purchase. This allows Blynk to finance servers, salaries and future development. As Blynk is also an open source project, designers allow us to change the amount of energy when we have our own private Blynk server. Here’s how.
Open the administration interface by entering the following address in a web browser
https://IP_BLINK_SERVER:7443/admin
The default certificate is self-signed. It will therefore be considered unreliable. You will have to accept the alert message displayed by your internet browser.
Open the Users tab and click your account.
Enter the energy points in the Energy field (for example, 200000).
Scroll down to the bottom of the page and save the change by pressing Save Changes. You can now enjoy the mobile app without any energy limitation.
Making the Blynk server accessible from the internet
To access your private Blynk server, you need to configure your internet box to make the Raspberry Pi available from the internet. For example, you can use an online service like noip.com. Some Internet service providers offer a free DynDNS service. You can also simply use your IP address if it is fixed (which is usually the case). You will need to add a rule in the NAT settings of your box to return calls from the external port 8443 to port 8443 of the Raspberry Pi.
Create your first IoT with Blynk
Now that everything is ready, we will make a small temperature sensor using an ESP8266. In this tutorial, we will quickly create a project. We will discover all the other widgets that Blynk offers in other tutorials.
Equipment used
Wemos D1 Mini | |
Ecran OLED SSD1306 I2C 64×48 pixels. Broches par défaut : D1 – SCL, D2 – SDA | |
Shield DHT22 ou DHT11 pour Wemos D1 Mini Le capteur est connecté à la broche D4 (GPIO2) |
Create your first Blynk project
After logging in, you will be on a blank project. Click Create New Project.
Give the project a name. What we have to remember is that we create a project for each board. It is then possible to create bridges between the projects (boards). Here I gave the name Wemos DHT22. Search the list ESP8266 or Wemos d1 mini. Both work. I have not seen a difference in code yet. There are certainly server-side optimizations. Finish with Create Project.
You must have received an email with the token (token) to paste into the Arduino code. If this is not the case, check the logs to make sure that the mail server settings are correct. You can return the token at any time by pressing the diamond
Now drag the edge of the screen from the right to bring up the list of Widgets.
Scroll down to the list and choose Gauge. The gauge is placed on the grid.
Each Widget therefore takes a number of rows and columns. It is not possible to move on the page (up / down, right / left) but there is a system of tabs if we have many elements to insert.
Click on the gauge to modify it:
- Give him a name. Here Temperature
- You can change the display color by pressing the color circle (green by default). You can choose between 6 colors: green, mauve, light blue, orange, purple, white
- Click PIN to bring up the list of choices. Choose a Virtual (Virtual) PIN and then V0 in the right list
- Specify the range. For example, from -10 (° C) to 50 (° C)
- Finish by pressing OK
Add a second gauge for moisture. It’s exactly the same thing but choosing V1
Finally, add an History Graph:
- It is possible to draw up to 4 measurements on a graph. Click on PIN and choose Virtual then V0 for temperature. Do the same on the second line by choosing V1 for moisture
- The title is automatically added (copied from the gauge), but it can be modified. For example, the unit
- You can let the application manage the Y-axis scale or manage it manually
- You can display the legend
So, everything is ready for the Blynk application.
Arduino Code
Retrieve the token in your emails and paste it into the Arduino code below. Also change the connection settings to your WiFi network. Upload the code and open the serial monitor. To run the OLED shield, you will need to add the Adafruit_SSD1306 library modified by Mike Causer available on Github here.
/* * Blynk DHT22 Shield for Wemos d1 mini * - Read temperature and humidity * - Display value on OLED Shield (64x48 pixels) * * - Lit la température et l'humidité sur le capteur DHT22 * - Affiche les mesures sur l'écran OLED (64x48 pixels) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * * Licence : MIT * Copyright (C) 2017 : www.projetsdiy.fr and www.diyprojects.io */ //#define BLYNK_DEBUG // Optional, this enables lots of prints #define BLYNK_PRINT Serial // Comment this out to disable prints and save space #include <SPI.h> #include <Wire.h> #include <Adafruit_GFX.h> #include "Adafruit_SSD1306.h" #include <BlynkSimpleEsp8266.h> #include <SimpleTimer.h> #include <DHT.h> #define OLED_RESET 0 // GPIO0 Adafruit_SSD1306 display(OLED_RESET); #define DHTPIN D4 #define DHTTYPE DHT22 DHT dht(DHTPIN, DHTTYPE); //SHT3X sht30(0x45); SimpleTimer timer; // Vos paramètres de connexion WiFi - Your WiFi credentials. char ssid[] = "yourSSID"; char pass[] = "yourPASSWORD"; // Jeton Blynk - Blynk Token char auth[] = "yourTOKEN"; void sendSensor(){ float t = dht.readTemperature(); float h = dht.readHumidity(); Blynk.virtualWrite(V0, t); Blynk.virtualWrite(V1, h); displaySensor(t,h); } void displaySensor(float t, float h){ display.clearDisplay(); display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(0,0); display.println("- Blynk -"); display.print("T "); display.print(t); display.println(" *C"); display.print("H "); display.print(h); display.println(" %"); display.println(""); display.println("projetsdiy"); display.display(); } void setup() { Serial.begin(115200); // Authentification auprès du serveur local Blynk - Authenticate to Blynk Private Server Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,24), 8442); // Initialise l'écran OLED sur e bus I2C à l'adresse 0x3C (64x48 pixels) // Initialize with the I2C addr 0x3C (for the 64x48) display.begin(SSD1306_SWITCHCAPVCC, 0x3C); display.display(); // Démarre un timer qui affiche et envoi les données chaque seconde // Setup a Timer to display and send values every second timer.setInterval(1000L, sendSensor); } void loop() { Blynk.run(); timer.run(); }
How does a Blynk program work?
The Blynk library (BlynkSimpleEsp8266.h) handles to connect to the WiFi network is to connect to the server. To connect to a local server, you must specify:
- The Token
- The WiFi network connection settings (ssid and password)
- The IP address of the Blynk server
- The communication port
Blynk.begin (auth, ssid, pass, IPAddress (192,168,1,24), 8442);
Each pass in the loop() Blynk loop must be called by using the blynk.run() command to retrieve data from the server.
To regularly publish measurements, the easiest way is to create a timer, a function that is called at regular intervals. The SimpleTimer library by Jean-Francois Turcot (GitHub repository) is ideal for this.
A timer object is created at the beginning of the program
SimpleTimer timer;
And in the setup(), we define the frequency of execution of a function. Here, the sendSensor function to send the measurements to the Blynk server will be executed every second.
timer.setInterval(1000L, sendSensor);
The sendSensor function reads temperature and humidity. The temperature is published on the Virtual PIN V0 temperature with the virtualWrite function
Blynk.virtualWrite(V0, t);
The same is done for v1 which contains the value of moisture
Start the Test with Blynk application
Once the Arduino program is uploaded, if the library did not return any errors on the serial monitor, you can return to the iOS or Android app
[1757] Connected to WiFi [1757] IP: 192.168.1.21 [1757] ___ __ __ / _ )/ /_ _____ / /__ / _ / / // / _ \/ '_/ /____/_/\_, /_//_/_/\_\ /___/ v0.4.3 on Arduino [5001] Connecting to 192.168.1.24 [5110] Ready (ping: 8ms).
At launch, the application connects to the server and updates the data.
Here you have just connected your first connected object to your smartphone. If you have made your Raspberry Pi accessible from the internet, you can even access your probes and actuators out of your home.
Here is a short demo video that summarizes this tutorial.
- Integrate Arduino / ESP8266 + Blynk IoT to iOS with Homebridge and HomeKit
- Recycle a remote-controlled car (RC car) with an ESP8266, Shield Motor Wemos d1 mini and Blynk
- Connecting an ESP8266 to Blynk on WiFi with Johnny-Five (Firmata WiFi)
- IoT development based on Orange Pi, Arduino (Firmata), Nodejs, Blynk and Johnny-Five
- Blynk + Node.js + Johnny-Five: drive a Pan-Tilt PTZ SG90 kit on Orange Pi with an Arduino / Firmata
- Blynk + ESP8266: drive a Pan-Tilt PTZ SG90 in WiFi with a smartphone or tablet
hello, I have a problem, I follow the steps as they are, but when trying to enter the web
https://IP_BLINK_SERVER:9443/admin (in the guide it says 7443 but in the configuration it is like 9443) it says the connection is not private, it also enters and ends in that the page is not found.
What can be? Should we open the port in some way from the raspberry?
From already thank you very much.
Hello Mth. Did you restart the serve after update the configuration ?