Let's make Spencer talk

Let's try something more advanced now - we're going to make Spencer generate and "say" a unique piece of dialogue every time we smack his big red button. 


Start fresh by creating a new sketch. Save it right away and name it something snazzy (I've named mine "best_code_ever").

random image

Next, we need to connect Spencer to a WiFi network.

Spencer needs internet access to "talk". The way this works is that Spencer connects to our server and sends the text it wants to say. The server then generates an audio clip using a TTS (Text To Speech) algorithm and sends it back to your Spencer. 

For connecting to the WiFi network, you'll need the "Set SSID and password" block located under the "Network" category. 

random image

Drag the block on the drawing area and enter your WiFi network's name and password.

SSID is the name of your network. 

random image

When we set the login details for the network, let's make Spencer show the "WiFi" animation. We can do this by adding this block over here: 

random image

Now you need to drag and drop the "Connect to WiFi network" block. This block initiates a connection routine.

random image

Place the block here: 

random image

Now we'll need the logic IF-THEN-ELSE block. We'll use this block to check whether the WiFi network connection was established successfully.  

random image

Take the "wifi is connected" condition from the "Network" section.

random image

Place it into the logic IF-THEN-ELSE block as a condition like this: 

random image

Let's play a specific sound sample and animation if the wifi connection is successfully established. 

I've chosen the smiling animation and a sound effect named "badum 0" because it sounded funny: 

random image

If the WiFi internet connection is not successfully established, let's play the animation and audio sample called "no WiFi".

random image

This program will proceed if Spencer successfully connects to the network. If not, Spencer will tell you that it's having trouble connecting to the WiFi network. 

Now let's add the "When button gets pressed" event block:

random image

Check whether WiFi is connected every time the button gets pressed. Do it with the same IF-THEN logic block and put "wifi is connected" as its condition: 

random image

Spencer will need a short time to generate the audio sample that we want. Let's play a loading animation while the sample is generating: 

random image

For the next step, open the category called "Speech" and find the "synthesize and say" block. 

random image

Place the block here and type in the sentence you want Spencer to say. I've typed in "Hellow world. This is my first program".

random image

When the dialogue voice sample gets generated (synthesized), it will be automatically played.

When that happens, let's make spencer's face look like he's talking by playing the animation named "talk".

When Spencer finishes talking, I'm going to play the animation called "wink".

random image

Remember to save your hard work by pressing the save button on the top left. 

Run the code and upload it to your Spencer. Every time you press Spencer's button, it should read aloud the text you have written into the "Synthesize and
say" block.

If your Spencer is having trouble connecting to the Wi-Fi network, check the network name and password and try turning your spencer off and on again.

Remember, Spencer only works with 2.4GHz (IEEE 802.11 b/g/n) WiFi access points.
5GHz (IEEE 802.11 ac) networks, unfortunately, aren't supported.  

 

If you're having trouble making your Spencer talk, reach out to us via contact@circuitmess.com, and we'll help.