Back to All
Project

Home Control on QCA402x

 

Skill Level Area of Focus Operating System Platform/Hardware
Intermediate IoT, Bluetooth, Security, Smart Home RTOS QCA 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.

Objective

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.

Materials Required / Parts List / Tools

Source Code / Source Examples / Application Executable

Build/Assembly Instructions

Parts Used:

 

Resources
  • 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);
        }
  }      

Usage Instructions

  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

Opinions expressed in the content posted here are the personal opinions of the original authors, and do not necessarily reflect those of Qualcomm Incorporated or its subsidiaries ("Qualcomm"). The content is provided for informational purposes only and is not meant to be an endorsement or representation by Qualcomm or any other party. This site may also provide links or references to non-Qualcomm sites and resources. Qualcomm makes no representations, warranties, or other commitments whatsoever about any non-Qualcomm sites or third-party resources that may be referenced, accessible from, or linked to this site.

Project Authors
GlobalEdge Software
Rakesh SankarSr, System Architect
Akshay KulkarniTechnical Lead
Jinka Venkata SaikiranSr. Software Engineer,
Sahil Munaf BandarSoftware Engineer,

Sign up for the Developer Newsletter.

Get software and hardware tool resources to help optimize your development delivered to your inbox weekly.

Qualcomm relentlessly innovates to deliver intelligent computing everywhere, helping the world tackle some of its most important challenges. Our leading-edge AI, high performance, low-power computing, and unrivaled connectivity deliver proven solutions that transform major industries. At Qualcomm, we are engineering human progress.

Stay connected

Get the latest Qualcomm and industry information delivered to your inbox.

Subscribe
Manage your subscription

© Qualcomm Technologies, Inc. and/or its affiliated companies.

Snapdragon and Qualcomm branded products are products of Qualcomm Technologies, Inc. and/or its subsidiaries. Qualcomm patented technologies are licensed by Qualcomm Incorporated.

Note: Certain services and materials may require you to accept additional terms and conditions before accessing or using those items.

References to "Qualcomm" may mean Qualcomm Incorporated, or subsidiaries or business units within the Qualcomm corporate structure, as applicable.

Qualcomm Incorporated includes our licensing business, QTL, and the vast majority of our patent portfolio. Qualcomm Technologies, Inc., a subsidiary of Qualcomm Incorporated, operates, along with its subsidiaries, substantially all of our engineering, research and development functions, and substantially all of our products and services businesses, including our QCT semiconductor business.

Materials that are as of a specific date, including but not limited to press releases, presentations, blog posts and webcasts, may have been superseded by subsequent events or disclosures.

Nothing in these materials is an offer to sell or license any of the services or materials referenced herein.