forked from bubbl/LCD-Monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpimonitor.py
110 lines (98 loc) · 3.2 KB
/
rpimonitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
''' An LCD to display my RPi and RPi-Monitor
This is a modified version of Xavier Berger's LCD display project. It can be found here: http://rpi-experiences.blogspot.fr/2013/08/a-lcd-display-my-rpi-and-rpi-monitor.html
This version adds a DS18B20 1-WIRE Digital Thermometer (wiring instructions:
http://learn.adafruit.com/downloads/pdf/adafruits-raspberry-pi-lesson-11-ds18b20-temperature-sensing.pdf)
to monitor room temperature.
It implements RPi-monitor https://github.com/XavierBerger/RPi-Monitor application to gather Raspberry Pi's and room temperature and display it on a screen.
Follow the instrucions provided by Xavier to set up RPi-monitor, install pcd8544 library:
https://github.com/XavierBerger/pcd8544 and run
sudo ./rpimonitor.py
or
sudo python rpimonitor.py
'''
#!/usr/bin/env python
import httplib, time, os, sys, json
import pcd8544.lcd as lcd
# class Process dedicated to process data get from Client
# and send information to LCD and console
class Process:
# Process constructor
def __init__(self):
# Initialize LCD
lcd.init()
# Turn the backlight on/off and adjust it
lcd.backlight(0)
def run(self, jsonString):
# Parse data as json
data = json.loads( jsonString )
text1 = "RPi-Monitor"
lcd.define_custom_char([0x00, 0x07, 0x05, 0x07, 0x00])
# Try to get data from json or return default value
try:
rpi_temperature = data['soc_temp']
except:
rpi_temperature="--.--"
try:
room_temp = data['living_room_temp']
except:
room_temp="--.--"
# Construct string to be displayed on screens
rpitemp = "%s" %rpi_temperature
roomtemp = "%s" %room_temp
lcd.centre_text(0, text1)
lcd.gotorc(1,0)
lcd.text("RPi Temp:")
lcd.gotorc(2,0)
lcd.text(rpitemp)
lcd.gotorc(2,8)
lcd.text("\x7fC")
lcd.gotorc(4,0)
lcd.text("Room Temp:")
lcd.gotorc(5,0)
lcd.text(roomtemp)
lcd.gotorc(5,8)
lcd.text("\x7fC")
# Class client design to work as web client and get information
# from RPi-Monitor embedded web server
class Client:
# Client constructor
def __init__(self):
# Create a Process object
self.process = Process()
def run(self):
# Infinite loop
while True:
try:
# Initiate a connection to RPi-Monitor embedded server
connection = httplib.HTTPConnection("localhost", 8889)
# Get the file dynamic.json
connection.request("GET","/dynamic.json")
# Get the server response
response = connection.getresponse()
if ( response.status == 200 ):
# If response is OK, read data
data = response.read()
# Run process object on extracted data
self.process.run(data)
# Close the connection to RPi-Monitor embedded server
connection.close()
finally:
# Wait x seconds before restarting the loop
time.sleep(3)
# Main function
def main():
try:
# Create a Client object
client = Client()
# Run it
client.run()
except KeyboardInterrupt:
# if Ctrl+C has been pressed
# turn off the lcd backlight
lcd.backlight(0);
lcd.cls()
# exit from the program
sys.exit(0)
# Execute main if the script is directly called
if __name__ == "__main__":
main()