feat: 通过 mDNS 发布服务节点。

This commit is contained in:
Ivan Li 2023-04-24 20:41:44 +08:00
parent 2da0f224a1
commit 7bff8e2c17
7 changed files with 58 additions and 32 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ build/
sdkconfig
sdkconfig.old
.vscode/
managed_components/

View File

@ -1,9 +1,15 @@
dependencies:
espressif/mdns:
component_hash: 3f3bf8ff67ec99dde4e935637af087ebc899b9fee396ffa1c9138d3775f1aca4
source:
service_url: https://api.components.espressif.com/
type: service
version: 1.0.9
idf:
component_hash: null
source:
type: idf
version: 4.4.4
manifest_hash: dcf4d39b94252de130019eadceb989d72b0dbc26b552cfdcbb50f6da531d2b92
version: 5.0.1
manifest_hash: d23f3b15e8b95dfe5da6cf83768d24158a3a0222f95694b1fa09c3ca8602f79f
target: esp32c3
version: 1.0.0

View File

@ -1,2 +1,2 @@
idf_component_register(SRCS "hw-ms03.c" "app_nvs.c" "apds_9960.c" "pca9555.c" "i2c.c" "asr_pro.c" "ci_03t.c" "ui_input.c" "ambient_light.c" "temperature.c" "embedded_display.c" "mqtt.c" "main.c" "wifi.c" "light.c" "mqtt.c" "led_strip_encoder/led_strip_encoder.c"
idf_component_register(SRCS "service_discovery.c" "app_nvs.c" "apds_9960.c" "pca9555.c" "i2c.c" "asr_pro.c" "ci_03t.c" "ui_input.c" "ambient_light.c" "temperature.c" "embedded_display.c" "mqtt.c" "main.c" "wifi.c" "light.c" "mqtt.c" "led_strip_encoder/led_strip_encoder.c"
INCLUDE_DIRS ".")

View File

@ -1,27 +0,0 @@
#pragma once
#include "driver/gpio.h"
#include "freeRTOS/FreeRTOS.h"
#define HW_MS03_INT_GPIO 6
#define BEEP_GPIO 7
void hw_ms03_int_handler(void *arg) {
gpio_set_level(BEEP_GPIO, gpio_get_level(HW_MS03_INT_GPIO));
}
void hw_ms03_init() {
gpio_config_t io_conf = {};
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pull_up_en = 0;
io_conf.pull_down_en = 1;
io_conf.intr_type = GPIO_INTR_ANYEDGE;
io_conf.pin_bit_mask = 1ULL << HW_MS03_INT_GPIO;
gpio_config(&io_conf);
io_conf.mode = GPIO_MODE_OUTPUT;
io_conf.pin_bit_mask = 1ULL << BEEP_GPIO;
io_conf.pull_down_en = 0;
gpio_config(&io_conf);
gpio_isr_handler_add(HW_MS03_INT_GPIO, hw_ms03_int_handler, NULL);
}

17
main/idf_component.yml Normal file
View File

@ -0,0 +1,17 @@
## IDF Component Manager Manifest File
dependencies:
espressif/mdns: "^1.0.9"
## Required IDF version
idf:
version: ">=5.0.1"
# # Put list of dependencies here
# # For components maintained by Espressif:
# component: "~1.0.0"
# # For 3rd party components:
# username/component: ">=1.0.0,<2.0.0"
# username2/component2:
# version: "~1.0.0"
# # For transient dependencies `public` flag can be set.
# # `public` flag doesn't have an effect dependencies of the `main` component.
# # All dependencies of `main` are public by default.
# public: true

View File

@ -5,12 +5,12 @@
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "hw-ms03.c"
#include "i2c.c"
#include "light.c"
#include "mqtt.c"
#include "pca9555.c"
#include "sdkconfig.h"
#include "service_discovery.c"
#include "temperature.c"
#include "ui_input.c"
#include "wifi.c"
@ -27,7 +27,7 @@ void app_main(void) {
init_display();
display_print8_str(0, 0, "Ambient Light");
// hw_ms03_init();
ci_03t_init();
apds_9960_init();
apds_9960_auto_fetch();
@ -41,6 +41,7 @@ void app_main(void) {
if (connect_wifi()) {
light_play(light_mode_idle);
}
service_discovery_init();
vTaskDelay(pdMS_TO_TICKS(1000));
mqtt_app_start();
if (waiting_for_mqtt_connected()) {

28
main/service_discovery.c Normal file
View File

@ -0,0 +1,28 @@
#pragma once
#include "esp_err.h"
#include "esp_log.h"
#include "mdns.h"
static const char *SERVICE_DISCOVERY_TAG = "SERVICE_DISCOVERY_TAG";
static void service_discovery_init() { // 初始化 mDNS 服务
esp_err_t err = mdns_init();
if (err) {
ESP_LOGE(SERVICE_DISCOVERY_TAG, "MDNS Init failed: %d\n", err);
return;
}
// 设置 hostname
mdns_hostname_set("ambient-light-board");
// 设置默认实例
mdns_instance_name_set("ivan");
// 添加服务
mdns_service_add(NULL, "_ambient_light", "_udp", 23042, NULL, 0);
mdns_service_add(NULL, "_http", "_tcp", 80, NULL, 0);
// 注意:必须先添加服务,然后才能设置其属性
// web 服务器使用自定义的实例名
mdns_service_instance_name_set("_ambientlight_web", "_tcp",
"Ambient Light Board Web Server");
}