本帖最后由 freedomzjsx 于 2022-4-17 16:47 编辑  
 
【起源】  
 【展示】 
  
 
【功能】 (1)行空板部署Blynk服务端 (2)BME680传感器连接掌控板发送温度、湿度、气压数据到Blynk服务端,数据存储到Blynk服务端。   
注意:程序不要直接上传,在自动生成的代码区域复制代码,然后粘贴到手动编辑,将IPAddress(116,62,49,166), 8080);中的端口8080修改成8088,然后上传程序。 (3)python读取Blynk服务的数据库温度、湿度、气压数据。  
(4)行空板屏幕显示读取的数据、利用datetime和time库显示当前时间日期。  
  
 
【器材】 行空板、掌控板、BME680传感器  
 
【后续】 
1、完善气象数据,缺传感器啊 
2、利用python库进行气象数据可视化,利用Flask进行web展示,技术能力不行还在学习中 
3、利用metabase进行数据可视化,web展示更方便 
4、完成外壳设计 
 
【程序】 
			
			
			- import time
 - import datetime
 - import psycopg2  #导入python连接postgresql数据库的第三方库
 - from unihiker import GUI
 - 
 - gui=GUI()
 - conn = psycopg2.connect(dbname="blynk_reporting",user="test",password="test",host="localhost",port="5432") #连接数据库blynk_reporting
 - t=datetime.datetime.today() #读取datatime库中的当前日
 - font_family = gui.load_font('RuiSong.ttf') #读取本地字库,可自行下载字体上传到同一目录下
 - img_imagebg=gui.draw_image(x=120,y=160,image="bg.png",origin='center')
 - 
 - def show_time():  #定义显示时间函数
 -     while True:
 -         time.sleep(0.5)
 -         timestrHM = time.strftime("%H:%M")  #小时:分钟
 -         timestrH = time.strftime("%H") #24小时制
 -         updateHM.config(text=timestrHM)
 - gui.start_thread(show_time)
 - 
 - def show_week():   
 -     week_list=["星期一","星期二","星期三","星期四","星期五","星期六","星期日"] #创建星期的中文显示列表
 -     week=week_list[t.weekday()]  #获取星期,0-6分别对应周一到周日,根据列表的索引显示值为中文
 -     updateweek.config(text=week) #更新文本为索引对应的值
 - 
 - def show_md():
 -     month=t.month  #获取当前月
 -     day=t.day      #获取当前日
 -     updatem.config(text=str(month)+"月") #更新文本为月的值
 -     updateD.config(text=day)   #更新文本为日的值
 - 
 - def get_temp():  #获取温度数据
 - 
 -     temp = conn.cursor()  #创建指针
 -     temp_sql="select doublevalue from reporting_raw_data where pintype like 'v' and pin=0 order by ts desc limit 1"
 -     temp.execute(temp_sql)
 -         #执行SQL语句:获取reporting_raw_data表中端口类型为V(虚拟端口),端口为0的以时间排序的最新一条数据,输出doublevalue
 -     temp_row=temp.fetchall()
 -     conn.commit()  #提交事务
 -     updatetemp.config(text=temp_row)
 - 
 - def get_hum():    #获取湿度数据   
 -     hum = conn.cursor()  #创建指针
 -     hum_sql="select doublevalue from reporting_raw_data where pintype like 'v' and pin=1 order by ts desc limit 1"
 -     hum.execute(hum_sql)
 -     #执行SQL语句:获取reporting_raw_data表中端口类型为V(虚拟端口),端口为1的以时间排序的最新一条数据,输出doublevalue
 -     hum_row=hum.fetchall()
 -     conn.commit()  #提交事务
 -     updatehum.config(text=hum_row)
 - 
 - 
 - def get_hpa():      #获取气压数据  
 -     hpa = conn.cursor()  #创建指针
 -     hpa_sql="select doublevalue from reporting_raw_data where pintype like 'v' and pin=2 order by ts desc limit 1"
 -     hpa.execute(hpa_sql)
 -     #执行SQL语句:获取reporting_raw_data表中端口类型为V(虚拟端口),端口为2的以时间排序的最新一条数据,输出doublevalue
 -     hpa_row=hpa.fetchall()
 -     conn.commit()  #提交事务
 -     updatehpa.config(text=hpa_row)
 - 
 - 
 - 
 - updateHM = gui.draw_text(x=75, y=70, text='',color=(128,0,128),origin='bottom',font_family = font_family,font_size=36)
 - #显示小时:分钟
 - 
 - updatem  = gui.draw_text(x=168, y=35, text='',color=(128,0,128),origin='bottom' ,font_family = font_family,font_size=12) #显示月
 - updateD  = gui.draw_text(x=208, y=55, text='',color=(128,0,128),origin='bottom' ,font_family = font_family,font_size=28) #显示日
 - updateweek  = gui.draw_text(x=208, y=70, text='',color=(128,0,128),origin='bottom' ,font_family = font_family,font_size=10) #显示星期
 - 
 - 
 - img_imagetemp=gui.draw_image(x=18,y=93,w=40,h=25,image="temp.png",origin='center')#显示温度表示图
 - updatetemp  = gui.draw_text(x=58, y=110, text='',color="#f36f34",origin='bottom' ,font_family = font_family,font_size=18) #显示温度
 - updatetemp2  = gui.draw_text(x=95, y=108, text='℃',color="#f36f34",origin='bottom' ,font_family = font_family,font_size=12)
 - 
 - 
 - img_imagehum = gui.draw_image(x=140,y=95,w=40,h=25,image="hum.png",origin='center')#显示湿度表示图
 - updatehum  = gui.draw_text(x=185, y=110, text='',color="#f36f34",origin='bottom' ,font_family = font_family,font_size=18) #显示湿度
 - updatehum2  = gui.draw_text(x=219, y=107, text='%',color="#f36f34",origin='bottom' ,font_family = font_family,font_size=12)
 - 
 - img_imagehpa = gui.draw_image(x=20,y=140,w=40,h=25,image="pa.png",origin='center')#显示气压表示图
 - updatehpa  = gui.draw_text(x=85, y=157, text='',color='#f36f34',origin='bottom' ,font_family = font_family,font_size=18) #显示气压
 - updatehpa2  = gui.draw_text(x=150, y=155, text='hPa',color="#f36f34",origin='bottom' ,font_family = font_family,font_size=12)
 - 
 - 
 - #gui.fill_round_rect(x=5, y=237, w=112, h=75, r=5,width=3, color="#57b5ff")
 - 
 - #gui.fill_round_rect(x=122, y=237, w=113, h=75, r=5,width=3, color="#57b5ff")
 - 
 - while True:   
 -     get_temp()
 -     get_hum()
 -     get_hpa()
 -     show_week()
 -     show_md()
 -     time.sleep(1)
 
  复制代码
  
 
 
 |