Compare commits

..
1 Commits
Author SHA1 Message Date
foley c96d0bb829 hostname and IP on display 2026-10-04 14:21:06 +00:00
+85
View File
@@ -0,0 +1,85 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
"""
This demo will fill the screen with white, draw a black box on top
and then print the device's hostname and IP address in the center of the display
This example is for use on (Linux) computers that are using CPython with
Adafruit Blinka to support CircuitPython libraries. CircuitPython does
not support PIL/pillow (python imaging library)!
"""
import board
import digitalio
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306
import socket
import platform
# Define the Reset Pin
oled_reset = None
try:
oled_reset = digitalio.DigitalInOut(board.D4)
except:
pass
# No reset pin
# Change these
# to the right size for your display!
WIDTH = 128
HEIGHT = 64
BORDER = 5
# Use for I2C.
i2c = board.I2C() # uses board.SCL and board.SDA
oled = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c, addr=0x3C, reset=oled_reset)
# Clear display.
oled.fill(0)
oled.show()
# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
image = Image.new("1", (oled.width, oled.height))
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
# Draw a white background
draw.rectangle((0, 0, oled.width, oled.height), outline=255, fill=255)
# Draw a smaller inner rectangle
draw.rectangle(
(BORDER, BORDER, oled.width - BORDER - 1, oled.height - BORDER - 1),
outline=0,
fill=0,
)
# Load default font.
font = ImageFont.load_default()
# Get the local IP address as a string.
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.connect(("8.8.8.8", 80))
ipaddr = s.getsockname()[0]
hostname = platform.node()
text = hostname + "\n" + ipaddr
bbox = font.getbbox(ipaddr)
(font_width, font_height) = bbox[2] - bbox[0], bbox[3] - bbox[1]
draw.multiline_text(
xy=(10,10),
text=text,
fill=255,
font=font,
spacing=2, #pixel gap between lines,
align="left",
)
# Display image
oled.image(image)
oled.show()