物联网语音播报硬件使用的esp8266做的板子...主要功能是继电器控制,温度湿度检测(DS18D20和dth11),光敏,火焰检测,三色灯,加了人体感应模块做人体检测.板子的系统用的是 nodemcu (板子的程序就不在这里发出来了,以后有机会再发) 用mqtt服务器(自己搭建的家里网不太稳定)做的远程监控.
下面程序是建立mqtt客户端用以查询我们需要的内容.要用的时候才会调用(实时效率比较差) 里面用到的模块我就不一一说了
# -*- coding: utf-8 -*-
import paho.mqtt.client as mqtt
import codecs
import time
import sys
import os
f = codecs.open("E:\dioyong\mqtt1.txt", "w",'utf-8') //用以做数据保存做以后用
def on_connect(client,userdata,password, rc):
print("Connected with result code " + str(rc))
client.subscribe("10000v18d20")
client.subscribe("10000vshidu")
client.subscribe("10000vguangmei")
client.subscribe("10000vwendu")
def on_message(client, userdata, msg):
print(str(msg.topic))
print(str(msg.payload))
if str(msg.topic)=="10000vguangmei":
f.write(u'光度'+msg.payload+'\n')
elif str(msg.topic)=="10000vwendu":
f.write(u'dth11温度'+msg.payload+u'度'+'\n')
elif str(msg.topic)=="10000vshidu":
f.write(u'dth湿度'+msg.payload+'\n')
elif str(msg.topic)=="10000v18d20":
f.write(u'18d20温度'+msg.payload+u'度'+'\n')
client.disconnect()
client = mqtt.Client("4000","False")
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set("帐户","密码")
client.connect("mqtt服务器地址",mqtt服务端口,60)
client.loop_forever()
下面是语音播报出我们想要的数据.我用的是百度语音aip做的文字转语音
#encoding:utf-8
import os
import codecs
import mp3play
import time
from aip import AipSpeech
APP_ID = '填写百度给出ID'
API_KEY = '填写百度给出KEY'
SECRET_KEY = '填写百度给出SECRET_KEY '
aipSpeech = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
os.system('E:\dioyong\mqtt1.py')
file_object = open('E:\dioyong\mqtt1.txt')
try:
n = file_object.read( )
finally:
file_object.close( )
result = aipSpeech.synthesis(n, 'zh', 1, {'spd': 2,'pit': 5,
'vol': 5,'per':0,
})
# 识别正确返回语音二进制 错误则返回dict 参照下面错误码
if not isinstance(result, dict):
with open('E:\dioyong\pobao1.mp3', 'wb') as f:
f.write(result)
clip = mp3play.load('E:\dioyong\pobao1.mp3')
clip.play()
time.sleep(min(3000000, clip.seconds()))
|