-
-
Save fvdbosch/fa442adf6e735969cf92ee371fac1cc3 to your computer and use it in GitHub Desktop.
Displaying emonPi data on PaPiRus Zero
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import sys | |
import mosquitto | |
import time | |
from datetime import datetime | |
from papirus import Papirus | |
from PIL import Image | |
from PIL import ImageDraw | |
from PIL import ImageFont | |
user = os.getuid() | |
if user != 0: | |
print "Please run script as root" | |
sys.exit() | |
papirus = Papirus() | |
ct1_value = "0" | |
ct2_value = "0" | |
ct1_prev_value = "0" | |
ct2_prev_value = "0" | |
previous_second = 0 | |
def on_message(mosq, obj, msg): | |
global ct1_value, ct2_value, ct1_prev_value, ct2_prev_value | |
if msg.topic == "emon/emonpi/power1": | |
ct1_value = str(msg.payload) | |
if msg.topic == "emon/emonpi/power2": | |
ct2_value = str(msg.payload) | |
if ct1_value != ct1_prev_value or ct2_value != ct2_prev_value: | |
ct1_prev_value = ct1_value | |
ct2_prev_value = ct2_value | |
display_data() | |
def display_data(): | |
global ct1_value, ct2_value, previous_second | |
now = datetime.today() | |
image = Image.new('1', papirus.size, 1) | |
draw = ImageDraw.Draw(image) | |
font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf" | |
font_title = ImageFont.truetype(font_path, 20) | |
font_values = ImageFont.truetype(font_path, 15) | |
draw.text( (0, 0) , "emonPi", font=font_title, fill=0) | |
draw.text( (0, 1) , "_________", font=font_title, fill=0) | |
draw.text( (0, 30) , "CT1: " + ct1_value + "W", font=font_values, fill=0) | |
draw.text( (0, 50) , "CT2: " + ct2_value + "W", font=font_values, fill=0) | |
draw.text( (0, 75) , "Total: " + str(int(ct1_value) + int(ct2_value)) + "W", font=font_values, fill=0) | |
papirus.display(image) | |
if now.second < previous_second: | |
papirus.update() | |
else: | |
papirus.partial_update() | |
previous_second = now.second | |
def get_ip_address(): | |
return os.popen("ifconfig wlan0 | grep 'inet addr' | awk -F: '{print $2}' | awk '{print $1}'").read().strip() | |
def main(): | |
client = mosquitto.Mosquitto() | |
client.on_message = on_message | |
client.username_pw_set("emonpi", "emonpimqtt2016") | |
client.connect("192.168.0.123", 1883, 60) | |
client.subscribe([("emon/emonpi/power1", 2), ("emon/emonpi/power2", 2)]) | |
while 1: | |
client.loop() | |
return 0 | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
why don't you use paho-mqtt?
This repository uses it.
https://raw.githubusercontent.com/DesignSparkrs/papirus/master/mqttPapirus