From 4878df9ca8041a8cf0d6b5d2f139b2388cbf013c Mon Sep 17 00:00:00 2001 From: Kristjan Komlosi Date: Mon, 13 Jul 2020 11:52:22 +0200 Subject: [PATCH] Shortened error messages --- Pipfile | 15 ++++ Pipfile.lock | 177 +++++++++++++++++++++++++++++++++++++++++++++ backend/app.py | 2 +- backend/sensors.py | 43 ++++++----- 4 files changed, 214 insertions(+), 23 deletions(-) create mode 100644 Pipfile create mode 100644 Pipfile.lock diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..f09e84b --- /dev/null +++ b/Pipfile @@ -0,0 +1,15 @@ +[[source]] +name = "pypi" +url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] + +[packages] +flask = "*" +pandas = "*" +smbus2 = "*" +pyserial = "*" + +[requires] +python_version = "3.7" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 0000000..9c62a67 --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,177 @@ +{ + "_meta": { + "hash": { + "sha256": "00a70f655d2d0c38499455d621e0c52e013adc7ff93d132534e49946b23f3d9b" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.7" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "click": { + "hashes": [ + "sha256:8a18b4ea89d8820c5d0c7da8a64b2c324b4dabb695804dbfea19b9be9d88c0cc", + "sha256:e345d143d80bf5ee7534056164e5e112ea5e22716bbb1ce727941f4c8b471b9a" + ], + "version": "==7.1.1" + }, + "flask": { + "hashes": [ + "sha256:13f9f196f330c7c2c5d7a5cf91af894110ca0215ac051b5844701f2bfd934d52", + "sha256:45eb5a6fd193d6cf7e0cf5d8a5b31f83d5faae0293695626f539a823e93b13f6" + ], + "index": "pypi", + "version": "==1.1.1" + }, + "itsdangerous": { + "hashes": [ + "sha256:321b033d07f2a4136d3ec762eac9f16a10ccd60f53c0c91af90217ace7ba1f19", + "sha256:b12271b2047cb23eeb98c8b5622e2e5c5e9abd9784a153e9d8ef9cb4dd09d749" + ], + "version": "==1.1.0" + }, + "jinja2": { + "hashes": [ + "sha256:93187ffbc7808079673ef52771baa950426fd664d3aad1d0fa3e95644360e250", + "sha256:b0eaf100007721b5c16c1fc1eecb87409464edc10469ddc9a22a27a99123be49" + ], + "version": "==2.11.1" + }, + "markupsafe": { + "hashes": [ + "sha256:00bc623926325b26bb9605ae9eae8a215691f33cae5df11ca5424f06f2d1f473", + "sha256:09027a7803a62ca78792ad89403b1b7a73a01c8cb65909cd876f7fcebd79b161", + "sha256:09c4b7f37d6c648cb13f9230d847adf22f8171b1ccc4d5682398e77f40309235", + "sha256:1027c282dad077d0bae18be6794e6b6b8c91d58ed8a8d89a89d59693b9131db5", + "sha256:13d3144e1e340870b25e7b10b98d779608c02016d5184cfb9927a9f10c689f42", + "sha256:24982cc2533820871eba85ba648cd53d8623687ff11cbb805be4ff7b4c971aff", + "sha256:29872e92839765e546828bb7754a68c418d927cd064fd4708fab9fe9c8bb116b", + "sha256:43a55c2930bbc139570ac2452adf3d70cdbb3cfe5912c71cdce1c2c6bbd9c5d1", + "sha256:46c99d2de99945ec5cb54f23c8cd5689f6d7177305ebff350a58ce5f8de1669e", + "sha256:500d4957e52ddc3351cabf489e79c91c17f6e0899158447047588650b5e69183", + "sha256:535f6fc4d397c1563d08b88e485c3496cf5784e927af890fb3c3aac7f933ec66", + "sha256:596510de112c685489095da617b5bcbbac7dd6384aeebeda4df6025d0256a81b", + "sha256:62fe6c95e3ec8a7fad637b7f3d372c15ec1caa01ab47926cfdf7a75b40e0eac1", + "sha256:6788b695d50a51edb699cb55e35487e430fa21f1ed838122d722e0ff0ac5ba15", + "sha256:6dd73240d2af64df90aa7c4e7481e23825ea70af4b4922f8ede5b9e35f78a3b1", + "sha256:717ba8fe3ae9cc0006d7c451f0bb265ee07739daf76355d06366154ee68d221e", + "sha256:79855e1c5b8da654cf486b830bd42c06e8780cea587384cf6545b7d9ac013a0b", + "sha256:7c1699dfe0cf8ff607dbdcc1e9b9af1755371f92a68f706051cc8c37d447c905", + "sha256:88e5fcfb52ee7b911e8bb6d6aa2fd21fbecc674eadd44118a9cc3863f938e735", + "sha256:8defac2f2ccd6805ebf65f5eeb132adcf2ab57aa11fdf4c0dd5169a004710e7d", + "sha256:98c7086708b163d425c67c7a91bad6e466bb99d797aa64f965e9d25c12111a5e", + "sha256:9add70b36c5666a2ed02b43b335fe19002ee5235efd4b8a89bfcf9005bebac0d", + "sha256:9bf40443012702a1d2070043cb6291650a0841ece432556f784f004937f0f32c", + "sha256:ade5e387d2ad0d7ebf59146cc00c8044acbd863725f887353a10df825fc8ae21", + "sha256:b00c1de48212e4cc9603895652c5c410df699856a2853135b3967591e4beebc2", + "sha256:b1282f8c00509d99fef04d8ba936b156d419be841854fe901d8ae224c59f0be5", + "sha256:b2051432115498d3562c084a49bba65d97cf251f5a331c64a12ee7e04dacc51b", + "sha256:ba59edeaa2fc6114428f1637ffff42da1e311e29382d81b339c1817d37ec93c6", + "sha256:c8716a48d94b06bb3b2524c2b77e055fb313aeb4ea620c8dd03a105574ba704f", + "sha256:cd5df75523866410809ca100dc9681e301e3c27567cf498077e8551b6d20e42f", + "sha256:cdb132fc825c38e1aeec2c8aa9338310d29d337bebbd7baa06889d09a60a1fa2", + "sha256:e249096428b3ae81b08327a63a485ad0878de3fb939049038579ac0ef61e17e7", + "sha256:e8313f01ba26fbbe36c7be1966a7b7424942f670f38e666995b88d012765b9be" + ], + "version": "==1.1.1" + }, + "numpy": { + "hashes": [ + "sha256:1786a08236f2c92ae0e70423c45e1e62788ed33028f94ca99c4df03f5be6b3c6", + "sha256:17aa7a81fe7599a10f2b7d95856dc5cf84a4eefa45bc96123cbbc3ebc568994e", + "sha256:20b26aaa5b3da029942cdcce719b363dbe58696ad182aff0e5dcb1687ec946dc", + "sha256:2d75908ab3ced4223ccba595b48e538afa5ecc37405923d1fea6906d7c3a50bc", + "sha256:39d2c685af15d3ce682c99ce5925cc66efc824652e10990d2462dfe9b8918c6a", + "sha256:56bc8ded6fcd9adea90f65377438f9fea8c05fcf7c5ba766bef258d0da1554aa", + "sha256:590355aeade1a2eaba17617c19edccb7db8d78760175256e3cf94590a1a964f3", + "sha256:70a840a26f4e61defa7bdf811d7498a284ced303dfbc35acb7be12a39b2aa121", + "sha256:77c3bfe65d8560487052ad55c6998a04b654c2fbc36d546aef2b2e511e760971", + "sha256:9537eecf179f566fd1c160a2e912ca0b8e02d773af0a7a1120ad4f7507cd0d26", + "sha256:9acdf933c1fd263c513a2df3dceecea6f3ff4419d80bf238510976bf9bcb26cd", + "sha256:ae0975f42ab1f28364dcda3dde3cf6c1ddab3e1d4b2909da0cb0191fa9ca0480", + "sha256:b3af02ecc999c8003e538e60c89a2b37646b39b688d4e44d7373e11c2debabec", + "sha256:b6ff59cee96b454516e47e7721098e6ceebef435e3e21ac2d6c3b8b02628eb77", + "sha256:b765ed3930b92812aa698a455847141869ef755a87e099fddd4ccf9d81fffb57", + "sha256:c98c5ffd7d41611407a1103ae11c8b634ad6a43606eca3e2a5a269e5d6e8eb07", + "sha256:cf7eb6b1025d3e169989416b1adcd676624c2dbed9e3bcb7137f51bfc8cc2572", + "sha256:d92350c22b150c1cae7ebb0ee8b5670cc84848f6359cf6b5d8f86617098a9b73", + "sha256:e422c3152921cece8b6a2fb6b0b4d73b6579bd20ae075e7d15143e711f3ca2ca", + "sha256:e840f552a509e3380b0f0ec977e8124d0dc34dc0e68289ca28f4d7c1d0d79474", + "sha256:f3d0a94ad151870978fb93538e95411c83899c9dc63e6fb65542f769568ecfa5" + ], + "version": "==1.18.1" + }, + "pandas": { + "hashes": [ + "sha256:23e177d43e4bf68950b0f8788b6a2fef2f478f4ec94883acb627b9264522a98a", + "sha256:2530aea4fe46e8df7829c3f05e0a0f821c893885d53cb8ac9b89cc67c143448c", + "sha256:303827f0bb40ff610fbada5b12d50014811efcc37aaf6ef03202dc3054bfdda1", + "sha256:3b019e3ea9f5d0cfee0efabae2cfd3976874e90bcc3e97b29600e5a9b345ae3d", + "sha256:3c07765308f091d81b6735d4f2242bb43c332cc3461cae60543df6b10967fe27", + "sha256:5036d4009012a44aa3e50173e482b664c1fae36decd277c49e453463798eca4e", + "sha256:6f38969e2325056f9959efbe06c27aa2e94dd35382265ad0703681d993036052", + "sha256:74a470d349d52b9d00a2ba192ae1ee22155bb0a300fd1ccb2961006c3fa98ed3", + "sha256:7d77034e402165b947f43050a8a415aa3205abfed38d127ea66e57a2b7b5a9e0", + "sha256:7f9a509f6f11fa8b9313002ebdf6f690a7aa1dd91efd95d90185371a0d68220e", + "sha256:942b5d04762feb0e55b2ad97ce2b254a0ffdd344b56493b04a627266e24f2d82", + "sha256:a9fbe41663416bb70ed05f4e16c5f377519c0dc292ba9aa45f5356e37df03a38", + "sha256:d10e83866b48c0cdb83281f786564e2a2b51a7ae7b8a950c3442ad3c9e36b48c", + "sha256:e2140e1bbf9c46db9936ee70f4be6584d15ff8dc3dfff1da022d71227d53bad3" + ], + "index": "pypi", + "version": "==1.0.1" + }, + "pyserial": { + "hashes": [ + "sha256:6e2d401fdee0eab996cf734e67773a0143b932772ca8b42451440cfed942c627", + "sha256:e0770fadba80c31013896c7e6ef703f72e7834965954a78e71a3049488d4d7d8" + ], + "index": "pypi", + "version": "==3.4" + }, + "python-dateutil": { + "hashes": [ + "sha256:73ebfe9dbf22e832286dafa60473e4cd239f8592f699aa5adaf10050e6e1823c", + "sha256:75bb3f31ea686f1197762692a9ee6a7550b59fc6ca3a1f4b5d7e32fb98e2da2a" + ], + "version": "==2.8.1" + }, + "pytz": { + "hashes": [ + "sha256:1c557d7d0e871de1f5ccd5833f60fb2550652da6be2693c1e02300743d21500d", + "sha256:b02c06db6cf09c12dd25137e563b31700d3b80fcc4ad23abb7a315f2789819be" + ], + "version": "==2019.3" + }, + "six": { + "hashes": [ + "sha256:236bdbdce46e6e6a3d61a337c0f8b763ca1e8717c03b369e87a7ec7ce1319c0a", + "sha256:8f3cd2e254d8f793e7f3d6d9df77b92252b52637291d0f0da013c76ea2724b6c" + ], + "version": "==1.14.0" + }, + "smbus2": { + "hashes": [ + "sha256:210e66eebe4d0b1fe836b3ec2751841942e1c4918c0b429b20a0e20a222228b4" + ], + "index": "pypi", + "version": "==0.3.0" + }, + "werkzeug": { + "hashes": [ + "sha256:169ba8a33788476292d04186ab33b01d6add475033dfc07215e6d219cc077096", + "sha256:6dc65cf9091cf750012f56f2cad759fa9e879f511b5ff8685e456b4e3bf90d16" + ], + "version": "==1.0.0" + } + }, + "develop": {} +} diff --git a/backend/app.py b/backend/app.py index a1ec7c0..81ec272 100644 --- a/backend/app.py +++ b/backend/app.py @@ -8,7 +8,7 @@ app = Flask(__name__) @app.route('/data') def sendData(): '''Responder function for /data route''' - s = sensors.Spectrometer(path='/dev/serial0', baudrate=115200, tout=1) + s = sensors.Spectrometer(path='/dev/serial0') u = sensors.UVSensor() l = sensors.LxMeter() response = jsonify([s.getData(), l.getData(), u.getABI()]) diff --git a/backend/sensors.py b/backend/sensors.py index 439e895..ef9161d 100644 --- a/backend/sensors.py +++ b/backend/sensors.py @@ -6,8 +6,10 @@ 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''' @@ -23,8 +25,7 @@ class Spectrometer: if rstring == 'ERROR': raise Exception # sensor is in error state except: - raise Exception( - 'An exception ocurred when performing spectrometer handshake') + raise Exception('No AT command response') def setParameters(self, parameters): '''applies the parameters like LED light and gain to the spectrometer''' @@ -53,8 +54,7 @@ class Spectrometer: self.serialObject.write('ATLED3={}\n'.format(led).encode()) self.serialObject.readline() except: - raise Exception( - 'An exception occured during spectrometer initialization') + raise Exception('Error setting spectrometer parameters') def getData(self): '''Returns spectral data in a pandas DataFrame.''' @@ -62,8 +62,7 @@ class Spectrometer: self.serialObject.write(b'ATCDATA\n') rawresp = self.serialObject.readline().decode() except: - raise Exception( - 'An exception occurred when polling for spectrometer data') + raise Exception('Error polling spectrometer data') else: responseorder = list('RSTUVWGHIJKLABCDEF') realorder = list('ABCDEFGHRISJTUVWKL') @@ -78,14 +77,14 @@ class Spectrometer: try: self.serialObject = ser.Serial(path, baudrate, timeout=tout) except: - raise Exception( - 'An exception occured when opening the serial port at {}'.format(path)) + 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: @@ -93,14 +92,14 @@ class LxMeter: self.bus = smbus2.SMBus(busNumber) except: raise Exception( - 'An exception occured opening the SMBus {}'.format(self.bus)) + '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('An exception occured when enabling lux meter') + raise Exception('Error enabling lux meter') def setGain(self, gain): '''Set the sensor gain. Either 1 or 16.''' @@ -109,15 +108,13 @@ class LxMeter: temp = self.bus.read_byte_data(self.addr, 0xa1) self.bus.write_byte_data(self.addr, 0xa1, 0xef & temp) except: - raise Exception( - 'An exception occured when setting lux meter gain') + 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( - 'An exception occured when setting lux meter gain') + raise Exception('Error setting lux meter gain') else: raise Exception('Invalid gain') @@ -176,6 +173,7 @@ class LxMeter: class UVSensor: '''Class representing VEML6075 UVA/B meter''' + def __init__(self, bus=1, addr=0x10): self.addr = addr try: @@ -189,13 +187,14 @@ class UVSensor: 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 + 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( - 'An exception occured when initalizing the UV Sensor') + 'Error initalizing the UV Sensor') def getABI(self): '''Calculates the UVA and UVB irradiances, @@ -208,14 +207,14 @@ class UVSensor: c1 = self.bus.read_word_data(self.addr, 0x0a) c2 = self.bus.read_word_data(self.addr, 0x0b) except: - raise Exception('An exception occured when fetching raw UV data') + 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 + i = (a + b) / 2 # calculate UV index return [a, b, i] def on(self): @@ -226,7 +225,7 @@ class UVSensor: self.bus.write_byte_data(self.addr, 0x00, 0x10) except: raise Exception( - 'An exception occured when turning the UV sensor on') + 'Error turning the UV sensor on') def off(self): '''Shuts the UV sensor down.''' @@ -236,4 +235,4 @@ class UVSensor: self.bus.write_byte_data(self.addr, 0x00, 0x11) except: raise Exception( - 'An exception occured when shutting the UV sensor down') + 'Error shutting the UV sensor down')