参考案例¶
Python轻应用提供了丰富的案例及文档供开发者参考,下面是模块部分和功能实现的参考。
注意事项
本章I/II/III小节的内容暂不适用于HaaS700开发板。
I 外设¶
Python轻应用提供了针对部分外设接口的示例程序,以方便开发者参考引用。
HaaS型号 |
硬件信息 |
---|---|
HaaS 100 |
|
HaaS 200 |
|
HaaS EDU K1 |
|
HaaS 506 |
1: UART读写¶
代码¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | # coding=utf-8
# This is a sample Python script.
import utime
from driver import UART
print("-------------------uart test--------------------")
print("-----How to test: haas100 connect PIN10 and PIN12-------")
print("-----How to test: haaseduk1 connect PIN19 and PIN21-------")
uart = UART();
uart.open("serial2")
utime.sleep_ms(1000)
writeBuf = bytearray([0x41, 0x42, 0x43, 0x44]);
for i in range(10):
uart.write(writeBuf)
utime.sleep_ms(1000)
readBuf = bytearray(4)
recvSize = uart.read(readBuf)
utime.sleep_ms(100)
print(recvSize)
print(readBuf)
uart.close()
|
2: 延时和时间¶
代码¶
1 2 3 4 5 6 7 | import utime
utime.sleep(1) # sleep for 1 second 延时1s
utime.sleep_ms(500) # sleep for 500 milliseconds 延时500ms
utime.sleep_us(10) # sleep for 10 microseconds 延时10us
start = utime.ticks_ms() # get value of millisecond counter 获取毫秒计数器的值
delta = utime.ticks_diff(utime.ticks_ms(), start) # compute time difference 计算时间差
|
3: LED 发光二极管¶
代码¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | # coding=utf-8
# This is a sample Python script.
import utime
from driver import GPIO
print("start led test")
gpio = GPIO()
leds=("led_r", "led_g", "led_b")
for i in range(5):
for led in leds:
gpio.open(led)
gpio.write(1)
utime.sleep_ms(200)
gpio.write(0)
utime.sleep_ms(200)
gpio.write(1)
utime.sleep_ms(200)
gpio.close()
print("end led test")
|
4: 管脚和GPIO¶
代码¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | # coding=utf-8
# This is a sample Python script.
import utime
from driver import GPIO
print("start gpio test")
def cb(obj):
print("gpio callback test")
print(obj)
gpio = GPIO()
gpio.open("GPIO23")
gpio.enableIrq(cb)
utime.sleep_ms(200)
gpio.disableIrq(cb)
gpio.clearIrq(cb)
gpio.close()
print("end gpio test")
|
5: PWM脉宽调制¶
代码¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | # coding=utf-8
# This is a sample Python script.
from driver import PWM
print("------------haas eduk1 无对外的PWM接口---------")
print("-------------------pwm test--------------------")
pwm = PWM()
pwm.open("pwm3")
duty = pwm.getDuty()
freq = pwm.getFreq()
print(duty)
print(freq)
pwm.setConfig(3250000, 0.5)
duty = pwm.getDuty()
freq = pwm.getFreq()
print(duty)
print(freq)
pwm.close()
print("-------------------pwm end--------------------")
|
6: SPI总线¶
代码¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | # coding=utf-8
# This is a sample Python script.
from driver import SPI
print("-------------------spi test--------------------")
spi = SPI()
spi.open("SPI0")
readBuf = bytearray(3)
writeBuf = bytearray([0x9f])
print(writeBuf)
print(readBuf)
value = spi.sendRecv(writeBuf, readBuf)
print(value)
print(writeBuf)
print(readBuf)
spi.close()
print("-------------------spi test--------------------")
|
7: I2C总线¶
代码¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | # coding=utf-8
# This is a sample Python script.
from driver import I2C
print("-------------------i2c test--------------------")
i2c = I2C()
i2c.open("mpu6050")
regval = bytearray(1)
regval[0] = 0x5
print(regval)
ret = i2c.write(regval)
print(ret)
i2c.close()
print("-------------------i2c test--------------------")
|
8: ADC 模拟信号转数字信号¶
代码¶
1 2 3 4 5 6 7 8 9 10 11 | # coding=utf-8
# This is a sample Python script.
from driver import ADC
print("-------------------start adc test--------------------")
adc = ADC()
adc.open("ADC2")
value = adc.read()
print(value)
adc.close()
print("-------------------end adc test--------------------")
|
II AI/多媒体¶
Python轻应用提供了针对部分AI场景的应用案例,以方便开发者参考引用。开发者下载 AliOS-Things 后就可以按照下面的步骤编译验证了。
1: 目标检测¶
基于Python轻应用识别一张给定的物体图片,需要先将图片上传到OSS服务器并获取到OSS返回的URL,然后用URL请求视觉智能云平台执行物体检测,视觉智能平台完成物体检测以后会返回结果,结果中包含置信度、物体位置、物体类别等信息.
代码¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | from minicv import ML
print("-------------------Welcome HaasAI MicroPython--------------------")
print("-----ml ucloud ObjectDet demo start-----")
OSS_ACCESS_KEY = "xxxx"
OSS_ACCESS_SECRET = "xxxx"
OSS_ENDPOINT = "xxxx"
OSS_BUCKET = "xxxx"
ml = ML()
ml.open(ml.ML_ENGINE_CLOUD)
ml.config(OSS_ACCESS_KEY, OSS_ACCESS_SECRET, OSS_ENDPOINT, OSS_BUCKET, "NULL")
ml.setInputData("/data/python-apps/ml/object-detect/res/test.jpg")
ml.loadNet("ObjectDet")
ml.predict()
responses_value = bytearray(10)
ml.getPredictResponses(responses_value)
print(responses_value)
ml.unLoadNet()
ml.close()
print("-----ml ucloud ObjectDet demo end-----")
|
运行验证¶
将代码存储成文件放到sdcard中执行,或者执行系统中预存储的python文件,需要连接wifi
python /data/python-apps/wifi/main.py wifi_ssid wifi_password
python /data/python-apps/ml/object-detect/main.py
2: 表情识别¶
基于Python轻应用识别一张给定的照片中的人脸表情,需要先将图片上传到OSS服务器并获取到OSS返回的URL,然后用URL请求视觉智能云平台执行表情识别,视觉智能平台完成表情识别以后会返回结果,结果中包含置信度、人脸位置、表情描述等信息.
代码¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | from minicv import ML
print("-------------------Welcome HaasAI MicroPython--------------------")
print("-----ml ucloud RecognizeExpression demo start-----")
OSS_ACCESS_KEY = "xxxx"
OSS_ACCESS_SECRET = "xxxx"
OSS_ENDPOINT = "xxxx"
OSS_BUCKET = "xxxx"
ml = ML()
ml.open(ml.ML_ENGINE_CLOUD)
ml.config(OSS_ACCESS_KEY, OSS_ACCESS_SECRET, OSS_ENDPOINT, OSS_BUCKET, "NULL")
ml.setInputData("/data/python-apps/ml/recognize-expression/res/test.jpg")
ml.loadNet("RecognizeExpression")
ml.predict()
responses_value = bytearray(10)
ml.getPredictResponses(responses_value)
print(responses_value)
ml.unLoadNet()
ml.close()
print("-----ml ucloud RecognizeExpression demo end-----")
|
运行验证¶
将代码存储成文件放到sdcard中执行,或者执行系统中预存储的python文件,需要连接wifi
python /data/python-apps/wifi/main.py wifi_ssid wifi_password
python /data/python-apps/ml/recognize-expression/main.py
3: 文字识别¶
基于Python轻应用识别一张给定的照片中的文字,需要先将图片上传到OSS服务器并获取到OSS返回的URL,然后用URL请求视觉智能云平台执行文字识别,视觉智能平台完成文字识别以后会返回结果,结果中包含置信度、文字位置、文字内容等信息.
代码¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | from minicv import ML
print("-------------------Welcome HaasAI MicroPython--------------------")
print("-----ml ucloud RecognizeCharacter demo start-----")
OSS_ACCESS_KEY = "xxxx"
OSS_ACCESS_SECRET = "xxxx"
OSS_ENDPOINT = "xxxx"
OSS_BUCKET = "xxxx"
ml = ML()
ml.open(ml.ML_ENGINE_CLOUD)
ml.config(OSS_ACCESS_KEY, OSS_ACCESS_SECRET, OSS_ENDPOINT, OSS_BUCKET, "NULL")
ml.setInputData("/data/python-apps/ml/recognize-character/res/test.jpg")
ml.loadNet("RecognizeCharacter")
ml.predict()
responses_value = bytearray(10)
ml.getPredictResponses(responses_value)
print(responses_value)
ml.unLoadNet()
ml.close()
print("-----ml ucloud RecognizeCharacter demo end-----")
|
运行验证¶
将代码存储成文件放到sdcard中执行,或者执行系统中预存储的python文件,需要连接wifi
netmgr_example;netmgr -t wifi -c wifi_ssid wifi_password
python /data/python-apps/ml/recognize-character/main.py
III 云端连接/控制¶
Python轻应用提供了同阿里云物联网平台连接和控制的接口,让开发者可以轻松实现数据上云,以及通过云端控制和调用设备端硬件接口和软件服务的能力。为了让开发者更快的上手阿里云物联网平台,在csdn上,我们提供了使用Python轻应用连接物联网平台并控制设备led的详细案例。
<<HaaS100使用Python连接物联网平台并控制设备硬件>>
上述文章中中实现的功能如下:
连接物联网平台
等待云端控制消息,并实现设备控制
文章代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | # -*- coding: UTF-8 -*-
import iot
import utime as time
import netmgr as nm
from driver import GPIO
import ujson
# 请替换物联网平台申请到的产品和设备信息,可以参考文章:https://blog.csdn.net/HaaSTech/article/details/114360517
# wifi 状态标志位
wifi_connected = False
# wifi热点信息
ssid = 'xxxx'
pwd = 'xxxx'
# 物联网平台连接标志位
on_connected = False
# 三元组信息
productKey = "xxxx"
deviceName = "xxxx"
deviceSecret = "xxxx"
def on_wifi_connected(status):
global wifi_connected
wifi_connected = True
# 连接wifi网络
def connect_wifi(ssid,pwd):
global wifi_connected,on_wifi_connected
nm.init()
wifi_connected = nm.getStatus()
if not wifi_connected:
nm.register_call_back(1,on_wifi_connected)
nm.connect(ssid,pwd)
while True :
if wifi_connected:
break
else:
print('Waiting for wifi connected')
time.sleep(1)
if nm.getStatus():
print('DeviceIP:' + nm.getInfo()['IP'])
else:
print('DeviceIP:get failed')
print("ConnectWifi finished")
# 物联网平台连接成功的回调函数
def on_connect():
global on_connected
on_connected = True
# 设置props 事件接收函数(本案例是云端控制led事件)
def on_props(request):
ON = 0
OFF = 1
#服务端返回的json 转换成dict
payload = ujson.loads(request)
#获取dict 中的led 状态字段
stat = payload["LEDSwitch"]
gpio = GPIO()
# open 函数支持的参数列表定义在平台的board.json
gpio.open('led1')
if stat == 1:
gpio.write(ON)
else:
gpio.write(OFF)
time.sleep_ms(200)
gpio.close()
# 连接物联网平台
def connect_lk(productKey,deviceName,deviceSecret):
global on_connect,on_props
# 初始化linkkit sdk
key_info = {
'region' : 'cn-shanghai' ,
'productKey': productKey ,
'deviceName': deviceName ,
'deviceSecret': deviceSecret ,
'productSecret': ""
}
device = iot.Device(key_info)
device.on('connect',on_connect)
device.on('props',on_props)
# 连接物联网平台
device.connect()
# 触发linkit sdk持续处理server端信息
while(True):
# device.do_yield(200)
if on_connected:
print('物联网平台连接成功')
break
else:
time.sleep(1)
while True:
time.sleep(0.2)
# 断开连接
device.close()
if __name__ == '__main__':
connect_wifi(ssid,pwd)
connect_lk(productKey,deviceName,deviceSecret)
device.close()
|
修改上述代码中ssid 和pwd 以及三元组信息,使用IDE 推送到机器里面就可以开机自动运行