from machine import Pin, SPI, I2C
import utime
import sys
import ntptime
import framebuf
from pcd8544 import PCD8544
from music import Music
import wlan
if(sys.platform=="esp32"):
led = Pin(2, Pin.OUT)
disp_sck = 18 # default SCK of SPI(1)
disp_mosi = 23 # default MOSI of SPI(1)
disp_miso = 19 # not use
disp_res = 4
disp_dc = 2
disp_cs = 5
disp_blk = 22
# 初始化 SPI
disp_spi = SPI(1, baudrate=20000000, polarity=1, phase=0, sck=Pin(disp_sck), mosi=Pin(disp_mosi), miso=Pin(disp_miso))
I2C_SDA =21 # GPIO21(I2C SDA)
I2C_SCL =22 # GPIO22(I2C SCL)
# Initialize I2C
i2c = I2C(0, scl=Pin(I2C_SCL), sda=Pin(I2C_SDA), freq=400000) #initializing the I2C method for ESP32
buzzer_pin = 5
button_pin = 35
elif(sys.platform=="rp2"):
led = Pin("LED", Pin.OUT)
disp_sck = 6 # default SCK of SPI(0)
disp_mosi = 7 # default MOSI of SPI(0)
disp_miso = 4 # not use
disp_res = 2
disp_dc = 1
disp_cs = 5
disp_blk = 3
# 初始化 SPI
disp_spi = SPI(0, baudrate=2000000, polarity=0, phase=0, sck=Pin(disp_sck), mosi=Pin(disp_mosi))
I2C_SDA =16 # GPIO16(I2C SDA)
I2C_SCL =17 # GPIO17(I2C SCL)
i2c = I2C(0, scl=Pin(I2C_SCL), sda=Pin(I2C_SDA), freq=400000)
buzzer_pin = 15
button_pin = 21
INTERVAL = 1000 # millisecond
# nokia logo bitmap (84x48/8=504 bytes)
image = bytearray([
0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x00,0x00,0x80,0x80,0x40,0x40,0x40,0x80,0x80,0xC0,0xC0,0x40,0xC0,0xA0,0xE0,0xC0,0xE0,0xE0,0xF0,0xF0,0xF8,0xF8,0xF8,0xFC,0xFC,0xFE,0xEE,0xF4,0xF0,0xF0,0x70,0x30,0x00,0x80,0x00,0x00,0x80,0x00,0x0C,0x9C,0x1C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0,0xF0,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xF0,0xF0,0xF0,0xF3,0xFF,0xFF,0xFF,0xFF,0xFF
])
'''
Source available at https://ithelp.ithome.com.tw/articles/10285468
用Raspberry Pi Pico發出久違的Nokia之歌
'''
Nokia = [
'E5:1', 'D5:1', 'F#4:2', 'G#4:2', 'C#5:1', 'B4:1',
'D4:2', 'E4:2', 'B4:1', 'A4:1', 'C#4:2', 'E4:2','A4:4'
]
"""
Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
(date(1970, 1, 1) - date(1900, 1, 1)).days * 24*60*60
(70*365+17)*24*60*60 = 2208988800 =>UTC0
28800 = 8*60*60
2208960000 = 2208988800-28800
"""
# Optional UTC+8 offset time (seconds), if not set, UTC0
# Timezone offset (India = UTC +8:00)
UTC_OFFSET = 28800 # 8 * 60 * 60
def sync_time_with_retry():
try:
print("Syncing with NTP server...")
ntptime.settime() # UTC time
# 獲取並顯示本地時間 (台灣時區 +8)
# utime.localtime() 取得 UTC 時間,所以要加上 8 小時
timer = utime.localtime(utime.time() + UTC_OFFSET)
print("Time synchronized.")
return timer
except OSError as e:
# print("Error syncing time:", e)
if e.args[0] == 110: # ETIMEDOUT
print("ETIMEDOUT error during time sync. Retrying in 5 seconds...")
utime.sleep(5)
sync_time_with_retry() # Recursive call to retry
else:
print(f"An unexpected error occurred: {e}")
# Function to handle button press interrupt (optional)
def button_press(pin):
global button_down
button_down = True
# Start Function
if __name__ == '__main__':
print("Hello, " + sys.platform + "!")
button = Pin(button_pin, Pin.IN, Pin.PULL_UP) # 按鍵接在 GPxx, Internal pull-up resistor
button.irq(trigger=Pin.IRQ_FALLING, handler=button_press) # Attach interrupt to the push button pin
button_down = False
cs = Pin(disp_cs, Pin.OUT)
dc = Pin(disp_dc, Pin.OUT)
rst = Pin(disp_res, Pin.OUT)
lcd = PCD8544(disp_spi, cs, dc, rst)
#Use a framebuffer to store the 4032 pixels (84x48):
buffer = bytearray((lcd.height // 8) * lcd.width)
framebuf = framebuf.FrameBuffer1(buffer, lcd.width, lcd.height)
#Light every pixel:
framebuf.fill(1)
lcd.data(buffer)
utime.sleep_ms(500)
#Clear screen:
framebuf.fill(0)
lcd.data(buffer)
# test pattern (50% on)
lcd.data(bytearray([0x55, 0xAA] * 42 * 6))
utime.sleep(1)
framebuf.fill(0)
framebuf.text("Hello Wokwi", 0, 0, 1)
framebuf.text("Nokia 5110", 0, 10, 1)
lcd.data(buffer)
# draw a 84x48 picture
lcd.data(image)
utime.sleep_ms(1000)
# 引腳定義
button = Pin(button_pin, Pin.IN, Pin.PULL_UP) # 按鍵接在 GPxx, Internal pull-up resistor
music = Music(buzzer_pin) # 蜂鳴器接在 GPxx
music.set_volume(1000)
music.tempo(4,120)
music.play(Nokia)
# ========== CONNECT WIFI ==========
wlan.connect_wifi() # Connecting to WiFi Router
utime.sleep_ms(1000)
# ========== NTP TIME SYNC ==========
led.on()
timer = sync_time_with_retry() # Synchronize the time
hour = "00"
minute = "00"
second = "00"
led.off()
lastTime = utime.ticks_ms()
# ========== MAIN LOOP ==========
while True:
try:
currTime = utime.ticks_ms()
if (currTime - lastTime > INTERVAL):
lastTime = currTime
time_now = utime.localtime(utime.time() + UTC_OFFSET)
year = "{:04d}".format(time_now[0])
month = "{:02d}".format(time_now[1])
day = "{:02d}".format(time_now[2])
hour = "{:02d}".format(time_now[3])
minute = "{:02d}".format(time_now[4])
second = "{:02d}".format(time_now[5])
# SERIAL PRINT
str_date = year + ":" + month + ":" + day
str_time = hour + ":" + minute + ":" + second
print("Date: " + str_date, end='')
print("; Time: " + str_time)
# lcd
framebuf.fill(0)
framebuf.text("Date: ", 0, 8, 1)
framebuf.text(year + "/" + month + "/" + day, 0, 16, 1)
framebuf.text("Time: ", 0, 24, 1)
framebuf.text(hour + ":" + minute + ":" + second, 0, 32, 1)
lcd.data(buffer)
if (hour == "12") and (minute == "00") and (second == "00"):
# ========== CONNECT WIFI ==========
wlan.connect_wifi() # Connecting to WiFi Router
utime.sleep_ms(1000)
led.on()
timer = sync_time_with_retry() # Synchronize the time
led.off()
if button_down == True:
# ========== CONNECT WIFI ==========
wlan.connect_wifi() # Connecting to WiFi Router
utime.sleep_ms(1000)
led.on()
timer = sync_time_with_retry() # Synchronize the time
button_down = False
led.off()
utime.sleep_ms(100)
except KeyboardInterrupt: # 使用者中斷執行(通常是輸入^C)
print("Stop")
break
Loading
nokia-5110
nokia-5110