Skip to content

Commit

Permalink
[ESP32] Added Lighting-Switch-App example (#17195)
Browse files Browse the repository at this point in the history
Add Lighting-Switch-App example for ESP32 boards.
  • Loading branch information
lcj446068124 authored May 3, 2022
1 parent 0b748ff commit 46c7f14
Show file tree
Hide file tree
Showing 18 changed files with 1,680 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/light-switch-app/esp32/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/build/
/sdkconfig
/sdkconfig.old
43 changes: 43 additions & 0 deletions examples/light-switch-app/esp32/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#
# Copyright (c) 2021 Project CHIP Authors
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# The following lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/../../common/cmake/idf_flashing.cmake)

set(EXTRA_COMPONENT_DIRS
"${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/config/esp32/components"
"${CMAKE_CURRENT_LIST_DIR}/../../common/QRCode"
)

project(chip-light-switch-app)

# C++17 is required for RPC build.
idf_build_set_property(CXX_COMPILE_OPTIONS "-std=gnu++17;-Os;-DLWIP_IPV6_SCOPES=0;-DCHIP_HAVE_CONFIG_H" APPEND)
idf_build_set_property(C_COMPILE_OPTIONS "-Os;-DLWIP_IPV6_SCOPES=0" APPEND)
# For the C3, project_include.cmake sets -Wno-format, but does not clear various
# flags that depend on -Wformat
idf_build_set_property(COMPILE_OPTIONS "-Wno-format-nonliteral;-Wno-format-security" APPEND)

# -Wmaybe-uninitialized has too many false positives, including on std::optional
# and chip::Optional. Make it nonfatal.
#
# See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80635
idf_build_set_property(COMPILE_OPTIONS "-Wno-error=maybe-uninitialized" APPEND)

flashing_script()
133 changes: 133 additions & 0 deletions examples/light-switch-app/esp32/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# Matter ESP32 Light-switch Example

This example demonstrates the Matter Light-switch application on ESP platforms.

---

- [Matter ESP32 Light-switch Example](#Matter-ESP32-Light-switch-Example)
- [Supported Devices](#supported-devices)
- [Building the Example Application](#building-the-example-application)
- [Commissioning over BLE using chip-tool](#commissioning-over-ble-using-chip-tool)
- [Testing the example](#testing-the-example)

---

## Supported Devices

The demo application is intended to work on following categories of ESP32
devices:

- [ESP32-DevKitC](https://www.espressif.com/en/products/hardware/esp32-devkitc/overview)
- [ESP32-WROVER-KIT_V4.1](https://www.espressif.com/en/products/hardware/esp-wrover-kit/overview)
- [ESP32C3-DevKitM](https://docs.espressif.com/projects/esp-idf/en/latest/esp32c3/hw-reference/esp32c3/user-guide-devkitm-1.html).
- [ESP32S3-DevKitM](https://docs.espressif.com/projects/esp-idf/en/latest/esp32s3/hw-reference/esp32s3/user-guide-devkitm-1.html).
- [ESP32-H2](https://www.espressif.com/en/news/ESP32_H2).

## Building the Example Application

Building the example application requires the use of the Espressif IoT
Development Framework ([ESP-IDF](https://github.com/espressif/esp-idf)).

The VSCode devcontainer has these components pre-installed, so you can skip this
step. To install these components manually, follow these steps:

- Clone the Espressif ESP-IDF and checkout
[v4.4 release](https://github.com/espressif/esp-idf/releases/tag/v4.4)

$ mkdir ${HOME}/tools
$ cd ${HOME}/tools
$ git clone https://github.com/espressif/esp-idf.git
$ cd esp-idf
$ git checkout v4.4
$ git submodule update --init
$ ./install.sh

- Install ninja-build

$ sudo apt-get install ninja-build

Currently building in VSCode _and_ deploying from native is not supported, so
make sure the IDF_PATH has been exported(See the manual setup steps above).

- Setting up the environment

$ cd ${HOME}/tools/esp-idf
$ ./install.sh
$ . ./export.sh
$ cd {path-to-connectedhomeip}

To download and install packages.

$ source ./scripts/bootstrap.sh
$ source ./scripts/activate.sh

If packages are already installed then simply activate them.

$ source ./scripts/activate.sh

- Target Set

$ idf.py set-target esp32
or
$ idf.py set-target esp32c3
or
$ idf.py set-target esp32s3

- To build the demo application.

$ idf.py build

- After building the application, to flash it outside of VSCode, connect your
device via USB. Then run the following command to flash the demo application
onto the device and then monitor its output. If necessary, replace
`/dev/tty.SLAB_USBtoUART`(MacOS) with the correct USB device name for your
system(like `/dev/ttyUSB0` on Linux). Note that sometimes you might have to
press and hold the `boot` button on the device while it's trying to connect
before flashing.

$ idf.py -p /dev/tty.SLAB_USBtoUART flash monitor

Note: Some users might have to install the
[VCP driver](https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers)
before the device shows up on `/dev/tty`.

- Quit the monitor by hitting `Ctrl+]`.

Note: You can see a menu of various monitor commands by hitting
`Ctrl+t Ctrl+h` while the monitor is running.

- If desired, the monitor can be run again like so:

$ idf.py -p /dev/tty.SLAB_USBtoUART monitor

## Commissioning over BLE using chip-tool

- Please build the standalone chip-tool as described [here](../../chip-tool)
- Commissioning the Lighting device

$ ./out/debug/chip-tool pairing ble-wifi 12344321 <ssid> <passphrase> 20202021 3840

- Commissioning the Lighting-switch device

$ ./out/debug/chip-tool pairing ble-wifi 12344320 <ssid> <passphrase> 20202021 3840

## Testing the example

- After successful commissioning, use the chip-tool to write the ACL in
Lighting device to allow access from Lighting-switch device and chip-tool.

$ ./out/debug/chip-tool accesscontrol write acl '[{"fabricIndex": 1, "privilege": 5, "authMode": 2, "subjects": [112233], "targets": null },{"fabricIndex": 1, "privilege": 3, "authMode": 2, "subjects": [12344320], "targets": null }]' 12344321 0

- we use matter shell to bind and test.

binding:

matter switch binding unicast 1 12344321 1

on:

matter switch onoff on

off:

matter switch onoff off
146 changes: 146 additions & 0 deletions examples/light-switch-app/esp32/main/AppTask.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
*
* Copyright (c) 2022 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "AppTask.h"
#include "BindingHandler.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"

#define APP_TASK_NAME "APP"
#define APP_EVENT_QUEUE_SIZE 10
#define APP_TASK_STACK_SIZE (3072)
#define BUTTON_PRESSED 1
#define APP_LIGHT_SWITCH 1

using namespace chip;

static const char * TAG = "app-task";

namespace {

QueueHandle_t sAppEventQueue;
TaskHandle_t sAppTaskHandle;

} // namespace

AppTask AppTask::sAppTask;

CHIP_ERROR AppTask::StartAppTask()
{
sAppEventQueue = xQueueCreate(APP_EVENT_QUEUE_SIZE, sizeof(AppEvent));
if (sAppEventQueue == NULL)
{
ESP_LOGE(TAG, "Failed to allocate app event queue");
return APP_ERROR_EVENT_QUEUE_FAILED;
}

// Start App task.
BaseType_t xReturned;
xReturned = xTaskCreate(AppTaskMain, APP_TASK_NAME, APP_TASK_STACK_SIZE, NULL, 1, &sAppTaskHandle);
return (xReturned == pdPASS) ? CHIP_NO_ERROR : APP_ERROR_CREATE_TASK_FAILED;
}

CHIP_ERROR AppTask::Init()
{
CHIP_ERROR err = CHIP_NO_ERROR;

return err;
}

void AppTask::AppTaskMain(void * pvParameter)
{
AppEvent event;
CHIP_ERROR err = sAppTask.Init();
if (err != CHIP_NO_ERROR)
{
ESP_LOGI(TAG, "AppTask.Init() failed due to %" CHIP_ERROR_FORMAT, err.Format());
return;
}

ESP_LOGI(TAG, "App Task started");

while (true)
{
BaseType_t eventReceived = xQueueReceive(sAppEventQueue, &event, pdMS_TO_TICKS(10));
while (eventReceived == pdTRUE)
{
sAppTask.DispatchEvent(&event);
eventReceived = xQueueReceive(sAppEventQueue, &event, 0); // return immediately if the queue is empty
}
}
}

void AppTask::PostEvent(const AppEvent * aEvent)
{
if (sAppEventQueue != NULL)
{
BaseType_t status;
if (xPortInIsrContext())
{
BaseType_t higherPrioTaskWoken = pdFALSE;
status = xQueueSendFromISR(sAppEventQueue, aEvent, &higherPrioTaskWoken);
}
else
{
status = xQueueSend(sAppEventQueue, aEvent, 1);
}
if (!status)
ESP_LOGE(TAG, "Failed to post event to app task event queue");
}
else
{
ESP_LOGE(TAG, "Event Queue is NULL should never happen");
}
}

void AppTask::DispatchEvent(AppEvent * aEvent)
{
if (aEvent->mHandler)
{
aEvent->mHandler(aEvent);
}
else
{
ESP_LOGI(TAG, "Event received with no handler. Dropping event.");
}
}

void AppTask::SwitchActionEventHandler(AppEvent * aEvent)
{
if (aEvent->Type == AppEvent::kEventType_Button)
{
BindingCommandData * data = Platform::New<BindingCommandData>();
data->commandId = chip::app::Clusters::OnOff::Commands::Toggle::Id;
data->clusterId = chip::app::Clusters::OnOff::Id;

DeviceLayer::PlatformMgr().ScheduleWork(SwitchWorkerFunction, reinterpret_cast<intptr_t>(data));
}
}

void AppTask::ButtonEventHandler(const uint8_t buttonHandle, uint8_t btnAction)
{
AppEvent button_event = {};
button_event.Type = AppEvent::kEventType_Button;
button_event.ButtonEvent.Action = btnAction;

if (buttonHandle == APP_LIGHT_SWITCH && btnAction == BUTTON_PRESSED)
{
button_event.mHandler = SwitchActionEventHandler;
sAppTask.PostEvent(&button_event);
}
}
Loading

0 comments on commit 46c7f14

Please sign in to comment.