After the Alexa Skill is configured and can successfully transfer a message from the user into the MQTT subtopic, it is time to program the ESP32 to be able to receive those messages and take some action. For those that aren’t aware, an ESP32 is a type of microcontroller that can access the internet, which is extremely useful because it allows me to hook it up to my Wi-Fi network, connect to AWS IoT and then run continuously while only connected to a battery pack.
To upload code to my ESP32 I used the Arduino IDE, which is a desktop app that makes it super simple to write C code and upload it to compatible microcontrollers via a USB connection. The specific ESP32 that I used is the HiLetgo ESP-WROOM-32. To configure the IDE correctly, I selected tools -> boards -> board manager and picked the DOIT ESP DevKit V1. I was not able to find the exact board that I purchased, but this seemed to be compatible with the one that I was using. Then, for development purposes, I selected the correct port under tools -> port and set the baud rate to 9600. This allowed me to be able to write logs to the serial port that could be seen in the Arduino IDE, making it much easier to debug.
The actual code that I wrote is adapted from a very helpful tutorial found on the AWS blogs. The main structure of any Arduino code is a setup and a loop function. The setup is what is done when the program first starts running and then the loop function runs continuously after that. In my setup function I first set the serial output value to be 9600 (as we mentioned before) then call connectAWS before setting the pin value of pin 14 to be OUTPUT and set it to high. What this pin outputting does is take pin 14 on the physical ESP32 chip and set it to be an output pin, meaning that I can connect wires to this pin and send an electrical output through it based on the code that I write. What the code for connectAWS does is take a “secrets.h” file (which I haven’t included here due to privacy purposes) that contains a few key values, like my IoT thing name, my Wi-Fi network and password, and the certificates that I mentioned before, and uses them to connect first to Wi-Fi and then to AWS IoT. It also uses the WiFi and MQTTClient libraries which can be installed in the Arduino IDE from sketch -> libraries -> manage libraries.
Once the device has been connected both to Wi-Fi and then to AWS IoT, the code will move into the loop function. The loop will first call a function that publishes some basic information to our MQTT topic such as the time. This isn’t really an essential step, but it can be useful for debugging. It then calls client.loop, which will cause the MQTTHandler to go through an iteration of itself, which in this case just means calling the MessageHandler function that is also included in the code.
What the MessageHandler does is receive any incoming message from the MQTT topic and first convert it from a serialized JSON into a string. This will leave us with the JSON message that we initially sent from our lambda function, which will either be “on” or “off” based on the input from the Alexa front end. If it is “on” it will call digitalWrite on our output pin 14 and then wait 5 seconds before calling digital write again and removing the output. What this does is simulate the pushing of the “on” button on the fan, which will become a little clearer after reading about the actual circuitry that connects the ESP32 to the fan. If it receives “off” it will basically do the same things to simulate pushing the power button a second time, turning off the fan.
After writing this code, I connected my ESP32 to my computer via a USB cable and clicked “upload” in the Arduino IDE, and just like that my ESP32 was running my code and able to connect to the internet and AWS IoT and receive messages.