from PyQt5.QtWidgets import QApplication
from ui.mainwindow_0_0_7 import MainWindow
from PyQt5.QtCore import *
import time
import datetime
import serial
serialport = "/dev/ttyACM0"
class SerialCom:
def __init__(self, serialport):
self.sc = serial.Serial(serialport,9600)
time.sleep(3)
def data_read(self):
rc_data = self.sc.readline()
return rc_data
def start_com(self):
self.sc.write(b'd')
self.sc.write(b'0')
self.sc.write(b'6')
self.sc.write(b'6')
def close(self):
self.sc.close()
def refresh_time():
#時刻更新
now = datetime.datetime.now()
ct = "現在時刻:" + now.strftime('%Y/%m/%d %H:%M:%S')
ui.labeltime.setText(ct)
#LED制御
n = now.time()
LED_TH = 999
#時刻制御1
cb_t1 = ui.checkBoxLightTime1.checkState()
t = ui.timeEditLedStart1.time()
LedStartTime1 = datetime.time(t.hour(), t.minute(), 0)
t = ui.timeEditLedEnd1.time()
LedEndTime1 = datetime.time(t.hour(), t.minute(), 0)
#時刻制御2
cb_t2 = ui.checkBoxLightTime2.checkState()
t = ui.timeEditLedStart2.time()
LedStartTime2 = datetime.time(t.hour(), t.minute(), 0)
t = ui.timeEditLedEnd2.time()
LedEndTime2 = datetime.time(t.hour(), t.minute(), 0)
if((cb_t1>0 and LedStartTime1<n<LedEndTime1) or (cb_t2>0 and LedStartTime2<n<LedEndTime2)):
ui.progressBarLight.setValue(100)
if(ui.checkBoxLightTh.checkState()):
LED_TH = ui.spinBoxLed.value()
else:
LED_TH = 999
ui.labelLight.setText(str(LED_TH))
else:
ui.progressBarLight.setValue(0)
def refresh_sensor():
myserial.start_com()
#温度更新
temp = myserial.data_read()
ui.labeltemp.setText("気温:\t" + str("{0:.2f}".format(float(temp))) + " [℃]")
#湿度更新
humid = myserial.data_read()
ui.labelhumid.setText("湿度:\t" + str("{0:.2f}".format(float(humid)))+ " [%]")
#気圧更新
press = myserial.data_read()
ui.labelpress.setText("大気圧:" + str("{0:.2f}".format(float(press))) + " [hPa]" )
#照度更新
bright = myserial.data_read()
ui.labelbright.setText("照度:\t" + str("{0:4}".format(int(bright))) + "[Lx]")
#水分量更新
water= myserial.data_read()
ui.labelwater.setText( "水分量:\t" + str("{0:4}".format(int(water))))
#水タンク満水検知
tank= myserial.data_read()
ui.labeltank.setText("タンク内水検知:" + str("{0:4}".format(int(tank))))
#LEDライトしきい値送信
light = myserial.data_read()
#ui.labelLight.setText(str("{000:}".format(int(light))))
if __name__=="__main__":
import sys
app = QApplication(sys.argv)
ui = MainWindow()
ui.showFullScreen()
myserial = SerialCom(serialport)
timer1 = QTimer()
timer1.timeout.connect(refresh_time)
timer1.start(100)
timer2 = QTimer()
timer2.timeout.connect(refresh_sensor)
timer2.start(1000)
ui.show()
sys.exit(app.exec_())
コメント