import network, json, max7219
import urequests
import _thread
from max7219p import *
import time, dht, font, ntptime
from machine import Pin, ADC, PWM, SPI
from umqtt.simple import MQTTClient
dht = dht.DHT22(Pin(26))
spi = SPI(1, 100000, sck=Pin(14), mosi=Pin(13))
#maxnum = 4
display1 = max7219.Matrix8x8(spi, Pin(15), maxnum)
display2 = max7219.Matrix8x8(spi, Pin(4), maxnum)
display3 = max7219.Matrix8x8(spi, Pin(5), maxnum)
display4 = max7219.Matrix8x8(spi, Pin(18), maxnum)
display5 = max7219.Matrix8x8(spi, Pin(19), maxnum)
display6 = max7219.Matrix8x8(spi, Pin(21), maxnum)
weather = ()
weather2 = ()
ntp_measure_time = w_measure_time = ow_measure_time = time.ticks_ms()
menu = 1
SSID = 'Wokwi-GUEST'
PASSWORD = ''
sta = network.WLAN(network.STA_IF)
sta.active(True)
if not sta.isconnected():
print("Connecting to network...")
sta.connect(SSID, PASSWORD)
while not sta.isconnected():
pass
print("network config:", sta.ifconfig())
scrollsymbol(display1, sta.ifconfig()[0], 0, 0, 3, 0.1)
ntptime.host = 'time.stdtime.gov.tw'
ntptime.settime()
def get_weather():
dht.measure()
temp=dht.temperature()
humi=dht.humidity()
#msg = '{}{}{} {}{:3.1f}%'.format(chr(204),temp,chr(128),chr(205),humi)
return '{}{:>2.0f} {}{:>2.0f}'.format(chr(204),temp,chr(205),humi)
def get_time():
tm = time.localtime(time.time()+8*60*60) #台灣為+8時區,較標準時間快8(hr)*60(min)*60(sec)秒
year,month,day,hour,minu,sec,week,yday = tm
time_msg = '{:02d}:{:02d}:{:02d}'.format(hour,minu,sec)
date_msg = '{:02d}-{:02d}-{:02d}'.format(year-2000, month, day)
return (time_msg, date_msg, week, hour, minu, sec)
def get_outdoor():
url = "https://opendata.cwa.gov.tw/api/v1/rest/datastore/O-A0001-001?Authorization=CWB-E39F65E3-5B26-409B-89E7-16BA1CC6EF0A&StationId=C0V710"
html = urequests.get(url)
datas = json.loads(html.text)
#html.close()
o_weather = datas['records']['Station'][0]['WeatherElement']['Weather']
o_temp = float(datas['records']['Station'][0]['WeatherElement']['AirTemperature'])//1
o_humi = float(datas['records']['Station'][0]['WeatherElement']['RelativeHumidity'])
if o_weather in ['晴']:
o_weather_chr = chr(193)
elif o_weather in ['陰']:
o_weather_chr = chr(199)
elif o_weather in ['雨','雷雨','陰有雨']:
o_weather_chr = chr(198)
else:
o_weather_chr = '--'
#msg = ('{} {}{:.0f}{}'.format(o_weather_chr,chr(204),o_temp,chr(128)), '{} {}{:.0f}%'.format(o_weather_chr, chr(205),o_humi), o_temp, o_humi)
msg = f'{o_weather_chr} {o_temp:>2.0f} {o_humi:>2.0f}'
return msg
def get_aqi():
url = 'https://data.moenv.gov.tw/api/v2/aqx_p_432?language=zh&offset=69&limit=1&api_key=ac6a13da-cdc1-40f2-a4bf-9241f90b152a'
html = urequests.get(url)
datas = json.loads(html.text)
aqi = datas['records'][0]['aqi']
pm25 = datas['records'][0]['pm2.5']
msg = ('{} {}'.format(chr(206), aqi), '{}{} {}'.format(chr(207), chr(208), pm25))
return msg
while True:
#if btn.value() == 0:
# if menu < 6:
# menu += 1
# else:
# menu = 1
# print('menu=',menu)
# time.sleep_ms(300)
time_msg = get_time()
print(time_msg[3], time_msg[4],time_msg[5])
current_time = time.ticks_ms()
if weather == () or current_time - w_measure_time >= 60*1000: #每1分鐘更新DHT22溫溼度資料
weather = get_weather()
print(type(weather[0]))
print(weather[0])
w_measure_time = current_time
if weather2 == () or current_time - ow_measure_time >= 60*60*1000: #每1小時更新
weather2 = get_outdoor()
aqi = get_aqi()
ow_measure_time = current_time
if current_time - ntp_measure_time >= 24*60*60*1000: #每1天自動校時
ntptime.settime()
ntp_measure_time = current_time
#顯示年月日
display1.fill(0)
showsymbol(display1, time_msg[1],2,0)
display1.show()
#顯示時間
display2.fill(0)
showsymbol(display2, time_msg[0],3,0)
display2.hline(time_msg[2]*4+2, 7, 4, 1) #一:2~5,二:6~9,三:10~13,四:14~17,五:18~21,六:22~25,日:26~29
display2.show()
time.sleep(1)
#顯示室內溫溼度(DHT22)(溫、溼度輸播)
display3.fill(0)
showsymbol(display3, weather[:], 1,0)
display3.show()
#scrollsymbol(display3, weather[0],0,0,2,0.05)
#scrollsymbol(display3, weather[1],0,0,2,0.05)
#顯示室外溫溼度(氣象局opendata)(溫、溼度輪播)
display4.fill(0)
showsymbol(display4, weather2[:], 0,0)
display4.show()
#scrollsymbol(display4, weather2[0],0,0,2,0.05)
#scrollsymbol(display4, weather2[1],0,0,2,0.05)
#顯示空氣品質(環保署opendata)(AQI)
display5.fill(0)
showsymbol(display5, aqi[0], 6,0)
display5.show()
#scrollsymbol(aqi[0],5,0,2,0.05)
#scrollsymbol(aqi[1],3,0,2,0.05)
#顯示空氣品質(環保署opendata)(PM2.5)
display6.fill(0)
showsymbol(display6, aqi[1], 3,0)
display6.show()