Alexa meets NodeMCU and PlatformIO

The Thingforward team engages in all applications of IoT, including Smart Home Systems and Personal Assistant Technology, both of which are gaining popularity. We are seeing an increase in options for Personal Assistants like Amazon Alexa, Apple’s Siri, Microsoft Cortana, or Google Home to name a few. In this post, we’ll focus on Amazon’s Alexa.

Image 1: Today's Setup

Image 1: Today's Setup

Alexa is Amazon’s digital assistant built into devices such as the Amazon Echo. There are several models of Echo on the market and we’ll be using Echo Dot for our post. The Echo Dot is a great beginner device to start programming and understanding Alexa.
By following this tutorial, you will be able to design your smart home device or connect an existing device to your home network. In order to show the basics of smart home device integration, we will control the LED status with Alexa commands. In other words; make your device smarter!

Shopping list for today's blogpost:

  • Amazon Echo Dot
  • NodeMCU v2
  • Grove LED Socket
  • Alexa App on iOS/Android
  • PlatformIO Toolchain on iOS/Windows/Linux
  • Optional: Relay Module

The credits of this blogpost go to the "Her Master's Voice" article from c't Hacks/Make: magazine, January/2018 press.
The full version of the repository is here

Let's start! First of all, connect your Alexa to your home network: https://www.amazon.com/gp/help/customer/display.html?nodeId=202011800
Next, follow the directions from Alexa app.

Download the Alexa app and plug your Echo Dot into a power outlet. You will see the rotating blue circle and wait for directions.
After seeing stable orange circle, find the HotSpot of Echo called "TT1" and connect to it. Right after, open Alexa app again and find your home WLAN then login. It should be fine! Test your Echo by saying: "Alexa, what's in the news?"
You’ve now connected Alexa Echo Dot to your home network and it is ready to control smart devices!

Let's keep on going with coding our smart device, NodeMCU. Check out the repository first:

$ git clone https://github.com/emirez/iot-alexa-nodemcu.git  

On main.cpp, you should:

  • Update the SSID name and password at 5. and 6. lines
  • Update the output port on 36, 39, 48 lines
  • Update the device name on 53. line

Plug your NodeMCU and LED socket to D3 port. Upload the code to your MCU:

$ cd path/to/project  
$ pio run -t upload && pio device monitor  

--- Miniterm on /dev/cu.SLAB_USBtoUART 9600,8,N,1 ---
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
..............................
[WIFI] STATION Mode, SSID: belkin.36ef, IP address: 192.168.2.10

Keep the serial monitor open, we will follow some updates there.

  • Optional: Instead of LED, you can also connect a relay module in order to control any other device that needs higher power outlet.

NodeMCU is connected to your network and it is now discoverable by Alexa App. To do so, open the Alexa Application on your mobile device again:

Image 2: Alexa App

Image 2: Alexa App

Open the menu on top left and proceed by tapping "Smart Home":

Image 3: Alexa App Menu

Image 3: Alexa App Menu

Give the Alexa App some time to make it find your UPnP-Broadcasting device in LAN:

Image 4: Give Alexa some time to find it

Image 4: Give Alexa some time to find it

Now, you will see the device called "Mein Licht" (My Light):

Image 5: Smart Home Devices

Image 5: Smart Home Devices

Let us try to switch our LED on and off!

  • "Alexa, schalte mein Licht an" (Alexa, switch on my light)
  • "Alexa, schalte mein Licht aus" (Alexa, switch off my light)

If you want to do this in English, change the name of the device. To do so, please go to line 53 on main.cpp and update following:

fauxmo.addDevice("YOUR_DEVICE_NAME"); 

Now you can control it in English:

  • "Alexa, switch on YOUR_DEVICE_NAME"
  • "Alexa, switch off YOUR_DEVICE_NAME"

Image 6: LED is off.

Image 6: LED is off.

Image 7: LED is on.

Image 7: LED is on.

Bonus: By tapping the device name, you can control the LED status from the application.

Image 8: Controlling the LED by Tapping

Image 8: Controlling the LED by Tapping

The LED status can be also seen in serial monitor:

--- Miniterm on /dev/cu.SLAB_USBtoUART 9600,8,N,1 ---
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
..............................
[WIFI] STATION Mode, SSID: belkin.36ef, IP address: 192.168.2.10
Device relay state: OFF
Device relay state: ON
Device relay state: OFF
Device relay state: ON

Here are the basic definitions of the libraries under the project file and what they do:

  • main.cpp: Our main sketch that the behaviour of the MCU and LED is programmed.
  • async_config.h: SSL and debug configuration header for tcp_axtls and ESPAsyncTCP headers.
  • ESPAsyncTCP: Asynchronous TCP library for Espressif MCUs.
  • tcp_axtls: Asynchronous TCP library for Espressif MCUs.
  • fauxmoESP: ESP8266-ESP32 library for emulating Belkin WeMo device in order to control it by Alexa which allows the MCU make UPnP-Broadcasting.
  • WeMo.h: WeMo emulation header for fauxmoESP library.

Controlling the device all start with trigger word, which by default is "Alexa". You then need to tell Alexa your "intention". The pre-programmed intentions invoke the special "skills" in AWS. These skills and intentions under are programmed as "lambda functions" in sake of generating a response according to upcoming request. These requests and responses are tokenised.

The digital port state is controlled on IoT device, so this will be the action of the special skill.

Image 9: Alexa Logic

Image 9: Alexa Logic

That's it! Now you can easily control your devices with the power of Alexa. By setting the digital output port state, you can switch on and off all the digital devices. If you have devices that you want to control with your voice, there is no obstacle to make them smarter! For power-hungry devices, you can use relay module by staying with same logic as this blogpost.

Stay tuned!

Your ThingForward Crew