So somthing like this (no idea if works, untested, just an example)turns in to this, with a description of how it should work and what you need to install etc.
Code:
import networkimport urequestsimport timefrom machine import Pin, SPIfrom umqtt.simple import MQTTClientfrom max7219 import Max7219# -----------------------------# USER SETTINGS# -----------------------------WIFI_SSID = "YOUR_WIFI"WIFI_PASS = "YOUR_PASSWORD"MQTT_BROKER = "YOUR_MQTT_SERVER_IP"MQTT_TOPIC = b"pico/button"OPENWEATHER_API_KEY = "YOUR_API_KEY"CITY = "City,County"# -----------------------------# HARDWARE SETUP# -----------------------------led = Pin(15, Pin.OUT)button = Pin(14, Pin.IN, Pin.PULL_UP)# SPI for MAX7219spi = SPI(1, baudrate=1_000_000, polarity=0, phase=0, sck=Pin(10), mosi=Pin(11))cs = Pin(13, Pin.OUT)matrix = Max7219(8, 8, spi, cs)# -----------------------------# WIFI CONNECT# -----------------------------def wifi_connect(): wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect(WIFI_SSID, WIFI_PASS) print("Connecting to WiFi...") while not wlan.isconnected(): time.sleep(0.5) print("Connected:", wlan.ifconfig())# -----------------------------# MQTT SEND# -----------------------------def mqtt_send(state): try: client = MQTTClient("pico", MQTT_BROKER) client.connect() client.publish(MQTT_TOPIC, state) client.disconnect() except Exception as e: print("MQTT error:", e)# -----------------------------# GET OUTSIDE TEMPERATURE# -----------------------------def get_temperature(): url = f"http://api.openweathermap.org/data/2.5/weather?q={CITY}&appid={OPENWEATHER_API_KEY}&units=metric" try: r = urequests.get(url) data = r.json() r.close() return data["main"]["temp"] except: return None# -----------------------------# DISPLAY TEMPERATURE# -----------------------------def display_temp(temp): matrix.clear() matrix.text(str(int(temp)), 0, 0) matrix.show()# -----------------------------# MAIN LOOP# -----------------------------wifi_connect()last_button = 1led_state = Falselast_temp_check = 0temperature = Nonewhile True: # BUTTON HANDLING current = button.value() if current == 0 and last_button == 1: led_state = not led_state led.value(led_state) mqtt_send(b"LED ON" if led_state else b"LED OFF") print("Button pressed, LED:", led_state) last_button = current # WEATHER UPDATE EVERY 60 SECONDS if time.time() - last_temp_check > 60: temperature = get_temperature() print("Temperature:", temperature) last_temp_check = time.time() # DISPLAY TEMPERATURE if temperature is not None: display_temp(temperature) time.sleep(0.5) matrix.clear() matrix.show() time.sleep(0.5) time.sleep(0.05)Code:
import timeimport gpiodimport requestsimport paho.mqtt.client as mqttimport spidev# -----------------------------# USER SETTINGS# -----------------------------WIFI_SSID = "YOUR_WIFI"WIFI_PASS = "YOUR_PASSWORD"MQTT_BROKER = "YOUR_MQTT_SERVER_IP"MQTT_TOPIC = "pico/button"OPENWEATHER_API_KEY = "YOUR_API_KEY"CITY = "City,County"# -----------------------------# HARDWARE SETUP# -----------------------------# GPIO Pin Numbers for Raspberry Piled_pin = 15 # GPIO Pin 15button_pin = 14 # GPIO Pin 14# SPI setup for MAX7219spi = spidev.SpiDev()spi.open(0, 0) # Open SPI bus 0, device 0spi.max_speed_hz = 1000000spi.mode = 0# MAX7219 setup (8x8 matrix)def send_to_max7219(data): for byte in data: spi.xfer([byte])# -----------------------------# MQTT SEND# -----------------------------def mqtt_send(state): try: client = mqtt.Client("raspberry") client.connect(MQTT_BROKER) client.publish(MQTT_TOPIC, state) client.disconnect() except Exception as e: print("MQTT error:", e)# -----------------------------# GET OUTSIDE TEMPERATURE# -----------------------------def get_temperature(): url = f"http://api.openweathermap.org/data/2.5/weather?q={CITY}&appid={OPENWEATHER_API_KEY}&units=metric" try: r = requests.get(url) data = r.json() return data["main"]["temp"] except Exception as e: print("Weather API error:", e) return None# -----------------------------# DISPLAY TEMPERATURE# -----------------------------def display_temp(temp): # You will need to send the data for the MAX7219 (8x8 LED matrix). # Here we just convert the temperature to a string and display. data = [0] * 8 # Assuming 8x8 matrix # Add logic to display the temperature as you prefer. send_to_max7219(data)# -----------------------------# MAIN LOOP# -----------------------------# Set up libgpiod context and GPIO chipchip = gpiod.Chip('gpiochip0') # Use gpiochip0, which is the default chip on Raspberry Piled_line = chip.get_line(led_pin) # Get the line object for the LED pinbutton_line = chip.get_line(button_pin) # Get the line object for the button pin# Configure the linesled_line.request(consumer="led_control", type=gpiod.LINE_REQ_DIR_OUT) # Set LED as outputbutton_line.request(consumer="button_input", type=gpiod.LINE_REQ_DIR_IN, default_vals=[1]) # Set button as input (pull-up)last_button = 1led_state = Falselast_temp_check = 0temperature = Nonewhile True: # BUTTON HANDLING current = button_line.get_value() # Read the button state if current == 0 and last_button == 1: # Button pressed (active low) led_state = not led_state led_line.set_value(led_state) # Toggle LED state mqtt_send("LED ON" if led_state else "LED OFF") print("Button pressed, LED:", led_state) last_button = current # WEATHER UPDATE EVERY 60 SECONDS if time.time() - last_temp_check > 60: temperature = get_temperature() print("Temperature:", temperature) last_temp_check = time.time() # DISPLAY TEMPERATURE if temperature is not None: display_temp(temperature) time.sleep(0.5) time.sleep(0.05)Statistics: Posted by bensimmo — Sun Jan 04, 2026 3:05 pm