完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
本帖最后由 北方· 于 2016-12-13 11:39 编辑 1. 项目介绍 这个项目是用于远程温度监测的项目,通过开发板采集温度传感器的温度数据,并通过订阅MQTT topic来上传到云端账号。对应于amazon开通一个和aws lambda驱动的语音入口UI,可以对这Amazon Echo说话,fetch temperature(取得温度),Echo音箱就会按照程序的编制取得对应的温度数据并用语音播报。 这个过程采用了MQTT订阅服务,在树莓派上测试过的,这个是在米尔板子上搭建了一个python环境并移植过来的。不过,目前echo只能支持英文,不能支持中文,所以只好用来测试。 这里的温度传感器数据采用虚拟数据,没有实际采集,因为暂时手头没有合适的独立温度传感器模块,相比树莓派,这个开发板还可以直接支持ADC采集的传感器,因此选择更多,可以用adafruit_BBIO.GPIO的ADC设置来采集。 在树莓派上的程序,需要下载amazon网站生成的公共证书CA,私有证书CA,和publicCA共3个CA,才能正确执行。这个部分属于AWS开发的过程。 2、简述AWS开发
然后选择, “Create a Thing”.创建“CableTracer”. 然后创建证书, Certificate,下载所述public key, private key, 和certificate。并和这个项目关联。创建成功如下所示。 然后编制Lambda function,进入https://console.aws.amazon.com ,开始按步骤设置。 转到 https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit 逐个设置 3. 开发板编程, 3.1 云端的node.js处理程序参考如下,主要是安全验证后,进行语音的识别和转换, function onIntent(intentRequest, session )是其中的事件处理,对应于request,进行handle, function buildSpeechletResponses()时生成语音模块的应答。 var awsIot = require('aws-iot-device-sdk'); var config = require("./config"); var deviceName = "CableTracer"; var mqtt_config = { "keyPath": config.privateKey, "certPath": config.certificate, "caPath": config.rootCA, "host": config.host, "port": 8883, "clientId": "Lambda-" + deviceName, //+ "-Lambda-" + (new Date().getTime()), "region":"us-east-1", "debug":true }; var ctx = null; var client = null; // Route the incoming request based on type (LaunchRequest, IntentRequest, etc.) The JSON body of the request is provided in the event parameter. exports.handler = function (event, context) { try { console.log("event.session.application.applicationId=" + event.session.application.applicationId); ctx = context; if (event.session.application.applicationId !== app_id) { ctx.fail("Invalid Application ID"); } client = awsIot.device(mqtt_config); client.on("connect",function(){ console.log("Connected to AWS IoT"); // callback(); }); if (event.session.new) { onSessionStarted({requestId: event.request.requestId}, event.session); } if (event.request.type === "LaunchRequest") { onLaunch(event.request, event.session); } else if (event.request.type === "IntentRequest") { onIntent(event.request, event.session); } else if (event.request.type === "SessionEndedRequest") { onSessionEnded(event.request, event.session); ctx.succeed(); } } catch (e) { console.log("EXCEPTION in handler: " + e); ctx.fail("Exception: " + e); } }; /** * Called when the session starts. */ function onSessionStarted(sessionStartedRequest, session) { console.log("onSessionStarted requestId=" + sessionStartedRequest.requestId + ", sessionId=" + session.sessionId); } /** * Called when the user launches the skill without specifying what they want. */ function onLaunch(launchRequest, session, callback) { console.log("onLaunch requestId=" + launchRequest.requestId + ", sessionId=" + session.sessionId); // Dispatch to your skill's launch. getWelcomeResponse(callback); } /** * Called when the user specifies an intent for this skill. */ function onIntent(intentRequest, session ) { //, callback) { console.log("onIntent requestId=" + intentRequest.requestId + ", sessionId=" + session.sessionId); var intent = intentRequest.intent, intentName = intentRequest.intent.name; console.log("REQUEST to string =" + JSON.stringify(intentRequest)); var callback = null; // Dispatch to your skill's intent handlers if ("Turn" === intentName) { doTurnIntent(intent, session); } else if ("Fetch" === intentName) { doFetchIntent(intent, session); } else if ("HelpIntent" === intentName) { getWelcomeResponse(); } else { throw "Invalid intent"; } } /** * Called when the user ends the session. * Is not called when the skill returns shouldEndSession=true. */ function onSessionEnded(sessionEndedRequest, session) { console.log("onSessionEnded requestId=" + sessionEndedRequest.requestId + ", sessionId=" + session.sessionId); // Add cleanup logic here } // --------------- Functions that control the skill's behavior ----------------------- function getWelcomeResponse() { // If we wanted to initialize the session to have some attributes we could add those here. var sessionAttributes = {}; var cardTitle = "Welcome"; var speechOutput = "Welcome to the Cable Tracer . "; var shouldEndSession = false; ctx.succeed(buildResponse(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession))); } /** * handles GO intent. */ function doTurnIntent(intent, session, callback) { // OnGoing code for Turn based on // mqttPublish() The codes send data to thingShadow, connection AWS IoT } function doFetchIntent(intent, session, callback) { // OnGoing code for Turn based on // mqttPublish() The codes send data to thingShadow, connection AWS IoT } function mqttPublish(intent, sessionAttributes, cardTitle, speechOutput, repromptText, shouldEndSession) { var strIntent = JSON.stringify(intent); console.log("mqttPublish: INTENT text = " + strIntent); // client.publish("cabletracer", strIntent, false); client.publish(config.topic, strIntent, false); client.end(); client.on("close", (function () { console.log("MQTT CLIENT CLOSE - thinks it's done, successfully. "); ctx.succeed(buildResponse(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession))); })); client.on("error", (function (err, granted) { console.log("MQTT CLIENT ERROR!! " + err); })); } // --------------- Helpers that build all of the responses ----------------------- function buildSpeechletResponse(title, output, repromptText, shouldEndSession) { return { outputSpeech: { type: "PlainText", text: output }, card: { type: "Simple", title: title, content: output }, reprompt: { outputSpeech: { type: "PlainText", text: repromptText } }, shouldEndSession: shouldEndSession } } function buildResponse(sessionAttributes, speechletResponse) { return { version: "1.0", sessionAttributes: sessionAttributes, response: speechletResponse } }"""python C 3.2 实际的开发板上的程序,命名为aws.py,运行时为 python aws.py则启动了本程序。本ASK没有通过amazon的认证,所以还没有发布到amazon app store,只能在用本账户登录的Echo音箱,或者echosim.io网站上模拟测试,是语音交互的方式。 python语言交互性较好,不需要更多注释,可以直接读懂, 其中, def do_fetch(voltage, current, temperature): 是其中关键的读取GPIO的函数,读出的数据作为publish的信息发布到MQTT的云端去。 ablot_cert,[size=1.4] """python Cable Tracer running on Raspberry Pi""" import adafruit_BBIO.GPIO as GPIO import paho.mqtt.client as mqtt import json, math, time, sys, ssl import logging, logging.handlers, traceback cert_path = "/Home/certs/" host = "/a1enbuzxyq4gt0.iot.us-east-1.amazonaws.com" topic = "$aws/things/CableTracer/shadow/update" root_cert = cert_path + "184db63325-public.pem" cert_file = cert_path + "184db63325-certificate.pem.crt" key_file = cert_path + "184db63325-private.pem.key" globalmessage = "" # to send status back to MQTT -- voltage, current, temperature. api = local_connect() logger = logging.getLogger('cabletracer') def do_turn(data): # {"name":"TurnIntent","slots":{"Onoff":{"name":"Onoff","value":"on"}}} Onoff = str(data["slots"]["Onoff"]["value"]) logger.info("Onoff = " + onoff) global globalmessage if Onoff == "on": globalmessage = "Turn On" print globalmessage cabletracer_onoff = TRUE elif task == "off": globalmessage = "Turn Off" print globalmessage cabletracer_onoff = FALSE def do_fetch(voltage, current, temperature): """ Seting Pin in analogIn and read data with analogue shield, since GPIO in Raspberry can only read digital LOW and digital HIGH """ voltage = 1.0 current = 1.0 temperature =20 # voltage = GPIO.analogIn(9) # current = GPIO.analogIn(10) # temperature = rpi.analogIn(11) return voltage, current, temperature def logger_init(): logger.setLevel(logging.DEBUG) log_file_size = 1024 * 1024 * 1 # 1 MB formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(process)d - %(name)s : %(message)s') fh = logging.handlers.RotatingFileHandler('logs/echodronectl.log', maxBytes=log_file_size, backupCount=5) fh.setFormatter(formatter) sh = logging.StreamHandler(sys.stdout) sh.setFormatter(formatter) logger.addHandler(fh) logger.addHandler(sh) logger.info('******************************************') logger.info('Starting up...') logger_init() logger.info('setting up mqtt client') client = mqtt.Client(client_id="echodronectl.py") logger.info('completed setting up mqtt client') client.on_connect = on_connect client.on_message = on_message client.on_log = on_log client.tls_set(root_cert, certfile = cert_file, keyfile = key_file, tls_version=ssl.PROTOCOL_TLSv1_2, ciphers=None) client.connect(host, 8883, 60) #client.loop_forever() run = True while run: client.loop() do_fetch() do_turn() time.sleep(1) try: mypayload = '''{ "Voltage": %s, "Current": %s, "Temperature": %s }''' % (voltage, current, temperature,cabletracer_onoff, globalmessage) client.publish(topic, mypayload) except (TypeError): pass 4、关于AWS开发是相对比较复杂的,对于其他的云端服务就相对容易得多。用简单API就可以实现,还可以在后台看到数据的历史记录。如果有时间,可以作为额外的对比内容。 |
|
相关推荐
4 个讨论
|
|
只有小组成员才能发言,加入小组>>
【米尔-紫光PG2L100H国产FPGA开发板试用】开箱评测!米尔电子PG2L100H开发板深度体验报告
871 浏览 0 评论
【米尔-Xilinx XC7A100T FPGA开发板试用】+04.SFP之Aurora测试(zmj)
654 浏览 0 评论
【米尔-Xilinx XC7A100T FPGA开发板试用】+03.SFP光口测试(zmj)
590 浏览 1 评论
【米尔-Xilinx XC7A100T FPGA开发板试用】+01.开箱(zmj)
689 浏览 0 评论
【米尔-紫光PG2L100H国产FPGA开发板试用】米尔-紫光PG2L100H国产FPGA开发板开箱评测
685 浏览 0 评论
【米尔-瑞米派兼容树莓派扩展模块-试用体验】基于ROS系统的三麦轮小车自主导航
3602浏览 2评论
【米尔NXP i.MX 93开发板试用评测】5、安装Debian和排除启动故障
612浏览 2评论
【米尔NXP i.MX 93开发板试用评测】2、异构通信环境搭建和源码编译
765浏览 2评论
【米尔-瑞米派兼容树莓派扩展模块-试用体验】Free RTOS应用开发环境部署
1394浏览 1评论
【米尔-芯驰D9开发板- 国产平台试用】- 03- 外设接口测试-U盘、485总线
6804浏览 1评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-4 04:36 , Processed in 0.556098 second(s), Total 48, Slave 39 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号