Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import network
- import urequests
- import time
- import ntptime
- from machine import Pin, SPI
- import ili934xnew as ili9341
- from glcdfont import _mvfont
- # --- Wi-Fi ---
- #CHAGE TO YOURS
- SSID = "WIFI-NAME"
- PASSWORD = "WIFI-PASSWORD"
- wlan = network.WLAN(network.STA_IF)
- wlan.active(True)
- # --- SPI + TFT ---
- spi = SPI(0, baudrate=40000000, sck=Pin(18), mosi=Pin(19))
- tft = ili9341.ILI9341(spi, Pin(17), Pin(16), Pin(20), 240, 320, 0)
- # --- Vyčistenie displeja ---
- tft.fill_rectangle(0, 0, tft.width, tft.height, ili9341.color565(0, 0, 0))
- # --- Fontové funkcie ---
- _mvfont = memoryview(_mvfont)
- def get_ch(ch):
- ordch = ord(ch)
- offset = ordch * 5
- buf = bytearray(6)
- buf[0] = 0
- buf[1:] = _mvfont[offset:offset + 5]
- return buf, 6
- def draw_char_scaled(x, y, ch, color, bgcolor, scale=3):
- buf, width = get_ch(ch)
- for i in range(5):
- line = buf[i + 1]
- for j in range(8):
- px_color = color if line & (1 << j) else bgcolor
- tft.fill_rectangle(x + i * scale, y + j * scale, scale, scale, px_color)
- def draw_text_scaled(x, y, text, color, bgcolor, scale=3):
- for idx, ch in enumerate(text):
- draw_char_scaled(x + idx * (6 * scale), y, ch, color, bgcolor, scale)
- # --- Boot screen ---
- draw_text_scaled(60, 90, "JadivClock Ultra", ili9341.color565(0, 255, 0), ili9341.color565(0, 0, 0), scale=2)
- # --- Wi-Fi pripojenie ---
- start = time.time()
- wlan.connect(SSID, PASSWORD)
- while not wlan.isconnected() and (time.time() - start < 30):
- time.sleep(0.1)
- print("Wi-Fi pripojené, IP:", wlan.ifconfig()[0] if wlan.isconnected() else "Nepodarilo sa")
- # --- Sekvencia farieb pri štarte ---
- draw_text_scaled(10, 90, "Please Wait", ili9341.color565(0, 255, 0), ili9341.color565(0, 0, 0), scale=4)
- time.sleep(1)
- colors = [
- (0, 0, 255),
- (0, 255, 0),
- (255, 255, 255),
- (255, 0, 0),
- (255, 255, 0),
- (0, 255, 255)
- ]
- for r, g, b in colors:
- tft.fill_rectangle(0, 0, tft.width, tft.height, ili9341.color565(r, g, b))
- time.sleep(1)
- for n in range(1, 9):
- draw_text_scaled(15 * n, 15 * n, str(n), ili9341.color565(0, 255, 0), ili9341.color565(0, 0, 0), scale=n)
- time.sleep(1)
- tft.fill_rectangle(0, 0, tft.width, tft.height, ili9341.color565(0, 0, 0))
- # --- Nastavenie času z NTP ---
- try:
- ntptime.host = "pool.ntp.org"
- ntptime.settime()
- except:
- pass
- # --- Home Assistant API ---
- # CAHNGE TO YOURS
- API_TEMP = "http://192.168.1.121:8123/api/states/input_number.sensor_pico2w_temperature"
- API_HUM = "http://192.168.1.121:8123/api/states/input_number.input_number_sensor_pico2w_humidita"
- TOKEN = "Bearer YOUR-TOKEN-AFTER-BEARER"
- # --- Predošlé hodnoty ---
- prev_cas = ""
- prev_datum = ""
- prev_temp_hum = ""
- # --- Zobrazenie dát ---
- def show_data(temp_value, hum_value):
- global prev_cas, prev_datum, prev_temp_hum
- # zimný čas: +1 hodina
- t = time.localtime(time.mktime(time.localtime()) + 3600)
- datum = "{:02d}.{:02d}.{:04d}".format(t[2], t[1], t[0])
- cas = "{:02d}:{:02d}".format(t[3], t[4])
- temp_hum = "{} C | {} %".format(temp_value, hum_value)
- # čas
- if cas != prev_cas:
- tft.fill_rectangle(0, 20, tft.width, 8 * 5 + 2, ili9341.color565(0, 0, 0))
- draw_text_scaled(84, 20, cas, ili9341.color565(0, 255, 255), ili9341.color565(0, 0, 0), scale=5)
- prev_cas = cas
- # teplota a vlhkosť
- if temp_hum != prev_temp_hum:
- tft.fill_rectangle(0, 100, tft.width, 8 * 3 + 2, ili9341.color565(0, 0, 0))
- draw_text_scaled(50, 100, temp_hum, ili9341.color565(0, 255, 255), ili9341.color565(0, 0, 0), scale=3)
- prev_temp_hum = temp_hum
- # dátum
- if datum != prev_datum:
- tft.fill_rectangle(0, 170, tft.width, 8 * 3 + 2, ili9341.color565(0, 0, 0))
- draw_text_scaled(70, 170, datum, ili9341.color565(0, 255, 255), ili9341.color565(0, 0, 0), scale=3)
- prev_datum = datum
- # --- Hlavná slučka ---
- while True:
- try:
- r1 = urequests.get(API_TEMP, headers={"Authorization": TOKEN})
- temp = r1.json().get("state", "?")
- r1.close()
- r2 = urequests.get(API_HUM, headers={"Authorization": TOKEN})
- hum = r2.json().get("state", "?")
- r2.close()
- except:
- temp, hum = "?", "?"
- show_data(temp, hum)
- time.sleep(30)
Advertisement
Add Comment
Please, Sign In to add comment