moved templates
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
# app.py - main backend program
|
||||
'''Main TeraHz backend program'''
|
||||
# All code in this file is licensed under the ISC license, provided in LICENSE.txt
|
||||
from flask import Flask, jsonify
|
||||
import sensors
|
||||
|
||||
app = Flask(__name__)
|
||||
@app.route('/data')
|
||||
def sendData():
|
||||
'''Responder function for /data route'''
|
||||
s = sensors.Spectrometer(path='/dev/serial0')
|
||||
u = sensors.UVSensor()
|
||||
l = sensors.LxMeter()
|
||||
response = jsonify([s.getData(), l.getData(), u.getABI()])
|
||||
response.headers.add('Access-Control-Allow-Origin', '*')
|
||||
return response
|
||||
|
||||
@app.route('/')
|
||||
def renderTable()
|
||||
return render_template('index.html')
|
||||
@@ -0,0 +1,44 @@
|
||||
// All code in this file is licensed under the ISC license, provided in LICENSE.txt
|
||||
$('#update').click(updateData);
|
||||
// jQuery event binder
|
||||
|
||||
function updateData () {
|
||||
const url = 'http://' + window.location.hostname + ':5000/data';
|
||||
$.ajax({ // spawn an AJAX request
|
||||
url: url,
|
||||
success: function (data, status) {
|
||||
console.log(data);
|
||||
graphSpectralData(data[0], 0);
|
||||
fillTableData(data);
|
||||
},
|
||||
timeout: 2500 // this should be a pretty sane timeout
|
||||
});
|
||||
}
|
||||
|
||||
function graphSpectralData (obj, dom) {
|
||||
// graph spectral data in obj into dom
|
||||
var graphPoints = [];
|
||||
var graphXTicks = [];
|
||||
|
||||
const specter = 'ABCDEFGHRISJTUVWKL';
|
||||
for (var i = 0; i < specter.length; i++) {
|
||||
graphPoints.push([i, obj[specter[i]]]);
|
||||
graphXTicks.push([i, specter[i]]);
|
||||
}
|
||||
const options = {
|
||||
grid: { color: 'white' },
|
||||
xaxis: { ticks: graphXTicks }
|
||||
};
|
||||
$.plot('#graph', [graphPoints], options);
|
||||
// flot expects an array of arrays (lines) of 2-element arrays (points)
|
||||
}
|
||||
|
||||
function fillTableData (obj) {
|
||||
// fill the obj data into HTML tables
|
||||
Object.keys(obj[0])
|
||||
.forEach((element) => { $('#' + element).text(obj[0][element]); });
|
||||
$('#lx').text(obj[1]);
|
||||
$('#uva').text(obj[2][0]);
|
||||
$('#uvb').text(obj[2][1]);
|
||||
$('#uvi').text(obj[2][2]);
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="utf8">
|
||||
<link rel="stylesheet" href="lib/bootstrap.min.css">
|
||||
<link rel="stylesheet" href="stylesheet.css">
|
||||
<title>TeraHz</title>
|
||||
<script src="lib/bootstrap.bundle.min.js"></script>
|
||||
<script src="lib/jquery-3.4.1.min.js"></script>
|
||||
<script src="lib/flot/jquery.flot.js"></script>
|
||||
<script src="frontend.js"></script>
|
||||
<script src="lib/flot/jquery.canvaswrapper.js"></script>
|
||||
<script src="lib/flot/jquery.colorhelpers.js"></script>
|
||||
<script src="lib/flot/jquery.flot.js"></script>
|
||||
<script src="lib/flot/jquery.flot.saturated.js"></script>
|
||||
<script src="lib/flot/jquery.flot.browser.js"></script>
|
||||
<script src="lib/flot/jquery.flot.drawSeries.js"></script>
|
||||
<script src="lib/flot/jquery.flot.uiConstants.js"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="container text-center">
|
||||
<h1><img src="lib/logo-sq.png" height="64px">TeraHz</h1>
|
||||
</div>
|
||||
|
||||
<div class="container">
|
||||
<button id="update" onclick="updateData()" class="btn btn-primary">Get data</button>
|
||||
<button id="download" class="btn btn-primary">Download</button>
|
||||
<button id="debug" class="btn btn-danger">DEBUG</button>
|
||||
</div>
|
||||
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="col-sm">
|
||||
<h4>Spectrogram</h4>
|
||||
<div id="graph" style="height:480px;width:720px"></div>
|
||||
</div>
|
||||
|
||||
<div class="col-sm">
|
||||
<h4>Visible+IR spectrum</h4>
|
||||
<table class="table table-dark table-sm" id="specter">
|
||||
<thead class="thead-dark">
|
||||
<tr>
|
||||
<th>Band</th>
|
||||
<th>Wavelength [nm]</th>
|
||||
<th>Irradiance [μW/cm²]</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tr>
|
||||
<td>A</td>
|
||||
<td>410 nm</td>
|
||||
<td id="A">---</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>B</td>
|
||||
<td>435 nm</td>
|
||||
<td id="B">---</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>C</td>
|
||||
<td>460 nm</td>
|
||||
<td id="C">---</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>D</td>
|
||||
<td>485 nm</td>
|
||||
<td id="D">---</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>E</td>
|
||||
<td>510 nm</td>
|
||||
<td id="E">---</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>F</td>
|
||||
<td>535 nm</td>
|
||||
<td id="F">---</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>G</td>
|
||||
<td>560 nm</td>
|
||||
<td id="G">---</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>H</td>
|
||||
<td>585 nm</td>
|
||||
<td id="H">---</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>R</td>
|
||||
<td>610 nm</td>
|
||||
<td id="R">---</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>I</td>
|
||||
<td>645 nm</td>
|
||||
<td id="I">---</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>S</td>
|
||||
<td>680 nm</td>
|
||||
<td id="S">---</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>J</td>
|
||||
<td>705 nm</td>
|
||||
<td id="J">---</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>T</td>
|
||||
<td>730 nm</td>
|
||||
<td id="T">---</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>U</td>
|
||||
<td>760 nm</td>
|
||||
<td id="U">---</td>
|
||||
</tr>
|
||||
<tr class="table-secondary">
|
||||
<td>V</td>
|
||||
<td>810 nm</td>
|
||||
<td id="V">---</td>
|
||||
</tr>
|
||||
<tr class="table-secondary">
|
||||
<td>W</td>
|
||||
<td>860 nm</td>
|
||||
<td id="W">---</td>
|
||||
</tr>
|
||||
<tr class="table-secondary">
|
||||
<td>K</td>
|
||||
<td>900 nm</td>
|
||||
<td id="K">---</td>
|
||||
</tr>
|
||||
<tr class="table-secondary">
|
||||
<td>L</td>
|
||||
<td>940 nm</td>
|
||||
<td id="L">---</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div class="col-sm">
|
||||
<h4>UV+Illuminance</h4>
|
||||
<table class="table-dark table table-sm" id="luxuv">
|
||||
<thead class="thead-dark">
|
||||
<tr>
|
||||
<th>Parameter</th>
|
||||
<th>Value</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tr>
|
||||
<td>Illuminance [lx]</td>
|
||||
<td id="lx">---</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>UVA irradiance [μW/cm²]</td>
|
||||
<td id="uva">---</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>UVB irradiance [μW/cm²]</td>
|
||||
<td id="uvb">---</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>UVA/UVB average [μW/cm²]</td>
|
||||
<td id="uvi">---</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,69 @@
|
||||
body {
|
||||
background: rgb(2,0,36);
|
||||
background: linear-gradient(138deg, rgba(2,0,36,1) 0%, rgba(5,5,209,1) 51%, rgba(0,212,255,1) 100%);
|
||||
color: white;
|
||||
}
|
||||
#A {
|
||||
background-color: #7e00db;
|
||||
color: white;
|
||||
}
|
||||
|
||||
#B {
|
||||
background-color: #2300ff;
|
||||
color: white;
|
||||
}
|
||||
|
||||
#C {
|
||||
background-color: #007bff;
|
||||
color: white;
|
||||
}
|
||||
|
||||
#D {
|
||||
background-color: #00eaff;
|
||||
}
|
||||
|
||||
#E {
|
||||
background-color: #00ff00;
|
||||
}
|
||||
|
||||
#F {
|
||||
background-color: #70ff00;
|
||||
}
|
||||
|
||||
#G {
|
||||
background-color: #c3ff00;
|
||||
}
|
||||
|
||||
#H {
|
||||
background-color: #ffef00;
|
||||
}
|
||||
|
||||
#R {
|
||||
background-color: #ff9b00;
|
||||
}
|
||||
|
||||
#I, #S {
|
||||
background-color: #ff0000;
|
||||
}
|
||||
|
||||
#J {
|
||||
background-color: #f60000;
|
||||
}
|
||||
|
||||
#T {
|
||||
background-color: #c80000;
|
||||
color: white;
|
||||
}
|
||||
|
||||
#U {
|
||||
background-color: #8d0000;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.table-secondary {
|
||||
color: black;
|
||||
}
|
||||
|
||||
.flot-tick-label {
|
||||
fill: white;
|
||||
}
|
||||
@@ -0,0 +1,237 @@
|
||||
# terahz.py - a module for interfacing to the sensors
|
||||
'''Module for interfacing with TeraHz sensors'''
|
||||
# Copyright 2019, 2020 Kristjan Komloši
|
||||
# All code in this file is licensed under the ISC license, provided in LICENSE.txt
|
||||
import serial as ser
|
||||
import pandas as pd
|
||||
import smbus2
|
||||
|
||||
class Spectrometer:
|
||||
'''Class representing the AS7265X specrometer'''
|
||||
|
||||
def initializeSensor(self):
|
||||
'''confirm the sensor is responding and proceed\
|
||||
with spectrometer initialization'''
|
||||
try:
|
||||
rstring = 'undefined' # just need it set to a value
|
||||
self.setParameters({'gain': 0})
|
||||
self.serialObject.write(b'AT\n')
|
||||
rstring = self.serialObject.readline().decode()
|
||||
if rstring == 'undefined':
|
||||
raise Exception # sensor didn't respond
|
||||
if rstring == 'OK':
|
||||
pass # handshake passed
|
||||
if rstring == 'ERROR':
|
||||
raise Exception # sensor is in error state
|
||||
except:
|
||||
raise Exception('No AT command response')
|
||||
|
||||
def setParameters(self, parameters):
|
||||
'''applies the parameters like LED light and gain to the spectrometer'''
|
||||
try:
|
||||
if 'it_time' in parameters:
|
||||
it_time = int(parameters['it_time'])
|
||||
if it_time <= 0:
|
||||
it_time = 1
|
||||
self.serialObject.write(
|
||||
'ATINTTIME={}\n'.format(str(it_time)).encode())
|
||||
self.serialObject.readline()
|
||||
|
||||
if 'gain' in parameters:
|
||||
gain = int(parameters['gain'])
|
||||
if gain < 0 or gain > 3:
|
||||
gain = 1
|
||||
self.serialObject.write('ATGAIN={}\n'.format(gain).encode())
|
||||
self.serialObject.readline()
|
||||
|
||||
if 'led' in parameters:
|
||||
led = bool(parameters['led'])
|
||||
if led:
|
||||
led = 1
|
||||
else:
|
||||
led = 0
|
||||
self.serialObject.write('ATLED3={}\n'.format(led).encode())
|
||||
self.serialObject.readline()
|
||||
except:
|
||||
raise Exception('Error setting spectrometer parameters')
|
||||
|
||||
def getData(self):
|
||||
'''Returns spectral data in a pandas DataFrame.'''
|
||||
try:
|
||||
self.serialObject.write(b'ATCDATA\n')
|
||||
rawresp = self.serialObject.readline().decode()
|
||||
except:
|
||||
raise Exception('Error polling spectrometer data')
|
||||
else:
|
||||
responseorder = list('RSTUVWGHIJKLABCDEF')
|
||||
realorder = list('ABCDEFGHRISJTUVWKL')
|
||||
response = pd.Series(
|
||||
[float(i) / 35.0 for i in rawresp[:-3].split(',')], index=responseorder)
|
||||
return pd.DataFrame(response, index=realorder, columns=['uW/cm^2']).to_dict()['uW/cm^2']
|
||||
|
||||
def __init__(self, path='/dev/ttyUSB0', baudrate=115200, tout=1):
|
||||
self.path = path
|
||||
self.baudrate = baudrate
|
||||
self.timeout = 1
|
||||
try:
|
||||
self.serialObject = ser.Serial(path, baudrate, timeout=tout)
|
||||
except:
|
||||
raise Exception('Error opening serial port: {}'.format(path))
|
||||
else:
|
||||
self.initializeSensor()
|
||||
|
||||
|
||||
class LxMeter:
|
||||
'''Class representing the APDS-9301 digital photometer.'''
|
||||
|
||||
def __init__(self, busNumber=1, addr=0x39):
|
||||
self.addr = addr
|
||||
try:
|
||||
# initialize the SMBus interface
|
||||
self.bus = smbus2.SMBus(busNumber)
|
||||
except:
|
||||
raise Exception(
|
||||
'Error opening SMBus {}'.format(self.bus))
|
||||
|
||||
try:
|
||||
self.bus.write_byte_data(
|
||||
self.addr, 0xa0, 0x03) # enable the sensor
|
||||
self.setGain(16)
|
||||
except:
|
||||
raise Exception('Error enabling lux meter')
|
||||
|
||||
def setGain(self, gain):
|
||||
'''Set the sensor gain. Either 1 or 16.'''
|
||||
if gain == 1:
|
||||
try:
|
||||
temp = self.bus.read_byte_data(self.addr, 0xa1)
|
||||
self.bus.write_byte_data(self.addr, 0xa1, 0xef & temp)
|
||||
except:
|
||||
raise Exception('Error setting lux meter gain')
|
||||
if gain == 16:
|
||||
try:
|
||||
temp = self.bus.read_byte_data(self.addr, 0xa1)
|
||||
self.bus.write_byte_data(self.addr, 0xa1, 0x10 | temp)
|
||||
except:
|
||||
raise Exception('Error setting lux meter gain')
|
||||
else:
|
||||
raise Exception('Invalid gain')
|
||||
|
||||
def getGain(self):
|
||||
'''Get the gain from the sensor.'''
|
||||
try:
|
||||
if self.bus.read_byte_data(self.addr, 0xa1) & 0x10 == 0x10:
|
||||
return 16
|
||||
if self.bus.read_byte_data(self.addr, 0xa1) & 0x10 == 0x00:
|
||||
return 1
|
||||
raise Exception('An error occured when getting lux meter gain')
|
||||
# Under normal conditions, this raise is unreachable.
|
||||
except:
|
||||
raise Exception('An error occured when getting lux meter gain')
|
||||
|
||||
def setIntTime(self, time):
|
||||
'''Set the lux sensor integration time. 0 to including 2'''
|
||||
if time < 0 or time > 2:
|
||||
raise Exception('Invalid integration time')
|
||||
try:
|
||||
temp = self.bus.read_byte_data(self.addr, 0xa1)
|
||||
self.bus.write_byte_data(self.addr, 0xa1, (temp & 0xfc) | time)
|
||||
except:
|
||||
raise Exception(
|
||||
'An exception occured setting lux integration time')
|
||||
|
||||
def getIntTime(self):
|
||||
'''Get the lux sensor integration time.'''
|
||||
try:
|
||||
return self.bus.read_byte_data(self.addr, 0xa1) & 0x03
|
||||
except:
|
||||
raise Exception(
|
||||
'An exception occured getting lux integration time')
|
||||
|
||||
def getData(self):
|
||||
'''return the calculated lux value'''
|
||||
try:
|
||||
chA = self.bus.read_word_data(self.addr, 0xac)
|
||||
chB = self.bus.read_word_data(self.addr, 0xae)
|
||||
except:
|
||||
raise Exception('An exception occured fetching lux channels')
|
||||
|
||||
# Refer to APDS-9301 datasheet!
|
||||
if chB / chA <= 0.5 and chB / chA > 0:
|
||||
lux = 0.0304 * chA - 0.062 * chA * (chB / chA)**1.4
|
||||
elif chB / chA <= 0.61 and chB / chA > 0.5:
|
||||
lux = 0.0224 * chA - 0.031 * chB
|
||||
elif chB / chA <= 0.8 and chB / chA > 0.61:
|
||||
lux = 0.0128 * chA - 0.0153 * chB
|
||||
elif chB / chA <= 1.3 and chB / chA > 0.8:
|
||||
lux = 0.00146 * chA - 0.00112 * chB
|
||||
else:
|
||||
lux = 0
|
||||
return lux
|
||||
|
||||
|
||||
class UVSensor:
|
||||
'''Class representing VEML6075 UVA/B meter'''
|
||||
|
||||
def __init__(self, bus=1, addr=0x10):
|
||||
self.addr = addr
|
||||
try:
|
||||
self.bus = smbus2.SMBus(bus)
|
||||
except:
|
||||
raise Exception(
|
||||
'An exception occured opening SMBus {}'.format(bus))
|
||||
|
||||
try:
|
||||
# enable the sensor and set the integration time
|
||||
self.bus.write_byte_data(self.addr, 0x00, 0b00010000)
|
||||
|
||||
# trigger a measurement to prevent bus errors at first read
|
||||
self.bus.write_byte_data(self.addr, 0x00, 0b00010010) # UV_AF=1
|
||||
self.bus.write_byte_data(self.addr, 0x00, 0b00010110) # UV_TRIG=1
|
||||
self.bus.write_byte_data(
|
||||
self.addr, 0x00, 0b00010000) # normal mode
|
||||
|
||||
except:
|
||||
raise Exception(
|
||||
'Error initalizing the UV Sensor')
|
||||
|
||||
def getABI(self):
|
||||
'''Calculates the UVA and UVB irradiances,
|
||||
along with UV index. Returns [a,b,i]'''
|
||||
|
||||
try:
|
||||
# read the raw UVA, UVB and compensation values from the sensor
|
||||
aRaw = self.bus.read_word_data(self.addr, 0x07)
|
||||
bRaw = self.bus.read_word_data(self.addr, 0x09)
|
||||
c1 = self.bus.read_word_data(self.addr, 0x0a)
|
||||
c2 = self.bus.read_word_data(self.addr, 0x0b)
|
||||
except:
|
||||
raise Exception('Error fetching raw UV data')
|
||||
# Refer to Vishay app note 84339 and Sparkfun VEML6075 documentation.
|
||||
# The computed values may be negative if UV is vastly weaker than
|
||||
# visible and IR light. This is not a bug!
|
||||
|
||||
a = (aRaw - 2.22 * c1 - 1.33 * c2) * 1.06
|
||||
b = (bRaw - 2.95 * c1 - 1.74 * c2) * 0.48
|
||||
i = (a + b) / 2 # calculate UV index
|
||||
return [a, b, i]
|
||||
|
||||
def on(self):
|
||||
'''Turns the UV sensor on after shutdown.'''
|
||||
try:
|
||||
# write the default value for power on
|
||||
# no configurable params = no bitmask
|
||||
self.bus.write_byte_data(self.addr, 0x00, 0x10)
|
||||
except:
|
||||
raise Exception(
|
||||
'Error turning the UV sensor on')
|
||||
|
||||
def off(self):
|
||||
'''Shuts the UV sensor down.'''
|
||||
try:
|
||||
# write the default value + the shutdown bit
|
||||
# no configurable params = no bitmask
|
||||
self.bus.write_byte_data(self.addr, 0x00, 0x11)
|
||||
except:
|
||||
raise Exception(
|
||||
'Error shutting the UV sensor down')
|
||||
Reference in New Issue
Block a user