Guest User

JADIVCLOCKULTRA

a guest
Oct 31st, 2025
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.31 KB | None | 0 0
  1. import network
  2. import urequests
  3. import time
  4. import ntptime
  5. from machine import Pin, SPI
  6. import ili934xnew as ili9341
  7. from glcdfont import _mvfont
  8.  
  9. # --- Wi-Fi ---
  10. #CHAGE TO YOURS
  11. SSID = "WIFI-NAME"
  12. PASSWORD = "WIFI-PASSWORD"
  13.  
  14. wlan = network.WLAN(network.STA_IF)
  15. wlan.active(True)
  16.  
  17. # --- SPI + TFT ---
  18. spi = SPI(0, baudrate=40000000, sck=Pin(18), mosi=Pin(19))
  19. tft = ili9341.ILI9341(spi, Pin(17), Pin(16), Pin(20), 240, 320, 0)
  20.  
  21. # --- Vyčistenie displeja ---
  22. tft.fill_rectangle(0, 0, tft.width, tft.height, ili9341.color565(0, 0, 0))
  23.  
  24. # --- Fontové funkcie ---
  25. _mvfont = memoryview(_mvfont)
  26.  
  27. def get_ch(ch):
  28.     ordch = ord(ch)
  29.     offset = ordch * 5
  30.     buf = bytearray(6)
  31.     buf[0] = 0
  32.     buf[1:] = _mvfont[offset:offset + 5]
  33.     return buf, 6
  34.  
  35. def draw_char_scaled(x, y, ch, color, bgcolor, scale=3):
  36.     buf, width = get_ch(ch)
  37.     for i in range(5):
  38.         line = buf[i + 1]
  39.         for j in range(8):
  40.             px_color = color if line & (1 << j) else bgcolor
  41.             tft.fill_rectangle(x + i * scale, y + j * scale, scale, scale, px_color)
  42.  
  43. def draw_text_scaled(x, y, text, color, bgcolor, scale=3):
  44.     for idx, ch in enumerate(text):
  45.         draw_char_scaled(x + idx * (6 * scale), y, ch, color, bgcolor, scale)
  46.  
  47. # --- Boot screen ---
  48. draw_text_scaled(60, 90, "JadivClock Ultra", ili9341.color565(0, 255, 0), ili9341.color565(0, 0, 0), scale=2)
  49.  
  50. # --- Wi-Fi pripojenie ---
  51. start = time.time()
  52. wlan.connect(SSID, PASSWORD)
  53. while not wlan.isconnected() and (time.time() - start < 30):
  54.     time.sleep(0.1)
  55.  
  56. print("Wi-Fi pripojené, IP:", wlan.ifconfig()[0] if wlan.isconnected() else "Nepodarilo sa")
  57.  
  58. # --- Sekvencia farieb pri štarte ---
  59. draw_text_scaled(10, 90, "Please Wait", ili9341.color565(0, 255, 0), ili9341.color565(0, 0, 0), scale=4)
  60. time.sleep(1)
  61. colors = [
  62.     (0, 0, 255),
  63.     (0, 255, 0),
  64.     (255, 255, 255),
  65.     (255, 0, 0),
  66.     (255, 255, 0),
  67.     (0, 255, 255)
  68. ]
  69. for r, g, b in colors:
  70.     tft.fill_rectangle(0, 0, tft.width, tft.height, ili9341.color565(r, g, b))
  71.     time.sleep(1)
  72.  
  73. for n in range(1, 9):
  74.     draw_text_scaled(15 * n, 15 * n, str(n), ili9341.color565(0, 255, 0), ili9341.color565(0, 0, 0), scale=n)
  75.     time.sleep(1)
  76.  
  77. tft.fill_rectangle(0, 0, tft.width, tft.height, ili9341.color565(0, 0, 0))
  78.  
  79. # --- Nastavenie času z NTP ---
  80. try:
  81.     ntptime.host = "pool.ntp.org"
  82.     ntptime.settime()
  83. except:
  84.     pass
  85.  
  86. # --- Home Assistant API ---
  87. # CAHNGE TO YOURS
  88. API_TEMP = "http://192.168.1.121:8123/api/states/input_number.sensor_pico2w_temperature"
  89. API_HUM = "http://192.168.1.121:8123/api/states/input_number.input_number_sensor_pico2w_humidita"
  90. TOKEN = "Bearer YOUR-TOKEN-AFTER-BEARER"
  91.  
  92. # --- Predošlé hodnoty ---
  93. prev_cas = ""
  94. prev_datum = ""
  95. prev_temp_hum = ""
  96.  
  97. # --- Zobrazenie dát ---
  98. def show_data(temp_value, hum_value):
  99.     global prev_cas, prev_datum, prev_temp_hum
  100.  
  101.     # zimný čas: +1 hodina
  102.     t = time.localtime(time.mktime(time.localtime()) + 3600)
  103.  
  104.     datum = "{:02d}.{:02d}.{:04d}".format(t[2], t[1], t[0])
  105.     cas = "{:02d}:{:02d}".format(t[3], t[4])
  106.     temp_hum = "{} C | {} %".format(temp_value, hum_value)
  107.  
  108.     # čas
  109.     if cas != prev_cas:
  110.         tft.fill_rectangle(0, 20, tft.width, 8 * 5 + 2, ili9341.color565(0, 0, 0))
  111.         draw_text_scaled(84, 20, cas, ili9341.color565(0, 255, 255), ili9341.color565(0, 0, 0), scale=5)
  112.         prev_cas = cas
  113.  
  114.     # teplota a vlhkosť
  115.     if temp_hum != prev_temp_hum:
  116.         tft.fill_rectangle(0, 100, tft.width, 8 * 3 + 2, ili9341.color565(0, 0, 0))
  117.         draw_text_scaled(50, 100, temp_hum, ili9341.color565(0, 255, 255), ili9341.color565(0, 0, 0), scale=3)
  118.         prev_temp_hum = temp_hum
  119.  
  120.     # dátum
  121.     if datum != prev_datum:
  122.         tft.fill_rectangle(0, 170, tft.width, 8 * 3 + 2, ili9341.color565(0, 0, 0))
  123.         draw_text_scaled(70, 170, datum, ili9341.color565(0, 255, 255), ili9341.color565(0, 0, 0), scale=3)
  124.         prev_datum = datum
  125.  
  126. # --- Hlavná slučka ---
  127. while True:
  128.     try:
  129.         r1 = urequests.get(API_TEMP, headers={"Authorization": TOKEN})
  130.         temp = r1.json().get("state", "?")
  131.         r1.close()
  132.  
  133.         r2 = urequests.get(API_HUM, headers={"Authorization": TOKEN})
  134.         hum = r2.json().get("state", "?")
  135.         r2.close()
  136.     except:
  137.         temp, hum = "?", "?"
  138.  
  139.     show_data(temp, hum)
  140.     time.sleep(30)
  141.  
Advertisement
Add Comment
Please, Sign In to add comment