This commit is contained in:
phm3
2019-02-01 15:00:51 +00:00
commit d44c021767
8 changed files with 1508 additions and 0 deletions

36
UI/bdsensors.py Normal file
View File

@@ -0,0 +1,36 @@
#!/usr/bin/env python
import os
import glob
import time
import subprocess
# this needs to be automated later TODO
base_dir = '/sys/bus/w1/devices/'
# here is the prefix of the hardware in this case, 10
device_folder = glob.glob(base_dir + '00*')[0]
device_file = device_folder + '/w1_bus_master1'
def read_temp_raw(): # This function reads the raw sensor chip information
catdata = subprocess.Popen(
['cat', device_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = catdata.communicate()
out_decode = out.decode('utf-8')
lines = out_decode.split('\n')
return lines
def read_temp(): # This function takes the the raw sensor chip information
lines = read_temp_raw() # strips it down and takes only the temperature out
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
# Calculates temperature in celcius and stores it in variable
temp_c = float(temp_string) / 1000.0
# Calculates temperature in fahreint and stores it in variable
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c # here it returns only the temperature in celcius

142
UI/bmp180.py Normal file
View File

@@ -0,0 +1,142 @@
#!/usr/bin/python
# --------------------------------------
# ___ ___ _ ____
# / _ \/ _ \(_) __/__ __ __
# / , _/ ___/ /\ \/ _ \/ // /
# /_/|_/_/ /_/___/ .__/\_, /
# /_/ /___/
#
# bmp180.py
# Read data from a digital pressure sensor.
#
# Author : Matt Hawkins
# Date : 17/02/2017
#
# http://www.raspberrypi-spy.co.uk/
#
# --------------------------------------
import smbus
import time
from ctypes import c_short
DEVICE = 0x77 # Default device I2C address
# bus = smbus.SMBus(0) # Rev 1 Pi uses 0
bus = smbus.SMBus(1) # Rev 2 Pi uses 1
def convertToString(data):
# Simple function to convert binary data into
# a string
return str((data[1] + (256 * data[0])) / 1.2)
def getShort(data, index):
# return two bytes from data as a signed 16-bit value
return c_short((data[index] << 8) + data[index + 1]).value
def getUshort(data, index):
# return two bytes from data as an unsigned 16-bit value
return (data[index] << 8) + data[index + 1]
def readBmp180Id(addr=DEVICE):
# Chip ID Register Address
REG_ID = 0xD0
(chip_id, chip_version) = bus.read_i2c_block_data(addr, REG_ID, 2)
return (chip_id, chip_version)
def readBmp180(addr=DEVICE):
# Register Addresses
REG_CALIB = 0xAA
REG_MEAS = 0xF4
REG_MSB = 0xF6
REG_LSB = 0xF7
# Control Register Address
CRV_TEMP = 0x2E
CRV_PRES = 0x34
# Oversample setting
OVERSAMPLE = 3 # 0 - 3
# Read calibration data
# Read calibration data from EEPROM
cal = bus.read_i2c_block_data(addr, REG_CALIB, 22)
# Convert byte data to word values
AC1 = getShort(cal, 0)
AC2 = getShort(cal, 2)
AC3 = getShort(cal, 4)
AC4 = getUshort(cal, 6)
AC5 = getUshort(cal, 8)
AC6 = getUshort(cal, 10)
B1 = getShort(cal, 12)
B2 = getShort(cal, 14)
MB = getShort(cal, 16)
MC = getShort(cal, 18)
MD = getShort(cal, 20)
# Read temperature
bus.write_byte_data(addr, REG_MEAS, CRV_TEMP)
time.sleep(0.005)
(msb, lsb) = bus.read_i2c_block_data(addr, REG_MSB, 2)
UT = (msb << 8) + lsb
# Read pressure
bus.write_byte_data(addr, REG_MEAS, CRV_PRES + (OVERSAMPLE << 6))
time.sleep(0.04)
(msb, lsb, xsb) = bus.read_i2c_block_data(addr, REG_MSB, 3)
UP = ((msb << 16) + (lsb << 8) + xsb) >> (8 - OVERSAMPLE)
# Refine temperature
X1 = ((UT - AC6) * AC5) >> 15
X2 = (MC << 11) / (X1 + MD)
B5 = X1 + X2
temperature = int(B5 + 8) >> 4
# Refine pressure
B6 = B5 - 4000
B62 = int(B6 * B6) >> 12
X1 = (B2 * B62) >> 11
X2 = int(AC2 * B6) >> 11
X3 = X1 + X2
B3 = (((AC1 * 4 + X3) << OVERSAMPLE) + 2) >> 2
X1 = int(AC3 * B6) >> 13
X2 = (B1 * B62) >> 16
X3 = ((X1 + X2) + 2) >> 2
B4 = (AC4 * (X3 + 32768)) >> 15
B7 = (UP - B3) * (50000 >> OVERSAMPLE)
P = (B7 * 2) / B4
X1 = (int(P) >> 8) * (int(P) >> 8)
X1 = (X1 * 3038) >> 16
X2 = int(-7357 * P) >> 16
pressure = int(P + ((X1 + X2 + 3791) >> 4))
return (temperature/10.0, pressure/100.0)
def main():
#(chip_id, chip_version) = readBmp180Id()
#print("Chip ID : {0}".format(chip_id))
#print("Version : {0}".format(chip_version))
# print
(temperature, pressure) = readBmp180()
#print("Temperature : {0} C".format(temperature))
#print("Pressure : {0} mbar".format(pressure))
return {"temperature": temperature, "pressure": pressure}
def readings():
(temperature, pressure) = readBmp180()
return pressure
# if __name__ == "__main__":
# main()

BIN
UI/justgage-1.2.2.zip Normal file

Binary file not shown.

18
UI/main.py Normal file
View File

@@ -0,0 +1,18 @@
#!/usr/bin/python
import bdsensors
import bmp180
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def main():
templateData = {'temperature': bdsensors.read_temp(),
'pressure': bmp180.readings()}
return render_template('main.html', **templateData)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80, debug=True)

3
UI/requirements.txt Normal file
View File

@@ -0,0 +1,3 @@
apt-get install python-pip
flask
sudo apt install python-smbus

1220
UI/static/justgage.js Normal file

File diff suppressed because it is too large Load Diff

12
UI/static/raphael-2.1.4.min.js vendored Normal file

File diff suppressed because one or more lines are too long

77
UI/templates/main.html Normal file
View File

@@ -0,0 +1,77 @@
<!doctype html>
<html>
<head>
<title>Dynamic Resize</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style>
body {
text-align: center;
padding: 0px;
margin: 0px;
}
.clear:before,
.clear:after {
content: "";
display: table;
}
.clear:after {
clear: both;
}
.clear {
*zoom: 1;
}
.wrapper {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: 30px;
border: 1px solid #cccccc;
}
.gauge {
display: block;
float: left;
}
#g1 {
width: 50%;
}
#g2 {
width: 50%;
}
</style>
</head>
<body>
<div class="wrapper clear">
<div id="g1" class="gauge"></div>
<div id="g2" class="gauge"></div>
</div>
<script src="{{ url_for('static', filename='raphael-2.1.4.min.js') }}"></script>
<script src="{{ url_for('static', filename='justgage.js') }}"></script>
<script>
document.addEventListener("DOMContentLoaded", function(event) {
var g1, g2, g3;
var g1 = new JustGage({
id: "g1",
value: {{temperature}},
min: 0,
max: 50,
title: "Temperature Sensor",
});
var g2 = new JustGage({
id: "g2",
value: {{pressure}},
min: 0,
max: 1000,
title: "Pressure",
});
});
</script>
</body>
</html>