现在位置: 首页  >  编程技术  >  物联网
esp32+毫米波雷达+4G模块,房屋内人体检测,长时间无人活动则向服务器发送警报
0 835


from machine import Pin
import time
from machine import UART

for i in range(5):
    print('启动中...' + str((i+1)/5*100) + '%')
    time.sleep(1)


uart = UART(2, 115200)

def sendMsg():
    # 在web后台配置网络通道

    # 读取imei
    uart.write("config,get,imei\r\n")  # 读取imei
    time.sleep(0.5)
    data=uart.read()
    data=data.decode('utf-8').strip()
    datas=data.split(',')
    imei=datas[3]
    print(imei)

    # 发送imei进行数据上报
    uart.write(imei) # http://ip:port/api/alert/alert?imei={imei}
    n=0
    data=None
    global msg_has_send
    while n<30 and data==None:
        n+=1
        time.sleep(1)
        buff=uart.read() # 大量的数据好像要分批读取,待测试
        if buff==None:
            nextBuff=None
        else:
            nextBuff=''
        
        while nextBuff!=None:
            time.sleep(1)
            nextBuff=uart.read()
            if nextBuff!=None:
                print('拼接缓存')
                buff+=nextBuff

        data=buff
        
        if not data==None:
            data=data.decode('utf-8')
            print('返回数据data:',data)
            data=data.split("\r\n\r\n")
            print('data.length:',len(data))
            if len(data)<2:
                msg_has_send=0
                data=None
            else:
                msg_has_send=1
                data=data[1]
                print('返回数据:',data)
        
        print('是否发送成功:',msg_has_send)



p15=Pin(15,Pin.IN) # 毫米波雷达反馈
last_state=p15.value()

msg_has_send=0 # 是否发送信息成功
same_state_count=0 # 相同状态累计次数
diff_count=0 # 状态变化观察期内状态与之前不一样的次数
in_view_diff=0 # 状态变化观察期内,如果期内状态与之前不一样,则确定是状态发生翻转

cycle=3 # 检测周期,单位为秒
alert_time=1 # 警告时间,单位为小时,这么长时间内没有人活动则发出警告
in_view_round=3  # 状态变化期时常,单位为检测周期


while 1:
    state=p15.value()
    
    if state!=last_state:
        in_view_diff=1
        diff_count+=1
        if diff_count>=in_view_round:
            diff_count=0
            in_view_diff=0
            last_state=state
            msg_has_send=0
            same_state_count=0
    else:
        in_view_diff=0
        diff_count=0
        
    print('判定状态:',last_state,'采集状态:',state,'上次状态:',last_state,'相同状态次数:',same_state_count,'是否在观察期:',in_view_diff,'状态变化观察期次数:',diff_count,'是否发送信息成功:',msg_has_send)
    
    if state==0:
        if in_view_diff==0:
            same_state_count+=1
            if same_state_count>alert_time*60*60/cycle: # 设定的小时*60分钟*60秒/检测周期
                same_state_count=alert_time*60*60/cycle # 固定到触发值,防止溢出
                if msg_has_send==0:
                    sendMsg()
                    # print('after sendMsg, msg_has_send:',msg_has_send)

    time.sleep(cycle)


 评论
 站内搜索