最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - (sg90 does not work in loop (RaspberryPi) - Stack Overflow

programmeradmin2浏览0评论

iam using rfid to verify a card and then based on that sg90 is rotated

from dotenv import load_dotenv
import os
from Read import readID
import requests
from time import sleep
import RPi.GPIO as GPIO

load_dotenv()
room_id = os.getenv('roomID')
name, password = os.getenv('name'), os.getenv('password')
url = os.getenv('url')

GPIO.setmode(GPIO.BOARD)
GPIO.setup(18, GPIO.OUT)
pwm = GPIO.PWM(18, 50)
pwm.start(0)

def open_doors():
    pwm.ChangeDutyCycle(5)  
    sleep(0.5)  
    pwm.ChangeDutyCycle(0)  
    sleep(2)  
    pwm.ChangeDutyCycle(10) 
    sleep(0.5)
    pwm.ChangeDutyCycle(0) 

token = requests.post(url+'/login', {'name': name, 'password': password}, headers={"Content-Type": "application/x-www-form-urlencoded"}).json()['token']
headers = {
        "Authorization": f"Bearer {token}",
        "Content-Type": "application/x-www-form-urlencoded"   }

while True:
    id = readID()
    response = requests.post(url+"/log", {'employeeId': id, 'roomId': room_id}, headers=headers)
    if response.status_code == 200:
        print("Access Successful, door opening...")
        open_doors()
        sleep(2)
    else:
        print("Access Denied")
        sleep(5)

the function open_doors works fine outside the loop, but for some reason the motor does not rotate when it is inside the loop, if there are any print statements in the functions they execute but the motor wont rotate

发布评论

评论列表(0)

  1. 暂无评论