-
1Step 1
Install you Raspberry Pi with NOOBS:
-
2Step 2
Install and configure SBFSpot for the logging of your SMA inverter
SBFSpot installation on the Rpi
Make sure that csv logging is enabled, since my python script uses these files.
-
3Step 3
Log in using ssh on the Pi
Install the wiringpi2 library on the raspberry:
sudo apt-get update sudo apt-get install python-dev python-pip sudo pip install wiringpi2
-
4Step 4
Now the actual script that filters the power data from the SMA logging:
mkdir /home/pi/scripts nano /home/pi/scripts/show_actual.py
Adjust the values in #defines to match your preferences
#!/usr/bin/python import time import datetime import wiringpi2 as wiringpi import os.path #defines: system_name="Your_Pv_System" # System namen in SBFspot sbfspot_log_location="/home/pi/smadata/" # logging of SBFspot meter_zero_offset=0 # normal range: 0-1024, offset range = 100-1024 meter_max_offset=0 # normal range: 0-1024, offser range = 0-824 max_kwh_day=30 # max kwh generated in 1 day max_power=6000 # max inverter AC power feed debug_mode=1 # print information while running #initialise PWM wiringpi.wiringPiSetupGpio() wiringpi.pinMode(18,2) # pwm only works on GPIO port 18 wiringpi.pwmWrite(18, 0) # duty cycle between 0 and 1024. 0 = off, 1024 = fully on previous_value = 0 daynight="none" while 1 == 1: prev_daynight=daynight #Read & parse SBFspot generated datafile fulldate= str(datetime.datetime.now().year) + str(datetime.datetime.now().month).zfill(2) + str(datetime.datetime.now().day).zfill(2) datafile=sbfspot_log_location + str(datetime.datetime.now().year) + "/" + system_name + "-Spot-" + fulldate + ".csv" if os.path.isfile(datafile) == True: file = open(datafile) for line in file: pass # Get last line in log file.close() actual_data = line.split(',') # Appoint values from csv columns actual_power = actual_data[20] total_kwh_today = actual_data[22] gridrelay_status = actual_data[29] #Daytime, gridrelay_status = "Closed" if gridrelay_status == "Closed": # Day mode: show actual power daynight = "day" actual_value = float(actual_power) maxvalue=max_power else: daynight = "night" # Night mode: show total energy generated today actual_value = float(total_kwh_today) maxvalue=max_kwh_day if actual_value > maxvalue: actual_value = maxvalue if (previous_value != actual_value) or (prev_daynight != daynight): if debug_mode==1: print "gridrelay: " + gridrelay_status print "Actual Power:\t\t" + str(actual_power) print "Total kWh today:\t" + str(total_kwh_today) for i in range (1,21): newvalue = previous_value+((actual_value-previous_value)/20)*i value_out = int(meter_zero_offset+(newvalue/maxvalue)*(1024-meter_zero_offset-meter_max_offset)) wiringpi.pwmWrite(18, value_out ) if debug_mode==1: print ("New val: %8.3f\tMax val: %d\tPct: %6.2f\t0-1024: %4d" % (newvalue, maxvalue, (newvalue/maxvalue)*100, value_out)) time.sleep( 0.25 ) previous_value = actual_value else: time.sleep( 1 ) else: time.sleep ( 5 )
-
5Step 5
Test the script by executing the following command:
sudo python /home/pi/scripts/show_actual.py
It should show something like the following output (nighttime):pi@raspberrypi ~ $ sudo python /home/pi/scripts/show_actual.py gridrelay: Open Actual Power: 0.000 Total kWh today: 5.987 New val: 0.299 Max val: 30 Pct: 1.00 0-1024: 10 New val: 0.599 Max val: 30 Pct: 2.00 0-1024: 20 New val: 0.898 Max val: 30 Pct: 2.99 0-1024: 30 New val: 1.197 Max val: 30 Pct: 3.99 0-1024: 40 New val: 1.497 Max val: 30 Pct: 4.99 0-1024: 51 New val: 1.796 Max val: 30 Pct: 5.99 0-1024: 61 New val: 2.095 Max val: 30 Pct: 6.98 0-1024: 71 New val: 2.395 Max val: 30 Pct: 7.98 0-1024: 81 New val: 2.694 Max val: 30 Pct: 8.98 0-1024: 91 New val: 2.994 Max val: 30 Pct: 9.98 0-1024: 102 New val: 3.293 Max val: 30 Pct: 10.98 0-1024: 112 New val: 3.592 Max val: 30 Pct: 11.97 0-1024: 122 New val: 3.892 Max val: 30 Pct: 12.97 0-1024: 132 New val: 4.191 Max val: 30 Pct: 13.97 0-1024: 143 New val: 4.490 Max val: 30 Pct: 14.97 0-1024: 153 New val: 4.790 Max val: 30 Pct: 15.97 0-1024: 163 New val: 5.089 Max val: 30 Pct: 16.96 0-1024: 173 New val: 5.388 Max val: 30 Pct: 17.96 0-1024: 183 New val: 5.688 Max val: 30 Pct: 18.96 0-1024: 194 New val: 5.987 Max val: 30 Pct: 19.96 0-1024: 204
Crtl+c to quit....
-
6Step 6
Now to make the datalogger and meter controller run automatically at boot:
sudo nano /etc/rc.local
And add the following.2.lines before the "exit 0"/home/pi/scripts/SBFspot.sh > /dev/null 2>&1 & python /home/pi/scripts/show_actual.py > /dev/null 2>&1 & exit 0
-
7Step 7
Create an additional script to run SBFSpot every 5 seconds, except in the night time...
Tweak the location to the SBFspot binary if neccessary
nano /home/pi/scripts/SBFspot.sh
#!/bin/bash sbfspot_bin_path="/home/pi/smaspot/SBFspot/bin/Release" while true do cd $sbfspot_bin_path if timeout 10s ./SBFspot -v | grep -q "it's dark"; then # It is dark... using 5 minute delays and skipping file writes sleep 300 else # Update log every 5 seconds sleep 5 fi done
-
8Step 8
(Optional): If you are using a wifi dongle, it's wise to monitor the connection (no auto reconnect on NOOBS)
sudo nano /home/pi/scripts/keep_wifi_alive.sh
add the following content:#!/bin/bash if ifconfig wlan0 | grep -q "inet addr:"; then echo else date=$(date) echo "Wifi down @ " $date ", Trying to reconnect" >> /tmp/wifi_status ifup --force wlan0 if ifconfig wlan0 | grep -q "inet addr:"; then echo "Reconnection succesful!" >> /tmp/wifi_status else echo "Reconnection not succesful, trying again in 1 minute" >> /tmp/wifi_status fi fi
Make it executable:
chmod +x /home/pi/scripts/keep_wifi_alive.sh
Now auto run the check every minute:
sudo crontab -e
add the following line in the bottom of the file*/1 * * * * sudo /home/pi/scripts/keep_wifi_alive.sh >/dev/null 2>&1
Strangely, the Rpi has a bug in crontab which requited te "sudo" above... Hell.. ugly, but it works ;)
-
9Step 9
Now the software configuration should be fine :) Reboot the Pi and start connecting the hardware... (instructions will be added when I find the time)
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.
Hi SBF: Of course there is no problem linking to this page! Thanks for the thumbs up ;)
In case someone runs into the gridrelay issue, i'll make sure to create a fix in the code so everyone can enjoy day/nicht settings...
Are you sure? yes | no
Hi Sander,
Very nice project! I added a link on the SBFspot project page, I hope you don't mind.
One remark: gridrelay_status is not available on all inverters (e.g. SB 3300), so there it might go wrong.
Are you sure? yes | no