Skip to content

Commit

Permalink
read data/ diff color #4 #7
Browse files Browse the repository at this point in the history
  • Loading branch information
Lohni committed Mar 18, 2022
1 parent 828e49d commit 0cefd18
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 22 deletions.
60 changes: 50 additions & 10 deletions ESP32/LedMain.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,81 @@ 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:
self.socket.listen(self.persistence)

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
18 changes: 8 additions & 10 deletions ESP32/Persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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:
Expand All @@ -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')
Expand Down
4 changes: 3 additions & 1 deletion ESP32/main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import MatrixSocket
import LedMain
import time

# load wlan config
matrixSocket = MatrixSocket.MatrixSocket()
main = LedMain.LedMain(matrixSocket)

while True:
main.run()
main.run()
time.sleep_ms(1000)
2 changes: 1 addition & 1 deletion ESP32/sandbox.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit 0cefd18

Please sign in to comment.