From 8564c71dd2aaf5d28bd242f72fc4ca79e14c120c Mon Sep 17 00:00:00 2001 From: Alexander Bobkov Date: Sun, 26 Jan 2025 23:32:19 -0500 Subject: [PATCH] . --- examples/led_pwm.py | 31 ++++++++++++++++++++++++++++ examples/wireless.py | 48 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 examples/led_pwm.py create mode 100644 examples/wireless.py diff --git a/examples/led_pwm.py b/examples/led_pwm.py new file mode 100644 index 0000000..6b2add7 --- /dev/null +++ b/examples/led_pwm.py @@ -0,0 +1,31 @@ +import machine +from machine import Pin +import time, math + +ONBOARD_LED = 10 # GPIO10, PIN 7 +ONBOARD_BTN = 3 # GPIO3, 13 + +# Configure on-board LED and push button +# Stated GPIOs correspond to the wiring schematic +onboard_button = Pin(ONBOARD_BTN, Pin.IN, Pin.PULL_UP) + +led = machine.PWM(ONBOARD_LED, freq=1000) + +def pulse(l, t): + for i in range(20): + l.duty(int(math.sin(i/10 * math.pi) * 500 + 500)) + time.sleep_ms(t) + l.duty(0) + +# Interrupt function to turn LED ON when on-board button is pressed +def button_interrupt(pin): + pulse(led, 70) + + +def main(): + + # Assign interrupt to the on-board push button + onboard_button.irq(trigger=Pin.IRQ_FALLING, handler=button_interrupt) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/examples/wireless.py b/examples/wireless.py new file mode 100644 index 0000000..2eefd7a --- /dev/null +++ b/examples/wireless.py @@ -0,0 +1,48 @@ +from machine import Pin +from machine import Timer +from utime import sleep_ms + +import network + +wlan = network.WLAN(network.STA_IF) +wlan.active(True) +networks = wlan.scan() + +ONBOARD_LED = 10 # GPIO10, PIN 7 +ONBOARD_BTN = 3 # GPIO3, 13 + +# Configure on-board LED and push button +# Stated GPIOs correspond to the wiring schematic +onboard_led = Pin(ONBOARD_LED, Pin.OUT) +onboard_button = Pin(ONBOARD_BTN, Pin.IN, Pin.PULL_UP) + +# Interrupt function to alternate on-board LED state +def led_interrupt(t): + onboard_led.value(not onboard_led.value()) + +# Interrupt function to turn LED ON when on-board button is pressed +def button_interrupt(pin): + print("Button was pressed") + onboard_led.value(1) + +def main(): + onboard_led_timer = Timer(0) +# connect_wireless() + + if not wlan.isconnected(): + onboard_led_timer.init(mode=Timer.PERIODIC,period=500,callback=led_interrupt) + print("Connecting to Wi-Fi ...") + wlan.connect('IoT_bots', '208208208') + + if wlan.isconnected(): + print("Connected to Wi-Fi: " +wlan.config('ssid')) + print("Channel: ", wlan.config('channel')) + print("Security: ", wlan.config('hostname')) + print(wlan.ifconfig()) + onboard_led_timer.init(mode=Timer.PERIODIC,period=1500,callback=led_interrupt) + + # Assign interrupt to the on-board push button + onboard_button.irq(trigger=Pin.IRQ_FALLING, handler=button_interrupt) + +if __name__ == '__main__': + main()