Skip to content

Commit

Permalink
ESP32 example app with basic and temperature cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
shana-apple committed Oct 5, 2020
1 parent b5b3d38 commit c405d59
Show file tree
Hide file tree
Showing 49 changed files with 52,381 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/temperature-measurement-app/esp32/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.vscode
39 changes: 39 additions & 0 deletions examples/temperature-measurement-app/esp32/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#
# Copyright (c) 2020 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.

#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#

PROJECT_NAME := chip-wifi-echo

EXTRA_COMPONENT_DIRS += $(PROJECT_PATH)/third_party/connectedhomeip/config/esp32/components \
$(PROJECT_PATH)/../../common/m5stack-tft/repo/components \
$(PROJECT_PATH)/../../common/QRCode \
$(PROJECT_PATH)/../../common/screen-framework \

CXXFLAGS += -std=c++11 -Os -DLWIP_IPV6_SCOPES=0
CPPFLAGS += -Os -DLWIP_IPV6_SCOPES=0
CFLAGS += -Os -DLWIP_IPV6_SCOPES=0

CHIP_BUILD_WITH_GN ?= ""

ifneq ($(CHIP_BUILD_WITH_GN),n)
CPPFLAGS += -DCHIP_SEPARATE_CONFIG_H
endif
export CHIP_BUILD_WITH_GN
include $(IDF_PATH)/make/project.mk
157 changes: 157 additions & 0 deletions examples/temperature-measurement-app/esp32/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
# CHIP WiFi Echo Server Example

A prototype appplication that uses CHIP to setup WiFi on the ESP32 and runs an
Echo Server. This example will evolve as more complex messaging is supported in
CHIP.

---

- [CHIP WiFi Echo Server Example](#chip-wifi-echo-server-example)
- [Supported Devices](#supported-devices)
- [Building the Example Application](#building-the-example-application)
- [To build the application, follow these steps:](#to-build-the-application-follow-these-steps)
- [Using the Echo Server](#using-the-echo-server)
- [Connect the ESP32 to a 2.4GHz Network of your choice](#connect-the-esp32-to-a-24ghz-network-of-your-choice)
- [Use the ESP32's Network](#use-the-esp32s-network)

---

## Supported Devices

The CHIP demo application is intended to work on two categories of ESP32
devices: the
[ESP32-DevKitC](https://www.espressif.com/en/products/hardware/esp32-devkitc/overview),
and the [M5Stack](http://m5stack.com). On the [M5Stack](http://m5stack.com) this
example displays a CHIP QRCode with the device's Soft-AP SSID encoded in the TLV
section.

## Building the Example Application

Building the example application requires the use of the Espressif ESP32 IoT
Development Framework and the xtensa-esp32-elf toolchain.

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 release tag v4.1

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

- Install ninja-build

$ sudo apt-get install ninja-build

### To build the application, follow these steps:

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).

- In the root of the example directory, sync the CHIP tree's submodules and
source `idf.sh`. Note: This does not have to be repeated for incremental
builds.

$ source idf.sh

- Next, if you want to use the M5Stack with its display and show a QRCode run
`menuconfig`.

$ idf make menuconfig

While in the configurator, navigate to `WiFi Echo Demo`->`Device Type` and
select `M5Stack`.

Otherwise, run the default config.

$ idf make defconfig

- Run make to build the demo application.

$ idf make

- 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. For ESP32-DevKitC devices this is labeled in the
[functional description diagram](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/hw-reference/esp32/get-started-devkitc.html#functional-description).

$ idf make flash monitor ESPPORT=/dev/tty.SLAB_USBtoUART

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 make monitor ESPPORT=/dev/tty.SLAB_USBtoUART

## Using the Echo Server

There are two ways to use the Echo Server running on the device.

### Connect the ESP32 to a 2.4GHz Network of your choice

1. To connect the device to your network, give it the network details via the
`menuconfig` target.

$ idf make menuconfig

2. While in the configurator, navigate to
`Component Config`->`CHIP Device Layer`->`WiFi Station Options` and fill out
the WiFi SSID and Password.

3. Now flash the device with the same command as before. (Use the right `/dev`
device)

$ idf make flash monitor ESPPORT=/dev/tty.SLAB_USBtoUART

4. The device should boot up and connect to your network. When that happens you
will see a log like this in the monitor.

I (5524) chip[DL]: SYSTEM_EVENT_STA_GOT_IP
I (5524) chip[DL]: IPv4 address changed on WiFi station interface: <IP_ADDRESS>...

Note: If you are using the M5Stack, the screen will display the server's IP
Address if it successfully conencts to the configured 2.4GHz Network.

5. Use
[standalone chip-tool](https://github.com/project-chip/connectedhomeip/tree/master/examples/chip-tool)
or
[iOS chip-tool app](https://github.com/project-chip/connectedhomeip/tree/master/src/darwin)
to communicate with the device.

Note: The ESP32 does not support 5GHz networks. Also, the Device will persist
your network configuration. To erase it, simply run.

$ idf make erase_flash ESPPORT=/dev/tty.SLAB_USBtoUART

### Use the ESP32's Network

Alternatively, you can connect to the ESP32's Soft-AP directly.

1. After the application has been flashed, connect to the ESP32's Soft-AP. If
you use the M5Stack, the Soft-AP's SSID is encoded in the TLV section of the
QRCode on screen. It's usually something like `CHIP-XXX` where the last 3
digits are from the setup payload discriminator.

2. Once you're connected, the server's IP can be found at the gateway address.

3. Use
[standalone chip-tool](https://github.com/project-chip/connectedhomeip/tree/master/examples/chip-tool)
or
[iOS chip-tool app](https://github.com/project-chip/connectedhomeip/tree/master/src/darwin)
to communicate with the device.
46 changes: 46 additions & 0 deletions examples/temperature-measurement-app/esp32/idf.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env bash
#
# Copyright (c) 2020 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.

# If this file is sourced, it exports a function called "idf" that initializes
# the espressif environment via the espressive export.sh script and runs
# a command presented as arguments
#
# This file can also be used as an executable

error() {
echo "$me: *** ERROR: " "${*}"
}

idf() {
[[ -d $IDF_PATH && -r $IDF_PATH/export.sh ]] || {
error "can't find IDF's export.sh, please set IDF_PATH"
return 1
}
(
# shellcheck source=/dev/null
. "$IDF_PATH/export.sh"
export IDF_PATH
"$@"
)
}
if [[ ${0} == "${BASH_SOURCE[0]}" ]]; then
me=${0##*/}
idf "${@}"
else
me=idf
[[ $PS1 =~ \[idf\].* ]] || PS1="[idf]$PS1"
fi
105 changes: 105 additions & 0 deletions examples/temperature-measurement-app/esp32/main/CHIPDeviceManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
*
* Copyright (c) 2020 Project CHIP Authors
*
* 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.
*/

/**
* @file
* This file implements the CHIP Device Interface that is used by
* applications to interact with the CHIP stack
*
*/

#include <stdlib.h>

#include "CHIPDeviceManager.h"
#include <lib/support/CodeUtils.h>
#include <setup_payload/SetupPayload.h>
#include <support/ErrorStr.h>

namespace chip {

namespace DeviceManager {

using namespace ::chip::DeviceLayer;

void CHIPDeviceManager::CommonDeviceEventHandler(const ChipDeviceEvent * event, intptr_t arg)
{
CHIPDeviceManagerCallbacks * cb = reinterpret_cast<CHIPDeviceManagerCallbacks *>(arg);
if (cb != nullptr)
{
cb->DeviceEventCallback(event, reinterpret_cast<intptr_t>(cb));
}
}

/**
*
*/
CHIP_ERROR CHIPDeviceManager::Init(CHIPDeviceManagerCallbacks * cb)
{
CHIP_ERROR err;
mCB = cb;

// Initialize the CHIP stack.
err = PlatformMgr().InitChipStack();
SuccessOrExit(err);

switch (static_cast<RendezvousInformationFlags>(CONFIG_RENDEZVOUS_MODE))
{
case RendezvousInformationFlags::kBLE:
ConnectivityMgr().SetBLEAdvertisingEnabled(ConnectivityManager::kCHIPoBLEServiceMode_Enabled);
break;

case RendezvousInformationFlags::kWiFi:
ConnectivityMgr().SetBLEAdvertisingEnabled(ConnectivityManager::kCHIPoBLEServiceMode_Disabled);
ConnectivityMgr().SetWiFiAPMode(ConnectivityManager::kWiFiAPMode_Enabled);
break;

case RendezvousInformationFlags::kNone:
// If rendezvous is bypassed, enable SoftAP so that the device can still
// be communicated with via its SoftAP as needed.
ConnectivityMgr().SetWiFiAPMode(ConnectivityManager::kWiFiAPMode_Enabled);
break;

default:
break;
}

// Register a function to receive events from the CHIP device layer. Note that calls to
// this function will happen on the CHIP event loop thread, not the app_main thread.
PlatformMgr().AddEventHandler(CHIPDeviceManager::CommonDeviceEventHandler, reinterpret_cast<intptr_t>(cb));

// Start a task to run the CHIP Device event loop.
err = PlatformMgr().StartEventLoopTask();
SuccessOrExit(err);

exit:
return err;
}

extern "C" {
void emberAfPostAttributeChangeCallback(uint8_t endpoint, EmberAfClusterId clusterId, EmberAfAttributeId attributeId, uint8_t mask,
uint16_t manufacturerCode, uint8_t type, uint8_t size, uint8_t * value)
{
CHIPDeviceManagerCallbacks * cb = CHIPDeviceManager::GetInstance().GetCHIPDeviceManagerCallbacks();
if (cb != nullptr)
{
cb->PostAttributeChangeCallback(endpoint, clusterId, attributeId, mask, manufacturerCode, type, size, value);
}
}
} // extern "C"

} // namespace DeviceManager
} // namespace chip
Loading

0 comments on commit c405d59

Please sign in to comment.