The purpose of this project is to create a small system that has lights and a fan and can be controlled with Alexa. The motivation for this project came from the following context: I have a router placed on the wall in a PVC box which has some cut out details in order to allow the air to flow; I also have an Echo Dot. Because of the way the router is placed, the air flow is not the best, so it could use a little help so that it doesn't get too hot inside the box. This is why the project makes use of a fan. As I said, the box has some cut out models, which look nice, and from an aesthetic point of view, lighting the box would also make for a nice feature. So this is why the project includes some lights. Now, because I have an Echo Dot I thought it would be the best if I could control this project using Alexa.
The hardware components used for this project are:
The ESP32 represents the core of the system. It controls the other components and also enables the network connectivity for the project. Because the fan requires 12V to function, I used a 12 power adapter which can be plugged intro a socket and a dedicated jack to accommodate the other end of the adapter. The relay module is needed in order to power the fan on and off. The DHT11 module is used for its temperature sensor. The idea behind this is that the fan should go on only if the temperature goes over a certain value, hence the need to read the temperature. The LEDs and the resistors are used for the lights of the system.
It's important to note that the temperature sensor has been connected and integrated in a low-power manner. Instead of having it connected to a Vcc pin, case in which it would have provided data continuously, it has the Vcc pin connected to a digital pin. By doing this, it's possible to turn it on/off in order to save power.
The code for this project is bare metal and was developed using the Arduino IDE:
The functionality of the code is described by the diagram below:
As it can bee seen above, when the system gets out of reset it tries to connect to WiFi using the credentials embedded in the code. The status LED on the ESP32 board will blink fast while trying to connect and will signal the fact that the board successfully connected to WiFi with a slower blink. In the setup phase, the pins used by the components are set accordingly as input/output and high/low.
The main loop of the application is split in 2 phases: checking for remote updates and updating the remotes based on local changes. Checking for remote updates means that using the application or the voice commands, the end user can change the state of the LEDs or the fan. If such changes are made, the corresponding callback will be executed in order to update the hardware. Apart from these changes, the system also periodically checks the value of the temperature in order to turn the fan on/off accordingly. After checking the temperature, the new value has to be sent remote, to the application for example, in order to be displayed. This is the second phase of the loop.
The project has been successfully implemented and set in position:
Some wires were hidden to try to make it look nicer:
Good thing it's all covered up :)
In order to get started, the devices have to be registered using the Alexa App:
After the registration is complete, the 2 devices will be available in the “Lights” section of the application under the names “WiFi Light” and “WiFi Fan”. From there they can be controlled with Alexa.
The Alexa library for Arduino is designed especially for lights. So using it to control the lights of this project was pretty straight forward because it implied to use this library as intended. Functionality for the lights include turning lights on/off, setting the brightness and setting the color:
For the fan on the other hand, I had to adapt these mechanisms available for lights to obtain the functionality I needed. Also, the fan is tightly dependent on the temperature sensor. So this is the behaviour for the fan and the sensor: