Skip to content

Commit 191caa2

Browse files
authored
Merge pull request #1 from konnected-io/prov
Add WiFi provisioning and connection instructions
2 parents f32ba8b + 9f91190 commit 191caa2

File tree

10 files changed

+47
-170
lines changed

10 files changed

+47
-170
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ dependencies.lock
44
.cache
55
.aider*
66
sdkconfig.old
7+
sdkconfig
8+
.vscode
9+
.clangd
10+

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
[submodule "components/gdolib"]
22
path = components/gdolib
33
url = https://github.com/konnected-io/gdolib.git
4+
[submodule "components/nvs_wifi_connect"]
5+
path = components/nvs_wifi_connect
6+
url = https://github.com/h2zero/nvs_wifi_connect.git
47
[submodule "esp-homekit-sdk"]
58
path = esp-homekit-sdk
69
url = https://github.com/espressif/esp-homekit-sdk.git

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Konnected HomeKit Firmware
2+
This is the firmware for Konnected's HomeKit-enabled door opener.
3+
4+
## Submodules
5+
6+
This repository uses git submodules. After cloning, be sure to initialize and update them:
7+
8+
```sh
9+
git submodule update --init --recursive
10+
```
11+
12+
## Provision WiFi and Add Accessory to HomeKit
13+
14+
This firmware uses the `nvs_wifi_connect` component to manage WiFi connections. The relevant code can be found in `main/wifi.cpp`.
15+
16+
The `nvs_wifi_connect` component will start an HTTP server for configuration if the device is not connected to a WiFi network. Connect to the access point created by the device with SSID `konnected-blaq-hk` and open a web browser to `http://192.168.4.1` and enter your WiFi credentials, then click `Write and Reboot`.
17+
18+
After connecting to WiFi and restarting wait about 10 seconds then open the Home app on your iOS device to add the accessory. Go to "Add Accessory" and then "more options..." to find the accessory on the network. Click on the found accessory and enter the setup code `251-02-023` when prompted and follow the instructions to complete the setup.

components/nvs_wifi_connect

Submodule nvs_wifi_connect added at 8f9e172

main/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
idf_component_register(SRCS "konnected-homekit.cpp" "homekit.cpp" "wifi.cpp"
22
INCLUDE_DIRS "."
3-
REQUIRES gdolib esp_hap_core esp_hap_apple_profiles esp_wifi nvs_flash wifi_provisioning
3+
REQUIRES gdolib esp_hap_core esp_hap_apple_profiles esp_wifi nvs_flash wifi_provisioning nvs_wifi_connect
44
)

main/homekit.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ void homekit_task_entry(void* ctx) {
140140

141141
hap_start();
142142

143-
wifi_init_sta();
144-
145143
GDOEvent e;
146144

147145
hap_char_t* dest = NULL;

main/konnected-homekit.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,24 +86,6 @@ static void gdo_event_handler(const gdo_status_t* status, gdo_cb_event_t event,
8686

8787
extern "C" void app_main(void)
8888
{
89-
gpio_config_t io_conf;
90-
io_conf.intr_type = GPIO_INTR_DISABLE;
91-
io_conf.mode = GPIO_MODE_INPUT;
92-
io_conf.pin_bit_mask = (1ULL << GPIO_NUM_5);
93-
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
94-
io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
95-
gpio_config(&io_conf);
96-
97-
int gpio_level = gpio_get_level(GPIO_NUM_5);
98-
if (gpio_level == 0) {
99-
ESP_LOGI(TAG, "reset is low, entering infinite loop");
100-
while (1) {
101-
vTaskDelay(portMAX_DELAY);
102-
}
103-
} else {
104-
ESP_LOGI(TAG, "reset not triggered");
105-
}
106-
10789
gdo_config_t gdo_conf;
10890
gdo_conf.invert_uart = true;
10991
gdo_conf.obst_from_status = true;

main/wifi.cpp

Lines changed: 16 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -1,155 +1,23 @@
1-
#include "freertos/FreeRTOS.h"
2-
#include "freertos/task.h"
3-
#include "esp_wifi.h"
4-
#include "esp_event.h"
5-
#include "esp_log.h"
6-
#include <cstring>
7-
8-
#include <wifi_provisioning/manager.h>
9-
#include <wifi_provisioning/scheme_ble.h>
1+
#include <esp_log.h>
2+
#include <esp_wifi.h>
3+
#include <nvs_wifi_connect.h>
104

115
static const char* TAG = "wifi";
12-
static const int WIFI_CONNECTED_EVENT = BIT0;
13-
static EventGroupHandle_t wifi_event_group;
14-
15-
static void get_device_service_name(char *service_name, size_t max)
16-
{
17-
uint8_t eth_mac[6];
18-
const char *ssid_prefix = "PROV_";
19-
esp_wifi_get_mac(WIFI_IF_STA, eth_mac);
20-
snprintf(service_name, max, "%s%02X%02X%02X",
21-
ssid_prefix, eth_mac[3], eth_mac[4], eth_mac[5]);
22-
}
23-
24-
static void event_handler(void* arg, esp_event_base_t event_base,
25-
int32_t event_id, void* event_data)
26-
{
27-
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
28-
esp_wifi_connect();
29-
ESP_LOGI(TAG, "Connecting to Wi-Fi...");
30-
31-
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_CONNECTED) {
32-
esp_netif_create_ip6_linklocal((esp_netif_t *)arg);
33-
34-
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
35-
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
36-
ESP_LOGI(TAG, "Connected with IP Address:" IPSTR, IP2STR(&event->ip_info.ip));
37-
xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_EVENT);
38-
39-
} else if (event_base == IP_EVENT && event_id == IP_EVENT_GOT_IP6) {
40-
ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data;
41-
ESP_LOGI(TAG, "Connected with IPv6 Address:" IPV6STR, IPV62STR(event->ip6_info.ip));
42-
43-
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
44-
ESP_LOGI(TAG, "Disconnected from Wi-Fi. Reconnecting...");
45-
esp_wifi_connect();
466

47-
} else if (event_base == WIFI_PROV_EVENT) {
48-
49-
switch (event_id) {
50-
51-
case WIFI_PROV_START:
52-
ESP_LOGI(TAG, "Provisioning started");
53-
break;
54-
55-
case WIFI_PROV_CRED_RECV: {
56-
wifi_sta_config_t *wifi_sta_cfg = (wifi_sta_config_t *)event_data;
57-
ESP_LOGI(TAG, "Received Wi-Fi credentials"
58-
"\n\tSSID : %s\n\tPassword : %s",
59-
(const char *) wifi_sta_cfg->ssid,
60-
(const char *) wifi_sta_cfg->password);
61-
break;
62-
}
63-
64-
case WIFI_PROV_CRED_FAIL: {
65-
wifi_prov_sta_fail_reason_t *reason = (wifi_prov_sta_fail_reason_t *)event_data;
66-
ESP_LOGE(TAG, "Provisioning failed!\n\tReason : %s"
67-
"\n\tPlease reset to factory and retry provisioning",
68-
(*reason == WIFI_PROV_STA_AUTH_ERROR) ?
69-
"Wi-Fi station authentication failed" : "Wi-Fi access-point not found");
70-
break;
71-
}
72-
73-
case WIFI_PROV_CRED_SUCCESS:
74-
ESP_LOGI(TAG, "Provisioning successful");
75-
break;
76-
77-
case WIFI_PROV_END:
78-
/* De-initialize manager once provisioning is finished */
79-
wifi_prov_mgr_deinit();
80-
break;
81-
default:
82-
break;
83-
}
7+
void app_wifi_init(void) {
8+
if (nvs_wifi_connect() != ESP_OK) {
9+
nvs_wifi_connect_start_http_server(NVS_WIFI_CONNECT_MODE_RESTART_ESP32, nullptr);
10+
return;
8411
}
85-
}
86-
87-
void app_wifi_init(void)
88-
{
89-
/* Initialize TCP/IP */
90-
esp_netif_init();
91-
92-
/* Initialize the event loop */
93-
ESP_ERROR_CHECK(esp_event_loop_create_default());
94-
wifi_event_group = xEventGroupCreate();
95-
96-
/* Initialize Wi-Fi including netif with default config */
97-
esp_netif_t *wifi_netif = esp_netif_create_default_wifi_sta();
98-
99-
/* Register our event handler for Wi-Fi, IP and Provisioning related events */
100-
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, wifi_netif));
101-
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
102-
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &event_handler, NULL));
103-
104-
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
105-
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
106-
}
107-
108-
void wifi_init_sta()
109-
{
110-
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
111-
ESP_ERROR_CHECK(esp_wifi_start());
112-
}
113-
114-
esp_err_t app_wifi_start(TickType_t ticks_to_wait)
115-
{
116-
wifi_prov_mgr_config_t config = {};
117-
config.scheme = wifi_prov_scheme_ble;
118-
config.scheme_event_handler = WIFI_PROV_SCHEME_BLE_EVENT_HANDLER_FREE_BTDM;
119-
120-
ESP_ERROR_CHECK(wifi_prov_mgr_init(config));
121-
122-
bool provisioned = false;
123-
ESP_ERROR_CHECK(wifi_prov_mgr_is_provisioned(&provisioned));
124-
125-
if (!provisioned) {
126-
ESP_LOGI(TAG, "Starting provisioning");
127-
esp_event_handler_register(WIFI_PROV_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL);
128-
char service_name[12];
129-
get_device_service_name(service_name, sizeof(service_name));
130-
131-
wifi_prov_security_t security = WIFI_PROV_SECURITY_0;
132-
133-
uint8_t custom_service_uuid[] = {
134-
/* This is a random uuid. This can be modified if you want to change the BLE uuid. */
135-
/* 12th and 13th bit will be replaced by internal bits. */
136-
0xb4, 0xdf, 0x5a, 0x1c, 0x3f, 0x6b, 0xf4, 0xbf,
137-
0xea, 0x4a, 0x82, 0x03, 0x04, 0x90, 0x1a, 0x02,
138-
};
139-
esp_err_t err = wifi_prov_scheme_ble_set_service_uuid(custom_service_uuid);
140-
if (err != ESP_OK) {
141-
ESP_LOGE(TAG, "wifi_prov_scheme_ble_set_service_uuid failed %d", err);
142-
return err;
143-
}
144-
145-
ESP_ERROR_CHECK(wifi_prov_mgr_start_provisioning(security, nullptr, service_name, nullptr));
146-
ESP_LOGI(TAG, "Provisioning Started. Name : %s", service_name);
12+
wifi_mode_t mode;
13+
esp_wifi_get_mode(&mode);
14+
if (mode == WIFI_MODE_STA) {
15+
ESP_LOGI(TAG, "Connected in STA mode");
16+
} else if (mode == WIFI_MODE_AP) {
17+
ESP_LOGI(TAG, "Running in AP mode");
18+
nvs_wifi_connect_start_http_server(NVS_WIFI_CONNECT_MODE_RESTART_ESP32, nullptr);
14719
} else {
148-
ESP_LOGI(TAG, "Already provisioned, starting Wi-Fi STA");
149-
wifi_prov_mgr_deinit();
150-
wifi_init_sta();
20+
ESP_LOGI(TAG, "WiFi not configured");
15121
}
152-
/* Wait for Wi-Fi connection */
153-
xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_EVENT, false, true, ticks_to_wait);
154-
return ESP_OK;
15522
}
23+

main/wifi.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#ifndef WIFI_H
22
#define WIFI_H
33

4-
void wifi_init_sta();
54
void app_wifi_init();
65

76
#endif // WIFI_H

sdkconfig.defaults

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y
77
CONFIG_BT_ENABLED=y
88
CONFIG_BT_NIMBLE_ENABLED=y
99
CONFIG_LWIP_MAX_SOCKETS=16
10+
CONFIG_HTTPD_WS_SUPPORT=y
11+
CONFIG_DEFAULT_NVS_WIFI_CONNECT_URI="/"
12+
CONFIG_DEFAULT_NVS_WIFI_CONNECT_WS_URI="/ws"
13+
CONFIG_DEFAULT_AP_ESP_WIFI_SSID="konnected-blaq-hk"

0 commit comments

Comments
 (0)