This repository has been archived by the owner on May 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial_io.py
47 lines (44 loc) · 1.64 KB
/
serial_io.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import serial
import asyncio
import os
import time
import logging as log
import sys
#ser = serial.Serial('/dev/ttyACM0',115200)
#s = [0,1]
#s[0] = str(int(ser.readline()))
#print (s[0])
class SerialIO:
def __init__(self, address, baudrate, callback):
self.ser = serial.Serial(port=address, baudrate=baudrate, timeout=5)
self.address = address
self.baudrate = baudrate
self.callback = callback
self.value_reading = False
if os.path.isfile("sensor_read"):
with open("sensor_read", mode="r") as sensor_read:
read = sensor_read.read()
if read != '':
self.sensor_last_read = float(read)
else:
self.sensor_last_read = 0
sensor_read.close()
else:
self.sensor_last_read = 0
def read_value(self):
self.value_reading = True
try:
value = [0,1]
value[0] = int(self.ser.readline()) # Attempts to read the sensor
asyncio.set_event_loop(asyncio.new_event_loop())
self.callback.remote.update_soil_moisture(1, value[0])
log.info("[SENSOR] Read sensor {} with value {}".format(self.baudrate, str(value[0])))
# Update time read to now
with open("sensor_read", mode="w") as sensor_write:
sensor_write.write(str(time.time()))
sensor_write.close()
self.sensor_last_read = time.time()
except Exception as e:
log.error("[SENSOR] Sensor value not read due to exception: {}.".format(str(e)))
finally:
self.value_reading = False