From 0cefd18e35e650f2cbd604039bac1d39a957d30d Mon Sep 17 00:00:00 2001 From: Lohni Date: Sat, 19 Mar 2022 00:11:37 +0100 Subject: [PATCH] read data/ diff color #4 #7 --- ESP32/LedMain.py | 60 ++++++++++++++++++++++++++++++++++++-------- ESP32/Persistence.py | 18 ++++++------- ESP32/main.py | 4 ++- ESP32/sandbox.py | 2 +- 4 files changed, 62 insertions(+), 22 deletions(-) diff --git a/ESP32/LedMain.py b/ESP32/LedMain.py index fccf4ad..efed96a 100644 --- a/ESP32/LedMain.py +++ b/ESP32/LedMain.py @@ -13,7 +13,9 @@ def __init__(self, mSocket: MatrixSocket): self.dhtSensor = dht.DHT22(machine.Pin(26)) self.ledUtil = LEDUtils(256) self.persistence = Persistence() - self.matrix_mode = '' + self.matrix_mode = 'tmp' + self.compare_interval_min = 5 + self.last_compare_value = [0, 0, 0] # [tmp, hum, min] def run(self): try: @@ -21,33 +23,71 @@ def run(self): try: self.dhtSensor.measure() + print(self.dhtSensor.temperature()) except OSError as err: print("DHT-Measure error") + local_time = time.localtime(self.persistence.getCurrentMEZ()) + tmp = str(self.dhtSensor.temperature()) + '°C' hum = str(int(self.dhtSensor.humidity())) + '%' if self.socket.req_mode == 'tmp' and self.matrix_mode != 'tmp': self.matrix_mode = 'tmp' - uasyncio.run(self.ledUtil.writeStringToMatrix(tmp)) print('Selected TMP-Mode') elif self.socket.req_mode == 'hum' and self.matrix_mode != 'hum': self.matrix_mode = 'hum' - uasyncio.run(self.ledUtil.writeStringToMatrix(hum)) print('Selected HUM-Mode') - local_time = time.localtime(self.persistence.getCurrentMEZ()) + # Set Color + if self.matrix_mode == 'tmp': + comparison_timestamp = local_time + tmp_color = (10, 5, 6) + + last_comp_val = self.last_compare_value[2] + 5 + if last_comp_val == 60: + last_comp_val = 0 + + if comparison_timestamp[4] % 5 == 0 and last_comp_val < comparison_timestamp[4] \ + or self.last_compare_value[0] == 0 or 0 == comparison_timestamp[4] and self.last_compare_value[2] == 50: + instant = time.mktime(comparison_timestamp) - (comparison_timestamp[4] % 5) * 60 + + if self.last_compare_value[0] != 0: + instant = instant - 5 * 60 + + comparison_data = self.persistence.getDataByTimestamp(time.localtime(instant)) + + if len(comparison_data) > 0: + self.last_compare_value[0] = comparison_data[0] + self.last_compare_value[1] = comparison_data[1] + self.last_compare_value[2] = time.localtime(instant)[4] + else: + self.last_compare_value[0] = self.dhtSensor.temperature() + self.last_compare_value[2] = time.localtime(instant)[4] + + print('LastCompareVal:' + str(self.last_compare_value)) + diff = self.last_compare_value[0] - self.dhtSensor.temperature() + if diff > 0: + tmp_color = (2, 2, 10) + elif diff < 0: + tmp_color = (10, 2, 2) + # Todo: calc color hues https://www.colorspire.com/rgb-color-wheel/ + + print(tmp_color) + # uasyncio.run(self.ledUtil.writeStringToMatrix(tmp)) + current_min = local_time[4] + print(str(current_min) + ' ... ' + str(self.persistence.last_persisted_minute)) if current_min % 5 == 0 and current_min != self.persistence.last_persisted_minute: - tmp = dht.temperature() - hum = dht.humidity() - self.persistence.persistValue(self.dhtSensor.temperature(), self.dhtSensor.humidity(), local_time) + tmp = self.dhtSensor.temperature() + hum = self.dhtSensor.humidity() + self.persistence.persistValue(tmp, hum, local_time) - print("Temperature: " + tmp) - print("Humidity: " + hum) + print("Temperature: " + str(tmp)) + print("Humidity: " + str(hum)) print("Persisted Data!") # Todo Timestamp? - time.sleep_ms(1000) except Exception as err: + print(err) pass # Todo: Error notification to Matrix, save error to file/ var?, error interface via http to see whats happening diff --git a/ESP32/Persistence.py b/ESP32/Persistence.py index e8d5600..4b7f5a1 100644 --- a/ESP32/Persistence.py +++ b/ESP32/Persistence.py @@ -5,7 +5,7 @@ class Persistence: def __init__(self): - self.last_persisted_minute = None + self.last_persisted_minute = -1 dataFileName = "sensorData.txt" if dataFileName not in os.listdir(): x = open(dataFileName, 'x') @@ -42,9 +42,7 @@ def persistedBytesToValues(self, persistedBytes: bytes) -> [float, int]: return [tempDec + tempComma, humidity] - async def persistValue(self, temperature: float, humidity: float, currDateMEZ: time): - print(currDateMEZ) - + def persistValue(self, temperature: float, humidity: float, currDateMEZ: time): dataFile = open('sensorData.txt', 'ab+') matchOrder = ['yy:'.encode() + int(currDateMEZ[0]).to_bytes(2, 'big'), @@ -120,13 +118,12 @@ def getDataByTimestamp(self, timestamp: []): 'hh:'.encode() + int(timestamp[3]).to_bytes(2, 'big')] dataTime = int(timestamp[4] / 5) * 5 - data = '' + data = ''.encode() currentMtcIndex = 0 - line = dataFile.read(5) - while data == '' and line[0] != ''.encode(): - if line.__contains__(matchOrder[currentMtcIndex]) and currentMtcIndex <= 3: + while data == ''.encode() and line != ''.encode(): + if currentMtcIndex <= 3 and line == (matchOrder[currentMtcIndex]): currentMtcIndex += 1 if currentMtcIndex == 4: @@ -136,10 +133,11 @@ def getDataByTimestamp(self, timestamp: []): line = dataFile.read(5) dataFile.close() - if data != '': + + if data != ''.encode(): return self.persistedBytesToValues(data.split(':'.encode(), 1)[1]) - return [0, 0] + return [] def decodeWholeFile(self): dataFile = open('sensorData.txt', 'rb') diff --git a/ESP32/main.py b/ESP32/main.py index abc1b82..df9350d 100644 --- a/ESP32/main.py +++ b/ESP32/main.py @@ -1,9 +1,11 @@ import MatrixSocket import LedMain +import time # load wlan config matrixSocket = MatrixSocket.MatrixSocket() main = LedMain.LedMain(matrixSocket) while True: - main.run() \ No newline at end of file + main.run() + time.sleep_ms(1000) \ No newline at end of file diff --git a/ESP32/sandbox.py b/ESP32/sandbox.py index 284bd03..f1332f2 100644 --- a/ESP32/sandbox.py +++ b/ESP32/sandbox.py @@ -1,5 +1,5 @@ i = [21.8, 21.6, 21.9, 21.2, 21.3] -i = 20.5 +i = float(20.5) j = 40 # Byte-Composition for Sensor-Data: