Home Control on QCA402x

Skill LevelArea of FocusOperating SystemPlatform/Hardware
IntermediateIoT, Bluetooth, Security, Smart HomeRTOSQCA 402x Wi-Fi/BLE/ZigBee

This project is designed to use the QCA4020 development kit to control a Smart Bulb, Smart Lock and Smoke Detector using a Mobile App via Bluetooth Low Energy (BLE) channel, and also demonstrates Home Automation. You can control the bulb, turn it on/off, lock and unlock, sense smoke and send data at regular intervals.

The main objective is to develop a home automation solution using the QCA4020 board and BLE channel to control smart devices like a Smart Bulb (turn on & off), smart lock (lock & unlock) and smoke detector. These devices are controlled by an Android-based application which is installed on smartphones like a Google Pixel.

Parts Used:

Resources of Home Control System on QCA402x Development Board

  • Mobile Phone with Android O operating system
  • QCA4020 Development Board
  • Smart Bulb
  • Smart Lock (Servo Motor)
  • Smoke Detector

Deploying the Project

How does it work?

The Home Control application on QCA4020 has the capabilities of locking/unlocking the doors, reading and sending the smoke detector values, and turning the bulb on and off via a mobile application.

Sensors such as a smoke detector, BLE bulb and servo motor are connected to the QCA4020 development board. An Android mobile app controls these sensors by directly communicating to QCA4020 via BLE. The data from the smoke detector is monitored at regular intervals and displayed as a chart.

Though the bulb can be controlled independently to turn on and off from the mobile application, it also depends on the state of the lock. The dependency of bulb on lock is as below –

  • When the user taps “LOCK” in the mobile app, the QCA4020 board should:
    • Trigger the servo
    • Wait for 1 second
    • Turn the bulb OFF
    • Show / change the “Bulb” icon status in the app accordingly.
  • When the user taps “UNLOCK” in the Android app, the QCA4020 board should:
    • Trigger the servo
    • Wait for 1 second
    • Turn the light bulb ON
    • Show / change the “Bulb” icon status in the app accordingly.

How multiple connections to the board are handled

The QCA4020 board is connected to the mobile app and the bulb via BLE. To make this happen, when connecting to the mobile, QCA4020 will act as Slave device which advertises to be discovered by the mobile application during BLE scan.

And to get connected with the bulb, the QCA4020 board will act as Master device, and the bulb as a Slave device. The board application is implemented in a manner that it discovers the bulb with the name provided (explicit filter applied), gets connected in the background, and notifies its state to the mobile application.

Operational Flow:

BLE GATT properties should be set to the each of the smart devices as below:

Smart Lock - BLE Properties set: READ and WRITE.
WRITE property is given for controlling the device.
READ property is for the getting the status of the device.

Smoke Detector- BLE Properties set: NOTIFY
NOTIFY property to notify about smoke sensed at regular intervals. In this application the values used to plot a graph for the user to check on the variations for the last. And the data of the smoke detector will be stored in the android application in for last active 1 hour.

Smart Bulb - BLE Properties set: WRITE and NOTIFY.
NOTIFY property will notify the bulb state
WRITE property is for the turning on and off the bulb

Initialize Home Automation Service and Peripherals Required:

The Home Automation module is initialized for Home Automation service registration, which initializes Bluetooth and starts a thread to discover and connect the smart bulb via BLE.

Peripheral registration is used to communicate to Smoke Detector and Smart Lock:

The following lines in /QDN_ControlEndDevicesApp/src/pal/pal.c, will reflect the above functionality mentioned.

  static void Initialize_Samples(void)
  {
      Initialize_SPPLE_Demo();
      Initialize_Peripherals_Demo();
      Initialize_Demo();
  }      

Initialize Bluetooth, start advertising and connect to smart bulb:

Once the Bluetooth command is registered, the board will start advertising with the Home Automation service. It will also initiate a thread to discover and connect to the bulb. The code related is in /QDN_ControlEndDevicesApp/spple/spple_demo.c

  InitializeBluetooth(0, 0);
  QCLI_Parameter_t add_params[2];
  memset(add_params, 0, sizeof(QCLI_Parameter_t)*2);
  add_params[0].Integer_Is_Valid = 1;
  add_params[0].Integer_Value = 1;
  AdvertiseLE(1, add_params);
    blb_demo2_init();  

Registering the Home Automation service and call back function:

The structure for smart bulb, smart lock, and the smoke detector is registered using the line of code below:

  const qapi_BLE_GATT_Service_Attribute_Entry_t BLE_IO_Service[]”

The callback function for home automation service is:

  GATT_ServerEventCallback_Home_Automation

defined in file:

  /QDN_ControlEndDevicesApp/spple/ota/ble_ota_service.c

Home Automation service is registered using the below line of code in

  /QDN_ControlEndDevicesApp/spple/ota/ble_ota_service.c

  Result = qapi_BLE_GATT_Register_Service(BT_Stk_Id(),
          QAPI_BLE_GATT_SERVICE_FLAGS_LE_SERVICE,
          BLE_LOCK_SERVICE_ATTRIBUTE_COUNT,
          (qapi_BLE_GATT_Service_Attribute_Entry_t *)BLE_IO_Service,
          &ServiceHandleGroup, GATT_ServerEventCallback_Home_Automation, 0)

Smart Lock Control:

The smart lock is controlled using PWM and the ADC output pin. The code to control the lock is in file /QDN_ControlEndDevicesApp/spple/ota/ble_ota_service.c

  static void lock()
  {
      QCLI_Parameter_t param[5];
      param[0].Integer_Value = 5000;
      param[0].Integer_Is_Valid = true;
      param[1].Integer_Value = 1200;
      param[1].Integer_Is_Valid = true;
      param[2].Integer_Value = 2000;
      param[2].Integer_Is_Valid = true;
      param[3].Integer_Value = 1;
      param[3].Integer_Is_Valid = true;
      param[4].Integer_Value = 1;
      param[4].Integer_Is_Valid = true;
      pwm_driver_test(5, param);
  }
  static void unlock()
  {
      QCLI_Parameter_t param[5];
      param[0].Integer_Value = 5000;
      param[0].Integer_Is_Valid = true;
      param[1].Integer_Value = 500;
      param[1].Integer_Is_Valid = true;
      param[2].Integer_Value = 2000;
      param[2].Integer_Is_Valid = true;
      param[3].Integer_Value = 1;
      param[3].Integer_Is_Valid = true;
      param[4].Integer_Value = 1;
      param[4].Integer_Is_Valid = true;
      pwm_driver_test(5, param);
  }  

Smart Bulb Control:

If bulb is active (connected to the board), the status sent is ‘1’ to the mobile application indicating bulb is ON. The status sent is ‘0’ to the mobile applications if the bulb is OFF.

In the case of inactive (no bulb connected to the board), the status sent is ‘2’ to the mobile application so that the application disables the user interaction to the bulb to control. The application provides the necessary intelligence to notify the state to the application dynamically.

The thread which takes care of connecting to the bulb and notify the status of bulb to mobile app is defined in /QDN_ControlEndDevicesApp/spple/spple_demo.c

Function to control the bulb is BLBDWriteMotionData() defined in /QDN_ControlEndDevicesApp/spple/spple_demo.c

  void BLBDWriteMotionData(uint32_t val)
  {
      uint16_t char_handle;
      int attr_len;
      int Result;
      int deviceIndex;
      //char* val;
      DeviceInfo_t *DeviceInfo;
      //val = "\x00\x00\xff\xff";
      attr_len = 4;
      QCLI_Printf(ble_group, "Value = %u", val);
      for(deviceIndex = 0; deviceIndex < BLB_NUM_BULBS; deviceIndex++)
      {
        if(DeviceInfo = BLBDGetDeviceInfo(deviceIndex))
        {
            char_handle = BLBDGetWriteCharHandle(deviceIndex);
            if(char_handle)
            {
              if((Result = qapi_BLE_GATT_Write_Without_Response_Request(BluetoothStackID,
                  DeviceInfo->ConnectionID, char_handle, attr_len,
                  &val)) > 0)
              {
                  QCLI_Printf(ble_group, "BLBDWriteMotionData write success = %u", Result);
              }
              else if (Result == QAPI_BLE_GATT_ERROR_INVALID_CONNECTION_ID)
              {
                  ResetBLBDDeviceData(deviceIndex);   
                  DisplayFunctionError("qapi_BLE_GATT_Write_Request", Result);
              }
              else
              {
                  DisplayFunctionError("qapi_BLE_GATT_Write_Request", Result);
                  QCLI_Printf(ble_group, "Conn_id = %d", DeviceInfo->ConnectionID);
              }
            }
        }
      }
  }

Smoke Detector Control:

Once the Application is connected to QCA4020, a thread is created to read the smoke detector values, and it is sent to the application every 30 seconds.

If the connection is inactive the thread is destroyed.

The code to read the smoke detector is defined in /QDN_ControlEndDevicesApp/spple/spple_demo.c

  void notify_thread()
  {                              
        uint32_t SmokeDetectorValue = 0;
        uint32_t Result = 0;
        while(notify_thread_flag) {
              SmokeDetectorValue = get_smokedetector_value();
              ASSIGN_HOST_DWORD_TO_LITTLE_ENDIAN_UNALIGNED_DWORD(&Result, SmokeDetectorValue);
              qapi_BLE_GATT_Handle_Value_Notification(ble_stack_id, gatt_sever->Event_Data.GATT_Read_Request_Data->ServiceID, gatt_sever->Event_Data.GATT_Read_Request_Data->ConnectionID, BLE_SMO_DET_ATTRIBUTE_OFFSET, sizeof(Result), &Result);
              Sleep(30000);
        }
  }      
  1. Power on the QCA4020 via the power button, and the QCA4020 will initialize BLE and advertise automatically with HOME AUTOMATION service
  2. Scan for device in mobile application and click connect/pair
  3. Start controlling the smart devices from the app once the device gets connected:
    • Control ON and OFF status of bulb on clicking the bulb icon
    • Control Lock and Unlock status of lock on clicking the lock
    • Values of smoke detector values on chart
NameEmailTitle/Company
Rakesh Sankar[email protected]Sr. System Architect - GlobalEdge Software, Ltd
Akshay Kulkarni[email protected]Technical Lead - GlobalEdge Software, Ltd
Jinka Venkata Saikiran[email protected]Software Engineer - GlobalEdge Software, Ltd
Sahil Munaf Bandar[email protected]Software Engineer - GlobalEdge Software, Ltd