#!/usr/bin/python3
# coding: utf-8
#
# Zeigt Messwerte auf einem LCD-Display an und stellt diese auch via bottle (webserver) bereit.
#
# 15.01.2016 Copyright (C) by meigrafd (meiraspi@gmail.com) published under the MIT License
#
from time import strftime, sleep
from sys import exit
from subprocess import check_output
import bottle
import multiprocessing as mp
from Adafruit_BMP085 import BMP085
import Adafruit_DHT
import Adafruit_CharLCD as LCD
#-------------------------------------------------------------------
http_port = 80
http_host = '0.0.0.0'
#Define sensors
dht_pin=14
sensor_type=Adafruit_DHT.AM2302
bmp_addr=0x77
#lcd connections
lcd_rs=27
lcd_en=22
lcd_d4=25
lcd_d5=24
lcd_d6=23
lcd_d7=18
lcd_backlight=4
#Define some lcd constants
lcd_columns=16
lcd_rows=2
#-------------------------------------------------------------------
def read_sensors():
degree_sign=u'\N{DEGREE SIGN}'
#Initialize sensors
bmp=BMP085(bmp_addr)
humidity, temperature = Adafruit_DHT.read_retry(sensor_type, dht_pin)
temp = "{:0.1f}{}C".format(temperature, degree_sign)
hum = "{:0.1f}%".format(humidity)
pressure = bmp.readPressure()
pressure = pressure/100
pres = "{:0.0f}*hPa".format(pressure)
return temp, hum, pres
def lcd_display():
#Initialize the LCD
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, lcd_backlight)
while True:
sleep(0.1)
try:
temp, hum, pres = read_sensors()
except Exception as error:
print("Error while reading sensors: %s" % error)
else:
print(temp, hum, pres)
datum = strftime("%a %d.%m.%Y")
zeit = strftime("%H:%M:%S")
lcd.clear()
#display Zeit
lcd.message("%s\n" % (datum))
lcd.set_cursor(5, 1)
lcd.message("%s\n" % (zeit))
sleep(5)
display = {"Temperatur:\n":temp, "Luftfeuchte:\n":hum, "Druck:\n":pres}
for measurement in sorted(display, reverse=True):
lcd.clear()
lcd.message(measurement)
toMove = lcd_columns - len(display[measurement])
lcd.set_cursor(toMove, 1)
lcd.message(display[measurement])
sleep(5)
# aktuelle Wertezeile in Textdatei Messwerte.txt auf USB schreiben
with open ('/media/usb/Messwerte.txt', 'w') as fileObject:
fileObject.write("%s %s \n Temperatur: %s \n Luftfeuchte: %s\n Luftdruck: %s \n" % (datum, zeit, temp, hum, pres))
# fortlaufend Messwerte in Textdatei Messwerte-all.txt auf USB schreiben
with open ('/media/usb/Messwerte-all.txt', 'a') as fileObject:
fileObject.write("%s %s %s %s %s \n" % (datum, zeit, temp, hum, pres))
# bottle webserver stuff
@bottle.route('/')
def Messwerte():
tpl = """\
<html>
<head>
<title>Messwerte-Ausgabe</title>
<meta http-equiv="refresh" content="{{ refresh }}">
</head>
<body>
<p><h1>Messwerte:</h1></p>
<table border="0">
<tr> <td>Temperatur:</td> <td>{{ temp }}</td> </tr>
<tr> <td>Luftfeuchte:</td> <td>{{ hum }}</td> </tr>
<tr> <td>Druck:</td> <td>{{ pres }}</td> </tr>
</table>
</body>
</html>
"""
try:
temp, hum, pres = read_sensors()
except Exception as error:
return bottle.template("Error while reading sensors: {{err}}", err=error)
else:
refresh = bottle.request.query.refresh or "3"
return bottle.template(tpl, temp=temp, hum=hum, pres=pres, refresh=refresh)
@bottle.route("/top")
def top_redirect():
bottle.redirect("/top/")
@bottle.route("/top/")
def top():
tpl = """\
<html>
<head>
<meta http-equiv="refresh" content="{{ refresh }}">
</head>
<body>
<pre>
{{ data }}
</pre>
</body>
</html>
"""
refresh = bottle.request.query.refresh or "3"
top = check_output(["top", "-cbn", "1"])
return bottle.template(tpl, data=top, refresh=refresh)
@bottle.route("/w")
def w_redirect():
bottle.redirect("/w/")
@bottle.route("/w/")
def w():
bottle.response.content_type = "text/plain"
return check_output(["w"])
# Main process
if __name__ == '__main__':
#mp.log_to_stderr(logging.DEBUG)
# Start Child Process
websrv = mp.Process(target=bottle.run, kwargs=dict(host=http_host, port=http_port))
websrv.daemon = True
websrv.start()
try:
lcd_display()
except KeyboardInterrupt:
exit()
#EOF