Pages

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
















Saturday, April 17, 2021

The new Arduino IDE

For an index to all my stories click this text

This hobby community never ceases to amaze me. Just a few weeks ago Arduino CC introduced the new Arduino IDE. I know it is a beta version but still.....and almost nobody noticed or paid attention to it !!! And that really is a shame because it brings a lot of new features. Features some of us have been waiting for for a long time.

The new Arduino IDE is called 2.0 beta. Basically this is a pre-release so it might have some bugs. But I have tried it now for a few weeks and think it is already functional allthough it has some minor bugs. You can get it from the Arduino site and it installs next to your trusted IDE. So don't worry, your projects are safe and not messed up when you install this new version. You can find it here: https://www.arduino.cc/en/software

So lets have a quick look at some of the new features.
 
A new look



The look is somewhat different from the old IDE, being a bit more modern. Most features are accessible trough the drop-down menu's just like you are used to. However there are some features accessible through the icons on the left.



The top left icon gives access to the board manager. A menu opens through which you can scroll. On the top is however a search function, just like in the old IDE.



The second icon gives access to the library manager. Like the board manager you can scroll through the list or use the search function at the top.



As you can see the library manager has what is called autocompletion. If you start typing it already alters the options depending on your search. In the above example I search for the Dallas DS18B20 library and typing dall is already sufficient to bring it up. You can see that the IDE indicated that I have installed version 3.8.0



As soon as your mouse hovers over the text the library manager shows (at the bottom of the text) that there is a new version and gives the option to install it. Clicking "more info" opens the website for that library where you can find additional information.

This works faster as the old library manager.

Installing new microcontrollers.

Not all microcontrollers are installed with the new IDE. In my case the ESP8266 and ESP32 boards were installed. The Arduino boards, like the Arduino Uno, were not. Strange as this is an Aruino prduct. But they must have their reasons (sponsoring ??).



Here I have choosen the Wemos D1 mini board. Clicking on that text brings up a pop-up menu with the option to select another board. Click on that and a new screen will open.



As you can see the Arduino Uno and several other boards are greyed out, so not available. Clicking on that board and on the OK button at the right bottom of the screen present s a pop-up screen.



So here you have the possibillity to install the Arduino boards.

Line numbering is standard activated

You will have noticed from the previous screendumps that line numbering is not an option but standard activated which I think is a great plus.

Autocompletion
 

As soon as you start typing a command, the editor can complete the command for you. Here I am starting a Serial communication for the serial monitor and the IDE gives me all options that I can use to finish the command.



As soon as you start typing parenthesis and/or quotation marks the IDE already puts the end quotation mark and parenteses on the screen.



And look at the small red sign at the end of the accolade (where the arrow points) and the red vertical line at the end of the screen where the other arrow points to.
The IDE indicates that it spotted an error. Can you see it ???

In both lines I forgot the ; at the end.



Put your mouse at the end-accolade and the IDE tries to give you a hint about what the error might be.

Try to move your mouse over a command and the IDE will give you also a hint about the syntax of that command.



And just look at these nice vertical lines that indicate which parts of the program belong together.....



Put your mouse next to the line numbers and down-arrows appear.



Click on the down-arrow and that part will collapse.
For a small program like this, this is not interesting. But it is an enormous aid in debugging large programs !!!!
Click again on the down-arror and the text is unfolded.

Any disadvantage ??

Yes there is a disadvantage. Be it a minor one.
When you have a file open and want to open another one, the new file opens in a new screen. That is not a disadvantage. The disadvantage is that it takes some time........

That's it for now.

I haven touched the debug function yet. There is a lot to be discovered there. And there are a lot of other new things to be discovered. This was just a quick look. And the verdict is YES. I am going to use this new IDE for programming my microcontrollers !!!

Till next time
Have fun

Luc Volders
















 

Friday, April 2, 2021

Blynk part 2: Sending data to Blynk

 For an index to all my stories click this text.

This is part 2 in the series about Blynk. If you have no experience with Blynk please read the first part first. This will tell you what Blynk is, how to install it on your Phone or tablet and how to install the software on your microcontroller. You can find the first part here: 

http://lucstechblog.blogspot.com/2021/03/blynk-part-1-starting-with-blynk.html

In the first part I connected a led to the ESP8266 and controlled it with Blynk.
Let's make things a bit more complicated. Let's dim the led.

On the top right side of the Blynk app screen there is a square. Press on it and it will change to a triangle. When the square is visible the app is running. When the triangle is visible the app is in edit mode. And that is what we want now.



Press on the + and a list of all widgets will be shown. Choose a slider.



Long press the slider and move it across the screen to an empty place. At the ends there are dots. With the dots you can enlarge the slider. Alter it to your liking.



Click on the slider access the settings page for it..



Click on PIN to set the pin to which this slider is attached.
On the bottom on the screen you can choose for a real pin (Digital on the ESP) or a virtual pin. Choose the Digital pin and attach it to gp14.
GP14 is where the led is connected on our ESP8266. And yes: both the slider as the button which is already present are connected to the same led.



I renamed the slider to "Fade the led"
You can also alter the number of steps. I left the numer of steps as it is from 0 to 1023.

Now run the program again by pressing on the triangle on top of the screen.



And this is how the screen looks completed.

The good part is that we do not have to alter anything at the ESP8266 side. The Blynk library takes care of all basic fuctions. Just try moving the slider and you will see the led fading more or less.

Adding a button to the ESP8266

Let's make things a bit more complicated. Let us add a button to the ESP8266 and show its state on our Blynk screen.

First thing to do is to add an indicator for the button to our Blynk screen. So open the screen editor again by pressing on the square at the top of your screen so it is changed into an arrow again.



Press + and add a LED



Long press on the led widget and move it to the right. Then enlarge it so it is as big as the button.



Change the PIN in Virtual V1.



Change the color to any color you like. I choose red. You can do that by pressing on the dot next to the name of the led. And just like we altered the name of the button I also altered the name of the led in ESP8266 Led. Name it like you want as for example: button, door sensor, drawer or whatever you are going to build with it.

We have now created a led on the Blynk screen which is connected to a virtual pin.

Virtual pins

The Blynk APP has no real input pins like an ESP8266, ESP32 or Arduino has. Therefore Blynk's makers introduced the virtual pins concept.

In your ESP8266, ESP32, Arduino etc program you can write data to such a virtual pin. The Blynk app receives the data and connects it to the virtual pin in the APP. In the APP the virtual pin is connected to a led on the screen, or a graph (which we will see later on) or any of the other output widgets.

There are 255 virtual pins available in each project. So we can send data from 255 sensors to 1 Blynk project.......

The ESP8266 program

We are using the same breadboard setup as in the first part in this series. So refer to that for building your hardware setup.

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
char auth[] = "YOUR TOKEN HERE";

// Your WiFi credentials.
char ssid[] = "YOUR ROUTERS NAME";
char pass[] = "PASSWORD";

const int button = D6;

WidgetLED led1(V1);

BlynkTimer timer;

void buttonLedWidget()
{
    if (digitalRead(button) == LOW) 
      {
      led1.on();
      } 
    else 
      {
      led1.off();
      }
}

void setup()
{
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

  pinMode(button, INPUT);

  timer.setInterval(500L, buttonLedWidget);
}

void loop()
{
  Blynk.run();
  timer.run();
}


As usual I go over the program to make clear what the code does.

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
char auth[] = "YOUR TOKEN HERE";

// Your WiFi credentials.
char ssid[] = "YOUR ROUTER
S NAME";
char pass[] = "PASSWORD";


This is the same as in the previous story. The libraries are loaded. Put your authentification token in where it says so and fill in your routers credentials.

const int button = D6;

The button is attached to D6 on the ESP8266 with a pull-up resistor.

WidgetLED led1(V1);

Here Blynk is told that a widget exists that is a led. We call it led1 and it is attached to V1 (virtual pin 1) in the app.

BlynkTimer timer;

A timer is activated and called timer......

void buttonLedWidget()
{
    if (digitalRead(button) == LOW)
      {
      led1.on();
      }
    else
      {
      led1.off();
      }
}


This function is called by the timer activated in the setup.
The code reads the state of the button and if the putton pin is LOW the button is pressed and led1.on() is activated. This puts the led in the Blynk app ON.
If the button pin is HIGH the button is not pressed and led1.off() is activated. This keeps the led in our Blynk app OFF.

void setup()
{
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

  pinMode(button, INPUT);

  timer.setInterval(500L, buttonLedWidget);
}


In the Setup() the serial monitor is activated. The Serial Monitor will show in its output wether the connection with the Blynk server succeeded. So you have some kind of feedback that shows what is going on.

Next Blynk is started with the authentification code and your routers credentials so an internet connection with the Blynk server can be established.

The pin to which the button is attached is defined as an input pin.

And lastly the timer is set. The interval is set at 500 which makes the timer run the buttonLedWidget routine every half second.

This results in the program testing two times per second if the button is pressed or not. And the state of the button is then send to V1 (virtual pin one) in the Blynk app in which the led on the APP's screen is set on or off.

The concept of virtual pins might be confusing in the beginning but the more you work with them the more you will get the hang of it.

Sending data to your Blynk app

One last step is to send data from the ESP8266 to Blynk. I am going to send random figures but you can attach all kinds of sensors like a Dallas DS18B20 thermometer, LDR, potmeter, flexsensor or whatever you like.

We are going to make all kind of visual representations of the random data on the Blynk screen. Lets start with a gauge.

First put the app in editing mode by clicking on the square at the top-right part on your screen. Then press + to add another widget.



From the display section choose the Gauge.



The Gauge appears on the edit menu. Long press it to move it to the position you want it and alter its size to your liking. Then click on it to alter its settings.



Just as with the other widgets Alter its PIN and this time into V2. I altered the values to be displayed in minumum 0 and maximum 20. I choose the large text size and altered the color into yellow



And there it is.

Now we need to alter the ESP8266 software to send random figures to Blynk.

ESP8266 program a with led, button and randon values.


#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
char auth[] = "YOUR TOKEN HERE";

// Your WiFi credentials.
char ssid[] = "YOUR ROUTERS NAME";
char pass[] = "PASSWORD";

const int button = D6;
int randomnr;

WidgetLED led1(V1);

BlynkTimer timer;

void buttonLedWidget()
{
    if (digitalRead(button) == LOW) 
      {
      led1.on();
      } 
    else 
      {
      led1.off();
      }
}

void sendrandom()
{
  randomnr = random(0, 20);
  Blynk.virtualWrite(V2, randomnr);
}

void setup()
{
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

  pinMode(button, INPUT);

  timer.setInterval(500L, buttonLedWidget);
  timer.setInterval(1000L, sendrandom);

  randomSeed(analogRead(0));
}

void loop()
{
  Blynk.run();
  timer.run();
}


This is the same program as the previous one with just some small changes.
I added another timer in setup() which calls the sendrandom routine.
And I added the sendrandom routine which sends a random value to Blynks V2.



Press the arrow button on the Blynk screen to activate the screen, and presto there is a Gauge that changes its value every second.

Now let's add some more displays for the same data.



I added 3 more widgets. The first is a Value Display, the second is a Horizontal Level and the third is a Superchart. On each I altered the PIN setting to V2 so they all give the same value. I also altered the color and texts. You can change them to your own liking.

Going further.

There are many possibillities opening here. I did not even cover a tenth of the available widgets.

Using the buttons you can switch leds but also relays on and off.
Using the leds on the Blynk APP you can have a visual feedback on door sensors, PIR sensors, your 3D printers filament sensor etc etc etc.

There is a joystick widget. With that you could build a remote controlled car. The ZEGBRA widget can set the color of led strips. The Gauges and text widgets can display temperatures, LDR values and also the values from your ESP's analog inputs.

There are numerous widgets and a lot of examples in the Arduino IDE and in the Example Code Builder that was discussed in the previous article.

Just use your imagination and start experimenting.

Running out of energy.

As we were adding more and more widgets to our screen the energy is getting lower and lower. At a certain moment there is not enough energy left to put more widgets to your screen. For most projects the amount of energy you are granted is enough. But for large projects it is not.

An option is then to buy energy from Blynk. This is one way how the Blynk company earns money to keep this service going. There is however another option which will also cost some money but gives you unlimited energy. That is by running your own Blynk server. And that is what we are going to do in a follow-up story.

So till next time.
Have fun

Luc Volders