from mpython import *
from bluebit import *
from machine import Timer
import network
import time
import audio
import urequests
import json
import machine
import ubinascii
import parrot
cs = None
my_wifi = wifi()
my_wifi.connectWiFi('SZ2G1', '123456789')
green = None
white = None
yellow = None
red = None
white = ["报纸。","书本。","玻璃。"]
red = ["电池。","灯管。","体温计。"]
yellow = ["纸巾。","果壳。","骨头。"]
green = ["苹果。","香蕉。","西瓜。"]
_event_changed_10 = False
tim10 = Timer(10)
def timer10_tick(_):
global _event_changed_10
if (ultrasonic.distance() <= 10):
if not _event_changed_10: _event_changed_10 = True; on_custom_event_10()
else: _event_changed_10 = False
def on_custom_event_10():
global cs
time.sleep_ms(10)
if ultrasonic.distance() <= 1: return
rgb.fill( (int(0), int(0), int(255)) )
rgb.write()
time.sleep_ms(1)
oled.fill(0)
cs = get_asr_result()
oled.DispChar(cs, 0, 32, 1)
oled.show()
if cs in red:
oled.DispChar("有害垃圾", 0, 1)
rgb.fill( (int(255), int(0), int(0)) )
rgb.write()
oled.show()
time.sleep_ms(1)
parrot.set_speed(parrot.MOTOR_1, 100)
time.sleep_ms(300)
parrot.set_speed(parrot.MOTOR_1, 0)
if cs in white:
oled.DispChar("可回收垃圾", 0, 1)
rgb.fill( (int(0), int(0), int(0)) )
rgb.write()
oled.show()
time.sleep_ms(1)
parrot.set_speed(parrot.MOTOR_1, 100)
time.sleep_ms(300)
parrot.set_speed(parrot.MOTOR_1, 0)
if cs in green:
oled.DispChar("湿垃圾", 0, 1)
rgb.fill( (int(0),int(225),int(0)) )
rgb.write()
oled.show()
time.sleep_ms(1)
parrot.set_speed(parrot.MOTOR_1, 100)
time.sleep_ms(300)
parrot.set_speed(parrot.MOTOR_1, 0)
if cs in yellow:
oled.DispChar('干垃圾', 0, 1)
rgb.fill( (int(225),int(225),int(0)) )
rgb.write()
oled.show()
time.sleep_ms(1)
parrot.set_speed(parrot.MOTOR_1, 100)
time.sleep_ms(300)
parrot.set_speed(parrot.MOTOR_1, 0)
def get_asr_result():
audio.recorder_init()
audio.record("temp.wav", 2)
audio.recorder_deinit()
_response = urequests.post("http://203.195.229.133:8895/v1/deviceai/asr",
files={"file":("temp.wav", "audio/wav")},
params={"appid":"1", "mediatype":"2", "deviceid":ubinascii.hexlify(machine.unique_id()).decode().upper()})
rsp_json = _response.json()
_response.close()
if "text" in rsp_json:
return rsp_json["text"]
elif "Code" in rsp_json:
return "Code:%s" % rsp_json["Code"]
else:
return rsp_json
tim10.init(period=100, mode=Timer.PERIODIC, callback=timer10_tick)
ultrasonic = Ultrasonic()
oled.DispChar((str(my_wifi.sta.ifconfig())), 0, 0, 1)
oled.DispChar('垃圾', 0, 16, 1)
oled.show()
点赞