from machine import Pin, PWM
from neopixel import NeoPixel
import time
from time import sleep
import ujson
from umqtt.simple import MQTTClient
import network
SSID = "Wokwi-GUEST"
PASSWORD = ""
MQTT_BROKER = "mqtt.netpie.io"
MQTT_PORT = 1883
MQTT_CLIENT_ID = "19b9b58a-b2a1-45a9-a17a-eafae193503e"
MQTT_USER = "noy8s87zMHMQV54LVeXNSCjf3C9XQ1mA"
MQTT_PASSWORD = "c5ij4eSMJZP25bGLHMse63y99vvBhVzr"
MQTT_TOPIC_SHADOW = "@shadow/data/update"
MQTT_TOPIC_CONTROL = "@msg/operator"
red = Pin(4, Pin.OUT)
green = Pin(0, Pin.OUT)
blue = Pin(2, Pin.OUT)
buzzer = PWM(Pin(14), Pin.OUT)
buzzer.duty(0)
npPin = Pin(23, Pin.OUT)
np = NeoPixel(npPin, 1)
def channel(color):
red.value(0)
green.value(0)
blue.value(0)
np[0] = (0, 0, 0)
if color == 'red':
red.value(1)
np[0] = (255, 0, 0)
elif color == 'green':
green.value(1)
np[0] = (0, 255, 0)
elif color == 'blue':
blue.value(1)
np[0] = (0, 0, 255)
np.write()
durations = []
channels = ['red', 'green', 'blue']
for i in range(3):
minutes = int(input(f"กรอกเวลานับถอยหลัง (นาที) จนถึงเวลากินยา ช่อง {i+1}: "))
target_time = time.time() + (minutes * 60)
durations.append((target_time, channels[i]))
print("ระบบตั้งเวลาสำเร็จ รอเตือน...")
while True:
now = time.time()
for i, (target_time, color) in enumerate(durations):
if target_time == -1:
continue
remaining = int(target_time - now)
if remaining > 0:
mins = remaining // 60
secs = remaining % 60
print(f"เหลืออีก {mins} นาที {secs} วินาที ถึงเวลากินยา {color}")
else:
print(f"ถึงเวลาช่องยา {color} แล้ว!")
channel(color)
buzzer.freq(1000)
buzzer.duty(512)
while True:
channel(color)
buzzer.freq(1000)
buzzer.duty(512)
if button.value() == 1:
print('หยุดการแจ้งเตือน')
buzzer.duty(0)
red.value(0)
green.value(0)
blue.value(0)
np[0] = (0, 0, 0)
np.write()
sleep(1)
while button.value() == 1:
sleep(0.1)
break
sleep(0.1)
durations[i] = (-1, color)
sleep(1)