Raspberry pi pico projekt #1 - upgrade

írta: saja, 9 hónapja

A szenzort rögzítettem.

ki szereti a danette-et? :DDD

A kód is kijavításra került. Most már az aktív idő alatt is figyeli a mozgást és nullázza a visszaszámlálót. A request-ek nem zavarnak, ha hibát dobnak.

import board
import digitalio
import pwmio
import asyncio as uasyncio
import os, time, sys, ipaddress, ssl, wifi, socketpool
import adafruit_requests

greenLED_PIN = board.GP11 # Pin number for the board's built in LED.
redLED_PIN = board.GP15
blueLED_PIN = board.GP2
PIR_PIN = board.GP0 # Pin number connected to PIR sensor output wire.
TRA_PIN = board.GP16

# Setup digital input for PIR sensor:
pir = digitalio.DigitalInOut(PIR_PIN)
pir.direction = digitalio.Direction.INPUT

# setup for transistor to turn on/off the 5v output for the LED strip
tra0 = digitalio.DigitalInOut(TRA_PIN)
tra0.direction = digitalio.Direction.OUTPUT

# Setup pwm output for LEDs:
green = pwmio.PWMOut(greenLED_PIN, frequency=1000)
red = pwmio.PWMOut(redLED_PIN, frequency=1000)
blue = pwmio.PWMOut(blueLED_PIN, frequency=1000)

maxPWM = 65535

def setPercent(val):
c = 1 - val/100
return int(c * maxPWM)

green.duty_cycle = setPercent(0)
blue.duty_cycle = setPercent(0)
red.duty_cycle = setPercent(0)
tra0.value = False

async def dimmUpUntil(ledPWM, val):
percentVal = 1
currentVal = setPercent(percentVal)
while percentVal < val:
#print("increasing led value " + str(currentVal))
ledPWM.duty_cycle = currentVal
percentVal = percentVal*(1+1-int(percentVal*0.01))
currentVal = setPercent(percentVal)
await uasyncio.sleep_ms(50)
currentVal = setPercent(val)
ledPWM.duty_cycle = currentVal

async def dimmDownSlowly(ledPWM, fromVal):
percentVal = fromVal
currentVal = setPercent(percentVal)
while percentVal > 2:
#print("decreasing led value " + str(currentVal))
if pir.value:
return 1
ledPWM.duty_cycle = currentVal
percentVal = percentVal-2
currentVal = setPercent(percentVal)
await uasyncio.sleep_ms(300)
ledPWM.duty_cycle = setPercent(0)
return 0

def connect():
try:
# Connect to the WIFI, use settings.toml to configure SSID and Password
wifi.radio.connect(<SSID>, <Password>) # wifi access point neve és a hozzátartozó jelszó kell ide
except Exception as e:
# Handle connection error
# For this example we will simply print a message and exit the program
return False
pool = socketpool.SocketPool(wifi.radio)
return adafruit_requests.Session(pool, ssl.create_default_context())

async def sendRequest(url):
requests = connect()
try:
resp = requests.get(url)
print(str(resp))
resp.close()
except Exception as e:
print("Error:\n", str(e))

async def stayOn():
count = 0
while count < 230000:
count += 1000
if pir.value:
print("still moving")
count = 0
print(count)
await uasyncio.sleep_ms(1000)

async def motionStarts(old_value):
tra0.value = True
# PIR is detecting movement! Turn on LED.
turnOnBlue = uasyncio.create_task(dimmUpUntil(blue, 65))
turnOnRed = uasyncio.create_task(dimmUpUntil(red, 100))
turnOnGreen = uasyncio.create_task(dimmUpUntil(green, 95))
# Check if this is the first time movement was
# detected and print a message!
if not old_value:
uasyncio.create_task(sendRequest("http://192.168.0.170/cgi-bin/assign.py?wc=activated"))
print('Motion detected')
await uasyncio.gather(turnOnBlue, turnOnRed, turnOnGreen)
await uasyncio.gather(uasyncio.create_task(stayOn()))

async def motionEnds(old_value):
# Again check if this is the first time movement
# stopped and print a message.
if old_value:
old_value = False
print('Motion ended!')
uasyncio.create_task(sendRequest("http://192.168.0.170/cgi-bin/assign.py?wc=deactivated"))

# PIR is not detecting movement. Turn off LED.
turnOffRed = uasyncio.create_task(dimmDownSlowly(red, 100))
turnOffGreen = uasyncio.create_task(dimmDownSlowly(green, 95))
turnOffBlue = uasyncio.create_task(dimmDownSlowly(blue, 65))
listOfResults = await uasyncio.gather(turnOffRed, turnOffGreen, turnOffBlue)
if sum(listOfResults) > 0:
return old_value
tra0.value = False
print('led has been turned down')
return old_value

# Main loop that will run forever:
async def main():
old_value = pir.value
while True:
pir_value = pir.value
if pir_value:
await motionStarts(old_value)
print(" motion happened ")
#pir_value = pir.value
old_value = pir_value
else:
old_value = await motionEnds(old_value)

uasyncio.run(main())

Itt az előzmény, akit érdekel.