Categories
Batteries

Batrium + Mate3 + Grafana : Part 2

Finally, the fun part.

In this guide, I will be using code blocks to show you what to enter into the command line interface (CLI), illustrated below.

sudo example-code-block -butts

# stuff prefixed with a hash is my comments

When a code block like this appears, it means that you have to enter each line individually into your CLI and hit enter.

I might add notes, just for you, which are prefaced with the hash symbol (#) – don’t put these into the CLI if you can help it.

Wanna skip ahead?
Part 1 : Hardware configuration
Part 2 : Installing software
Part 3 : Configuring Grafana
Part 4 : Credits and final thoughts

Changing your password, and enabling SSH

First we need to connect your Pi4 to your Wifi network, using the little Wifi icon on the taskbar. Select your network SSID, and enter your password.

Next we need to change your Pi4’s user password from the default, and turn on SSH so we can do the rest of the hard work from the comfort of your personal computer.

Once we get through these early stages of setting up the Pi4, you’ll be using PuTTY to access your Pi4 remotely using SSH, so you should probably install in on your PC.

Now, open the Pi4’s CLI (called terminal)

Remember, each line has to be entered separately and ENTER hit before going to the next line!

Change your password:

sudo raspi-config
#hit enter

passwd

#you will be prompted to enter your password. 
#For the purposes of this tutorial I'll use 123456 - but please, for the love of god, use something a little harder to crack.
# Do it, and hit enter. 
# You need to do this so that you're not hacked immediately once we enable SSH. 

Pi4 username and password is now Pi/1234.

Turn on SSH:

sudo systemctl enable ssh
sudo systemctl start ssh

Find out your Pi’s IP address

ifconfig
Loading Debian (Ubilinux) on the Edison - learn.sparkfun.com
There should be a block of text with “wlan0” or something similar to the left of it. You need the addr: number, in this illustration it’s 10.7.0.47.

If you’re using a wired connection, you want the “eth0” IP address. “eth” stands for ethernet. You should probably know that.

The reason it says eth0 is because in the computer language world, stuff counts from 0.

So if you have three bananas, you would count them Banana 0, Banana 1, Banana 2.

I promise this will become important later, when we’re dealing with JSON files.

It’s a very good idea to write this IP address down somewhere, as we’ll be referring to it often.

Head back to your computer, and install PuTTY.

Open PuTTY, and create a new connection by;

  1. Entering your Pi4’s IP address in the Host Name box (our example uses 10.7.0.47)
  2. Typing Pi (or whatever you want) in the Saved Sessions box.
  3. Hit “Save”, so you don’t have to type the IP address again in the future.
  4. Either double click the now saved Pi session, or make sure it’s selected and click on “Open”

to paste code into PuTTY’s CLI, copy the code from this page and right-click inside the CLI – the code should paste auto-magically!

You’ll now be presented with a CLI window, asking for your username.

Type in;

pi

The CLI will now ask you for your password, this is the password we set earlier, as an example we used 1234.

1234

then hit enter again (the password text won’t be displayed).

if everything does well, you should see something like this.

Updating everything on your before we go on.

On the now logged-in and authenticated PuTTY terminal, enter the following:

sudo apt-get update
sudo apt-get upgrade -y

If at any stage through this tutorial the CLI asks for confirmation to install/update, type Y and hit enter.

Installing Grafana

sudo apt-get install -y apt-transport-https

sudo apt-get install -y software-properties-common wget

wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee -a /etc/apt/sources.list.d/grafana.list

# that paragraph above is all a single line

sudo systemctl enable --now grafana-server

sudo /bin/systemctl daemon-reload 

sudo /bin/systemctl enable grafana-server

sudo /bin/systemctl start grafana-server 

systemctl status grafana-server.service 

sudo apt -y install ufw

sudo ufw enable

sudo ufw allow ssh

sudo ufw allow 3000/tcp

#enter admin and admin for default username, (Default will need to be reset after first login.)





Check to make sure Grafana is working

Open your PC’s web browser, and enter the IP address of your Pi4 into your URL bar, suffixed with :3000 – ie, 10.7.0.47:3000
You’ll be asked to choose a new password, for this example we’ll choose 123456.

Grafana username and password is now admin/123456.

Installing InfluxDB

get -qO- https://repos.influxdata.com/influxdb.key | sudo apt-key add -
source /etc/os-release

echo "deb https://repos.influxdata.com/debian $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/influxdb.list

sudo apt-get update && sudo apt-get install influxdb

sudo service influxdb start

sudo systemctl enable --now influxdb 
#enable and run on system boot


sudo systemctl status influxdb 
#check status

#hit ctrl-c to close


sudo ufw allow 8086/tcp 
#This command will open the default port for Influxdb
Create InfluxDB Database for your Batrium data.
influx 

#type 'influx' you will notice a different CLI prompt. This is normal, you are now in the influx CLI.


create database batrium

create user batrium with password '123456'

grant all on batrium to batrium

show databases

show users

exit

influx -username 'batrium' -password '123456' -database batrium

exit

Congratulations, you now have an empty Batrium database!

InfluxDB username and password is now batrium/123456.

Installing node-red

get -qO- https://repos.influxdata.com/influxdb.key | sudo apt-key add -source /etc/os-release

echo "deb https://repos.influxdata.com/debian $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/influxdb.list

sudo apt-get update && sudo apt-get install influxdb

sudo apt install nodejs

nodejs -v

sudo apt install npm

sudo npm install -g --unsafe-perm node-red node-red-admin

sudo ufw allow 1880

sudo ufw allow 18542/udp 
#Opening this port allows Node-Red to "listen" for the data from the Batrium

node-red 
#This starts node-red.


Check to make sure node-red is working

Open your PC’s web browser, and enter the IP address of your Pi4 into your URL bar, suffixed with :1880– ie, 10.7.0.47:1880

Tell node-red to boot on startup.
sudo nano /etc/systemd/system/node-red.service 
# this should open up a different graphical interface in your CLI, paste in the following
.

[Unit]
Description=Node-RED
After=syslog.target network.target

[Service]
ExecStart=/pi/local/bin/node-red
Restart=on-failure
KillSignal=SIGINT

# log output to syslog as 'node-red'
SyslogIdentifier=node-red
StandardOutput=syslog

# non-root user to run as
WorkingDirectory=/home/pi/
User=pi
Group=pi

[Install]
WantedBy=multi-user.target

#once you have pasted in the text above, hit:

ctrl-o
#this saves the file

ctrl-x
#this exits the file

sudo systemctl enable node-red 

sudo systemctl start node-red 

sudo systemctl stop node-red

#Reboot and test to ensure node-red starts up.
Installing binary-parser, required by node-red.
#Navigate to your Node-Red install folder. 

sudo -i

cd /home/pi/.node-red 
#substitute 'pi' for you current Linux user.

npm install binary-parser

#We now need to edit our Node-Red settings.js file to enable the binary-parser

sudo nano /home/pi/.node-re/settings.js 
#again substitute 'pi' for your current linux user.



#this will open up a blue text editor screen with a lot of information. You need to add some text after the following line: "functionGlobalContext:"

#This line can be found if you hit "page-down" on your keyboard around six or seven times. It's annoying.

binary_parser:require('binary-parser').Parser,

ctrl-o
ctrl-X

Y
enter

Importing the Batrium flow into node-red

Open Node-Red in your PC web browser.
IP address of your Pi4 into your URL bar, suffixed with :1880- ie, 10.7.0.47:1880
In the top-right menu icon, click on Manage palette.

You need to install two new palettes (plug-ings, basically)

  • node-red-contrib-influxdb
  • node-red-dashboard

Put these two palette names into the install tab search box, and click on “install“. Accept any prompts that come up.

  • Click HERE to copy the Batrium flow JSON

Import Batrium flow json by clicking the top right menu icon, then click on import.

Past the Batrium JSON file, import to “new flow”, and click “import”

Your node-red flow should look something like this.

Of note here is that this flow is set up to listen for the Batrium UDP data on port 18542. We opened up this UDP port during the Node-Red install. This flow is also set up for a string of 16 longmons. If you have more, you will need to edit some of the nodes to reflect this.

Feel free to hit the big red deploy button!

Now we need to Deploy the Node-Red flow and check the debug tab for any errors. If you see some “TypeErrors” then it is usually because the Binary-Parser was not installed correctly. Also, you can toggle the green msg.payload debug filters on and off to see the data being received in the debug window.

If all you’re interested in is getting data from a Batrium, click here to go to Part 3: Setting up Grafana


…or click this link to read how to set up the Mate3 JSON ingest.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s