import machine import utime import network import urequests # SPI Setup for display spi = machine.SPI(1, baudrate=1000000, polarity=0, phase=0, sck=machine.Pin(10), mosi=machine.Pin(11), miso=machine.Pin(8) ) latch = machine.Pin(9, machine.Pin.OUT) def write16(value): hb = (value >> 8) & 0xFF lb = value & 0xFF latch.value(0) spi.write(bytes([hb, lb])) latch.value(1) # 7-segment map with DP as bit 7 SEGMENTS = [ 0b00111111, # 0 0b00000110, # 1 0b01011011, # 2 0b01001111, # 3 0b01100110, # 4 0b01101101, # 5 0b01111101, # 6 0b00000111, # 7 0b01111111, # 8 0b01101111 # 9 ] digit_bits = [8, 9, 10, 11] # active-low digit selects # WiFi Configuration WIFI_SSID = "SSID" WIFI_PASSWORD = "PASS" # Temperature endpoint TEMP_URL = "http://" # Initialize WiFi wlan = network.WLAN(network.STA_IF) wlan.active(True) def connect_wifi(): if not wlan.isconnected(): print("Connecting to WiFi...") wlan.connect(WIFI_SSID, WIFI_PASSWORD) max_wait = 10 while max_wait > 0: if wlan.isconnected(): break max_wait -= 1 print("Waiting for WiFi...") utime.sleep(1) if not wlan.isconnected(): print("WiFi connection failed") return False print("WiFi connected:", wlan.ifconfig()) return True def read_cpu_temp(): adc = machine.ADC(4) voltage = adc.read_u16() * 3.3 / 65535 temp_c = 27 - (voltage - 0.706) / 0.001721 return round(temp_c) def fetch_outside_temp(): try: print("Fetching outside temp...") response = urequests.get(TEMP_URL) print("Response status:", response.status_code) print("Response text:", response.text) if response.status_code == 200: temp = float(response.text.strip()) print("Parsed temperature:", temp) return round(temp) return None except Exception as e: print("Error fetching temp:", e) return None finally: if 'response' in locals(): response.close() # Initialize display values current_digits = [0, 0, 0, 0] last_outside_temp = 0 display_active = True # Control variable for display refresh def update_display(): global display_active if not display_active: return # Refresh all digits in quick succession for i in range(4): seg = SEGMENTS[current_digits[i]] if i == 1: # Decimal point between CPU and outside temp seg |= 0b10000000 digit_select = ~(1 << digit_bits[i]) & 0x0F00 word = seg | digit_select write16(word) utime.sleep_us(800) # Optimized delay - critical for stability # Initial setup if connect_wifi(): outside_temp = fetch_outside_temp() if outside_temp is not None: last_outside_temp = outside_temp current_digits[2] = last_outside_temp // 10 current_digits[3] = last_outside_temp % 10 cpu_temp = read_cpu_temp() current_digits[0] = cpu_temp // 10 current_digits[1] = cpu_temp % 10 # Timers last_cpu_update = utime.ticks_ms() last_outside_update = utime.ticks_ms() last_display_refresh = utime.ticks_ms() while True: current_time = utime.ticks_ms() # CPU temp update (10s) if utime.ticks_diff(current_time, last_cpu_update) >= 10000: display_active = False # Pause display during update cpu_temp = read_cpu_temp() current_digits[0] = cpu_temp // 10 current_digits[1] = cpu_temp % 10 last_cpu_update = current_time display_active = True # Outside temp update (5min) if utime.ticks_diff(current_time, last_outside_update) >= 300000: display_active = False if wlan.isconnected() or connect_wifi(): outside_temp = fetch_outside_temp() if outside_temp is not None: last_outside_temp = outside_temp current_digits[2] = last_outside_temp // 10 current_digits[3] = last_outside_temp % 10 last_outside_update = current_time display_active = True # Continuous display refresh (optimized timing) if utime.ticks_diff(current_time, last_display_refresh) >= 1: # 1ms refresh cycle update_display() last_display_refresh = current_time utime.sleep_us(100) # Tiny delay to prevent bus contention