Skip to content

Instantly share code, notes, and snippets.

@877dev
Last active April 28, 2021 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 877dev/cfa52044336e1e44d74156ce8b1458aa to your computer and use it in GitHub Desktop.
Save 877dev/cfa52044336e1e44d74156ce8b1458aa to your computer and use it in GitHub Desktop.
IOTstack - Blynk setup

(Maintained by 877dev)

First setup:

Run ./menu.sh and select Blynk to add it to the stack, as with any other container.

Before you "up" the stack, Blynk server requires two configuration files before it can run. Templates are automatically created on first install in ~/IOTstack/volumes/blynk_server/data/config.

Configuring for first run

Go to ~/IOTstack/volumes/blynk_server/data/config and edit the two files as you would with a normal local Blynk server.

The configs created with the install script will work "out of the box", you just need to edit mail.properties and add your gmail details as per these requirements. Note - if you don't have 2FA enabled, then "allow less secure apps" in gmail settings, if you do have 2FA you can create an "App password".

Example files are found on Blynk github here: server.properties mail.properties

Now you can "up" the stack and the check it's working.

Optional configuration

Custom or 'boilerplate' message with the emailed token
Create a file called single_token_mail_body.txt and place it in the config directory (same as the .properties files). A template can be found here , credit to DefPlayr on the Blynk forum for this information.

Enable local data storage for Superchart widgets
This is not tested fully and may be subject to change. Supercharts widgets needs local storage to retain the historic data, if you edit the server.properties and set enable.raw.db.data.store=true it saves data to /home/pi/IOTstack/volumes/blynk_server/data/data/<user>. These files could become very large over time, a USB/SSD drive is recommended. On my setup I have enable.db=false and it still seems to work. More info here and here.

Updating the container to the latest Blynk server version (or changing to any version)

Make sure your project is up to date, details on the main IOTstack page. I would also advise reading the info on updating containers to make sure you understand what to do here.

However the quick and dirty guide that worked for me is:

Run 'menu.sh' and select Blynk server, and pull from template. Alternatively you can manually edit Dockerfile in ~/IOTstack/services/blynk_server. Here is where you can change the version to any one you want.

As this container uses a Dockerfile, the command to update is slightly different to a non-Dockerfile container:
docker-compose up --build -d blynk_server

As the container builds, you will see the version number in the terminal at step 3/11.

To tidy up run docker system prune.

Quickstart Blynk guide

Admin interface

Log into admin panel at https://youripaddress:9443/admin (Use your Pi's IP address, and ignore Chrome warning).

Default credentials:
user: admin@blynk.cc
pass: admin

You can create a new account in the app, see "iOS/Android app setup" section.

Change username and password: Click on Users > "email address" and edit email, name and password.
Save changes
Restarting the container using Portainer may be required to take effect.

NOTE IGNORE THE CONFIG SECTION, DOES NOT CURRENTLY WORK AND CHANGES ARE NOT SAVED info

iOS/Android app setup

Login the app as per the photos HERE
You can also "Create New Account" here if you want to, make sure to choose custom server.
Press "New Project".
Give it a name, choose device "Raspberry Pi 3 B" so you have plenty of virtual pins available, and lastly select WiFi.
Create project and the auth token will be emailed to you (if emails configured). You can also find the token in app under the phone app settings, or in the admin web interface by clicking Users>"email address" and scroll down to token.

Quick usage guide for app:

Press on the empty page, the widgets will appear from the right.
Select your widget, let's say a button.
It appears on the page, press on it to configure.
Give it a name and colour if you want.
Press on PIN, and select virtual. Choose any pin i.e. V0
Press ok.
To start the project running, press top right Play button.
You will get an offline message, because no devices are connected to your project via the token.
Enter node red.....

Node red:

Install node-red-contrib-blynk-ws from palette manager
Drag a "write event" node into your flow, and connect to a debug node
Configure the Blynk node for the first time:
URL: wss://youripaddress:9443/websockets more info HERE
Note for non-SSL you need to use port 8180 instead of 8080. Why?
Enter your auth token from before and save/exit.
When you deploy the flow, notice the app shows connected message, as does the Blynk node.
Press the button on the app, you will notice the payload is sent to the debug node.

Further reading

Further information and advanced setup: https://github.com/blynkkk/blynk-server

Check the documentation: https://docs.blynk.cc/

Visit the community forum pages: https://community.blynk.cc/

Interesting post on MQTT/Node Red flows: https://community.blynk.cc/t/my-home-automation-projects-built-with-mqtt-and-node-red/29045

Some Blynk flow examples: https://github.com/877dev/Node-Red-flow-examples

Useful terminal commands

Stop the container docker-compose -f ~/IOTstack/docker-compose.yml stop blynk_server

Delete the container (use with caution) docker-compose -f ~/IOTstack/docker-compose.yml rm -f blynk_server

Start the container (rebuild if deleted) docker-compose -f ~/IOTstack/docker-compose.yml up --build -d blynk_server

Edit server.properties (requires nano editor) sudo nano ~/IOTstack/volumes/blynk_server/data/config/server.properties

Edit mail.properties (requires nano editor) sudo nano ~/IOTstack/volumes/blynk_server/data/config/mail.properties

Troubleshooting:

Check you have actually started the stack (see useful commands section).

Portainer is recommended, where you can check if the container is running. Click on the Blynk container, and under "quick actions" select "Logs" to see useful messages.

Additionally check the Blynk logs at ~/IOTstack/volumes/blynk_server/data/logs

Config files not being created - run ./menu.sh and make sure Blynk selected, then choose "Pull full service from template". This ensures the directoryfix.sh script is run and the sample config files are created in the correct location.

If you want to delete and start over, or have an existing older Blynk container, follow the "Nuclear option" section.

Nuclear option

Only use this if you messed up, have an existing container, or just want a fresh start. You will lose all data! Backup you config files if needed!

Make sure that IOTstack is up to date, so you get the latest Dockerfile, service.yml and script files.

  1. Stop the container docker-compose -f ~/IOTstack/docker-compose.yml stop blynk_server
  2. Delete the container docker-compose -f ~/IOTstack/docker-compose.yml rm -f blynk_server
  3. Delete all old data sudo rm -rf ~/IOTstack/volumes/blynk_server
  4. In Portainer, go to images and delete the old Blynk image (may not need to do this, but just in case)
  5. Rebuild the container and start docker-compose -f ~/IOTstack/docker-compose.yml up --build -d blynk_server Note this will recreate standard config files, as you deleted them in step 3. You will need to edit them again as per the "Configuring for first run" section, or restore from your backup (you did backup right?).
  6. Any issues with config files not getting created, see Troubleshooting section.

Developer information

Included files:

Dockerfile, service.yml and directoryfix.sh.

Possible future improvements

  1. Fine tune the server.properties file
  2. Add instructions for certificates
  3. Dockerfile links to "latest" version of server, rather than having to edit it manually. Issue created on github here

General issues & thoughts

  1. The dockerfile attempts to create a server.properties file, but it does not persist. Possible issue caused with creating of VOLUME in build process. See mistake no 9 here
  2. The 'normal' way of running the Blynk server is to call a script from the dockerfile, which creates the *.properties files and runs the container. But i think this would overwrite the *.properties files each time the container is run. I also had issues getting the script to be copied over and executed, possibly a lack of knowledge.
  3. Currently the server version has to be defined in the dockerfile via ENV BLYNK_SERVER_VERSION 0.41.14. Possibly there is some way to just use the latest version.
#!/bin/bash
# Create config files for Blynk custom server
#current user
u=$(whoami)
#Check if the config directory already exists:
if [ ! -d ./volumes/blynk_server/data/config ]; then
#Create the config directory
sudo mkdir -p ./volumes/blynk_server/data/config
#Create the properties files:
#cd ~/IOTstack/volumes/blynk_server/data/config
sudo touch ./volumes/blynk_server/data/config/server.properties
sudo touch ./volumes/blynk_server/data/config/mail.properties
#Give permissions:
sudo chown -R $u:$u ./volumes/blynk_server/data/config
#Populate the server.properties file:
sudo echo "hardware.mqtt.port=8440
http.port=8080
force.port.80.for.csv=false
force.port.80.for.redirect=true
https.port=9443
data.folder=./data
logs.folder=./logs
log.level=info
user.devices.limit=10
user.tags.limit=100
user.dashboard.max.limit=100
user.widget.max.size.limit=20
user.message.quota.limit=100
notifications.queue.limit=2000
blocking.processor.thread.pool.limit=6
notifications.frequency.user.quota.limit=5
webhooks.frequency.user.quota.limit=1000
webhooks.response.size.limit=96
user.profile.max.size=128
terminal.strings.pool.size=25
map.strings.pool.size=25
lcd.strings.pool.size=6
table.rows.pool.size=100
profile.save.worker.period=60000
stats.print.worker.period=60000
web.request.max.size=524288
csv.export.data.points.max=43200
hard.socket.idle.timeout=10
enable.db=false
enable.raw.db.data.store=false
async.logger.ring.buffer.size=2048
allow.reading.widget.without.active.app=false
allow.store.ip=true
initial.energy=1000000
admin.rootPath=/admin
restore.host=blynk-cloud.com
product.name=Blynk
admin.email=admin@blynk.cc
admin.pass=admin
" > ./volumes/blynk_server/data/config/server.properties
#Populate the mail.properties file:
sudo echo "mail.smtp.auth=true
mail.smtp.starttls.enable=true
mail.smtp.host=smtp.gmail.com
mail.smtp.port=587
mail.smtp.username=YOUR_GMAIL@gmail.com
mail.smtp.password=YOUR_GMAIL_APP_PASSWORD
mail.smtp.connectiontimeout=30000
mail.smtp.timeout=120000
" > ./volumes/blynk_server/data/config/mail.properties
#Information messages:
echo "Sample properties files created in ~/IOTstack/volumes/blynk_server/data/config"
echo "Make sure you edit the files with your details, and restart the container to take effect."
fi
FROM adoptopenjdk/openjdk14
MAINTAINER 877dev <877dev@gmail.com>
#RUN apt-get update
#RUN apt-get install -y apt-utils
#RUN apt-get install -y default-jdk curl
ENV BLYNK_SERVER_VERSION 0.41.14
RUN mkdir /blynk
RUN curl -L https://github.com/blynkkk/blynk-server/releases/download/v${BLYNK_SERVER_VERSION}/server-${BLYNK_SERVER_VERSION}.jar > /blynk/server.jar
# Create data folder. To persist data, map a volume to /data
RUN mkdir /data
# Create configuration folder. To persist data, map a file to /config/server.properties
RUN mkdir /config && touch /config/server.properties
VOLUME ["/config", "/data/backup"]
# IP port listing:
# 8080: Hardware without ssl/tls support
# 9443: Blynk app, https, web sockets, admin port
EXPOSE 8080 9443
WORKDIR /data
ENTRYPOINT ["java", "-jar", "/blynk/server.jar", "-dataFolder", "/data", "-serverConfig", "/config/server.properties", "-mailConfig", "/config/mail.properties"]
blynk_server:
build: ./services/blynk_server/.
container_name: blynk_server
restart: unless-stopped
ports:
- "8180:8080"
- "8441:8441"
- "9443:9443"
volumes:
- ./volumes/blynk_server/data:/data
- ./volumes/blynk_server/data/config:/config
@Zephael86
Copy link

Migrating data from existing Blynk Local Server

These steps assume local data storage and does not cover if data is stored in a separate database.
[Old_Blynk] commands were executed in the root of the data.folder.

[Old Blynk Server]
Stop the old Blynk server. Assuming you have a Linux box e.g.: killall java
Remove every startup script on your old box that is related to the old Blynk.

[Docker Host]
Stop container if it is running. docker stop blynk_server

1, SSL certs related steps
[Old Blynk]
Check whether you are using any custom certificates? cat server.properties | grep ssl

If the 'server.ssl.cert' and 'server.ssl.key' are set, copy these files to the new Blynk.
Also note down the value of 'server.ssl.key.pass'.

[Old Blynk]
By default 'root' ssh access is not allowed on Raspbian. You have to login with 'pi' and use an intermediate folder.
Use scp, e.g.: scp server.* pi@IP_OF_DOCKER_HOST:/tmp/

[Docker Host]
Move the certificates to a folder that is mapped to a Volume. I suggest the 'config' folder. Make 'root' the owner.

sudo mkdir ~/IOTstack/volumes/blynk_server/data/config/ssl
sudo mv /tmp/server.* ~/IOTstack/volumes/blynk_server/data/config/ssl/
sudo chown root:root ~/IOTstack/volumes/blynk_server/data/config/ssl/*

Edit the 'server.properties' file to read your certificates. e.g.:

server.ssl.cert=/config/ssl/server.crt
server.ssl.key=/config/ssl/server.pem
server.ssl.key.pass=%PREVIOUSLY_NOTED_SECRET_PASS%

2, Data related steps

Users and their projects are stored in separate files inside the root of the 'data.folder' in format 'LOGON_EMAIL.Blynk.user'.
Copy these files to the new machine as seen in the previous step.

[Old Blynk]
scp *.Blynk.user pi@IP_OF_DOCKER_HOST:/tmp/

[Docker Host]
Move the user files to the 'data.folder', and make 'root' the owner.

sudo mv /tmp/*.Blynk.user ~/IOTstack/volumes/blynk_server/data/
sudo chown root:root ~/IOTstack/volumes/blynk_server/data/*Blynk.user

Historical data is stored inside 'data.folder' within a folder called 'data'.
If 'data.folder=/data' then this folder is '/data/data' inside the container.
On the docker host it is '~/IOTstack/volumes/blynk_server/data/data/'

[Old Blynk]
scp -r data pi@IP_OF_DOCKER_HOST:/tmp/

[Docker Host]
Move the data inside the 'data.folder' and change ownership.

sudo mv /tmp/data ~/IOTstack/volumes/blynk_server/data/
sudo chown -R root:root ~/IOTstack/volumes/blynk_server/data/data/

3, Port forward (if any)
Please do not forget to adapt the port forwards rules in your Rooter from the [Old_Blynk_Server] to the [Docker_Host].
This could mean the port 8080 and/or 9443.

4, Restart container and check
Start your container.

[Docker Host]
docker start blynk_server

Logon to the admin panel (see: Quick start Blynk guide chapter) using your migrated user.
You should be able to see your projects/devices.
The Blynk Android application should be able to connect without changing anything.

@877dev
Copy link
Author

877dev commented Oct 14, 2020

3, Port forward (if any)
Please do not forget to adapt the port forwards rules in your Rooter from the [Old_Blynk_Server] to the [Docker_Host].
This could mean the port 8080 and/or 9443.

Remember IOTstack Blynk uses port 8180 to avoid conflicts, check the service.yml.
Otherwise it looks good but I cannot test it myself :)

@Zephael86
Copy link

Possible future improvements
Add instructions for certificates

Suggestion: Link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment