import network
import utime
import ntptime
from machine import Pin, I2C, PWM
import ssd1306
i2c = I2C(0, scl=Pin(13), sda=Pin(12))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
buzzer = PWM(Pin(26))
buzzer.duty(0)
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASS = ""
TIMEZONE = 2
C4=262; D4=294; E4=330; F4=349; G4=392; A4=440; B4=494; C5=523; E5=659; G5=784
def play_tone(freq, duration):
if freq == 0:
buzzer.duty(0)
else:
buzzer.duty(512)
buzzer.freq(freq)
utime.sleep_ms(duration)
buzzer.duty(0)
utime.sleep_ms(20)
def tone(freq, time_ms):
play_tone(freq, time_ms)
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(WIFI_SSID, WIFI_PASS)
oled.fill(0)
oled.text("Connecting...", 0, 0)
oled.show()
while not wlan.isconnected():
utime.sleep(0.5)
print("WiFi Connected")
def sync_time():
oled.fill(0)
oled.text("Sync time...", 0, 0)
oled.show()
try:
ntptime.settime()
print("Time Synced")
except:
print("Sync Failed")
def effect_1():
for _ in range(3): play_tone(440, 100)
def effect_2():
for _ in range(5): play_tone(880, 50)
def effect_3():
for f in range(400, 1000, 10): play_tone(f, 10)
def effect_4():
for f in range(1000, 400, -10): play_tone(f, 10)
def effect_5():
for f in [C4, E4, G4, C5]: play_tone(f, 100)
def effect_6():
for f in [C5, G4, E4, C4]: play_tone(f, 100)
def effect_7():
play_tone(660, 150); play_tone(0, 50); play_tone(660, 150)
def effect_8():
for f in [440, 880]: play_tone(f, 80)
def effect_9():
for f in [300, 500, 300, 700]: play_tone(f, 150)
def effect_10():
play_tone(1000, 500)
def effect_11():
for f in [200, 200, 600]: play_tone(f, 200)
def effect_12():
for f in [C4, D4, E4, F4, G4, A4, B4, C5]: play_tone(f, 80)
def effect_13():
for _ in range(2):
for f in [600, 900]: play_tone(f, 200)
def effect_14():
for f in range(100, 2000, 50): play_tone(f, 5)
def effect_15():
for f in [440, 330, 440, 330]: play_tone(f, 200)
def effect_16():
for f in [440, 494, 523, 587, 440]: play_tone(f, 150)
effects = [effect_1, effect_2, effect_3, effect_4,
effect_5, effect_6, effect_7, effect_8,
effect_9, effect_10, effect_11, effect_12,
effect_13, effect_14, effect_15, effect_16]
EFFECT_NAMES = [
"Pip-pip-pip", "Fast beep", "Siren", "Falling",
"Arp up", "Arp down", "Double", "Jump",
"Robot", "Long beep", "Low start","Rainbow",
"Alarm", "Space", "Call", "Finale",
]
def format_time(seconds):
h = seconds // 3600
m = (seconds % 3600) // 60
s = seconds % 60
return f'{h:02}:{m:02}:{s:02}'
def oled_center(text, y):
x = max(0, (128 - len(text) * 8) // 2)
oled.text(text, x, y)
def run_timer():
oled.fill(0)
oled.text("HH:MM:SS", 16, 20)
oled.show()
raw = input("Timer HH:MM:SS -> ")
h, m, s = map(int, raw.split(":"))
duration = h * 3600 + m * 60 + s
start = utime.ticks_ms()
while True:
rest = utime.ticks_diff(utime.ticks_ms(), start) // 1000
remaining = duration - rest
oled.fill(0)
oled_center("TIMER", 0)
oled_center(format_time(remaining), 25)
oled.show()
print(format_time(remaining))
if rest >= duration:
break
next_tick = utime.ticks_add(start, (rest + 1) * 1000)
while utime.ticks_diff(next_tick, utime.ticks_ms()) > 0:
utime.sleep_ms(10)
while True:
oled.fill(0)
oled_center("TIMER", 0)
oled_center("00:00:00", 25)
oled.show()
notes = [261,261,392,392,440,440,392,349,329,329,393,261]
waits = [300,300,300,300,300,300,600,300,300,300,300,600]
for n, w in zip(notes, waits):
tone(n, w)
utime.sleep_ms(300)
utime.sleep(2)
def run_effects():
oled.fill(0)
oled_center("EFFECTS", 0)
oled.text("0-15:", 0, 20)
oled.show()
choice = int(input("Effect 0-15 -> "))
choice = max(0, min(15, choice))
oled.fill(0)
oled_center("EFFECTS", 0)
oled_center(f"#{choice+1}", 20)
name = EFFECT_NAMES[choice][:16]
oled_center(name, 36)
oled.show()
print(f"Playing effect #{choice+1}: {EFFECT_NAMES[choice]}")
effects[choice]()
oled.fill(0)
oled_center("Done!", 28)
oled.show()
utime.sleep(1)
connect_wifi()
sync_time()
while True:
oled.fill(0)
oled_center("0 - Timer", 16)
oled_center("1 - Signal", 32)
oled.show()
mode = input("Choose 0/1 -> ").strip()
if mode == "0":
run_timer()
elif mode == "1":
run_effects()
else:
oled.fill(0)
oled_center("Invalid!", 28)
oled.show()
utime.sleep(1)