Skip to content

Commit 50ac62d

Browse files
Move project configurations to Kconfig.projbuild file
Project configurations can be set using ESP IDF's menuconfig command Improve variable names for better understanding Improve logging
1 parent 907bf4d commit 50ac62d

10 files changed

+96
-30
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
build
33
cmake-build-debug
44
sdkconfig
5+
sdkconfig.old
56
dependencies.lock
67
.clang-format

Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ clean: ## Clean the project
4040
. $(IDF_PATH)/export.sh && \
4141
idf.py clean
4242

43+
configure: ## Open the IDF menuconfig
44+
. $(IDF_PATH)/export.sh && \
45+
idf.py menuconfig
46+
4347
full-clean: ## Clean the project and the build directory
4448
. $(IDF_PATH)/export.sh && \
4549
idf.py fullclean

README.md

+22-4
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,31 @@ These ports can be found by running the following in the terminal:
3838
ls /dev/tty*
3939
```
4040

41-
To build and flash the firmware to the ESP32 devices, use the following commands:
41+
First, install and configure ESP-IDF:
4242

4343
```sh
4444
make install
45+
```
46+
47+
Before building and flashing, configure the project:
48+
49+
```sh
50+
make configure
51+
```
52+
53+
The following configuration options are available specific to the project under the `Project Configuration` menu:
54+
55+
- Total Devices: The total number of ESP32 devices in the network.
56+
- Set Device ID?: Boolean value whether to set the device ID or not. If checked, the device ID can be set.
57+
- Device ID: The device ID of the ESP32 device. This is only available if `Set Device ID?` is checked.
58+
- Less Interference Channel: The channel with less interference. This is the channel that the ESP32 devices will use
59+
for communication.
60+
- Send Frequency: The frequency at which the ESP32 devices send packets to each other.
61+
- Timeout: The number of frequency cycles before the next device sends a packet again.
62+
63+
To build and flash the firmware to the ESP32 devices, use the following commands:
64+
65+
```sh
4566
make build
4667
make flash DEVICE=<device_id>
4768
make monitor DEVICE=<device_id>
@@ -50,9 +71,6 @@ make monitor DEVICE=<device_id>
5071
Replace `<device_id>` with the device ID of the ESP32 device you want to flash. The device ID should be an integer.
5172
If the device ID is not specified, the default device ID is 0.
5273

53-
If the device is being flash for the first time, set the `DEFAULT_DEVICE_ID` variable in the `constants.h` file to the
54-
device ID of the device being flashed.
55-
5674
To flash the firmware and then monitor the serial output, use:
5775

5876
```sh

main/Kconfig.projbuild

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
menu "Project Configuration"
2+
config TOTAL_DEVICES
3+
int "Total Devices"
4+
default 1
5+
help
6+
Total number of ESP32 devices in the network.
7+
8+
config IS_SET_DEVICE_ID
9+
bool "Set Device ID?"
10+
default "n"
11+
help
12+
Whether to set the device ID of the ESP32 to a specific value.
13+
14+
config DEVICE_ID
15+
depends on IS_SET_DEVICE_ID
16+
int "Device ID"
17+
default 0
18+
range 0 TOTAL_DEVICES
19+
help
20+
The device ID of the ESP32. This is a unique integer value that identifies the device in the network.
21+
The range of the device ID is from 0 to TOTAL_DEVICES - 1.
22+
23+
config LESS_INTERFERENCE_CHANNEL
24+
int "Less Interference Channel"
25+
default 11
26+
range 1 13
27+
help
28+
Channel number to be used for Wi-Fi. This is the channel number that has the least interference in the area.
29+
30+
config SEND_FREQUENCY
31+
int "Send Frequency"
32+
default 25
33+
help
34+
Frequency at which the device sends data to the server in Hertz.
35+
36+
config CYCLE_TIMEOUT
37+
int "Cycle Timeout"
38+
default 10
39+
help
40+
Timeout for receiving packet from previous device in frequency cycles.
41+
endmenu

main/app_main.c

+12-10
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@
1616
/**
1717
* Returns the number of devices between the current and last device IDs.
1818
* @param device_id The current device ID.
19-
* @param last_id The last device ID.
19+
* @param previous_packet_device_id The previous packet sender device ID.
2020
* @return The number of devices between the current and last device IDs.
2121
*/
22-
uint8_t turns_away(const uint8_t device_id, const uint8_t last_id) {
22+
uint8_t turns_away(const uint8_t device_id, const uint8_t previous_packet_device_id) {
2323
// device 2, last device 0, 2 - 0 = 2
24-
if (device_id > last_id) {
25-
return device_id - last_id;
24+
if (device_id > previous_packet_device_id) {
25+
return device_id - previous_packet_device_id;
2626
}
2727
// device 0, last device 2, 3 - 2 + 0 = 1
28-
return TOTAL_DEVICES - last_id + device_id;
28+
return CONFIG_TOTAL_DEVICES - previous_packet_device_id + device_id;
2929
}
3030

3131
void app_main() {
@@ -37,10 +37,12 @@ void app_main() {
3737
}
3838
ESP_ERROR_CHECK(ret);
3939

40-
// Get the device ID from NVS or set the default ID
40+
#if CONFIG_IS_SET_DEVICE_ID
41+
store_device_id(CONFIG_DEVICE_ID);
42+
#endif
43+
4144
payload.device_id = get_device_id();
42-
ESP_LOGI(TAG, "Device ID: %d", payload.device_id);
43-
last_id = (payload.device_id + TOTAL_DEVICES - 1) % TOTAL_DEVICES;
45+
prev_packet_device_id = (payload.device_id + CONFIG_TOTAL_DEVICES - 1) % CONFIG_TOTAL_DEVICES;
4446

4547
// Initialize the payload length and index
4648
payload.csi_data_arr_len = 0;
@@ -75,10 +77,10 @@ void app_main() {
7577

7678
// Determine the multiplier based on the distance between the current and last device IDs
7779
// This multiplier is used to increase the timeout count for devices further away
78-
const int multiplier = turns_away(payload.device_id, last_id);
80+
const int multiplier = turns_away(payload.device_id, prev_packet_device_id);
7981

8082
// If the timeout count exceeds the timeout threshold, send CSI data
81-
if (timeout_count >= TIMEOUT * multiplier * multiplier) {
83+
if (timeout_count >= CONFIG_CYCLE_TIMEOUT * multiplier * multiplier) {
8284
ESP_LOGW(TAG, "ESP-NOW Timeout %d", timeout_count);
8385
send_csi_data(last_time_index++);
8486
timeout_count = 0;

main/app_nvs.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void store_device_id(const uint8_t device_id) {
1818
ESP_ERROR_CHECK(nvs_set_u8(nvs_handle, "device_id", device_id));
1919
ESP_ERROR_CHECK(nvs_commit(nvs_handle));
2020
nvs_close(nvs_handle);
21-
ESP_LOGI(TAG, "Device ID stored: %d", device_id);
21+
ESP_LOGI(TAG, "Setting device ID to %d", device_id);
2222
}
2323

2424
uint8_t get_device_id() {
@@ -27,11 +27,10 @@ uint8_t get_device_id() {
2727
uint8_t device_id;
2828
const esp_err_t ret = nvs_get_u8(nvs_handle, "device_id", &device_id);
2929
if (ret == ESP_ERR_NVS_NOT_FOUND) {
30-
ESP_LOGI(TAG, "Device ID not found, setting default ID: %d", DEFAULT_DEVICE_ID);
31-
store_device_id(DEFAULT_DEVICE_ID);
32-
} else {
33-
ESP_LOGI(TAG, "Device ID found: %d", device_id);
30+
ESP_LOGI(TAG, "Device ID of device has not been set. Shutting down...");
31+
esp_restart();
3432
}
33+
ESP_LOGI(TAG, "Using device ID %d", device_id);
3534
nvs_close(nvs_handle);
3635
return device_id;
3736
}

main/constants.h

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
#ifndef CONSTANTS_H
22
#define CONSTANTS_H
33

4-
#define CONFIG_LESS_INTERFERENCE_CHANNEL 11
5-
#define CONFIG_SEND_FREQUENCY 25
6-
#define TOTAL_DEVICES 4
7-
#define DEFAULT_DEVICE_ID 2
8-
#define TIMEOUT 10
94
#define MICROSECONDS_IN_SECOND 1000000
105
#define BROADCAST_ADDR {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
116
#define TAG "csi_send_recv"
127
#define CSI_DATA_LENGTH 256
13-
#define CSI_DATA_ARR_LEN 4
8+
#define CSI_DATA_ARR_LEN CONFIG_TOTAL_DEVICES
149
#define ORG_ID {0x18, 0xfe, 0x34}
1510
#define PAYLOAD_MAGIC_NUMBER {0x0F, 0xA7, 0x5A, 0x09}
1611

main/csi_data.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
payload_t payload;
1212

13-
uint8_t last_id;
13+
uint8_t prev_packet_device_id;
1414

1515
uint16_t last_time_index;
1616

@@ -100,7 +100,7 @@ void wifi_csi_rx_cb(void *ctx, wifi_csi_info_t *info) {
100100
}
101101

102102
// Check if the device ID is valid
103-
if (p->device_id >= TOTAL_DEVICES)
103+
if (p->device_id >= CONFIG_TOTAL_DEVICES)
104104
return;
105105

106106
if (payload.device_id != 0) {
@@ -133,13 +133,13 @@ void wifi_csi_rx_cb(void *ctx, wifi_csi_info_t *info) {
133133
if (payload.device_id == 0)
134134
print_payload(p);
135135

136-
last_id = p->device_id;
136+
prev_packet_device_id = p->device_id;
137137
last_time_index = p->time_index;
138138
timeout_count = 0;
139139
payload.csi_data_arr_len = min(payload.csi_data_arr_len + 1, CSI_DATA_ARR_LEN - 1);
140140
payload_index++;
141141

142-
if (payload.device_id == (p->device_id + 1) % TOTAL_DEVICES) {
142+
if (payload.device_id == (p->device_id + 1) % CONFIG_TOTAL_DEVICES) {
143143
// Print payload of sent packet if current device is the first device
144144
if (payload.device_id == 0)
145145
print_payload(&payload);

main/csi_data.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extern payload_t payload;
4040
/**
4141
* The last device ID from which CSI data was received.
4242
*/
43-
extern uint8_t last_id;
43+
extern uint8_t prev_packet_device_id;
4444

4545
/**
4646
* The time index of the last received CSI data.

sdkconfig.defaults

+6
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@ CONFIG_ESP_TASK_WDT_TIMEOUT_S=30
1717

1818
CONFIG_ESPTOOLPY_MONITOR_BAUD_921600B=y
1919
CONFIG_ESPTOOLPY_MONITOR_BAUD=921600
20+
21+
#
22+
# ESP-IDF Project Configuration
23+
#
24+
CONFIG_TOTAL_DEVICES=4
25+
CONFIG_DEVICE_ID=0

0 commit comments

Comments
 (0)