16457| 0
|
[M10项目] 基于MQTT的桌面天气提醒助理 |
本帖最后由 hmilycheng 于 2022-9-19 21:13 编辑 一、项目灵感 1、每日早晨起来,需要查看当日的天气预报,来决定今天所穿的衣服。 2、每日睡觉前,需要查看次日的课程表来整理书包。 3、有时需要远程发布提醒信息。 二、硬件组成 行空板 x1 三、软件与程序 (一)Mind+连接行空板 1、使用Type c数据线将行空板连接到电脑,并切换到Python模式。 2、依次选择“扩展-官方库-行空板”,添加行空板库。 3、 4、访问http://10.1.2.3/打开行空板设置菜单主页。 5、点击“应用开关”菜单,启用SLOT服务。 6、网络设置-连接wifi (二)注册天气API 1、打开天气API网站https://tianqiapi.com/index 2、注册用户名并登录 3、打开API文档 4、打开免费基础实况天气 查看数据 6、请求参数说明: 7、响应JSON 8、响应参数说明 (三)编写代码 1、mind+切换至代码模式 2、新建weather文件夹 3、weather文件夹下新建main.py文件,并编写代码 # 代码如下 from unihiker import GUI import siot # 事件回调函数 def on_message_callback(client, userdata, msg): global mqtt_msg1,mqtt_msg2,mqtt_msg3 print(msg.topic) print(msg.payload.decode()) if (msg.topic == "Msg1/txt"): mqtt_msg1 = msg.payload.decode() elif (msg.topic == "Msg2/txt"): mqtt_msg2 = msg.payload.decode() elif (msg.topic == "Msg3/txt"): mqtt_msg3 = msg.payload.decode() # 自定义函数 def weather_getWeather(): #天气api weather_serveraddr = (str("https://v0.yiketianqi.com/free/day?appid=")) weather_appid = "86943926" weather_appsecret = "Tj6vFcS0" r = requests.get(weather_serveraddr + weather_appid + "&appsecret=" + weather_appsecret + "&cityid=" + "&unescape=1") r.encoding = "utf-8" data = json.loads(r.text) print(data) return data["city"], data["date"], data["week"], data["update_time"], data["wea"], data["wea_img"], data["tem"], data["tem_day"], data["tem_night"], data["win"], data["win_speed"], data["win_meter"], data["air"], data["pressure"], data["humidity"] u_gui=GUI() import requests import json import time global weather_serveraddr global weather_key global weather_location global w cityid = "101110108" w = weather_getWeather() class_table_am= ["体育、数学、语文、英语", "数学、作文、作文、体育", "数学、语文、体育、英语","语文、体育、数学、信息","数学、英语、语文、美术"] class_table_pm=["音乐、科学、语文、数学","道德、英语、阅读、活动","美术、诵读、英语、书法","科学、英语、社团","英语、班会、数学、语文"] i = -1 print(w) print(w[0]) print(w[1]) print(w[2]) print(w[3]) print(w[4]) print(w[5]) print(w[6]) print(w[7]) print(w[8]) print(w[9]) print(w[10]) print(w[11]) print(w[12]) print(w[13]) print(w[14]) u_gui.fill_rect(x=0,y=0,w=240,h=320,color="#000000") siot.init(client_id="",server="192.168.2.61",port=1883,user="siot",password="dfrobot") u_gui=GUI() siot.connect() siot.loop() siot.set_callback(on_message_callback) global mqtt_msg1,mqtt_msg2,mqtt_msg3 u_gui.fill_rect(x=0,y=0,w=240,h=320,color="#000000") txt=u_gui.draw_text(text="MQTT连接成功",x=55,y=0,font_size=15, color="#FFFFFF") siot.getsubscribe(topic="Msg1/txt") siot.publish(topic="Msg1/txt", data="连接成功") siot.getsubscribe(topic="Msg2/txt") siot.publish(topic="Msg2/txt", data="连接成功") siot.getsubscribe(topic="Msg3/txt") siot.publish(topic="Msg3/txt", data="连接成功") time.sleep(2) u_gui.fill_rect(x=0,y=0,w=240,h=320,color="#000000") if (w[2] == "星期一"): i = 0 elif (w[2] == "星期二"): i = 1 elif (w[2] == "星期三"): i = 2 elif (w[2] == "星期四"): i = 3 elif (w[2] == "星期五"): i = 4 if i> -1 and i < 5: u_gui.draw_text(text=w[2] + "课程表" ,x=60,y=150,font_size=13, color="#FFFFFF")#课程表 u_gui.draw_text(text="上:" +class_table_am,x=0,y=180,font_size=13, color="#FFFFFF")#课程表 u_gui.draw_text(text="下:" + class_table_pm,x=0,y=210,font_size=13, color="#FFFFFF")#课程表 else: u_gui.draw_text(text="星期一" + "课程表" ,x=60,y=150,font_size=13, color="#FFFFFF")#课程表 u_gui.draw_text(text="上:" +class_table_am[0],x=0,y=180,font_size=13, color="#FFFFFF")#课程表 u_gui.draw_text(text="下:" + class_table_pm[0],x=0,y=210,font_size=13, color="#FFFFFF")#课程表 msg1=u_gui.draw_text(text="",x=0,y=240,font_size=13, color="#FFFFFF")#留言板 msg2=u_gui.draw_text(text="",x=0,y=270,font_size=13, color="#FFFFFF")#留言板 msg3=u_gui.draw_text(text="",x=0,y=300,font_size=13, color="#FFFFFF")#留言板 while True: u_gui.draw_text(text=w[1],x=5,y=0,font_size=15, color="#FFFFFF")#日期 u_gui.draw_text(text=w[2],x=160,y=0,font_size=15, color="#FFFFFF")#星期 u_gui.draw_text(text=w[0],x=50,y=35,font_size=12, color="#FFFFFF")#城市 u_gui.draw_text(text="风向 " + w[9] + " " + w[10],x=0,y=65,font_size=12, color="#FFFFFF") u_gui.draw_text(text=w[8] + " ~ " + w[7] + "℃",x=0,y=90,font_size=15, color="#FFFFFF") u_gui.draw_text(text=w[14] ,x=0,y=115,font_size=15, color="#FFFFFF")#湿度 u_gui.draw_image(image=w[5] + ".png" ,x=130,y=40) u_gui.draw_text(text=w[4],x=150,y=120,font_size=12, color="#FFFFFF")#天气 msg1.config(text=mqtt_msg1) msg2.config(text=mqtt_msg2) msg3.config(text=mqtt_msg3) 4、下载天气图标,并上传到weather文件夹下 链接: https://pan.baidu.com/s/16YqQBNhNfd-Lm1cFvVY1mQ?pwd=3pnj 提取码: 3pnj 5、点击“运行” 四、附件 weather_api.zip |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed