from machine import I2C, Pin, ADC
import network
import ntptime
import time
from ds1307 import DS1307
from i2c_lcd import I2cLcd
# ---------------- WIFI ----------------
ssid = "Wokwi-GUEST"
password = ""
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(ssid, password)
while not wifi.isconnected():
pass
print("==============================")
print(" Hello Smart-ENEng. of RMUTI ")
print("==============================")
print("WiFi Connected")
# ---------------- I2C ----------------
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=100000)
# ---------------- LCD ----------------
lcd = I2cLcd(i2c, 0x27, 20, 4)
# ---------------- RTC ----------------
rtc = DS1307(i2c)
# ---------------- Sync Time จาก NTP ----------------
ntptime.settime()
t = time.localtime(time.time() + 25200) # +7 Thailand
year = t[0]
month = t[1]
day = t[2]
hour = t[3]
minute= t[4]
sec = t[5]
# ตั้งเวลา DS1307 แค่ครั้งเดียว
rtc.set_time(year, month, day, 0, hour, minute, sec)
# ---------------- POT ----------------
pot = ADC(Pin(39))
pot.atten(ADC.ATTN_11DB)
pot.width(ADC.WIDTH_12BIT)
# ---------------- Welcome Screen ----------------
lcd.clear()
lcd.move_to(2,1)
lcd.putstr("Hello Smart-ENEng")
lcd.move_to(7,2)
lcd.putstr("RMUTI")
time.sleep(3)
lcd.clear()
# ---------------- LOOP ----------------
while True:
dt = rtc.datetime()
year = dt[0]
month = dt[1]
day = dt[2]
hour = dt[4]
minute = dt[5]
sec = dt[6]
pot_value = pot.read()
lcd.move_to(0,0)
lcd.putstr(" Smart ENEng System ")
lcd.move_to(0,1)
lcd.putstr("Date: {:02d}/{:02d}/{:04d}".format(day, month, year))
lcd.move_to(0,2)
lcd.putstr("Time: {:02d}:{:02d}:{:02d}".format(hour, minute, sec))
lcd.move_to(3,3)
lcd.putstr("slide : {:4d} ".format(pot_value))
# -------- Serial Monitor --------
print("Date: {:02d}/{:02d}/{:04d}".format(day, month, year))
print("Time: {:02d}:{:02d}:{:02d}".format(hour, minute, sec))
print("Slide:", pot_value)
print("--------------------------")
time.sleep(1)