Visualisierung des Stromverbrauchs / PV Einspeisung per farbiger LEDs
Damit die LEDs per serieller Schnittstelle angesteuert werden können, muss die Schnittstelle aktiviert werden. Dazu installiert man (sofern noch nicht geschehen) die python-dev und git Tools mit
apt-get install git python-dev
Dann holt man sich py-spidev und installiert es:
git clone git://github.com/doceme/py-spidev cd py-spidev sudo python setup.py install
Das PixelPi Script von https://github.com/scottjgibson/PixelPi wurde verschlankt und etwas umgebaut. So ist dieses Python Scripte entstanden. Das Scrpit übernimmt folgende Funktionen:
import httplib2 as http import json from pprint import pprint import argparse import csv import socket import time try: from urlparse import urlparse except ImportError: from urllib.parse import urlparse headers = { 'Accept': 'application/json', 'Content-Type': 'application/json; charset=UTF-8' } targetELW = urlparse('http://sqlserver/middleware.php/data/f2059450-bbbe-11e3-aabc-7bfab5f4b02e.json?from=now') targetHeizung = urlparse('http://sqlserver/middleware.php/data/26bce7c0-bbc2-11e3-b634-3334d2c934ae.json?from=now') targetHaus = urlparse('http://sqlserver/middleware.php/data/32e8d3d0-bbc2-11e3-8f42-eb76d7115f4b.json?from=now') targetPV = urlparse('http://sqlserver/middleware.php/data/4186f3e0-bbc2-11e3-a87a-8b10388c97ce.json?from=now') gamma = bytearray(256) spi_dev_name = '/dev/spidev0.0' PIXEL_SIZE = 3 SCALE = 500 BLACK = bytearray(b'\x00\x00\x00') BLUE = bytearray(b'\x00\x00\xff') CYAN = bytearray(b'\x00\xff\xff') FUCHSIA = bytearray(b'\xff\x00\xff') GREEN = bytearray(b'\x00\xff\x00') RED = bytearray(b'\xff\x00\x00') WHITE = bytearray(b'\xff\xff\xff') YELLOW = bytearray(b'\xff\xff\x00') # Apply Gamma Correction and RGB / GRB reordering # Optionally perform brightness adjustment def filter_pixel(input_pixel, brightness): output_pixel = bytearray(PIXEL_SIZE) input_pixel[0] = int(brightness * input_pixel[0]) input_pixel[1] = int(brightness * input_pixel[1]) input_pixel[2] = int(brightness * input_pixel[2]) output_pixel[0] = gamma[input_pixel[0]] output_pixel[1] = gamma[input_pixel[1]] output_pixel[2] = gamma[input_pixel[2]] return output_pixel def write_stream(pixels): spidev.write(pixels) return # MAIN spidev = file(spi_dev_name, "wb") for i in range(256): gamma[i] = int(pow(float(i) / 255.0, 2.5) * 255.0) Blinker = 0 pixel_output = bytearray(16 * PIXEL_SIZE + 3) method = 'GET' body = '' h = http.Http() while True: responseELW, contentELW = h.request(targetELW.geturl(),method,body,headers) ELW = json.loads(contentELW) ELWled = int(float(ELW["data"]["tuples"][0][1])) / SCALE responseHeizung, contentHeizung = h.request(targetHeizung.geturl(),method,body,headers) Heizung = json.loads(contentHeizung) Heizungled = int(float(Heizung["data"]["tuples"][0][1])) / SCALE responseHaus, contentHaus = h.request(targetHaus.geturl(),method,body,headers) Haus = json.loads(contentHaus) Haus["data"]["tuples"][0][1] = Haus["data"]["tuples"][0][1] - ELW["data"]["tuples"][0][1] Hausled = int(float(Haus["data"]["tuples"][0][1])) / SCALE responsePV, contentPV = h.request(targetPV.geturl(),method,body,headers) PV = json.loads(contentPV) PVled = int(float(PV["data"]["tuples"][0][1])) / SCALE led = 0 print "Haus: ", int(float(Haus["data"]["tuples"][0][1])), " Hausled: ",Hausled, "PV: ", int(float(PV["data"]["tuples"][0][1])) , " PVled ",PVled for led in range(19): pixel_output[led * PIXEL_SIZE:] = filter_pixel(BLACK, 1) if Hausled > led: pixel_output[(led * PIXEL_SIZE)+0] = '\xff' elif (Hausled == led) and (int(float(Haus["data"]["tuples"][0][1])) > 440): pixel_output[(led * PIXEL_SIZE)+0] = int(( float(Haus["data"]["tuples"][0][1]) % SCALE) / SCALE * 255) if -Hausled > led: pixel_output[(led * PIXEL_SIZE)+1] = '\xff' elif (-Hausled == led) and ( int(float(Haus["data"]["tuples"][0][1])) < -440): pixel_output[(led * PIXEL_SIZE)+1] = int(( float(Haus["data"]["tuples"][0][1]) % SCALE) / SCALE * 255) if (PVled > led) and (PVled > -Hausled) and (PVled > Hausled): pixel_output[(led * PIXEL_SIZE)+2] = '\xff' elif PVled == led: pixel_output[(led * PIXEL_SIZE)+2] = int(( float(PV["data"]["tuples"][0][1]) % SCALE) / SCALE * 255) if (led == 18): if (Blinker == 0): Blinker = 1 pixel_output[led * PIXEL_SIZE:] = filter_pixel( bytearray(b'\x00\x1C\x00'), 1) else: Blinker = 0 pixel_output[led * PIXEL_SIZE:] = filter_pixel(BLACK, 1) for c in pixel_output: print(c), print('\n') write_stream(pixel_output) spidev.flush() time.sleep(2)