通过树莓派根据当前时间控制终端开关

树莓派python3.7   通过串口下发数据 环境PyCharm Community Edition 2019.3.3 x64  安装后pip3 install几个模块

import serial
import time
import queue
import json
import struct
from datetime import datetime

setmpctQue = queue.Queue()

command_power = {
            'switch_on': [  # 多路电源终端1-8路开
                        ('XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX'),  # 第一路开
                        ('XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX'),
                        ('XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX'),
                        ('XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX'),
                        ('XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX'),
                        ('XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX'),
                        ('XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX'),
                        ('XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX'),  # 第八路开
                        ],
            'switch_off': [  # 多路电源终端1-8路关
                        ('XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX'),  # 第一路关
                        ('XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX'),
                        ('XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX'),
                        ('XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX'),
                        ('XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX'),
                        ('XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX'),
                        ('XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX'),
                        ('XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX'),  # 第八路关
                        ],

                }

def sensorData():  # start those thread and and guard them
    creatCCD = threading.Thread(target=CCthreadT)  # 开启串口收集发送线程
    creatMPCT = threading.Thread(target=SetMPCTThread)   # 设置多路电源终端线程
    creatCCD.start()
    creatMPCT.start()

def CCthreadT():
    threadCC = threading.Thread(target=colectTred)
    threadCC.start()
    while True:
        if not threadCC.is_alive():  # 如果串口采集线程卡死 隔60s再次开启
            threadCC = threading.Thread(target=colectTred)
            threadCC.start()
        time.sleep(60)

def SetMPCTThread(): #设置多路电源终端线程
    threadPut = threading.Thread(target=setmpct)
    threadPut.start()
    while True:
        if not threadPut.is_alive():   # 如果设置多路电源终端线程卡死 隔60s再次开启
            threadPut = threading.Thread(target=setmpct)
            threadPut.start()
        time.sleep(60)

def colectTred():  # 串口收集数据 选要改动加接口 by xxxxxxx
    # s0=serial.Serial("/dev/ttyAMA0",115200)  # 树莓派端
    s0 = serial.Serial("COM11", 115200)  # PC端
    temp = 0
    while True:
       while s0.inWaiting():
            try:
                temp = s0.read()
            except Exception as e:
                pass
       while not setmpctQue.empty():  # 当收到数据时
            data = setmpctQue.get()
            # Hex_str = bytes.fromhex(data)  # 文本转换Hex
            s0.write(data)
        time.sleep(0.01)  

##################################### 配置多路电源开关 ############################
def setmpct():  # 根据时间设置多路电源终端开关状态
    print('zcollect 334 creat setmpct')
    on = '14:35:00'  # 开灯时间
    off = '14:36:00'  # 关灯时间

    on_send_num = 0
    off_send_num = 0

    while True:
       now_time = datetime.now()
       nowtime2str = now_time.strftime("%H:%M:%S")

       if nowtime2str >= on and nowtime2str <= off:  # 开灯时间
          if on_send_num < 8:
              # if on_send_num != 1 and on_send_num != 2 and on_send_num != 3:  # 不对某路进行控制
              hex = bytes.fromhex(command_power['switch_on'][on_send_num])
              setmpctQue.put(hex)  # 发送
              on_send_num += 1
              off_send_num = 0
              time.sleep(2)
       elif  off_send_num < 8:  # 关灯时间
           hex = bytes.fromhex(command_power['switch_off'][off_send_num])
           setmpctQue.put(hex)  # 发送
           off_send_num += 1
           on_send_num = 0
           time.sleep(2)


       time.sleep(2)

if __name__ == '__main__':
    sensorData()
    pass


 

你可能感兴趣的:(Python,DAY,TIME)