This ansible playbook helps me setup Raspberry Pi which I am using for capturing multiple metrics:
- home energy consumption
- EV charger
- metrics from Polish electrical grid operator
Automation sets up following components:
Data pipeline is setup automatically (up to InfluxDB), but Grafana requires manual setup.
All components are run using single docker-compose and are setup for automatic restart.
graph LR;
Influx[(Influx)]
Telegraf((Telegraf))
MQTT[[MQTT]]
Grafana
HTTP{{REST API}}
Config(["Telegraf Config: <br>MQTT<br>http/JSON" ])
subgraph raspberry pi
Influx-->Grafana
Telegraf-->Influx
MQTT-->Telegraf
HTTP-->Telegraf
Config-.-Telegraf
end
OpenEVSE{{OpenEVSE}}
EM{{EnergyMeter}}
EM-. MQTT .-> MQTT
OpenEVSE-. MQTT.->MQTT
Initial Versions (run on Ubuntu 20.04)
- ansible 2.9.6
- python 3.8.10
Current version (run on MacOS Ventura 13.0.1)
- ansible 2.13.6
- python 3.11.0
You will need sshpass installed on your host machine.
Ubuntu:
sudo apt-get install sshpass
MacOS:
brew install hudochenkov/sshpass/sshpass
I am using Raspberry Pi OS Lite.
Currently tested release: 22.09.2022 (2022-09-22-raspios-bullseye-armhf-lite.img)
System will automatically install docker and docker-compose. Here are tested versions:
Docker version 20.10.5+dfsg1, build 55c4c88
docker-compose version 1.25.0, build unknown
Newer versions of docker-compose installed from PIP have issues with building correctly cryptography package.
I've switched to older docker-compose version from RaspiOS apt-get
package.
Please adjust configuration files for your needs:
- Configure WiFi network and password in wpa_supplicant.conf
- Set your Raspberry Pi IP address in inventory raspberries
- Burn image (MacOS version, you can also use Raspberry Pi Imager)
- Find your SD card (later I will use diskN)
diskutil list
- unmount any mounted partitions of SD card
diskutil unmountDisk diskN
- write image using dd command
sudo dd bs=1M if=raspberrypi_image.img of=/dev/diskN status=progress
-
Copy configuration files from
sdcard
into sd card's/boot
directoryPlease configure WiFi network and password in wpa_supplicant.conf file first!
File Description ssh Empty file. It enables SSH console userconf.txt Defines user and it's password. Setup to default pi:raspberry
. Useopenssl passwd -6
for generating other passwordwpa_supplicant.conf Defines WiFi connection. Use wpa_passphrase
for generating encrypted passwordYou can find more documentation Setting up a headless raspberry pi
-
Put a card into RaspberryPi and wait for magic to happen. RaspiOS will install the system creating a default user User: pi Password: raspberry
password can be modified simply by changing the content of userconf.txt file.
Modification of user name would require changing of multiple playbook files. -
Additional steps are required
Addtional steps are required because of rfkill issue (See enable_wifi.bash).
Root cause is related to race condition between setting up of country and setting up of Wifi network based onwpa_supplicant.conf
file.
Manual setup of country is a known workaround for Those additional steps requires keyboard and screen.
- Manual setup of country using raspi-config
sudo raspi-config nonint do_wifi_country GB
ansible-playbook rpisetup.yml --tags="allinone" -i raspberries --ask-pass -c ssh
Grafana's setup is not automated.
After containers are up and running you should be able to open grafana
Grafana
IP: http://192.168.68.100:3000/
User: admin
Psw: admin
In Grafana: select data source and setup connection to InfluxDB
Data source: InfluxDB
URL: http://influxdb:8086
Database: telegraf
User: telegraf_user
Psw: telegraf_password
You can also run step by step installation process. Use tags in the following order:
- update
- dockerinstall
- dppdirectories
- mqtt
- telegraf
- dockerrun
- Add description about using Ansible vault or headless installation of ssh keys
- Check possibility to run newer docker-compose
Add ssh fingerprint
ssh-keyscan -H 192.168.68.100 >> ~/.ssh/known_hosts