from IRremote import IrReceiver
from machine import Pin,PWM,SoftI2C
from HIESP import HCSR04,SSD1306_I2C
import time
import dht
receiver = IrReceiver(14) # 接收器的引脚定义
led = Pin(26, Pin.OUT)
buzzer =PWM(Pin(5, Pin.OUT))
servo= PWM(Pin(25,Pin.OUT),freq=51)
p18=Pin(18)
sensor=dht.DHT22(p18)
oled=SoftI2C(sda=Pin(16),scl=Pin(17))
dis_play=SSD1306_I2C(128,64,oled)
tric=Pin(33,Pin.OUT)
echo=Pin(32,Pin.IN)
hc_sensor=HCSR04(tric,echo)
while True:
if receiver.decode():
print(receiver.decodedData)
if str(receiver.decodedData) == 'ff0cf3':
for i in range(0,5):
led.on()
time.sleep(0.5)
led.off()
time.sleep(0.5)
elif str(receiver.decodedData) == 'ff18e7':
buzzer.freq(200)
buzzer.duty(1)
time.sleep(5)
buzzer.duty(0)
elif str(receiver.decodedData) == 'ff5ea1':
servo.duty_u16(1638)
time.sleep(1)
servo.duty_u16(4915)
time.sleep(1)
elif str(receiver.decodedData) == 'ff08f7':
def display_context(distance):
dis_play.fill(0)
dis_play.text(str(distance),0,0)
dis_play.show()
def run():
distance=hc_sensor.getDistance()
display_context(distance)
time.sleep(1)
run()
elif str(receiver.decodedData) == 'ff1ce3':
sensor.measure()
temp=sensor.temperature()
hum=sensor.humidity()
print('当前温度:',temp)
print('当前湿度:',hum)
time.sleep(2)
else:
# 其他键:打印错误信息
print('error! please put it again')