Last active
November 25, 2021 09:42
-
-
Save idriszmy/1ce402706a1b85a9a5a914282e157691 to your computer and use it in GitHub Desktop.
Displaying IP address on OLED using Raspberry Pi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Displaying IP address on OLED using Raspberry Pi Zero 2 W | |
- Raspberry Pi Zero 2 W | |
https://my.cytron.io/p-raspberry-pi-zero-2-w-wch | |
- Maker pHAT | |
https://my.cytron.io/p-maker-phat-simplifying-raspberry-pi-for-education | |
- Grove - OLED Display 0.96 inch | |
https://my.cytron.io/p-grove-oled-display-0p96-inch-ssd1315 | |
Install additional library: | |
Open Terminal and write following command | |
$ pip install adafruit-circuitpython-ssd1306 | |
Use this example code, save as "oled_displayIP.py" under "Documents" folder | |
Auto run at boot | |
$ crontab -e | |
$ @reboot python /home/pi/Documents/oled_displayIP.py | |
Last Modified: 25 Nov 2021 | |
""" | |
import time | |
import board | |
import digitalio | |
import adafruit_ssd1306 | |
from PIL import Image, ImageDraw, ImageFont | |
import subprocess | |
import os | |
buzzer = digitalio.DigitalInOut(board.D26) | |
buzzer.direction = digitalio.Direction.OUTPUT | |
i2c = board.I2C() | |
oled = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c) | |
def beep(times, delay): | |
for i in range(times): | |
buzzer.value = True | |
time.sleep(delay) | |
buzzer.value = False | |
time.sleep(delay) | |
oled.fill(0) | |
oled.show() | |
# Create blank image for drawing. | |
image = Image.new("1", (oled.width, oled.height)) | |
draw = ImageDraw.Draw(image) | |
# Load a font in 2 different sizes. | |
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSansCondensed-Bold.ttf", 14) | |
font2 = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSansCondensed.ttf", 12) | |
print("Wait for network") | |
draw.text((15, 25), "Wait for network", font=font2, fill=255) | |
# Display image | |
oled.image(image) | |
oled.show() | |
beep(1, 0.1) | |
while True: | |
# Clear the image | |
draw.rectangle((0, 0, oled.width, oled.height), outline=0, fill=0) | |
try: | |
IP = subprocess.check_output(["hostname", "-I"]).split()[0] | |
print("{}".format(IP.decode("utf-8"))) | |
# Draw the text | |
draw.text((0, 0), "IP Address:", font=font2, fill=255) | |
draw.text((0, 19), "{}".format(IP.decode("utf-8")), font=font, fill=255) | |
draw.text((0, 38), "Raspberry Pi 4B", font=font2, fill=255) | |
draw.text((0, 51), "8GB RAM", font=font2, fill=255) | |
break | |
except: | |
print("Retrying...") | |
# Draw the text | |
#draw.text((35, 25), "Retrying...", font=font2, fill=255) | |
time.sleep(5) | |
beep(1, 0.1) | |
# Display image | |
oled.image(image) | |
oled.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment