Set a timer - Wio Terminal
In this part of the lesson, you will call your serverless code to understand the speech, and set a timer on your Wio Terminal based off the results.
Set a timer
The text that comes back from the speech to text call needs to be sent to your serverless code to be processed by LUIS, getting back the number of seconds for the timer. This number of seconds can be used to set a timer.
Microcontrollers don't natively have support for multiple threads in Arduino, so there are no standard timer classes like you might find when coding in Python or other higher-level languages. Instead you can use timer libraries that work by measuring elapsed time in the loop
function, and calling functions when the time is up.
Task - send the text to the serverless function
-
Open the
smart-timer
project in VS Code if it is not already open. -
Open the
config.h
header file and add the URL for your function app:const char *TEXT_TO_TIMER_FUNCTION_URL = "<URL>";
Replace
<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 calledlanguage_understanding.h
. This will be used to define a class to send the recognized speech to your function app to be converted to seconds using LUIS. -
Add the following to the top of this file:
#pragma once
#include <Arduino.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include <WiFiClient.h>
#include "config.h"This includes some needed header files.
-
Define a class called
LanguageUnderstanding
, and declare an instance of this class:class LanguageUnderstanding
{
public:
private:
};
LanguageUnderstanding languageUnderstanding; -
To call your functions app, you need to declare a WiFi client. Add the following to the
private
section of the class:WiFiClient _client;
-
In the
public
section, declare a method calledGetTimerDuration
to call the functions app:int GetTimerDuration(String text)
{
} -
In the
GetTimerDuration
method, add the following code to build the JSON to be sent to the functions app:DynamicJsonDocument doc(1024);
doc["text"] = text;
String body;
serializeJson(doc, body);This coverts the text passed to the
GetTimerDuration
method into the following JSON:{
"text" : "<text>"
}where
<text>
is the text passed to the function. -
Below this, add the following code to make the functions app call:
HTTPClient httpClient;
httpClient.begin(_client, TEXT_TO_TIMER_FUNCTION_URL);
int httpResponseCode = httpClient.POST(body);This makes a POST request to the functions app, passing the JSON body and getting the response code.
-
Add the following code below this:
int seconds = 0;
if (httpResponseCode == 200)
{
String result = httpClient.getString();
Serial.println(result);
DynamicJsonDocument doc(1024);
deserializeJson(doc, result.c_str());
JsonObject obj = doc.as<JsonObject>();
seconds = obj["seconds"].as<int>();
}
else
{
Serial.print("Failed to understand text - error ");
Serial.println(httpResponseCode);
}This code checks the response code. If it is 200 (success), then the number of seconds for the time is retrieved from the response body. Otherwise an error is sent to the serial monitor and the number of seconds is set to 0.
-
Add the following code to the end of this method to close the HTTP connection and return the number of seconds:
httpClient.end();
return seconds; -
In the
main.cpp
file, include this new header:#include "speech_to_text.h"
-
On the end of the
processAudio
function, call theGetTimerDuration
method to get the timer duration:int total_seconds = languageUnderstanding.GetTimerDuration(text);
This converts the text from the call to the
SpeechToText
class into the number of seconds for the timer.
Task - set a timer
The number of seconds can be used to set a timer.
-
Add the following library dependency to the
platformio.ini
file to add a library to set a timer:contrem/arduino-timer @ 2.3.0
-
Add an include directive for this library to the
main.cpp
file:#include <arduino-timer.h>
-
Above the
processAudio
function, add the following code:auto timer = timer_create_default();
This code declares a timer called
timer
. -
Below this, add the following code:
void say(String text)
{
Serial.print("Saying ");
Serial.println(text);
}