5.32 综合案例2.0 - TTS语音云播报(支持M320开发板)

news/2025/3/5 1:04:31/

HaaS506 - TTS语音云播报

  • 简介
  • 准备
  • 硬件接口
  • 代码流程
  • 功能实现
    • 1、物联网平台开发
    • 2、设备端开发
        • 代码
        • 调试
    • 3.应用平台开发
        • 3.1新建‘普通项目’
        • 3.2关联产品和设备
        • 3.3新建移动应用

简介

用python开发板写的云喇叭,文字转语音,附教程源码

手机端发送文字,开发板发出对应语音信息。

  • 本案例需要使用到阿里云平台连接网络。通过阿里IOT studio应用开发移动应用,手机端发送文字信息,文字信息通过阿里云下发给开发板,开发板播放对应文字语音。

准备

本案例需要的硬件

器材数量
HaaS506开发板1
喇叭1
SIM卡1
4G天线1

硬件接口

在这里插入图片描述
在这里插入图片描述

代码流程

1、连接阿里云平台。
2、接收云平台下发物模型信息。
3、根据物模型信息播放语音

功能实现

1、物联网平台开发

第一次使用物联网平台的读者,需要开通实例后使用物联网平台功能。也可以使用免费的公共实例进行开发,在阿里云物联网平台中,左上角选择‘华东2-上海’,点击‘公共实例’,即可开通。

1、平台产品创建可参考haas506 2.0开发教程-aliyunIoT

2、创建产品属性(添加物模型)

  • 选择产品功能定义编辑草稿
    在这里插入图片描述

  • 添加自定义功能

  • 设置标识符数据类型读写类型参数,标识符(light)要与代码保持一致。点击确定
    在这里插入图片描述

  • 发布上线,点击确定
    在这里插入图片描述

2、设备端开发

  • 第一次使用开发板的读者可以按照haas5062.0开发教程-导学篇搭建开发环境。
  • 搭建完后复制以下代码到Visual Studio Code,复制产品证书到代码相应位置。
    在这里插入图片描述

代码

复制代码到VS code

main.py

# coding=utf-8
import network
import ujson
import utime as time
import modem
from  aliyunIoT import Device
import ota
import kv
import TTS as tts#更改产品信息
###############################
productKey = "********"
productSecret = "*********"
###############################
global deviceName,g_connect_status,device_dyn_resigter_succed,netw
g_connect_status = False
netw = None
device = None
deviceSecret = None
device_dyn_resigter_succed = False
connect_state = False
#初始化物联网平台Device类,获取device实例
device = Device()
# 定义需要升级的模块和版本号
module_name = 'default'
app_version = '1.0.1'
# 定义升级包的下载和安装路径,其中url,hash_type和hash 会通过服务端推送被保存下来
info = {'url': '','store_path': '/data/pyamp/app.zip','install_path': '/data/pyamp/','length': 0,'hash_type': '','hash': ''
}# ota 消息推送的接受函数
def on_trigger(data):global info# 保存服务端推送的ota信息info['url'] = data['url']info['length'] = data['length']info['module_name'] = data['module_name']info['version'] = data['version']info['hash'] = data['hash']info['hash_type'] = data['hash_type']# 开始ota 包下载dl_data = {}dl_data['url'] = info['url']dl_data['store_path'] = info['store_path']ota.download(dl_data)# ota 升级包下载结果回调函数
def on_download(data):global infoif data >= 0:print('Ota download succeed')# 开始ota包校验param = {}param['length'] = info['length']param['store_path'] = info['store_path']param['hash_type'] = info['hash_type']param['hash'] = info['hash']ota.verify(param)# ota 升级包校验结果回调函数
def on_verify(data):global infoprint(data)if data >= 0 :print('Ota verify succeed')print('Start Upgrade')# 开始ota升级param = {}param['length'] = info['length']param['store_path'] = info['store_path']param['install_path'] = info['install_path']ota.upgrade(param)# ota 升级包结果回调函数
def on_upgrade(data):if data >= 0 :print('Ota succeed')#ota升完级后 重启设备reboot()def tts_play_cb(v):if v==7:print('play finish...')elif v==0:print('play start...')def tts_play(tx):global tts_play_cbtts.setCallback(tts_play_cb)tts.setVolume(1)tts.setSpeed(6)ret = tts.play(2,0,1,tx)    #优先级;打断模式;模式1-UTF-8/2-GBK;字符串if(0 == ret):print('tts play success')elif(-1 == ret):print('tts play failed')while  tts.getState() == 1:time.sleep_ms(200)print('-----------')#当iot设备连接到物联网平台的时候触发'connect' 事件
def on_connect(data):global module_name,default_ver,productKey,deviceName,deviceSecret,on_trigger,on_download,on_verify,on_upgrade,connect_stateprint('***** connect lp succeed****')data_handle = {}data_handle['device_handle'] = device.getDeviceHandle()# 初始化ota服务ota.init(data_handle)connect_state = True# ota 回调函数注册ota.on(1,on_trigger)ota.on(2,on_download)ota.on(3,on_verify)ota.on(4,on_upgrade)report_info = {"device_handle": data_handle['device_handle'],"product_key": productKey ,"device_name": deviceName ,"module_name": module_name ,"version": app_version}# 上报本机ota相关信息,上报版本信息返回以后程序返回,知道后台推送ota升级包,才会调用on_trigger函数ota.report(report_info)   txt = ''
state = 0
#当iot云端下发属性设置时,触发'props'事件
def on_props(request):global txt,stateprint('clound req data is {}'.format(request))# # # #获取消息中的params数据params=request['params']# #去除字符串的'',得到字典数据params=eval(params)if "txt" in params :txt = params["txt"]time.sleep_ms(20)if "send" in params :state = params['send']if state ==1:tts_play(txt)   #播放语音文件state = 0up_data({'send':state})#当连接断开时,触发'disconnect'事件
def on_disconnect():print('linkkit is disconnected')#当iot云端调用设备service时,触发'service'事件
def on_service(id,request):print('clound req id  is {} , req is {}'.format(id,request))
#当设备跟iot平台通信过程中遇到错误时,触发'error'事件
def on_error(err):print('err msg is {} '.format(err))#网络连接的回调函数
def on_4g_cb(args):global g_connect_statuspdp = args[0]netwk_sta = args[1]if netwk_sta == 1:g_connect_status = Trueelse:g_connect_status = False#网络连接
def connect_network():global netw,on_4g_cb,g_connect_status#NetWorkClient该类是一个单例类,实现网络管理相关的功能,包括初始化,联网,状态信息等.netw = network.NetWorkClient()g_register_network = Falseif netw._stagecode is not None and netw._stagecode == 3 and netw._subcode == 1:g_register_network = Trueelse:g_register_network = Falseif g_register_network:#注册网络连接的回调函数on(self,id,func);  1代表连接,func 回调函数  ;return 0 成功netw.on(1,on_4g_cb)netw.connect(None)else:print('网络注册失败')while True:if g_connect_status:print('网络连接成功')breaktime.sleep_ms(20)#动态注册回调函数
def on_dynreg_cb(data):global deviceSecret,device_dyn_resigter_succeddeviceSecret = datadevice_dyn_resigter_succed = True# 连接物联网平台
def dyn_register_device(productKey,productSecret,deviceName):global on_dynreg_cb,device,deviceSecret,device_dyn_resigter_succedkey = '_amp_customer_devicesecret'deviceSecretdict = kv.get(key)print("deviceSecretdict:",deviceSecretdict)if isinstance(deviceSecretdict,str):    deviceSecret = deviceSecretdict if deviceSecretdict is None or deviceSecret is None:key_info = {'productKey': productKey  ,'productSecret': productSecret ,'deviceName': deviceName}# 动态注册一个设备,获取设备的deviceSecret#下面的if防止多次注册,当前若是注册过一次了,重启设备再次注册就会卡住,if not device_dyn_resigter_succed:device.register(key_info,on_dynreg_cb)  def connect():global deviceName,g_connect_status,device_dyn_resigter_succed,connect_statedeviceName = None# 获取设备的IMEI 作为deviceName 进行动态注册deviceName = modem.info.getDevImei()# 连接网络connect_network()if deviceName is not None and len(deviceName) > 0 :#动态注册一个设备dyn_register_device(productKey,productSecret,deviceName)else:print("获取设备IMEI失败,无法进行动态注册")while deviceSecret is None:time.sleep(0.2)print('动态注册成功:' + deviceSecret)key_info = {'region' : 'cn-shanghai' ,'productKey': productKey ,'deviceName': deviceName ,'deviceSecret': deviceSecret ,'keepaliveSec': 60,}#打印设备信息print(key_info)#device.ON_CONNECT 是事件,on_connect是事件处理函数/回调函数device.on(device.ON_CONNECT,on_connect)device.on(device.ON_DISCONNECT,on_disconnect)device.on(device.ON_PROPS,on_props)device.on(device.ON_SERVICE,on_service)device.on(device.ON_ERROR,on_error)device.connect(key_info)while not connect_state:time.sleep_ms(10)print('--------------------- aliyun connect --------------------------')up_data({'txt':'请输入语音信息'})up_data({'send':state})def up_data(d):           d_str = ujson.dumps(d)data={'params':d_str}device.postProps(data)if __name__ == '__main__':connect()       #连接阿里云

调试

1、串口调试工具log打印log

POWERONREASON:0x0000,parse:NORMAL_REBOOT.
mpthread_init stack:0x80afbb5c 0x80afbb5c
device_obj->iot_device_handle:0x80b9ff00
[  11.134]<E>ACTIVATION_REPORT2 activation_report2:286 report http data:POST /device/add HTTP/1.1
Host:nps.huntercat.cn
User-Agent: AliOS-Things
Content-Length:157
Accept: */*
Content-Type:application/json
Connection: Keep-Alive{"activationStr":"V=3.0.0&P=Enginelf&A=HaaS506_App&B=HaaS506&C=M601&N=Cellular&X=HaaS506-M320&S=Cellular&O=FreeRTOS&T=DTU&M=866907051837598&Y=Alibaba-Cloud"}网络连接成功
deviceSecretdict: ********
动态注册成功:********
{'deviceName': '*******', 'deviceSecret': '********', 'region': 'cn-shanghai', 'productKey': '*******', 'keepaliveSec': 60}
[1670291375.666][LK-0313] MQTT user calls aiot_mqtt_connect api, connect
[1670291375.666][LK-0317] *********
[1670291375.666][LK-0318] *********
[1670291376.333][LK-0313] MQTT connect success in 724 ms
<I>UA uagent_ext_comm_init[63]: [uA]prepare start uagent comm <I>UA uagent_ext_comm_init[74]: [uA]Subsrcibe TOPIC /sys/****/****/_thing/service/invoke[1670291376.333][LK-0309] sub: /sys/****/****/_thing/service/invoke
<I>UA uagent_ext_comm_init[80]: [uA]IOT_MQTT_Subscribe(/sys/****/*****/_thing/service/invoke) success[1670291376.333][LK-0309] sub: /sys/****/****/_thing/service/post_reply
<I>UA uagent_ext_comm_init[89]: [uA]IOT_MQTT_Subscribe(/sys/****/****/_thing/service/post_reply) success[1670291376.333][LK-0309] pub: /sys/****/****/thing/config/log/get[LK-030A] > 7B 22 69 64 22 3A 22 31  22 2C 22 76 65 72 73 69 | {"id":"1","versi
[LK-030A] > 6F 6E 22 3A 22 31 2E 30  22 2C 22 70 61 72 61 6D | on":"1.0","param
[LK-030A] > 73 22 3A 7B 22 67 65 74  54 79 70 65 22 3A 22 63 | s":{"getType":"c
[LK-030A] > 6F 6E 74 65 6E 74 22 2C  22 63 6F 6E 66 69 67 53 | ontent","configS
[LK-030A] > 63 6F 70 65 22 3A 22 64  65 76 69 63 65 22 7D 7D | cope":"device"}}[1670291376.333][LK-0309] pub: /sys/a1laDtv9VrO/866907051837598/thing/deviceinfo/update[LK-030A] > 7B 22 69 64 22 3A 22 32  22 2C 22 76 65 72 73 69 | {"id":"2","versi
[LK-030A] > 6F 6E 22 3A 22 31 2E 30  22 2C 22 70 61 72 61 6D | on":"1.0","param
[LK-030A] > 73 22 3A 5B 7B 22 61 74  74 72 4B 65 79 22 3A 22 | s":[{"attrKey":"
[LK-030A] > 53 59 53 5F 53 44 4B 5F  4C 41 4E 47 55 41 47 45 | SYS_SDK_LANGUAGE
[LK-030A] > 22 2C 22 61 74 74 72 56  61 6C 75 65 22 3A 22 43 | ","attrValue":"C
[LK-030A] > 22 2C 22 64 6F 6D 61 69  6E 22 3A 22 53 59 53 54 | ","domain":"SYST
[LK-030A] > 45 4D 22 7D 7B 22 61 74  74 72 4B 65 79 22 3A 22 | EM"}{"attrKey":"
[LK-030A] > 53 59 53 5F 4C 50 5F 53  44 4B 5F 56 45 52 53 49 | SYS_LP_SDK_VERSI
[LK-030A] > 4F 4E 22 2C 22 61 74 74  72 56 61 6C 75 65 22 3A | ON","attrValue":
[LK-030A] > 22 61 6F 73 2D 72 2D 33  2E 30 2E 30 22 2C 22 64 | "aos-r-3.0.0","d
[LK-030A] > 6F 6D 61 69 6E 22 3A 22  53 59 53 54 45 4D 22 7D | omain":"SYSTEM"}
[LK-030A] > 7B 22 61
***** connect lp succeed****
handle 0x80b9ff00
iot_device_handle:0x80b9ff00
ota register default status cb[1670291376.444][LK-0309] sub: /ota/device/upgrade//****/****/
ota Public topic:/sys/****/****/thing/deviceinfo/updateota Public msg:{"id":"0","version":"1.0","params":[{"attrKey":"SYS_OTA_ID","attrValue":"HOTA-3.3.0-1-0-0"}],"method":"thing.deviceinfo.update"}[1670291376.444][LK-0309] pub: /sys/****/****/thing/deviceinfo/update[LK-030A] > 7B[LK-030A] > 73 22 3A 5B 7B 22 61 74  74 72 4B 65 79 22 3A 22 | s":[{"attrKey":"
[LK-030A] > 53 59 53 5F 4F 54 41 5F  49 44 22 2[LK-030A] > 33 2E 30 2D 31 2D 30 2D  30 22 7D 5D 2C 22 6D 65 | 3.0-1-0-0"}],"me
[LK-030A] > 74 68 6F 64 22 3A 22 74  68 69 6E 67 2E 64 65 76 | thod":"thing.dev
[LK-030A] > 69 63 65 69 6E 66 6F 2E  75 70 64 61 74 65 22 7D | iceinfo.update"}
[LK-030A] > 0ota rollback err1:-1ota ota init fail, ret:-1[  12.000]<E>APP_OTA customer ota init failed!ota report submode versionota Public topic:/ota/device/inform/a1laDtv9VrO/866907051837598 msg:{"id":0,"params":{"version":"amp-v2.03","module":"system"}}[167029137[LK-030A] > 2D 76 32 2E 30 33 22 2C  22 6D 6F 64 75 6C 65 22 | -v2.03","module"
[LK-030A] > 3A 22 73 79 73 74 65 6D  22 7D 7D 0ota Public topic:/ota/device/inform/a1laDtv9VrO/866907051837598 msg:{"id":0,"params":{"version":"1.0.1"}}[1670291376.555][LK-0309] pub: /ota/device/inform/a1laDtv9VrO/866907051837598[LK-030A] > 7B 22 69 64 22 3A 30 2C  22 70 61 72 61 6D 73 22 | {"id":0[LK-030A] > 3A 7B 22 76 65 72 73 69  6F 6E 22 3A 22 31 2E 30 | :{"version":"1.0
[LK-030A] > 2E 31 22 7D 7D 00
[LK-030A] < 7B 22 63 6F 64 65 22 3A  32 30 30 2C 22 64 61 74 | {"code":200,"dat
[LK-030A] < 61 22 3A 7B 22 63 6F 6E  74 65 6E[LK-030A] < 64 22 3A 22 31 22 2C 22  6D 65 74 68 6F 64 22 3A | d":"1","method":
[LK-030A] < 22 74 68 69 6E 67 2E 63  6F 6E 66 6[LK-030A] < 3A 22 31 2E 30 22 7D                             | :"1.0"}[1670291376.666][LK-1507] LOGPOST user log config arrived
user log switch state is: 0
toggle it using the switch in device detail page in https://iot.console.aliyun.com
[[LK-030A] < 73 22 2C 22 6D 65 74 68  6F 64 22 3A 22 74 68 69 | s","method":"thi
[LK-030A] < 6E 67 2E 64 65 76 69 63  65 69 6E 6[LK-030A] < 73 22 2C 22 6D 65 74 68  6F 64 22 3A 22 74 68 69 | s","method":"thi
[LK-030A] < 6E 67 2E 64 65 76 69 63  65 69 6E 66 6F 2E 75 70 | ng.deviceinfo.up
[LK-030A] < 64 61 74 65 22 2C 22 76  65 72 73 69 6F 6E 22 3A | date","version":
[LK-030A] < 2--------------------- aliyun connect --------------------------
[1670291376.666][LK-0309] pub: /sys/a1laDtv9VrO/866907051837598/thing/event/property/post[LK-030A] > 7B 22 69 64 22 3A 22 33  22 2C 22 76 65 72 73 69 | {"id":"3","versi
[LK-030A] > 6F 6E[LK-030A] > BE 93 E5 85 A5 E8 AF AD  E9 9F B3 E4 BF A1 E6 81 | ................
[LK-030A] > AF 22 7D 2C 22 73 79 73  22 3A 7B 2MicroPython f036710-dirty on 2022-09-21, 14:50:41; haas506 with SLM320
Type "help()" for more information.
>>> [1670291376.777][LK-0309] pub: /sys/a1laDtv9VrO/866907051837598/thing/event/property/post_reply[LK-030A] < 7B 22 63 6F 64 65 22 3A  32 30 30 2C 22 64 61 74 | {"code":200,"dat
[LK-030A] < 61 22 3A 7B 7D 2C 22 69  64 22 3A 22 33 22 2C 22 | a":{},"id":"3","
[LK-030A] < 6D 65 73 73 61 67 65 22  3A 22 73 75 63 63 65 73 | message":"succes
[LK-030A] < 73 22 2C 22 6D 65 74 68  6F 64 22 3A 22 74 68 69 | s","method":"thi
[LK-030A] < 6E 67 2E 65 76 65 6E 74  2E 70 72 6F 70 65 72 74 | ng.event.propert
[LK-030A] < 79 2E 70 6F 73 74 22 2C  22 76 65 72 73 69 6F 6E | y.post","version
[LK-030A] < 22 3A 22 31 2E 30 22 7D                          | ":"1.0"}[1670291376.777][LK-0A08] DM recv generic reply
[1670291376.888][LK-0309] pub: /sys/a1laDtv9VrO/866907051837598/thing/event/property/post_reply[LK-030A] < 7B 22 63 6F 64 65 22 3A  32 30 30 2C 22 64 61 74 | {"code":200,"dat
[LK-030A] < 61 22 3A 7B 7D 2C 22 69  64 22 3A 22 34 22 2C 22 | a":{},"id":"4","
[LK-030A] < 6D 65 73 73 61 67 65 22  3A 22 73 75 63 63 65 73 | message":"succes
[LK-030A] < 73 22 2C 22 6D 65 74 68  6F 64 22 3A 22 74 68 69 | s","method":"thi
[LK-030A] < 6E 67 2E 65 76 65 6E 74  2E 70 72 6F 70 65 72 74 | ng.event.propert
[LK-030A] < 79 2E 70 6F 73 74 22 2C  22 76 65 72 73 69 6F 6E | y.post","version
[LK-030A] < 22 3A 22 31 2E 30 22 7D                          | ":"1.0"}[1670291376.888][LK-0A08] DM recv generic reply

2、阿里云平台,打开实时刷新,物模型会接收到开机默认物模型数据。

在这里插入图片描述

3.应用平台开发

以下是物联网应用开发流程,接下来按以下流程介绍移动端应用的开发。

3.1新建‘普通项目’

  • 使用阿里云IoTStudio创建项目。
  • 在项目管理新建空白项目
    在这里插入图片描述
    在这里插入图片描述

3.2关联产品和设备

在这里插入图片描述
在这里插入图片描述

3.3新建移动应用

在这里插入图片描述

  • 添加组件
    在这里插入图片描述

  • 配置组件信息

  • 1,文字在这里插入图片描述

  • 2,文本框

    • 配置数据源在这里插入图片描述

    • 数据源在这里插入图片描述

    • 配置交互在这里插入图片描述

  • 配置按钮在这里插入图片描述

    • 配置交互在这里插入图片描述
  • 保存与预览
    在这里插入图片描述

  • 手机扫描二维码就可在移动端应用
    在这里插入图片描述


http://www.ppmy.cn/news/561907.html

相关文章

MongoDB University课程M320 Data Modeling 学习笔记

讲数据模型&#xff0c;课程介绍参见这里。 Chapter 1: Introduction to Data Modeling 需要具备的基础知识 MongoDB Concepts and Vocabulary Database and Collection in MongoDB Performing joins with $lookup Relational Database Concepts and Vocabulary Table (W…

学计算机的雷蛇与苹果,雷蛇做了什么 让学霸苹果也抄袭他了

【IT168 资讯】苹果总是以那种&#xff1a;永远被抄袭却从未被超越的姿态展示在世人面前&#xff0c;似乎苹果就是那种不作弊的学生&#xff0c;永远只有他被抄作业却从来没有抄袭别人。不过这种观点&#xff0c;笔者只能说图样图森破!你肯定没见过比苹果还优秀的学生。 最近网…

CVE-2019-13142:雷蛇影音软件(Razer Surround)的权限提升漏洞

软件版本&#xff1a;Razer Surround 1.1.63.0 操作系统版本&#xff1a;Windows 10 1803&#xff08;x64&#xff09; 漏洞说明&#xff1a;Razer Surround软件中文件夹设置缺陷导致的权限提升 目的 我希望这篇文章能鼓励更多的人加入漏洞研究这个看似很困难的领域。虽然这…

绕过雷蛇官网的动态验证码

大家好&#xff0c;我是dhakal_ananda&#xff0c;来自尼泊尔&#xff0c;这是我在Hackerone上参加的雷蛇漏洞悬赏项目的一部分。一开始这个漏洞悬赏项目是一个非公开项目&#xff0c;我接到邀请后并没有参加&#xff1b;后来它变成了公开项目&#xff0c;我反而对它起了兴趣。…

铸博皇御:炒黄金入门这几点别忽略

炒黄金和投资股票一样&#xff0c;也是一种高风险回报大的投资方式。但无论如何&#xff0c;新手投资黄金须谨慎&#xff0c;一定要做好风险抵抗。那么&#xff0c;炒黄金入门需要注意哪几点呢&#xff1f; 任何投资都需要遵循一定的规律&#xff0c;开始以为很简单&#xff0c…

自媒体应该怎么入门,这几点教会你

2022年了&#xff0c;才开始做自媒体还来得及吗&#xff1f;答案是一定来得及&#xff0c;现在自媒体发展相对来说是比较成熟的阶段&#xff0c;那么自媒体新手应该怎么入门&#xff0c;下面这几点教会你&#xff01; 1、了解各平台机制 每个平台的机制是不同的&#xff0c;选…

7-6 然后是几点(15 分)

7-6 然后是几点&#xff08;15 分&#xff09; 有时候人们用四位数字表示一个时间&#xff0c;比如1106表示11点零6分。现在&#xff0c;你的程序要根据起始时间和流逝的时间计算出终止时间。 读入两个数字&#xff0c;第一个数字以这样的四位数字表示当前时间&#xff0c;第二…

java定时执行(设置每天几点执行一次)

今日收获 理解并学习了如何使用定时器完成设置每天早上八点程序执行一次 可自行扩展&#xff1a; public void TimerTaskTest{ // 创建一个定时器任务TimerTask timerTask new TimerTask() {Overridepublic void run() {/*写业务逻辑主体system.out.prinln("早上…