Friday, April 23, 2021

Blynk part 3: your own Blynk server

 For an index to all my stories click this text

This is the third story in my series about Blynk. If you have never heard of Blynk or never done anything with this IOT dashboard software I urge you to read the previous stories first. You can find them here:
http://lucstechblog.blogspot.com/2021/03/blynk-part-1-starting-with-blynk.html
http://lucstechblog.blogspot.com/2021/04/blynk-part-2-sending-data-to-blynk.html

Blynk works fantastic. There is however a small problem. Blynk works with a system called energy.



When you start Blynk you get 2000 energy points. You can see that when choosing the + sign in the editing part.

As you can see from the list of widgets, every widget has its price.



In our previous article we build a dashboard with a button, a slider, a led, a text field, a gauge and a chart.



And now we have only 100 energy points left. Just enough to add a nother led and we are done.



Pressing the ADD button gives us the opportunity to buy extra energy. The prices are reasonable in my opinion and you support the company that gave us this great environment for free.

There is however another option. This costs more but gives you unlimited energy. Build your own server !!!

Blynk Server

The Blynk server is in the cloud. And as shown in the first story it sits between the APP and your microcontroller. So the APP sends a command to the server and the server passes it on to your microcontroller and the other way round.

That server also keeps track of the amount of energy you have left.

Luckily for us Blynk has provided us with the opportunity to use your own server. That makes you independend of their server in the cloud and also saves the company some money as all traffic will go through your own server and not through theirs.

And like I said before: your own server will supply you with an unlimited amount of energy for every user. So let us start building one.

Raspberry server.

I build my Blynk server with a Raspberry 3B.

At first I found stories on the internet on how some guys got a Blynk server working on a Pi-Zero. So I followed their instructions. There were several tutorials and they used different ways to install the Blynk server. I tried all the tutorials and none worked. I gues that this is due to the Java core that needs to be installed and that does not work anymore on the Raspberry with the latest OS.
After wasting several days (installing on a Pi Zero is tedious slow) I called it a day and switched to the Pi 3 and that worked like a charm.

So I am going to show you here how its done.

What you will need.

To build your own server you will need:
- Raspberry Pi 3 or 4
- SD card
- Power supply
- Ethernet cable

You can ommit the ethernet cable if you use the Raspberry's build in Wifi. I do not do that. I always connect my computers and servers wired.

All in all this is more expensive as buying 28000 energy points from Blynk but your own server will give you 200.000 energy points per user !!!!

Installing the Raspberry Blynk Server

Start with putting the OS on the SD card. There are numerous instructions on the internet so I will not get into that.

When the card is written put a file with the name SSH on it. The name SSH may not have an extension. If you do not know how to do that read this story: http://lucstechblog.blogspot.com/2020/11/creating-file-with-no-extension-in.html

Remove the card from your PC, put it into the Raspberry and power the Pi up.
Now look into your router for the Pi's IP adress.

I run my Raspberry's headless. That means that I have no keyboard or monitor attached to them. You might do otherwise if you like.

Install (if you do not have it) Putty on your PC. If you not already have done so, you can download it here: https://www.putty.org/
Start Putty and connect to the PI's IP adress you found in your router.

All the steps that follow can be done from Putty. However I activate the VNC server on the Raspberry so I can work in the Raspberry's GUI on my PC. Plenty of instructions on how to do that on the internet.

If you do not know how to activate the VNC server and work with the Raspberry GUI on your PC's screen drop me a line. My email adress is on the top-right part of this screen. If enough people are interested I will do a tutorial on that.

If VNC server is started open a terminal window. The next steps can be done in that terminal window or in Putty.

Make sure you are in the Home directory.

Start with making a subdirectory for Blynk

pi@raspberrypi:~ $ mkdir Blynk

Then move into that subdirectory

pi@raspberrypi:~ $ cd Blynk

The first thing to install is Java.

pi@raspberrypi:~ $ sudo apt install openjdk-8-jdk openjdk-8-jre

And the next step is to install the Blynk server

pi@raspberrypi:~ $ wget "https://github.com/blynkkk/blynk-server/releases/download/v0.41.13/server-0.41.13-java8.jar"

When this is finished you should make sure that the Blynk server is started when the Raspberry is booted. We can do that by altering the crontab. Move back to the home directory.

pi@raspberrypi:~ $ cd ..

Then give the command to alter the crontab.

pi@raspberrypi:/home $ crontab -e

I always use Nano as the editor. Feel free to change crontab with the editor of your choice.

Scroll to the bottom of the file and add the next line:

@reboot java -jar server-0.41.13-java8.jar -dataFolder /home/pi/Blynk &

Then exit the editor with (for Nano) CTRL-X
Nano asks if it should save the file and naturally the answer should be Yes.

There is just one last step to do and that is easiest done in the Raspberry GUI. So use VNC on your PC to connect to the PI.

Open the file manager



Now open the Blynk directory



We need to make 2 files in this directory. The names are mail.properties and server.properties

Start with pressing the right mouse button and choose New File. Raspberry asks for the name of the file and give it the name mail.properties

Click on the file so it opens and put the following lines in:

mail.smtp.auth=true
mail.smtp.starttls.enable=true
mail.smtp.host=smtp.gmail.com
mail.smtp.port=587
mail.smtp.username=
mail.smtp.password=
mail.smtp.connectiontimeout=30000
mail.smtp.timeout=120000


Then save the file.

The second file we need to add is the server.properties file.
This is a large file that can be found here:
https://github.com/blynkkk/blynk-server/blob/master/server/core/src/main/resources/server.properties

Copy this file and put it into the editor and then save it.

Now you should have 2 files like the example shows with the names mail.properties and server.properties.

Time to reboot the Raspberry.

Blynk App

We have to get back to the Blynk App. Restart the App on your phone or tablet.



Go back to the start page and choose Create New Account.



Look at the bottom of the Log In page. There is a small sign. Click on that.



Now you can choose between the Blynk server in the Cloud and your own Blynk server. So set the switch to custom.



Fill in the IP adress from your local server. My Raspberry's IP is 192.168.1.87 in my local network so I filled that in. Alter this to your settings.
Do not alter the Port number (9443) unless you altered it in the server.properties file which is unlikely.

Click on OK and then Sign up.

Blynk gives you an information screen on energy, but that is not important anymore.



Start a new project. I called mine Servertest and chose the ESP8266 board again. Click at the bottom of the page on create.
Just like shown in the previous story a message comes up that tells you that a mail with the authentification token has been send to your email-adress.

We are presented with a empty screen for our project. Click on the + for adding widgets.



And presto we have 100.000 energy points.

Adding more energy

So now we have 100.000 energy points, and we are connected to our own Blynk server !!!

Let's have a look at the server itself.

Open your webbrowser on your pc and point the URL to the IP adress of the Raspberry followed by 9443. So in my case it is 192.168.1.87:9443

The browser alerts for a possible safety issue. Ignore that. Accept the risk and carry on. Hey it is our own Raspberry we are looking at.......


 

And there is the login screen from our own Blynk server !!!

The account name is admin@blynk.cc the password is admin.



The servers dashboard opens. Now click on the users section.



As you can see I have several user accounts. You can even have a fake account like example@example.com. Just remember that Blynk sends an authentification number to that email adress.



At the red arrow you can see the amount of energy that is allocated to that user. Alter this to your own liking. But the starting 100.000 energy points should be enough for the most extensive projects.

In this users section scroll down and there you will find the authentification token. So even if you have used a fake email adres you can still find the authentification token.

When you start multilple projects as the same user you can find all tokens in the server at that users section.

There is a lot more to discover in the server, so look araound and see what

There is a lot still to be discovered about Blynk and I have several things at hand. I am building a communication system between Blynk and Domoticz and I am using Blynk to have ESP8266's and ESP32's communicate with eachother. But that's for another story.

Till next time.
have fun


Luc Volders