设置计时器 - Wio Terminal 在这节课的这一部分,你将调用你的无服务器代码来理解语音,并根据识别结果在Wio终端上设置一个计时器。 设置计时器 从语音到文本的调用返回的文本需要发送到你的无服务器代码中,通过LUIS进行处理,获取用于计时器的秒数。这个秒数可以用来设置计时器。 微控制器在Arduino中没有原生支持多线程,因此不像使用Python或其他高级语言那样有标准的计时器类。相反,你可以使用通过测量在 loop 函数中经过的时间并调用函数来实现的计时器库,当时间到了就调用函数。 任务 - 将文本发送到无服务器函数 如果尚未打开,请在VS Code中打开 smart-timer 项目。 打开 config.
在这节课的这一部分,你将调用你的无服务器代码来理解语音,并根据识别结果在Wio终端上设置一个计时器。
从语音到文本的调用返回的文本需要发送到你的无服务器代码中,通过LUIS进行处理,获取用于计时器的秒数。这个秒数可以用来设置计时器。
微控制器在Arduino中没有原生支持多线程,因此不像使用Python或其他高级语言那样有标准的计时器类。相反,你可以使用通过测量在loop函数中经过的时间并调用函数来实现的计时器库,当时间到了就调用函数。
如果尚未打开,请在VS Code中打开smart-timer项目。
打开config.h头文件,并添加你的函数应用的URL:
#define FUNCTION_APP_URL "<URL>"
替换<URL> with the URL for your function app that you obtained in the last step of the last lesson, pointing to the IP address of your local machine that is running the function app.
Create a new file in the src folder called language_understanding.h。这将用于定义一个类,将识别的语音发送到你的无服务器函数应用,并通过LUIS将其转换为秒数。
在此文件顶部添加以下内容:
#include <ArduinoHttpClient.h> #include <ArduinoJson.h>
这包括一些必要的头文件。
定义一个名为LanguageUnderstanding的类,并声明该类的一个实例:
class LanguageUnderstanding { public: int GetTimerDuration(String text); private: WiFiClient client; };
要调用你的函数应用,你需要声明一个WiFi客户端。在类的private部分添加以下内容:
WiFiClient client;
在GetTimerDuration方法中,添加以下代码以构建要发送到函数应用的JSON:
String payload; StaticJsonDocument<200> doc; doc["text"] = text; serializeJson(doc, payload);
这将把传递给GetTimerDuration方法的文本转换成以下JSON:
{ "text": "<text>" }
其中<text>是传递给该函数的文本。
在这之下,添加以下代码以进行函数应用调用:
HttpClient http(client); http.begin(FUNCTION_APP_URL); http.addHeader("Content-Type", "application/json"); int httpResponseCode = http.POST(payload);
这将向函数应用发出POST请求,传递JSON主体并获取响应代码。
在这之后添加以下代码:
if (httpResponseCode == 200) { String response = http.getString(); StaticJsonDocument<200> doc; deserializeJson(doc, response); return doc["seconds"]; } else { Serial.println("Error: " + String(httpResponseCode)); return 0; }
这段代码检查响应代码。如果它是200(成功),则从响应体中检索出计时器的秒数。否则,向串行监视器发送错误信息,并将秒数设置为0。
在该方法末尾添加以下代码以关闭HTTP连接并返回秒数:
http.end(); return seconds;
在main.cpp文件中,包含这个新头文件:
#include "language_understanding.h"
在processAudio function, call the GetTimerDuration方法中,以获取计时器的持续时间:
int duration = languageUnderstanding.GetTimerDuration(text);
这将把从SpeechToText class into the number of seconds for the timer.
The number of seconds can be used to set a timer.
Add the following library dependency to the platformio.ini文件中添加一个库以设置计时器:
[env:wio_terminal] platform = atmelsam board = wio_terminal framework = arduino lib_deps = ArduinoJson ArduinoHttpClient
在main.cpp文件中添加对这个库的包含指令:
#include <TimerOne.h>
在processAudio函数上方添加以下代码:
TimerOne timer;
在这之后,添加以下代码:
void timerExpired() { Serial.println(end_message); timer.stop(); }
这是一个回调函数,当计时器到期时会被调用。它会传递一个消息来表示计时器到期的时间。计时器可以重复,这可以通过此回调的返回值来控制——这里返回false, to tell the timer to not run again.
Add the following code to the end of the processAudio函数中:
if (duration == 0) { return; } int minutes = duration / 60; int seconds = duration % 60;
在这段代码下方,添加以下内容以创建一条消息表示计时器开始:
String start_message = "Timer started for " + String(minutes) + " minutes and " + String(seconds) + " seconds."; Serial.println(start_message);
在这之后,添加类似的代码以创建一条消息表示计时器已到期:
String end_message = "Timer expired after " + String(minutes) + " minutes and " + String(seconds) + " seconds.";
在这之后,说出计时器开始的消息:
Serial.println(start_message);
在该函数末尾,启动计时器:
timer.start(duration * 1000);
这将触发计时器。计时器设置使用毫秒,所以总秒数乘以1,000转换为毫秒。timerExpired function is passed as the callback, and the end_message is passed as an argument to pass to the callback. This callback only takes void *参数,所以字符串被适当地转换。
最后,计时器需要“滴答”一下,这是在loop函数中完成的。在loop函数末尾添加以下代码:
void loop() { // 其他代码 timer.update(); }
构建这段代码,上传到你的Wio终端并通过串行监视器测试。一旦你在串行监视器中看到Ready,按下C按钮(最左侧的按钮,靠近电源开关),然后说话。4秒的音频将被捕捉、转换为文本,然后发送到你的无服务器函数应用,并设置一个计时器。确保你的无服务器函数应用正在本地运行。
你会看到计时器何时开始以及何时结束。
--- Available filters and text transformations: colorize, debug, default, direct, hexlify, log2file, nocontrol, printable, send_on_enter, time --- More details at http://bit.ly/pio-monitor-filters --- Miniterm on /dev/cu.usbmodem1101 9600,8,N,1 --- --- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H --- Connecting to WiFi.. Connected! Got access token. Ready. Starting recording... Finished recording Sending speech... Speech sent! {"RecognitionStatus":"Success","DisplayText":"Set a 2 minute and 27 second timer.","Offset":4700000,"Duration":35300000} Set a 2 minute and 27 second timer. {"seconds": 147} 2 minute 27 second timer started. Times up on your 2 minute 27 second timer.
你可以在code-timer/wio-terminal文件夹中找到这段代码。
你的计时器程序成功了!
声明:
本文件灏天文库团队进行了翻译。尽管我们力求准确,但请注意,翻译可能包含错误或不准确之处。原文档以其原始语言为准。我们不对因使用此翻译而产生的任何误解或误译负责。