Installing mosquitto
Setting up the Raspberry Pi is quite simple and straight forward. I used the default Raspbian image. Then a MQTT broker is needed. Under Linux this usually is mosquitto and it can be installed using the package manager.
So type in:
sudo apt-get install mosquitto mosquitto-clients
This installs mosquitto and the command line tools to control it. and this is it. Mosquitto starts automatically at every start at the system.
Testing mosquitto
As written in my previous blog post I decided to transfer the data under the topic /power_priority
To view all data under this topic you just type in:
mosquitto_sub -h localhost -t /power_priority
To send data to this topic type in:
mosquitto_pub -h localhost -t /power_priority -m 23
Automatic testing
These command line commands can be used to setup a script which sends values from 0 to 100 to the topic in a loop.
#!/bin/bash sleeptime=2 while true; do i=0 while [ $i -lt 100 ]; do echo Prio: $i mosquitto_pub -h localhost -t /power_priority -m $i sleep $sleeptime i=$[$i+10]; done while [ $i -gt 0 ]; do echo Prio: $i mosquitto_pub -h localhost -t /power_priority -m $i sleep $sleeptime i=$[$i-10]; done done
Using data from aWATTar
The next step is to use the data from aWATTar. Their API is described here https://www.awattar.at/services/api/
The prices for the next 24 hours can be obtained with
curl "https://api.awattar.at/v1/marketdata"
I only need the price for the running hour. So the output can be stripped using bash tools.
curl -s "https://api.awattar.at/v1/marketdata" | grep -m1 marketprice | tr -dc '0-9.'
This is transformed into a bash script:
#!/bin/bash marketprice=$(curl -s "https://api.awattar.at/v1/marketdata" | grep -m1 marketprice | tr -dc '0-9.') mosquitto_pub -h localhost -t /power_priority -m $marketprice
And this script is called every hour using cron
2 * * * * /home/pi/mqtt/awattar.sh > /dev/null
At the moment I directly publish the prices to the topic. For the start and testing this should be OK.