参考案例¶
Python轻应用提供了丰富的案例及文档供开发者参考,下面是模块部分和功能实现的参考。
I 外设¶
Python轻应用提供了针对部分外设接口的示例程序,以方便开发者参考引用。
1: UART读写¶
代码¶
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 UART
print("-------------------uart test--------------------")
print("-----How to test: connect PIN10 and PIN12-------")
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=("led1", "led2", "led3", "led4", "led5")
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 | # coding=utf-8
# This is a sample Python script.
import utime
from driver import GPIO
print("start gpio test")
gpio = GPIO()
gpio.open(35)
gpio.write(1)
gpio.write(0)
value = gpio.read(35)
print(value)
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 | # coding=utf-8
# This is a sample Python script.
from driver import 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("pca9544")
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("ADC0")
value = adc.read()
print(value)
adc.close()
print("-------------------end adc test--------------------")
|
9: RTC 实时时钟¶
代码¶
1 2 3 4 5 6 7 8 9 10 11 12 | # coding=utf-8
# This is a sample Python script.
from driver import RTC
print("-------------------rtc test--------------------")
rtc = RTC()
rtc.open()
rtc.setTime(21, 3, 19, 18, 25, 8)
value = rtc.getTime()
print(value)
rtc.close()
print("-------------------rtc test--------------------")
|
II AI/多媒体¶
Python轻应用提供了针对部分AI场景的应用案例,以方便开发者参考引用。开发者下载 AliOS-Things 后就可以按照下面的步骤编译验证了。
需要切换到 dev_3.1.0_haas
分支
git clone https://github.com/alibaba/AliOS-Things.git -b dev_3.1.0_haas
1: 数字识别¶
通过minicv模块,完成数据源的封装处理,图⽚的解码,图⽚数据的格式转换和缩放等功能,最后将处理好的数据喂给ML 模块进⾏推理得到预期结果.
代码¶
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 | from minicv import ML
print("-------------------Welcome HaasAI MicroPython--------------------")
print("-----ml odla demo start-----")
#test data
#"/data/python/resource/odla/test0.png"
#"/data/python/resource/odla/test1.png"
#"/data/python/resource/odla/test2.png"
#"/data/python/resource/odla/test3.png"
#"/data/python/resource/odla/test4.png"
#"/data/python/resource/odla/test5.png"
#"/data/python/resource/odla/test6.png"
#"/data/python/resource/odla/test7.png"
#"/data/python/resource/odla/test8.png"
#"/data/python/resource/odla/test9.png"
ml = ML()
ml.open(ml.ML_ENGINE_ODLA)
ml.setInputData("/data/python/resource/odla/test4.png")
ml.loadNet("default")
ml.predict()
responses_value = bytearray(10)
ml.getPredictResponses(responses_value)
print(responses_value)
ml.unLoadNet()
ml.close()
print("-----ml odla demo end-----")
|
2: 人脸对比¶
通过minicv模块,完成数据源的封装处理,图⽚的解码,图⽚数据的格式转换和缩放等功能,最后将处理好的数据喂给ML 模块,ML 模块通过Alibaba Cloud SDK引擎和达摩院的视觉开放智能平台进⾏交互,得到预期结果.
代码¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | from minicv import ML
print("-------------------Welcome HaasAI MicroPython--------------------")
print("-----ml ucloud FacebodyComparing demo start-----")
OSS_ACCESS_KEY = "xxxx"
OSS_ACCESS_SECRET = "xxxx"
OSS_ENDPOINT = "xxxx"
OSS_BUCKET = "xxxx"
ML_MYFACE_PATH = "xxxx"
ml = ML()
ml.open(ml.ML_ENGINE_CLOUD)
ml.config(OSS_ACCESS_KEY, OSS_ACCESS_SECRET, OSS_ENDPOINT, OSS_BUCKET, ML_MYFACE_PATH)
ml.setInputData("/data/python/resource/capture.jpg")
ml.loadNet("FacebodyComparing")
ml.predict()
responses_value = bytearray(10)
ml.getPredictResponses(responses_value)
print(responses_value)
ml.unLoadNet()
ml.close()
print("-----ml ucloud FacebodyComparing demo end-----")
|
运行验证¶
将代码存储成文件放到sdcard中执行,或者执行系统中预存储的python文件,需要连接wifi
python /data/python/network/ConnectWifi.py wifi_ssid wifi_password
python /data/python/ml/FacebodyComparingDemo.py
3: 人物动漫化¶
通过minicv 模块,完成数据源的封装处理,图⽚的解码,图⽚数据的格式转换和缩放等功能,最后将处理好的数据喂给ML 模块,ML 模块通过Alibaba Cloud SDK引擎和达摩院的视觉开放智能平台进⾏交互,得到预期结果.
代码¶
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 AnimeStyle 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/resource/capture.jpg")
ml.loadNet("AnimeStyle")
ml.predict()
responses_value = bytearray(10)
ml.getPredictResponses(responses_value)
print(responses_value)
ml.unLoadNet()
ml.close()
print("-----ml ucloud AnimeStyle demo end-----")
|
运行验证¶
将代码存储成文件放到sdcard中执行,或者执行系统中预存储的python文件,需要连接wifi
python /data/python/network/ConnectWifi.py wifi_ssid wifi_password
python /data/python/ml/AnimeStyleDemo.py
4: 目标检测¶
通过minicv 模块,完成数据源的封装处理,图⽚的解码,图⽚数据的格式转换和缩放等功能,最后将处理好的数据喂给ML 模块,ML 模块通过Alibaba Cloud SDK引擎和达摩院的视觉开放智能平台进⾏交互,得到预期结果.
代码¶
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/resource/capture.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/network/ConnectWifi.py wifi_ssid wifi_password
python /data/python/ml/ObjectDetDemo.py
5: 表情识别¶
通过minicv 模块,完成数据源的封装处理,图⽚的解码,图⽚数据的格式转换和缩放等功能,最后将处理好的数据喂给ML 模块,ML 模块通过Alibaba Cloud SDK引擎和达摩院的视觉开放智能平台进⾏交互,得到预期结果.
代码¶
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/resource/capture.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/network/ConnectWifi.py wifi_ssid wifi_password
python /data/python/ml/RecognizeExpressionDemo.py
6: 文字识别¶
通过minicv 模块,完成数据源的封装处理,图⽚的解码,图⽚数据的格式转换和缩放等功能,最后将处理好的数据喂给ML 模块,ML 模块通过Alibaba Cloud SDK引擎和达摩院的视觉开放智能平台进⾏交互,得到预期结果.
代码¶
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/resource/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
python /data/python/network/ConnectWifi.py wifi_ssid wifi_password
python /data/python/ml/RecognizeCharacter.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 | # -*- coding: UTF-8 -*-
import iot
import utime
# 请替换物联网平台申请到的产品和设备信息,可以参考文章:https://blog.csdn.net/HaaSTech/article/details/114360517
productKey = "a1uTFk4xjko"
productSecret = "xxxxxxx"
deviceName = "mpy_001"
deviceSecret = "xxxxxxxxxxxxxxx"
# 初始化linkkit sdk
key_info = {
'region' : 'cn-shanghai' ,
'productKey': productKey ,
'deviceName': deviceName ,
'deviceSecret': deviceSecret ,
'productSecret': productSecret
}
device = iot.Device(key_info)
# 物联网平台连接成功的回调函数
def on_connect():
print('linkkit is connected ')
device.on('connect',on_connect)
# 设置props 事件接收函数(本案例是云端控制led事件)
def on_props(request):
from driver import GPIO
import ujson
ON = 0
OFF = 1
#服务端返回的json 转换成dict
payload = ujson.loads(request)
#获取dict 中的led 状态字段
stat = payload["LEDSwitch"]
gpio = GPIO()
# open 函数支持的参数列表如下:
# led1;led2;led3;led4;led5
gpio.open('led1')
if stat == 1:
gpio.write(ON)
else:
gpio.write(OFF)
utime.sleep_ms(200)
gpio.close()
device.on('props',on_props)
# 连接物联网平台
device.connect()
# 触发linkit sdk持续处理server端信息
while(True):
device.do_yield(200)
# 断开连接
device.close()
|
修改上述代码中物联网相关key信息,将修改过后的代码保存到sdcard根目录的linkkit_test.py 文件中,机器插入sdcard并重启,运行demo之前确保机器已经联网成功。
联网指令:
python /data/python/network/ConnectWifi.py "SSID" "PassWord"
运行指令:
python /sdcard/linkkit_test.py