Smart Home System with Alexa App

Description

The control of 2 light bulbs is achieved in 3 ways:

  • using the manual switches: This is the traditional way of controlling light bulbs where a switch is used to turn the light on or off.
  • using the Alexa voice command: This can be done through a device such as an Echo Dot or through a phone app that has the Alexa app installed. The user can give voice commands such as “Alexa, turn on light 1” to control the light.
  • using a Firebase app for state switching: Firebase is a mobile and web application development platform. It can be used to develop an app that controls the state of the light bulbs. This can be done by connecting the light bulbs to ESP32 controller, which can then be controlled through the app.

Hardware Description

The proper use and integration of hardware components is crucial for the successful functioning and performance of any computer system or electronic project. The following components were chosen based on the ability to achieve a desired functional outcome while minimizing expenses.

  • The ESP32 WROOM is a low-power, low-cost microcontroller that is often used for IoT projects. It has built-in WiFi and Bluetooth capabilities, making it well-suited for connecting to other devices wirelessly.

  • The Alexa app is used to control the ESP32 through voice commands. The app can be downloaded onto a smartphone or tablet and used to connect to the ESP32 over a WiFi network. In this case, it is not mandatory to have an Alexa device(eg. Echo Dot).

  • In a smart home system, lightbulbs can be controlled by voice commands or through a mobile app to turn on or off. This allows for adjusting the lighting in the home to fit different needs, such as creating a comfortable atmosphere for watching TV or setting a wake-up light in the morning.

  • Buttons are used to control the system, to turn on or off lights.

  • An optocoupler relay module is important for electrical isolation between the input and output circuits. This means that the input circuit, which is connected to a microcontroller is completely separate from the output circuit, which is connected to a higher voltage or current load. This isolation helps to protect the input device from damage and also helps to prevent electrical noise and interference from affecting the input circuit.

All these components are connected together to create a final project that allows the control of lightbulbs. The result is a fully-automated and voice-controlled Smart Home System that gives the user the ability to control various aspects of their home with ease.

The final diagram of all the components can be seen below and was made with the help of the EasyEDA program.

Software Description

Libraries

  • AsyncTCP - This is a fully asynchronous TCP library, aimed at enabling trouble-free, multi-connection network environment for Espressif's ESP32 MCUs.
  • Button-1.0.0 - This is a tiny library to make reading buttons very simple. It handles debouncing automatically, and monitoring of state.
  • Firebase-ESP-Client - The managed, complete, fast and secure Firebase Client Library that supports ESP8266 and ESP32 MCU from Espressif
  • fauxmoESP - This is a library for ESP8266/ESP32-based/Raspberry Pi Pico W devices that emulates Philips Hue lights and thus allows you to control them using this protocol, in particular from Alexa-powered devices like the Amazon Echo or the Dot

Functionality

Depending on how you want to change the state of the light bulb between on and off, we will go through each situation.

Manual switches

This traditional method is the most used for controlling light bulbs. The user can turn on or off the light using 2 buttons that transmit the information to the ESP and further the controller checks and turns on or off the status of the lights depending on the situation. A few stages of the entire program are described below:

  • Performing the initial settings: implementing pins 1 and 2 for the 2 lightbulbs.
Alexa-esp32.ino
    // LED
    pinMode(RELAY_PIN_1, OUTPUT);
    digitalWrite(RELAY_PIN_1, LOW);
 
    pinMode(RELAY_PIN_2, OUTPUT);
    digitalWrite(RELAY_PIN_2, LOW);
  • Status change function. At the push of a button, if the light bulb is on, it will turn it off and if it is off, it will turn it on.
Alexa-esp32.ino
fauxmo.onSetState([](unsigned char device_id, const char* device_name, bool state, unsigned char value)
    {
        event_processed = false;
 
        if ((strcmp(device_name, LAMP_1) == 0))
        {           
            alexa_flag_turn_on_red_lamp = state ? true : false;
            alexa_flag_turn_off_red_lamp = state ? false : true;
        }
        if ((strcmp(device_name, LAMP_2) == 0))
        {
            alexa_flag_turn_on_blue_lamp = state ? true : false;
            alexa_flag_turn_off_blue_lamp = state ? false : true;
        }
    });
Alexa App

This method allows us to use voice control with the help of an Echo Dot with the included Alexa application in order to change the state of the lights. The user can give the following commands:

  • “Alexa, turn on the red light.” / “Alexa, turn off the red light.”
  • “Alexa, turn on the blue light.” / “Alexa, turn off the blue light.”
  • “Alexa, turn on/off the bedroom lights.” - the 2 lightbulbs are configured in the “bedroom” group
  • “Alexa, good morning!” - this will turn on the lightbulbs
  • “Alexa, good night!” - this will turn off the lightbulbs

Implementation of switching the blue lamp between the two states using Alexa App.

Alexa-esp32.ino
Serial.println(fbdo_write.dataType());
            }
            else
            {
                error_flag = true;
            }
        }
        else if (alexa_flag_turn_on_blue_lamp)
        {
            digitalWrite(RELAY_PIN_2, LOW);
            alexa_flag_turn_on_blue_lamp = false;
 
            // Write an Int number on the database path test/int
            if (Firebase.RTDB.setInt(&fbdo_write, "lamp/blue", 1))
            {
                Serial.print("Successfully wrote in RTDB to [data path]:"); Serial.print(fbdo_write.dataPath()); Serial.print(", [data type]: "); Serial.println(fbdo_write.dataType());
            }
            else
            {
                error_flag = true;
            }
        }
        else if (alexa_flag_turn_off_blue_lamp)
        {
            digitalWrite(RELAY_PIN_2, HIGH);
            alexa_flag_turn_off_blue_lamp = false;
 
            if (Firebase.RTDB.setInt(&fbdo_write, "lamp/blue", 0))
            {
                Serial.print("Successfully wrote in RTDB to [data path]:"); Serial.print(fbdo_write.dataPath()); Serial.print(", [data type]: ");

At each modification, the change is checked. If the status cannot be changed, it displays an error message.

Alexa-esp32.ino
Serial.println(fbdo_write.dataType());
            }
            else
            {
                error_flag = true;
            }
        }
        if (error_flag)
        {
            Serial.println("Failed to update the RTDB :( "); Serial.println("REASON: " + fbdo_write.errorReason());
        }
    }
 
}
Firebase App

Within the buttonsHandling() function, all the necessary changes are made to transmit the information to Firebase. The status of each lightbulb is checked and if any changes have been made, this will be displayed in real-time in the application. The stages of implementation are: changing the status, ascertaining the status, and implementing the application.

The web page consists of 2 buttons where you can see the status of the 2 lights. The page is interactive and we can press the buttons to change the state of the lights. Also, if the status has been changed by voice command or by pressing the physical buttons, this can be seen in real-time on the web page.

Conclusion

  • In the case of this type of application using ESP32 and Firebase, I found many well-documented projects on the Internet and I was able to easily find solutions to problems encountered along the way.
  • This application implements 3 simple ways to control 2 lights: with the help of physical buttons, with the help of the Alexa application, and with the help of a Firebase application where you can see the status of the lights in real-time.
  • By introducing the application in everyday life, some activities can be made easier and the level of comfort can be increased, improving the quality of life.

Resources

Pictures sources

Software information

iothings/proiecte/2022/smart_home_system_with_alexa_app.txt · Last modified: 2023/01/20 04:17 by catalina.sirbu
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0