def touch_check(ser, num, flag, sleep_flag): #总开关触摸传感器 if num == '01': flag = not flag if flag == True: f1 = 'OPEN' else: f1 = 'CLOSE' #更新状态 con = mc.mysqlconnect() cursor = con.cursor() cursor.execute('update sensorinfo set flag=%s where id=0', f1) con.commit() #节能开关传感器 if num == '02': sleep_flag = not sleep_flag if sleep_flag == True: f2 = 'OPEN' else: f2 = 'CLOSE' con = mc.mysqlconnect() cursor = con.cursor() cursor.execute('update sensorinfo set sleep_flag=%s where id=0', f2) con.commit() cursor.close() con.close()
def photores_store(data): ph = float(int(data[10:12], 16)) pl = float(int(data[12:14], 16)) p = ph * 256 + pl con = mc.mysqlconnect() cursor = con.cursor() cursor.execute( 'insert into photores(illumination,addtime) values(%s,%s)', (str(p), time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) con.commit() #更新状态 cursor.execute('update sensorinfo set ill=%s where id=0', str(p)) con.commit() cursor.close() con.close() #linux mysql linux_con = mc.linux_mysqlconnect() linux_cursor = linux_con.cursor() linux_cursor.execute( 'insert into photores(illumination,addtime) values(%s,%s)', (str(p), time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) linux_con.commit() linux_cursor.close() linux_con.close() print 'light: ', p, 'xl' return p
def opp_motor(ser, op, num): con = mc.mysqlconnect() cursor = con.cursor() if op == 'open': senddata = 'ccee' + num + '090b00000000000000000000ff' senddata = binascii.b2a_hex(senddata) try: ser.write(senddata) cursor.execute( 'update sensorinfo set motor%s_status=%s where id=0', (num[:-1], 'OPEN')) con.commit() print 'motor ' + num + ' status start!' except: print 'motor ' + num + ' status close!' if op == 'close': senddata = 'ccee' + num + '090b00000000000000000000ff' senddata = binascii.b2a_hex(senddata) try: ser.write(senddata) cursor.execute( 'update sensorinfo set motor%s_status=%s where id=0', (num[:-1], 'CLOSE')) con.commit() print 'motor ' + num + ' status start!' except: print 'motor ' + num + ' status close!' cursor.close() con.close()
def temp_hum_store(data): xh = float(int(data[10:12], 16)) xl = float(int(data[12:14], 16)) temp = (xh * 256 + xl) / 100 ph = float(int(data[14:16], 16)) pl = float(int(data[16:18], 16)) hum = (ph * 256 + pl) / 100 con = mc.mysqlconnect() cursor = con.cursor() cursor.execute('insert into temp_hum(temp,hum,addtime) values(%s,%s,%s)', (str(temp), str(hum), time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) con.commit() #更新状态 cursor.execute('update sensorinfo set temp=%s,hum=%s where id=0', (str(temp), str(hum))) con.commit() cursor.close() con.close() #linux mysql linux_con = mc.linux_mysqlconnect() linux_cursor = linux_con.cursor() linux_cursor.execute( 'insert into temp_hum(temp,hum,addtime) values(%s,%s,%s)', (str(temp), str(hum), time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) linux_con.commit() linux_cursor.close() linux_con.close() temp_hum = {'temp': temp, 'hum': hum} print 'temp: ', temp, ' hum: ', hum return temp_hum
def gas(data): #更新状态 con=mc.mysqlconnect() cursor=con.cursor() if data[4:6]=="01": cursor.execute("update sensorinfo set led='NO' where id=0") con.commit() print "no combustible gas" if data[4:6]=="02": cursor.execute("update sensorinfo set led='YES' where id=0") con.commit() print "have combustible gas" cursor.close() con.close()
def dealdata(): set_temp_hum={} set_temp_hum['min_temp']=input("please input min start temp:\n") set_temp_hum['min_hum']=input("please input min start hum:\n") #设置温度值更新 con=mc.mysqlconnect() cursor=con.cursor() cursor.execute('update sensorinfo set set_temp=%s,set_hum=%s where id=0',(set_temp_hum['min_temp'],set_temp_hum['min_hum'])) con.commit() cursor.close() con.close() flag=True sleep_flag=False ser=ci.CoorConnectPc() while 1 : data=ci.ReadPort(ser) if data[8:10]!='aa': #温湿度模块 if data[6:8]=='03': now_temp_hum=th.temp_hum_store(data) print 'now temp hum:',now_temp_hum['temp'],now_temp_hum['hum'] print 'set temp hum:',set_temp_hum['min_temp'],set_temp_hum['min_hum'] if flag==True: th.judge_temp_hum(ser,now_temp_hum,set_temp_hum) #光敏模块 if data[6:8]=='02': light=pr.photores_store(data) #智能开关打开且不是睡眠模式 if flag==True and sleep_flag==False: pr.dimming(ser,light) #触摸模块 #有触摸时 if flag==True and data[6:8]=='0e' and data[10:12]=='01': tc.touch(ser,data[8:10],flag,sleep_flag) if sleep_flag==True: #关闭全部灯 pwm.led_light(ser,'00') #关棚帘 mt.time_motor(ser,'03',3) #电机模块 if flag==True and data[6:8]=='09': if data[8:9]=='dd': mt.status_motor(ser,data)
def status2_motor(temp, hum): con = mc.mysqlconnect() cursor = con.cursor() if float(temp) > 30 or float(hum) > 50: cursor.execute( "update sensorinfo set motor2_status='TH OPEN' where id=0") con.commit() print 'Motor TEMP_HUM OPEN !' else: cursor.execute( "update sensorinfo set motor2_status='TH CLOSE' where id=0") con.commit() cursor.close() con.close()
def draw(root): #读取温湿度光照强度数据10条 con=mc.mysqlconnect() recent_10_temphum_data_sql='select temp,hum,addtime from temp_hum group by id desc limit 10' recent_10_temphum_data=pd.read_sql(recent_10_temphum_data_sql,con) recent_10_ill_data_sql='select illumination,addtime from photores group by id desc limit 10' recent_10_ill_data=pd.read_sql(recent_10_ill_data_sql,con) con.close() #创建画板并进行图形绘制 f = Figure(figsize=(10,4), dpi=100) temp_photo=f.add_subplot(221) hum_photo=f.add_subplot(222) ill_photo=f.add_subplot(223) th_x=recent_10_temphum_data['addtime'] temhum_x=[th_x[9],th_x[8],th_x[7],th_x[6],th_x[5],th_x[4],th_x[3],th_x[2],th_x[1],th_x[0]] ty=recent_10_temphum_data['temp'] temp_y=[ty[9],ty[8],ty[7],ty[6],ty[5],ty[4],ty[3],ty[2],ty[1],ty[0]] hy=recent_10_temphum_data['hum'] hum_y=[hy[9],hy[8],hy[7],hy[6],hy[5],hy[4],hy[3],hy[2],hy[1],hy[0]] ix=recent_10_ill_data['addtime'] i_x=[ix[9],ix[8],ix[7],ix[6],ix[5],ix[4],ix[3],ix[2],ix[1],ix[0]] iy=recent_10_ill_data['illumination'] i_y=[iy[9],iy[8],iy[7],iy[6],iy[5],iy[4],iy[3],iy[2],iy[1],iy[0]] #设置横纵坐标 temp_photo.plot(th_x,temp_y) hum_photo.plot(th_x,hum_y) ill_photo.plot(i_x,i_y) temp_photo.set_xlabel('time') temp_photo.set_ylabel('temp') hum_photo.set_xlabel('time') hum_photo.set_ylabel('hum') ill_photo.set_xlabel('time') ill_photo.set_ylabel('illumination') #图像展示 temp_photo.grid() hum_photo.grid() ill_photo.grid() dataPlot = FigureCanvasTkAgg(f, master=root) dataPlot.show() dataPlot.get_tk_widget().pack()
def flush(flag,sleep_flag,set_temp,set_hum,temp,hum,ill,mo1_st,mo2_st,root): con=mc.mysqlconnect() realtime_data_sql='select * from sensorinfo' realtime_data=pd.read_sql(realtime_data_sql,con) con.close() flag.set(realtime_data['flag'][0]) sleep_flag.set(realtime_data['sleep_flag'][0]) set_temp.set(realtime_data['set_temp'][0]) set_hum.set(realtime_data['set_hum'][0]) temp.set(realtime_data['temp'][0]) hum.set(realtime_data['hum'][0]) ill.set(realtime_data['ill'][0]) mo1_st.set(realtime_data['motor1_status'][0]) mo2_st.set(realtime_data['motor2_status'][0]) #500ms刷新数据 root.after(500,flush,flag,sleep_flag,set_temp,set_hum,temp,hum,ill,mo1_st,mo2_st,root)
def insertlog(data): con = mc.mysqlconnect() cursor = con.cursor() cursor.execute( 'insert into log(addtime,data) values(%s,%s)', (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), data)) con.commit() cursor.close() con.close() #linux mysql linux_con = mc.linux_mysqlconnect() linux_cursor = linux_con.cursor() linux_cursor.execute( 'insert into log(addtime,data) values(%s,%s)', (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), data)) linux_con.commit() linux_cursor.close() linux_con.close()
def status_motor(data): con = mc.mysqlconnect() cursor = con.cursor() if data[4:6] == '01': cursor.execute( "update sensorinfo set motor1_status='ILL CLOSE' where id=0") con.commit() print 'Motor ILL CLOSE !' if data[4:6] == '02': cursor.execute( "update sensorinfo set motor1_status='CW OPEN' where id=0") con.commit() print 'Motor CW !' if data[4:6] == '03': cursor.execute( "update sensorinfo set motor1_status='CCW OPEN' where id=0") con.commit() print 'Motor CCW !' cursor.close() con.close()
def dealdata(): set_temp_hum = {} set_temp_hum['min_temp'] = '30' set_temp_hum['min_hum'] = '50' #设置温度值更新 con = mc.mysqlconnect() cursor = con.cursor() cursor.execute('update sensorinfo set set_temp=%s,set_hum=%s where id=0', (set_temp_hum['min_temp'], set_temp_hum['min_hum'])) con.commit() cursor.close() con.close() flag = True ser = ci.CoorConnectPc() while 1: data = ci.ReadPort(ser) if data[0:2] == 'SD': #温湿度模块 if data[2:4] == '01': now_temp_hum = th.temp_hum_store(data) print 'now temp hum:', now_temp_hum['temp'], now_temp_hum[ 'hum'] mt.status2_motor(now_temp_hum['temp'], now_temp_hum['hum']) #print 'set temp hum:',set_temp_hum['min_temp'],set_temp_hum['min_hum'] #if flag==True: #th.judge_temp_hum(ser,now_temp_hum,set_temp_hum) #光敏模块 if data[2:4] == '02': light = pr.photores_store(data) #电机模块 if data[2:4] == '03': mt.status_motor(data) #可燃气体 if data[2:4] == '03': G.gas(data)
def temp_hum_store(data): temp=data[4:6] hum=data[6:8] con=mc.mysqlconnect() cursor=con.cursor() cursor.execute('insert into temp_hum(temp,hum,addtime) values(%s,%s,%s)',(str(temp),str(hum),time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) con.commit() #更新状态 cursor.execute('update sensorinfo set temp=%s,hum=%s where id=0',(str(temp),str(hum))) con.commit() cursor.close() con.close() #linux mysql linux_con=mc.linux_mysqlconnect() linux_cursor=linux_con.cursor() linux_cursor.execute('insert into temp_hum(temp,hum,addtime) values(%s,%s,%s)',(str(temp),str(hum),time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))) linux_con.commit() linux_cursor.close() linux_con.close() temp_hum={'temp':temp,'hum':hum} #print 'temp: ',temp,' hum: ',hum return temp_hum