SWITCH ENERGY MONITORING
When using a managable PoE switch accesible through CLI as in the the adaptive cluster, the energy use can be read directly from the switch using the same strategy as for the rest of the automation.
The instructions below are based for a Cisco 250 Smart Switch and run in a linux setup, for other switches the commands may be different, but the strategy should be similar. “Expect” needs to be installed in the system.
#!/usr/bin/expect -f
log_user 0
spawn ssh -oHostKeyAlgorithms=+ssh-dss user@ip
expect "#"
send "show power inline\r"
expect -re "(.*)#"
foreach line [split $expect_out(1,string) \n] {
if {[string match *CBS250-8PP-D* $line]} {
puts $line
}
}
send "exit\r"
sleep 1
exit
This expect file is then called from the main python file and the value saved to a json so it can be access by the website. This function can be called with a timer to update at certain time periods.
import subprocess
def read_energy():
output = subprocess.check_output("expect /home/pi/control/energy.exp | cut -c 32-34", shell=True)
watt = output.decode("utf-8")
dictionary = {
"w": watt,
"timestamp": time.time(),
}
with open("/var/www/cellar/energy.json", "w") as outfile:
json.dump(dictionary, outfile)
with open("/var/www/cellar/history.json", "a") as outfile:
json.dump(dictionary, outfile)
outfile.write(',')