Startseite bisherige Projekte Tools/Snippets Bücherempfehlungen Publikationen Impressum Datenschutzerklärung

Pi Pico Uhr

Sommer 2023


Ein Pico W kann die Uhrzeit aus dem Internet auslesen und per LED-Matrix darstellen.
Das Beispiel unten benötigt die Bibliothek von Pi Pico und Unicorn HAT Mini.


# see here: https://projects.raspberrypi.org/en/projects/get-started-pico-w/2
from UnicornHATMini import UnicornHATMini
import network
import socket
from time import sleep
from picozero import pico_temp_sensor, pico_led
import machine
import ntptime
import time
import framebuf

ssid = 'HomeZonePoint'
password = 'xyzxyzxyzxyz'

def connect():
	#Connect to WLAN
	wlan = network.WLAN(network.STA_IF)
	wlan.active(True)
	wlan.connect(ssid, password)
    
	# wait for connection
	while wlan.isconnected() == False:
		print('Waiting for connection...')
		sleep(1)
    
	ip = wlan.ifconfig()[0]
	print(f'Connected on {ip}')
	return ip

try:
	ip = connect()
	ntptime.settime()
    
    
except KeyboardInterrupt:
	machine.reset()	# also resets wlan connection
    
brightness = 0.1
def change_brightness():
	global brightness
	brightness = max(0.1, (brightness + 0.1) % 1);
	unicornhatmini.set_brightness(brightness)

unicornhatmini = UnicornHATMini()

unicornhatmini.set_brightness(0.1)

unicornhatmini.callback_x = change_brightness;

width, height = unicornhatmini.get_shape()

# a framebuffer for rendering the text
buffer = bytearray(width*height*2)
fb = framebuf.FrameBuffer(buffer, width, height, framebuf.RGB565)


text = ''
offset = -17;
# see examples here:
# https://github.com/pimoroni/unicornhatmini-python/blob/master/examples/text.py
while True:
	fb.fill(0)
	fb.text(text, -offset, 0, 0xF000)
	offset += 1
	if offset > len(text)*8:
		offset = -17

	# Update time
	UTC_OFFSET = 2 * 60 * 60
	lt = time.localtime(time.time() + UTC_OFFSET)
	text = str(lt[3]) + ':' + str(lt[4]) + ':' + str(lt [5])

	# Transfer text to LEDs 
	for x in range(0,width):
		for y in range(0,height):
			fbcol = fb.pixel(x,y)
            
			# see here https://stackoverflow.com/questions/2442576/how-does-one-convert-16-bit-rgb565-to-24-bit-rgb888
			r5 = (fbcol & 0xF800) >> 11;
			g6 =  (fbcol & 0x07e0) >> 5; 
			b5 = fbcol & 0x001f;
            
			r8 = ( r5 * 527 + 23 ) >> 6;
			g8 = ( g6 * 259 + 33 ) >> 6;
			b8 = ( b5 * 527 + 23 ) >> 6;
            
			unicornhatmini.set_pixel(x, y, r8, g8, b8)
    
	unicornhatmini.show()
	time.sleep(1.0 / 60)









Impressum - Datenschutzerklärung