From 6ee071158082caa9d25f1a1d4467e4f6b33d5965 Mon Sep 17 00:00:00 2001 From: Jonathan Wonneberger Date: Wed, 17 Jul 2024 22:34:14 +0200 Subject: [PATCH] init - new files just a start --- boot.py | 38 +++++++++++++ main.py | 122 +++++++++++++++++++++++++++++++++++++++++ wifi_config.py.default | 10 ++++ 3 files changed, 170 insertions(+) create mode 100755 boot.py create mode 100644 main.py create mode 100644 wifi_config.py.default diff --git a/boot.py b/boot.py new file mode 100755 index 0000000..c6861eb --- /dev/null +++ b/boot.py @@ -0,0 +1,38 @@ +# This file is executed on every boot (including wake-boot from deepsleep) +import uos, machine, time, network, gc, webrepl + +# start webinterface and do a garbage collection (bc of reasons) +webrepl.start() +gc.collect() + +# get wifi credentials +from wifi_config import NETWORKS + +def connect_to_wifi(ssid, password, timeout = 15): + wlan = network.WLAN(network.STA_IF) + wlan.active(True) + print(f"connecting to network \"{ssid}\" ...") + wlan.connect(ssid, password) + start_time = time.time() + while not wlan.isconnected(): + print("start: ", start_time) + print("time:", time.time()) + print("condition: ", start_time - time.time() > timeout) + if time.time() - start_time > timeout: + print(f"failed to connect to {ssid} within {timeout} seconds") + return False + time.sleep(1) + print(f"connection to {ssid} established") + print("network config: ", wlan.ifconfig()) + return True + +# try to connect to networks +for wifi in NETWORKS.values(): + ssid = wifi["ssid"] + password = wifi["password"] + if connect_to_wifi(ssid, password): + break + +# no connection established +if not network.WLAN(network.STA_IF).isconnected(): + print("failed to connect to any wifi network") diff --git a/main.py b/main.py new file mode 100644 index 0000000..a914acb --- /dev/null +++ b/main.py @@ -0,0 +1,122 @@ +import os, machine, time, neopixel, micropython + +micropython.alloc_emergency_exception_buf(100) + +# how many LEDs are there in a row? +Nled = 16 + +# define pin for led control +# see reference for GPIO numbers: https://microcontrollerslab.com/wp-content/uploads/2019/05/ESP8266-12E-Wemos-D1-Mini-pinout.jpg +# not all GPIO pins can be used ... +pin_number = 5 +neopin = machine.Pin(pin_number, machine.Pin.OUT) +# initialize neopixel object to write to +np = neopixel.NeoPixel(neopin, Nled) + +# define pin for button +button = machine.Pin(13, machine.Pin.IN) + +# variable and function for button handling +button_pressed = False +def handle_button(pin): + global button_pressed + button_pressed = True + time.sleep_ms(200) + +# register button function for interrupt +button.irq(trigger=machine.Pin.IRQ_RISING, handler=handle_button) + +# define ranges of LEDs +full = range(0, Nled) + +# define colors for LEDs +red = (255, 0, 0) +green = (0, 255, 0) +blue = (0, 0, 255) +orange = (220, 35, 0) +purple = (255, 0, 255) +white = (255, 255, 255) +off = (0, 0, 0) + +# color the leds +def paint(color = white, section = full, single = False): + if single: + section = [section] + for i in section: + if i not in full: + i %= Nled + np[i] = color + np.write() + +# get rainbow colors +# Input a value 0 to 255 to get a color value. +# The colours are a transition r - g - b - back to r. +def wheel(pos): + + if pos < 0 or pos > 255: + return (0, 0, 0) + if pos < 85: + return (255 - pos * 3, pos * 3, 0) + if pos < 170: + pos -= 85 + return (0, 255 - pos * 3, pos * 3) + pos -= 170 + return (pos * 3, 0, 255 - pos * 3) + +# cycle rainbow colors +def rainbow_cycle(wait): + global button_pressed + n = Nled + while not button_pressed: + for j in range(255): + for i in range(n): + rc_index = (i * 256 // n) + j + np[i] = wheel(rc_index & 255) + np.write() + if button_pressed: + break + time.sleep_ms(wait) + button_pressed = False + +# pulsating rainbow +def solid_rainbow(wait): + global button_pressed + while not button_pressed: + for j in range(255): + paint(wheel(j)) + if button_pressed: + break + time.sleep_ms(wait) + button_pressed = False + +# just a single color +def solid_color(color): + global button_pressed + paint(color) + while not button_pressed: + time.sleep_ms(100) + button_pressed = False + +# single led rotating +def chase_single(color, wait): + global button_pressed + while not button_pressed: + for i in full: + paint(color, i, True) + paint(off, i - 1, True) + if button_pressed: + break + time.sleep_ms(wait) + button_pressed = False + +while True: + rainbow_cycle(5) + solid_rainbow(200) + chase_single(orange, 50) + chase_single(white, 50) + chase_single(purple, 50) + solid_color(red) + solid_color(green) + solid_color(blue) + + diff --git a/wifi_config.py.default b/wifi_config.py.default new file mode 100644 index 0000000..fa8a9c9 --- /dev/null +++ b/wifi_config.py.default @@ -0,0 +1,10 @@ +# Dict of WiFi networks + +NETWORKS = { + "my_wifi": { + "ssid": "look at me", "password": "password123" + }, + "neighbor_wifi": { + "ssid": "get out", "password": "Start#123" + } +} \ No newline at end of file