如何每 X 分钟从 openweather api 获取数据?我想使用 Raspberry Pi Zero W 在 16x2 LCD 上显示此数据。import lcddriverimport timeimport datetimeimport requests, json display = lcddriver.lcd()complete_url = "http://api.openweathermap.org/data/2.5/weather?q=CITY&APPID=****HIDE_API*****" response = requests.get(complete_url)x = response.json() if x["cod"] != "404": y = x["main"] current_temperature = y["temp"] current_pressure = y["pressure"] current_humidiy = y["humidity"] z = x["weather"] weather_description = z[0]["description"] try: print("Writing to display") display.lcd_display_string("Temperatura zew:",1) display.lcd_display_string(str(current_temperature-273.15) + " C", 2) time.sleep(10) display.lcd_clear() display.lcd_display_string("Cisnienie ", 1) display.lcd_display_string(str(current_pressure) + " hPa",2) time.sleep(10) display.lcd_clear() display.lcd_display_string("Wilgotnosc ", 1) display.lcd_display_string(str(current_humidiy) + " %",2) time.sleep(10) display.lcd_clear() time.sleep(1) except KeyboardInterrupt: # If there is a KeyboardInterrupt (when you press ctrl+c), exit the program and cleanup print("Cleaning up!") display.lcd_clear()
3 回答
青春有我
TA贡献1784条经验 获得超8个赞
假设您的代码工作正常,因为我没有看到错误。您可以将代码置于无限循环中。
import time
x = 0
while True:
print(x)
x += 1
time.sleep(1)
上面的示例代码将打印,直到程序以 1 秒的间隔停止:
0
1
2
3
.
.
.
你可以做同样的事情而不是使用time.sleep(12*60).
隔江千里
TA贡献1906条经验 获得超10个赞
import threading, time
def fetch_data():
threading.Timer(5.0, fetch_data).start()
print(time.time())
# Fetch data from api
# Update LCD
fetch_data()
添加回答
举报
0/150
提交
取消