You should already have the DHT-11 library downloaded from the example in Chapter 9, Environment Sensors, but you will need to download the MOVI library. If you go to the library manager and do a search for Movi, you will find several libraries that match that term. Look for the MOVI Voice Dialog Shield library by Audeme LLC and download it.
We will begin the sketch by including both the MOVI and the DHT libraries. The following code shows how to include both:
#include <DHT.h> #include <MOVIShield.h>
Next, we will define the DHT pin/type and create an instance of the DHT type as we did in Chapter 9, Environment Sensors:
#define DHT_PIN 3 #define DHT_TYPE DHT11 DHT dht(DHT_PIN, DHT_TYPE);
Now we will want to create an instance of the MOVI type as shown in the following line of code. The Boolean false value indicates that we do not want serial debugging turned on:
MOVI movi(false);
Finally, we will need a character array that will be used to create the sentence that contains the current temperature so the MOVI shield can tell us the temperature when we ask for it.
char answer[21];
Within the setup() function we will need to initialize both the DHT temperature sensor and also the MOVI shield. The following code shows the setup() function:
void setup() { dht.begin(); movi.init(); movi.callSign("buddy"); movi.addSentence("temp"); movi.train(); }
This function starts off by initializing the DHT temperature sensor by calling the begin() function from the dht type. Next, we initiate the movi type by calling the init() function. This function must be called first to initialize the movi type.
Most voice-activated devices like Amazon Echo are activated by a call sign. For Amazon's devices, the call sign is "Alexa." In our example, the MOVI shield will also use a call sign that will activate it. A call sign can be set up using the callSign() method, where the call sign to use is passed in as a string. In this example, the call sign will be "buddy".
Next, we will want to add a sentence or words that the MOVI will match. We do this with the addSentence() function. For this example, we will try to match the word "temp." We do have the option of training the MOVI shield with complete sentences or words. If you wish for the MOVI shield to recognize a sentence, it is recommended to add the complete sentence even if you need to add multiple versions of the same sentence. By adding the complete sentence, the MOVI's algorithm can be used to identify the sentence. This gives greater accuracy. It is also recommended that all trained sentences be close to the same size. A single long sentence will be favored over much smaller ones if a lot of words are spoken.
Finally, the train() method is called to tell the MOVI shield that we have added all of the sentences and the call sign. The first time you add a sentence or call sign the MOVI shield will take some time to train, but if the call sign and sentence stay the same between builds of your application, then the MOVI shield will start up very quickly.
Now that the setup() function is complete, let's look at the loop() function. The following code shows the loop() function:
void loop() { signed int res=movi.poll(); if (res == 1) { float fahreheit = dht.readTemperature(true); int tmp = (int)fahreheit; sprintf(answer, "The temperature is %02d", tmp); movi.say(answer); } }
In the first line, we use the poll() function from the movi instance. This function will poll for any match to the trained sentences. This function would return zero (0) if no event happened or a positive number if it matches a sentence. The number that is returned is the number of the sentence that it matched. In our example, we only have one sentence. Therefore, the only possible match is to sentence number 1.
If a match to the sentence is found, the current temperature is read from the DHT-11 temperature sensor and then it is converted from a float value to an integer value by typecasting.
To construct the string that we wish for the MOVI shield to say, the sprintf() function is used. This function can be used to construct a character array. In this example, we start with the sentence The temperature is and then add the temperature value using the %02d format. This tells the sprintf() function to add a two-digit integer to the string. The character array that is created is stored in the answer array that was created at the beginning of this sketch.
We use the say() function from the movi instance to have the MOVI shield tell us the current temperature through the connected headphones or speaker.
Now let's run the project.