Skip to content

Commit ee5f959

Browse files
committed
Add LED status
Improve gitignore Refactor WiFi part Add partitions scheme Add default config Add ZigBee menu Add ZigBee support
1 parent ae51816 commit ee5f959

21 files changed

+1059
-2168
lines changed

.gitignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
1+
.config
2+
*.o
3+
*.pyc
4+
5+
.DS_Store
6+
.cache/
7+
8+
.vscode/
9+
10+
*.sublime-project
11+
*.sublime-workspace
12+
113
.idea
14+
cmake-build-*/
15+
16+
*.swp
17+
*.swo
18+
219
/build/
320
cmake-*
421
/sdkconfig.old
22+
/sdkconfig
23+
dependencies.lock
24+
managed_components
25+
venv

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@ cmake_minimum_required(VERSION 3.16)
22

33
set(EXTRA_COMPONENT_DIRS vendor/esp-idf-lib/components)
44

5+
execute_process(COMMAND git rev-parse --short HEAD
6+
OUTPUT_VARIABLE GIT_COMMIT_HASH
7+
OUTPUT_STRIP_TRAILING_WHITESPACE)
8+
add_definitions(-DGIT_COMMIT_HASH="${GIT_COMMIT_HASH}")
9+
510
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
611
project(xiao_esp32c6_wifi_sensor)

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- [Getting Started](#getting-started)
1515
- [Hardware](#hardware)
1616
- [Software](#software)
17+
- [ZigBee](#zigbee)
1718
- [Contributing](#contributing)
1819
- [License](#license)
1920
- [TODO](#todo)
@@ -31,6 +32,7 @@ The Smart IoT Sensor is a power-efficient device built using the [XIAO ESP32C6](
3132
- **Environmental Sensing**: Supports [Bosch BME280](https://www.bosch-sensortec.com/products/environmental-sensors/humidity-sensors-bme280/) and [BME680](https://www.bosch-sensortec.com/products/environmental-sensors/gas-sensors/bme680/) sensors for temperature, humidity, and pressure measurements.
3233
- **Power Management**: Efficiently manages power using LiPo batteries with built-in charge management.
3334
- **Deep Sleep Mode**: Extends battery life by enabling deep sleep between data transmissions.
35+
- **ZigBee Connectivity**: Seamless integration with ZigBee networks. Supports standard ZigBee clusters, easily pair your sensor with ZigBee coordinators like Home Assistant.
3436
- **Optional Features**:
3537
- Battery voltage monitoring
3638
- Connection duration tracking
@@ -313,6 +315,21 @@ Follow these steps to set up your Smart IoT Sensor:
313315
idf.py -p <TARGET_PORT> flash
314316
```
315317
318+
## ZigBee
319+
ZigBee can be activated in the menu:
320+
```bash
321+
idf.py menuconfig
322+
```
323+
Navigate to `XIAO Sensor Configuration` → `Select Sensor Connection Type` → `ZigBee`.
324+
325+
Next, go to `Component config` → `Zigbee` → `Zigbee Enable` and set it to ON. Then, go to `XIAO Sensor Configuration` → `ZigBee Configuration` and set a `Sensor ID`.
326+
327+
Build and flash the firmware. After that, the sensor will be ready and will begin commissioning.
328+
329+
To add the sensor to Home Assistant: go to `Settings` → `Devices & Services` → `Devices` → `Add Device` → `Add Zigbee device`. Once completed, your sensor will appear on the Devices page:
330+
331+
<img alt="SensorXIAO_zb_device.png" src="assets/SensorXIAO_zb_device.png" width="500"/>
332+
316333
## Contributing
317334
318335
Contributions are welcome! Please follow these steps:

assets/SensorXIAO_zb_device.png

199 KB
Loading

main/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ idf_component_register(SRCS
66
"sensor_gpio.c"
77
"sensor_adc.c"
88
"sensor_bme.c"
9+
"sensor_zb.c"
10+
"sensor_led.c"
911
INCLUDE_DIRS "."
1012
)

main/Kconfig.projbuild

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
menu "XIAO Sensor Configuration"
2+
choice SENSOR_CONNECTION_TYPE
3+
prompt "Select Sensor Connection Type"
4+
default SENSOR_CONNECTION_WIFI_MQTT
5+
help
6+
Choose the type of sensor connection to use.
7+
- **WiFi + MQTT**: WiFi and MQTT.
8+
- **ZigBee**: ZigBee.
9+
10+
config SENSOR_CONNECTION_WIFI_MQTT
11+
bool "WiFi + MQTT"
12+
help
13+
Enable WiFi and MQTT connection.
214

15+
config SENSOR_CONNECTION_ZIGBEE
16+
bool "ZigBee"
17+
help
18+
Enable ZigBee connection.
19+
20+
endchoice
321
menu "Wi-Fi Configuration"
22+
depends on SENSOR_CONNECTION_WIFI_MQTT
423
config ESP_WIFI_SSID
524
string "WiFi SSID"
625
default "myssid"
@@ -112,6 +131,7 @@ menu "XIAO Sensor Configuration"
112131
endmenu
113132

114133
menu "MQTT Configuration"
134+
depends on SENSOR_CONNECTION_WIFI_MQTT
115135
config MQTT_BROKER_URI
116136
string "MQTT Broker URI"
117137
default "mqtt://192.168.1.100"
@@ -130,7 +150,7 @@ menu "XIAO Sensor Configuration"
130150
help
131151
MQTT topic for publishing sensor data.
132152

133-
config MQTT_SENSOR_ID
153+
config SENSOR_ID
134154
string "Sensor ID"
135155
default "7i29r9k9ltaxmbev"
136156
help
@@ -139,7 +159,7 @@ menu "XIAO Sensor Configuration"
139159
This ID is used to construct MQTT topics for publishing data.
140160

141161
**Example:**
142-
If `MQTT_TOPIC` is set to "xiao/sensor" and `MQTT_SENSOR_ID` is "7i29r9k9ltaxmbev",
162+
If `MQTT_TOPIC` is set to "xiao/sensor" and `SENSOR_ID` is "7i29r9k9ltaxmbev",
143163
the resulting MQTT topic for data will be:
144164
`xiao/sensor/7i29r9k9ltaxmbev/data`
145165

@@ -202,6 +222,19 @@ menu "XIAO Sensor Configuration"
202222
Password for authenticating with the MQTT broker.
203223
endmenu
204224

225+
menu "ZigBee Configuration"
226+
depends on SENSOR_CONNECTION_ZIGBEE
227+
config SENSOR_ID
228+
string "Sensor ID"
229+
default "7i29r9k9ltaxmbev"
230+
help
231+
Unique identifier for the sensor.
232+
config ZIGBEE_ANTENNA_EXTERNAL
233+
bool "External Antenna"
234+
help
235+
Use an external antenna for ZigBee connectivity.
236+
endmenu
237+
205238
# Sensor Type Selection
206239
choice SENSOR_TYPE
207240
prompt "Select Sensor Type"

main/idf_component.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## IDF Component Manager Manifest File
2+
dependencies:
3+
espressif/esp-zboss-lib: "~1.5.0"
4+
espressif/esp-zigbee-lib: "~1.5.0"
5+
## Required IDF version
6+
idf:
7+
version: ">=5.3.1"

main/main.c

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,25 @@
1111
*/
1212

1313
#include "sdkconfig.h"
14-
#include <cJSON.h>
14+
1515
#include "sensor_nvs.h"
16-
#include "sensor_wifi.h"
17-
#include "sensor_mqtt.h"
1816
#include "sensor_gpio.h"
1917
#include "esp_sleep.h"
2018
#include "sys/time.h"
2119

20+
#if !defined CONFIG_SENSOR_ID
21+
#error Define CONFIG_SENSOR_ID in idf.py menuconfig to compile source code.
22+
#endif
23+
24+
#ifdef CONFIG_SENSOR_CONNECTION_WIFI_MQTT
25+
#include "sensor_wifi.h"
26+
#include "sensor_mqtt.h"
27+
#endif
28+
29+
#ifdef CONFIG_SENSOR_CONNECTION_ZIGBEE
30+
#include "sensor_zb.h"
31+
#endif
32+
2233
#ifdef CONFIG_ENABLE_BATTERY_CHECK
2334
#include "sensor_adc.h"
2435
#endif
@@ -34,15 +45,16 @@ float humidity = 0;
3445
float pressure = 0;
3546

3647
void app_main(void) {
37-
3848
#ifdef CONFIG_ENABLE_BATTERY_CHECK
3949
init_adc();
4050
get_battery_voltage(&battery_voltage);
4151
ESP_LOGI(TAG, "Battery Voltage: %.2f V", battery_voltage);
52+
ESP_LOGI(TAG, "SW version: %s", GIT_COMMIT_HASH);
4253
deinit_adc();
4354
#endif
4455
init_nvs();
4556
init_gpio();
57+
init_led();
4658
#ifndef CONFIG_SENSOR_NO_SENSOR
4759
init_bme();
4860
read_bme(&temperature, &humidity, &pressure);
@@ -51,59 +63,44 @@ void app_main(void) {
5163
struct timeval start_to_connect;
5264
gettimeofday(&start_to_connect, NULL);
5365

66+
#ifdef CONFIG_SENSOR_CONNECTION_WIFI_MQTT
5467
init_wifi_sta();
5568
esp_mqtt_client_handle_t mqtt_client = init_mqtt_client();
69+
#endif
70+
71+
#ifdef CONFIG_SENSOR_CONNECTION_ZIGBEE
72+
init_zb();
73+
74+
start_zb();
75+
#endif
5676

5777
struct timeval end_to_connect;
5878
gettimeofday(&end_to_connect, NULL);
5979

80+
#ifdef CONFIG_SENSOR_CONNECTION_WIFI_MQTT
6081
int rssi = -100;
6182
esp_wifi_sta_get_rssi(&rssi);
6283
ESP_LOGI(TAG, "RSSI: %d", rssi);
6384

64-
// Create cJSON object
65-
cJSON *json = cJSON_CreateObject();
66-
cJSON_AddStringToObject(json, "ID", CONFIG_MQTT_SENSOR_ID);
67-
cJSON_AddNumberToObject(json, "RSSI", rssi);
68-
cJSON_AddNumberToObject(json, "battery_voltage", battery_voltage);
69-
70-
char temperature_str[16];
71-
snprintf(temperature_str, sizeof(temperature_str), "%.2f", temperature);
72-
cJSON_AddStringToObject(json, "temperature", temperature_str);
73-
74-
char humidity_str[16];
75-
snprintf(humidity_str, sizeof(humidity_str), "%.2f", humidity);
76-
cJSON_AddStringToObject(json, "humidity", humidity_str);
85+
char *json_string = (char *)malloc(MQTT_MSG_MAX_LEN * sizeof(char));
86+
mqtt_prepare_json(json_string, rssi, battery_voltage, temperature, humidity, pressure, start_to_connect, end_to_connect);
7787

78-
char pressure_str[16];
79-
snprintf(pressure_str, sizeof(pressure_str), "%.2f", pressure);
80-
cJSON_AddStringToObject(json, "pressure", pressure_str);
88+
mqtt_publish(mqtt_client, json_string);
8189

82-
cJSON_AddNumberToObject(json, "connection_duration_ms",
83-
(end_to_connect.tv_sec - start_to_connect.tv_sec) * 1000 +
84-
(end_to_connect.tv_usec - start_to_connect.tv_usec) / 1000);
85-
86-
// Convert cJSON to string
87-
char *json_string = cJSON_PrintUnformatted(json);
88-
89-
while (true) {
90-
mqtt_publish(mqtt_client, json_string);
91-
92-
// Cleanup
93-
free(json_string);
94-
cJSON_Delete(json);
90+
// Cleanup
91+
free(json_string);
92+
#endif
9593

96-
int wakeup_time_sec = CONFIG_WAKEUP_TIME_SEC;
94+
int wakeup_time_sec = CONFIG_WAKEUP_TIME_SEC;
9795

98-
if (wakeup_time_sec < 5 || wakeup_time_sec > 86400) {
99-
ESP_LOGW(TAG, "Invalid WAKEUP_TIME_SEC: %d. Using default 300 seconds.", wakeup_time_sec);
100-
wakeup_time_sec = 300;
101-
}
96+
if (wakeup_time_sec < 5 || wakeup_time_sec > 86400) {
97+
ESP_LOGW(TAG, "Invalid WAKEUP_TIME_SEC: %d. Using default 300 seconds.", wakeup_time_sec);
98+
wakeup_time_sec = 300;
99+
}
102100

103-
ESP_LOGI(TAG, "Enabling timer wakeup, %ds\n", wakeup_time_sec);
104-
ESP_ERROR_CHECK(esp_sleep_enable_timer_wakeup(wakeup_time_sec * 1000000));
101+
ESP_LOGI(TAG, "Enabling timer wakeup, %ds\n", wakeup_time_sec);
102+
ESP_ERROR_CHECK(esp_sleep_enable_timer_wakeup(wakeup_time_sec * 1000000));
105103

106-
// enter deep sleep
107-
esp_deep_sleep_start();
108-
}
104+
// Enter deep sleep
105+
//esp_deep_sleep_start();
109106
}

main/sensor_bme.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ void init_bme(void) {
6666
gpio_reset_pin(CONFIG_BME280_POWER_PIN);
6767
gpio_set_direction(CONFIG_BME280_POWER_PIN, GPIO_MODE_OUTPUT);
6868

69+
gpio_set_level(CONFIG_BME280_POWER_PIN, HIGH);
70+
vTaskDelay(pdMS_TO_TICKS(100));
71+
gpio_set_level(CONFIG_BME280_POWER_PIN, LOW);
72+
vTaskDelay(pdMS_TO_TICKS(100));
6973
gpio_set_level(CONFIG_BME280_POWER_PIN, HIGH);
7074
ESP_LOGI(TAG, "BME280 powered on");
7175

main/sensor_gpio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#define HIGH 1
1616

1717
void init_gpio(void) {
18-
#ifdef CONFIG_WIFI_ANTENNA_EXTERNAL
18+
#if defined(CONFIG_WIFI_ANTENNA_EXTERNAL) || defined(CONFIG_ZIGBEE_ANTENNA_EXTERNAL)
1919
gpio_reset_pin(GPIO_NUM_3);
2020
gpio_set_direction(GPIO_NUM_3, GPIO_MODE_OUTPUT);
2121
gpio_set_level(GPIO_NUM_3, LOW);

0 commit comments

Comments
 (0)