2162 views|6 replies

2549

Posts

0

Resources
The OP
 

[Xingkong Board Python Programming Learning Main Control Board] Seven: Keyboard Switch Pages [Copy link]

This post was last edited by Digital Leaf on 2022-12-10 22:03

The previous article can already display the obtained network weather information, but the weather data is relatively large, and the previous article only displays some weather information for the day. Generally, the actual weather data displayed is one day or seven days. Therefore, like the previous article, one page cannot display all the data, unless the font is reduced, but that is not intuitive. It is more appropriate to switch pages, and different pages display different information. The line blank board happens to have two user buttons that can be used to control the page switching.

To use the buttons, you need to use the pinpong library. There are two ways to call buttons in the pinpong library. One is direct read-query mode, and the second is interrupt event callback mode. The pinpong library gives the usage method

if button_a.is_pressed() == True: #判断按钮是否被按下
    print("按钮A按下")
if button_b.is_pressed() == True: #判断按钮是否被按下
    print("按钮B按下")
def btn_a_rasing_handler(pin):#中断事件回调函数
  print("按钮A按下")

def btn_b_falling_handler(pin):#中断事件回调函数
  print("按钮B松开")

button_a.irq(trigger=Pin.IRQ_RISING, handler = btn_a_rasing_handler)#上升沿触发
button_b.irq(trigger=Pin.IRQ_FALLING, handler = btn_b_falling_handler)#下降沿触发

Actual tests found that the query method also needs to be debounced, otherwise one call will actually be triggered several times. The interrupt method is very stable, so the interrupt method is used in the end.

button_a.irq(trigger=Pin.IRQ_RISING, handler = Analysis_RealDaydate)
button_b.irq(trigger=Pin.IRQ_RISING, handler = Analysis_SevenDaydate)

Press the upper button to display the weather for the day, and press the lower button to display the weather for the week. For the weather for the day, just use the display effect of the previous article, and you only need to adjust the weather effect for the next week.

Similarly, you can find the seven-day data request API from several weather websites. Still use the free version, which is sufficient for use.

Similarly, we use the requests and json modules to obtain weather data. The seven-day response data parsing is slightly more complicated than the single-day response data parsing. The returned data is a dictionary composed of dictionaries and lists.

{"nums":22,"cityid":"101220301","city":"芜湖","update_time":"2022-12-10 19:53:49","data":[{"date":"2022-12-10","wea":"雾转多云","wea_img":"yun","tem_day":"11","tem_night":"2","win":"西北风","win_speed":"3-4级"},{"date":"2022-12-11","wea":"阴转多云","wea_img":"yun","tem_day":"8","tem_night":"0","win":"东北风","win_speed":"3-4级转<3级"},{"date":"2022-12-12","wea":"多云转晴","wea_img":"yun","tem_day":"8","tem_night":"0","win":"西风","win_speed":"3-4级转<3级"},{"date":"2022-12-13","wea":"晴","wea_img":"qing","tem_day":"11","tem_night":"-1","win":"北风","win_speed":"3-4级转<3级"},{"date":"2022-12-14","wea":"晴","wea_img":"qing","tem_day":"6","tem_night":"-1","win":"东风","win_speed":"<3级"},{"date":"2022-12-15","wea":"多云转阴","wea_img":"yun","tem_day":"11","tem_night":"-1","win":"东北风","win_speed":"<3级转3-4级"},{"date":"2022-12-16","wea":"小雪转多云","wea_img":"yun","tem_day":"5","tem_night":"-5","win":"北风","win_speed":"3-4级"}]}

Careful observation reveals that when parsing, just select the value of the date key.

week_data = SevenDayweather_data['data']

Its key value is a list of dictionaries, containing all the required seven-day weather data

[{'date': '2022-12-10', 'wea': '雾转多云', 'wea_img': 'yun', 'tem_day': '11', 'tem_night': '2', 'win': '西北风', 'win_speed': '3-4级'}, {'date': '2022-12-11', 'wea': '阴转多云', 'wea_img': 'yun', 'tem_day': '8', 'tem_night': '0', 'win': '东北风', 'win_speed': '3-4级转<3级'}, {'date': '2022-12-12', 'wea': '多云转晴', 'wea_img': 'yun', 'tem_day': '8', 'tem_night': '0', 'win': '西风', 'win_speed': '3-4级转<3级'}, {'date': '2022-12-13', 'wea': '晴', 'wea_img': 'qing', 'tem_day': '11', 'tem_night': '-1', 'win': '北风', 'win_speed': '3-4级转<3级'}, {'date': '2022-12-14', 'wea': '晴', 'wea_img': 'qing', 'tem_day': '6', 'tem_night': '-1', 'win': '东风', 'win_speed': '<3级'}, {'date': '2022-12-15', 'wea': '多云转阴', 'wea_img': 'yun', 'tem_day': '11', 'tem_night': '-1', 'win': '东北风', 'win_speed': '<3级转3-4级'}, {'date': '2022-12-16', 'wea': '小雪转多云', 'wea_img': 'yun', 'tem_day': '5', 'tem_night': '-5', 'win': '北风', 'win_speed': '3-4级'}]

Therefore, just parse this list.

day1_date.config(text=str(week_data[0]['date']))
day1_wea.config(text=str(week_data[0]['wea']))
day1_temnight.config(text=str(week_data[0]['tem_night'])+"~")
day1_temday.config(text=str(week_data[0]['tem_day'])+"oC")

Finally, it still takes a little time to adjust the display coordinates

The last problem is the frequency of obtaining weather data. Because the free version is limited, you cannot obtain weather data all the time. Moreover, obtaining weather data too frequently does not make sense. In practice, weather data also changes every few hours. So I thought of a timer to get it once every period of time. However, whether it is a delay in a loop or a timer module, it seems to be suitable for S-level and ms-level tasks, not for every few hours. Finally, I found the schedule module ( python-schedule module (scheduled task) based on the official document summary - Tencent Cloud Developer Community - Tencent Cloud (tencent.com) )

schedule.every().seconds 每秒运行一次
schedule.every(2).seconds 每2秒运行一次
schedule.every(1).to(5).seconds 每1-5秒运行一次
schedule.every().minutes 每分钟运行一次
schedule.every().hour 每小时运行一次
schedule.every().day 每天运行一次如果后面没有at表示每天当前时间执行一次
schedule.every().day.at("00:00"). 每天凌晨运行一次
schedule.every().week每周凌晨运行一次
schedule.every().wednesday.at("00:00") 每周三凌晨运行一次

It just happens to be perfect for the need to obtain weather data every few hours.

schedule.every().day.at("00:00").do(Get_NetWeather)
schedule.every().day.at("04:00").do(Get_NetWeather)
schedule.every().day.at("08:00").do(Get_NetWeather)
schedule.every().day.at("12:00").do(Get_NetWeather)
schedule.every().day.at("16:00").do(Get_NetWeather)
schedule.every().day.at("20:00").do(Get_NetWeather)

Because no GUI is used, the actual switch is just redrawing the components on the screen. Fortunately, the unihiker library provides a method to delete the controls at once:

gui.clear()

All the problems have been solved, and the purpose of switching weather data by keystroke can be achieved.


This post is from Programming Basics

Latest reply

As soon as I saw the JSON format data, I felt that most of the functions were implemented, and then I parsed the JSON data and it was displayed.  Details Published on 2023-1-28 15:44
 

1295

Posts

0

Resources
2
 

Can I get GPS information?


This post is from Programming Basics

Comments

That requires adding a GPS module, which is not possible at present.  Details Published on 2022-12-11 12:39
 
 
 

303

Posts

5

Resources
3
 

This is very practical for obtaining weather conditions. I will try it when I have time later. By the way, is this baseplate purchased from the DFrobot store?

This post is from Programming Basics

Comments

No... But DFrobot also has IO port expansion boards  Details Published on 2022-12-11 12:40
 
 
 

2549

Posts

0

Resources
4
 

That requires adding a GPS module, which is not possible at present.

This post is from Programming Basics
 
 
 

2549

Posts

0

Resources
5
 
yaoquan5201314 posted on 2022-12-11 10:48 This is very practical for getting weather conditions. I will try it when I have time later. By the way, did you buy this baseboard at the DFrobot store?

No... But DFrobot also has IO port expansion boards

This post is from Programming Basics

Comments

OK, I'll look for it. I'll first buy an expansion board to study the interface method of the gold finger, and then I'll draw an expansion board that suits me.  Details Published on 2022-12-11 12:49
 
 
 

303

Posts

5

Resources
6
 
Digital Leaf published on 2022-12-11 12:40 No... But DFrobot store also has IO port expansion board

OK, I'll look for it. I'll first buy an expansion board to study the interface method of the gold finger, and then I'll draw an expansion board that suits me.

This post is from Programming Basics
 
 
 

33

Posts

0

Resources
7
 
As soon as I saw the JSON format data, I felt that most of the functions were implemented, and then I parsed the JSON data and it was displayed.
This post is from Programming Basics
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building, Block B, 18 Zhongguancun Street, Haidian District, Beijing 100190, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list