From 5f53cc3024eda7afb1e2e881d19af04bae2de406 Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Mon, 7 Sep 2026 19:31:51 -0400 Subject: [PATCH] Made working app; updated README with instructions on how to flash --- README.md | 21 ++++++++++---- config.example.py | 6 ++-- main.py | 71 +++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 4 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 main.py create mode 100644 requirements.txt diff --git a/README.md b/README.md index 589f8ac..038ca60 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Pico Time ![Woodpecker CI badge](https://builds.metaunix.net/api/badges/93/status.svg) -![shields.io version badge](https://img.shields.io/gitea/v/release/BitGoblin/pico-weather?gitea_url=https://git.metaunix.net) +![shields.io version badge](https://img.shields.io/gitea/v/release/BitGoblin/pico-time?gitea_url=https://git.metaunix.net) A [MicroPython](https://micropython.org/) app to display local time on an SSD1306 display. @@ -33,13 +33,24 @@ These are not necessary to run the app, but can be useful for testing and/or fla ### Requirements * [MicroPython](https://micropython.org/download/) - MicroPython downloads +* [mpremote](https://docs.micropython.org/en/latest/reference/mpremote.html) - MicroPython remote serial tool +* (Linux users) Your user should be a part of the `dialout` group ### Steps -1. Flash your Raspberry Pi Pico with MicroPython. -2. Copy `config.example.py` to `config.py` and edit the settings to match your environment. -3. Copy `main.py` and `config.py` to the root of the filesystem on the Pico. -4. Connect the SSD1306 display to your Pico and power on. +1. Connect your SSD1306 display to your Pico. +2. Connect your Pico to your PC via a micro-USB to USB type-A cable. +3. [Flash your Raspberry Pi Pico with MicroPython](https://docs.micropython.org/en/latest/rp2/tutorial/reset.html). + a. This has been tested with MicroPython 1.29, but other versions should work. +4. Clone this git repository: `git clone https://git.metaunix.net/BitGoblin/pico-time`. +5. `cd` into the repo: `cd pico-time`. +6. Copy `config.example.py` to `config.py` and edit the settings to match your environment. +7. Copy the Python code over to the Pico: + a. `mpremote cp main.py :main.py` + b. `mpremote cp config.py :config.py` +8. Reset the Pico: `mpremote reset` + +If it worked you should see the display show a message about connecting to WiFi, then syncing time and finally displaying the local time. If it failed, you can monitor the app via `mpremote repl` or using an IDE like [Thonny](https://thonny.org/). ## License diff --git a/config.example.py b/config.example.py index e506cb3..8aacab1 100644 --- a/config.example.py +++ b/config.example.py @@ -1,6 +1,6 @@ # Wi-Fi credentials WIFI_SSID = "YourSSID" # Set this to your Wi-Fi network name -WIFI_PASS = "YourPassword" # Set this to your Wi-Fi network password +WIFI_PASSWORD = "YourPassword" # Set this to your Wi-Fi network password -# NTP settings -NTP_HOST = "time.google.com" # Set this to your desired NTP server +# Timezone settings +UTC_OFFSET = -5 # Set this to your desired NTP server diff --git a/main.py b/main.py new file mode 100644 index 0000000..fef38cf --- /dev/null +++ b/main.py @@ -0,0 +1,71 @@ +import network +import ntptime +import time +from machine import Pin, I2C +import ssd1306 + +# --- Configuration --- +try: + import config as Config +except ImportError: + print("ERROR: config.py not found.") + print("Create config.py on the device with your Wi-Fi and NTP settings.") + while True: + time.sleep(2) + +# --- Hardware Setup --- +i2c = I2C(0, sda=Pin(16), scl=Pin(17), freq=400000) +oled = ssd1306.SSD1306_I2C(128, 64, i2c) + + +def display_message(line1, line2=""): + oled.fill(0) + oled.text(line1, 0, 16) + oled.text(line2, 0, 32) + oled.show() + + +# --- Wi-Fi Connection --- +display_message("Connecting to", "Wi-Fi...") +wlan = network.WLAN(network.STA_IF) +wlan.active(True) +wlan.connect(Config.WIFI_SSID, Config.WIFI_PASSWORD) + +timeout = 20 +while not wlan.isconnected() and timeout > 0: + time.sleep(1) + timeout -= 1 + +if not wlan.isconnected(): + display_message("Wi-Fi Error", "Check settings") + raise RuntimeError("Failed to connect to Wi-Fi") + +# --- NTP Synchronization --- +display_message("Syncing time...", "NTP server") +try: + ntptime.settime() # Sets the Pico internal RTC to UTC +except Exception as e: + display_message("NTP Failed", str(e)) + time.sleep(3) + +# Disconnect Wi-Fi to conserve power once time is synchronized +wlan.active(False) + +# --- Main Loop --- +DAYS = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"] + +while True: + # Apply UTC offset to local epoch + local_epoch = time.time() + (Config.UTC_OFFSET * 3600) + year, month, day, hour, minute, second, weekday, _ = time.localtime(local_epoch) + + time_str = f"{hour:02d}:{minute:02d}:{second:02d}" + date_str = f"{DAYS[weekday]} {year}-{month:02d}-{day:02d}" + + oled.fill(0) + # Center-align roughly for 8x8 font + oled.text(time_str, 32, 20) + oled.text(date_str, 12, 38) + oled.show() + + time.sleep(0.5) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4c67bf7 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +ssd1306