623 views|2 replies

141

Posts

4

Resources
The OP
 

[Digi-Key Follow Me Issue 2] Task 4-Sub-task 1: Calendar & Clock [Copy link]

 This post was last edited by Tristan_C on 2023-9-2 22:03

This time we are completing Task 4, which is an optional task. I chose Task 1, Calendar & Clock - complete a perpetual calendar clock that can be updated via the Internet and display local weather information.

First is the Internet function. In the previous task, this function was realized through WiFi and obtained the Internet time, so this task can also use it. Next, a function to obtain local weather information from the Internet. There are many online websites for obtaining weather on the Internet, such as Xinzhi Weather and China Meteorological Administration, which provide online API interfaces. We will choose the interface of China Meteorological Administration. Its interface is as follows

http://t.weather.sojson.com/api/weather/city/City ID

The city number is 101210101, taking Hangzhou as an example. Just replace it with the city number and fill it into the browser to test as follows

The returned JSON content is partially parsed as follows, including more detailed information such as city, temperature, humidity, air quality, etc.

We define the interface in settings.toml, similar to getting Suning's time

The method of the interface here is the same as obtaining the time, just use the get of request.

In order to display Chinese city, weather and other information, we also need to use the Chinese display function in Task 1, which has the display screen display function. Therefore, the content that needs to be imported this time is as follows

For ease of use, the function of obtaining time can be made into a separate function interface, which returns a time string

Similarly, getting the weather is also made into a separate function interface, and the json data is parsed to obtain the required content, such as city, temperature and humidity, and then formed into a string and returned

There is also an update of the display screen content. The interface passes in the content string and then updates the string to the display screen

Finally, we call the interface for obtaining time and weather information and loop regularly to update the weather and time.

Start printing information

After connecting to the network, get the time information and weather information and print the information as follows

The updated display information on the onboard display is as follows

The complete code is as follows:

import os
import wifi
import ssl
import socketpool
import adafruit_requests
import time
import displayio
import board
import digitalio

from adafruit_display_text import label
from adafruit_bitmap_font  import bitmap_font

print("Flow Me Task 4-1:Weter station & RTC")

updata_cnt    = 0 #用于更新的计数

display                     = board.DISPLAY
board.DISPLAY.brightness    = 0.5
board.DISPLAY.rotation      = 0
font                        = bitmap_font.load_font("lib\wenquanyi_9pt.pcf")

str_disp_0  = "欢迎参加Flow Me\n这是第二期"
str_disp_1  = "本次任务4-1:\n日历&时钟\n"
str_disp_2  = "通过互联网更新万年历时钟\n并显示当地天气"
color       = 0xFFFFFF

text_group  = displayio.Group()
text_area   = label.Label(font, text=str_disp_0 + str_disp_1 + str_disp_2, color=color)
text_area.x = 10
text_area.y = 10

#启动屏幕显示
text_group.append(text_area)
display.show(text_group)

print(f"ESP32 connecting to {os.getenv('WIFI_SSID')}")
wifi.radio.connect(os.getenv("WIFI_SSID"), os.getenv("WIFI_PASSWORD"))
print(f"ESP32 connected to {os.getenv('WIFI_SSID')}")
print(f"My MAC address: {[hex(i) for i in wifi.radio.mac_address]}")
print(f"My IP address: {wifi.radio.ipv4_address}")

socket_get      = socketpool.SocketPool(wifi.radio)
requests_get    = adafruit_requests.Session(socket_get, ssl.create_default_context())

def request_time():
    print("Get time from SUNING.")
    response_time = requests_get.get(os.getenv("SUNING_TIME_API"))
    time_str = response_time.json()['sysTime2']
    timestamp_get = response_time.json()['sysTime2']
    print(time_str)
    return time_str

def request_weater():
    print("Get weather from China National Meteorological.")
    try:
        response_weather = requests_get.get(os.getenv("HANGZHOU_WEATHER_API"))
    except ConnectionError as e:
        print("Connection Error:", e)
        print("Retrying in 60 seconds")

    weather_all = response_weather.json()

    city_info = weather_all['cityInfo']
    city_str = city_info['city']                            #取城市

    weather_data        = weather_all['data']
    temperature_str     = weather_data['wendu']             #取温度
    humidity_str        = weather_data['shidu']             #取湿度
    weather_data_now    = weather_data['forecast']
    type_str            = weather_data_now[0]['type']       #取天气类型

    weather_str = city_str + "\n" + temperature_str + "°C    " + humidity_str + " " * 4 + type_str

    print(weather_str)

    return weather_str

def updata_display(text_disp):
    text_area = label.Label(font, text=text_disp, scale=2)
    text_area.x = 10
    text_area.y = 10
    board.DISPLAY.show(text_area)

while True:
    weather_time = request_weater() + "\n" + request_time()

    updata_display(weather_time)

    #每秒获取一次时间
    time.sleep(0.9)

This post is from DigiKey Technology Zone

Latest reply

This time we are completing Task 4, which is an optional task. I chose Task 1, Calendar & Clock - complete a perpetual calendar clock that can be updated via the Internet and display local weather information. Congratulations on completing your assignment!   Details Published on 2023-9-2 16:36
 
 

6824

Posts

11

Resources
2
 

This time we are completing Task 4, which is an optional task. I chose Task 1, Calendar & Clock - complete a perpetual calendar clock that can be updated via the Internet and display local weather information.

Congratulations on completing your assignment!

This post is from DigiKey Technology Zone

Comments

Let's study together. The seniors are all great.  Details Published on 2023-9-2 23:35
 
 
 

141

Posts

4

Resources
3
 
lugl4313820 posted on 2023-9-2 16:36 This time we are completing Task 4, which is an optional task. I chose Task 1, Calendar & Clock - Complete a program that can be updated via the Internet...

Let's study together. The seniors are all great.

This post is from DigiKey Technology Zone
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

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