trz0332 发表于 2023-12-5 14:01:35

ESPHOME202311月更新,可以修改mqtt参数了

本帖最后由 trz0332 于 2023-12-5 20:03 编辑

从最早开始用esphome,到现在,见证了它一步步变得完美
从最早得连wifi密码只能写死,到后面能ap模式练上去改密码
以前一直用esphome的原生api,后面发现设备多了。经常出现串设备。
只能转头mqtt协议了。mqtt也面临这同一个问题,mqtt的链接参数只能写死
直到202311月更新,出了一个text的空间。这下就好办了,研究了一个晚上,终于搞定了让esphome在web页面上修改mqtt参数
下面是效果图,web页面上有一个输入组件,填写对应的参数之后,点一下setupmqtt这个按钮,然后再点一下restart按钮,esphome重启之后就会用新的参数去连接mqtt服务器了但是切记,要点网页的restart按钮,断电重启不一定会保存参数。
点reset按钮可以让esphome恢复成默认参数

在hass的自动发现页面如下效果图



下面是喜闻乐见的代码部分了,将下面的配置插入到你的配置文件,你自己有 的部分就修改为我配置内容的,你自己配置文件没有的就把这些复制到你的配置文件。




substitutions:
mqtt_server_: mqtt.iot.org
mqtt_port_: '1883'
mqtt_user_: pi
mqtt_password_: raspberry
mqtt_prefix_: homeassistant



esphome:
on_boot:
    - priority: 300
      then:
      - lambda: |-
            MQTTMessage new_birth_message;
            new_birth_message.topic = App.get_name() + "/" + "status";
            new_birth_message.payload = "online";
            new_birth_message.qos = 1;
            new_birth_message.retain = true;
            id(mqtt_client).set_birth_message(std::move(new_birth_message));

            MQTTMessage new_last_message;
            new_last_message.topic =   App.get_name() + "/" + "status";
            new_last_message.payload = "offline";
            new_last_message.qos = 1;
            new_last_message.retain = true;
            id(mqtt_client).set_last_will(std::move(new_last_message));

            MQTTMessage new_shutdown_message;
            new_shutdown_message.topic =   App.get_name() + "/" + "status";
            new_shutdown_message.payload = "offline";
            new_shutdown_message.qos = 1;
            new_shutdown_message.retain = true;
            id(mqtt_client).set_shutdown_message(std::move(new_shutdown_message));

            id(mqtt_client).set_topic_prefix(App.get_name());

            ESP_LOGE("ble_adv", "address: %s", id(mqtt_server).state.c_str());
            id(mqtt_client).set_broker_address(id(g_address));
            id(mqtt_client).set_broker_port(id(g_port));
            id(mqtt_client).set_username(id(g_username));
            id(mqtt_client).set_password(id(g_password));

            id(mqtt_client).set_discovery_info(id(g_prefix).c_str(), mqtt::MQTT_MAC_ADDRESS_UNIQUE_ID_GENERATOR, mqtt::MQTT_DEVICE_NAME_OBJECT_ID_GENERATOR, true);

    - priority: -100
      then:
      - lambda: |-
            id(getmqttconfig).press();




mqtt:
id: mqtt_client
broker: '$mqtt_server_'
port: '$mqtt_port_'
username: '$mqtt_user_'
password: '$mqtt_password_'
discovery_prefix: '$mqtt_prefix_'
discovery_object_id_generator: device_name
discovery_unique_id_generator: mac
log_topic:
    topic: "${device_name}/logs"
    level: debug
discovery: true

globals:
- id: g_address
    type:std::string
    initial_value: '"$mqtt_server_"'
    restore_value: true

- id: g_port
    type: int
    initial_value: '$mqtt_port_'
    restore_value: true

- id: g_username
    type:std::string
    initial_value: '"$mqtt_user_"'
    restore_value: true

- id: g_password
    type:std::string
    initial_value: '"$mqtt_password_"'
    restore_value: true

- id: g_prefix
    type:std::string
    initial_value: '"$mqtt_prefix_"'
    restore_value: true



text:
- platform: template
    name: "1mqtt_server"
    id: 'mqtt_server'
    optimistic: true
    min_length: 0
    max_length: 100
    mode: text
    disabled_by_default: true
    discovery: false
- platform: template
    name: "2mqtt_port"
    id: 'mqtt_port'
    optimistic: true
    min_length: 0
    max_length: 100
    mode: text
    disabled_by_default: true
    discovery: false
- platform: template
    name: "3mqtt_user"
    id: 'mqtt_user'
    optimistic: true
    min_length: 0
    max_length: 100
    mode: text
    disabled_by_default: true
    discovery: false
- platform: template
    name: "4mqtt_password"
    id: 'mqtt_password'
    optimistic: true
    min_length: 0
    max_length: 100
    mode: password
    disabled_by_default: true
    discovery: false
- platform: template
    name: "5mqtt_prefix"
    id: 'mqtt_prefix'
    optimistic: true
    min_length: 0
    max_length: 100
    mode: text
    disabled_by_default: true
    discovery: false


button:
- platform: factory_reset
    name: "Reset"
- platform: restart
    name: "Restart"
- platform: template
    name: "6getmqttconfig"
    id: "getmqttconfig"
    internal: false
    disabled_by_default: true
    discovery: false
    on_press:
      then:
      lambda: |-
            ESP_LOGE("ble_adv", "address: %s", id(g_address).c_str());
            auto call = id(mqtt_server).make_call();
            call.set_value(id(g_address).c_str());
            call.perform();

            auto call1 = id(mqtt_user).make_call();
            call1.set_value(id(g_username).c_str());
            call1.perform();

            auto call2 = id(mqtt_password).make_call();
            call2.set_value(id(g_password).c_str());
            call2.perform();

            auto call3 = id(mqtt_prefix).make_call();
            call3.set_value(id(g_prefix).c_str());
            call3.perform();

            auto call4 = id(mqtt_port).make_call();
            call4.set_value(std::to_string(id(g_port)));
            call4.perform();



- platform: template
    name: "7setupmqtt"
    id: "setupmqtt"
    internal: false
    disabled_by_default: true
    discovery: false
    on_press:
      then:
      lambda: |-
            ESP_LOGE("ble_adv", "address: %s", id(mqtt_server).state.c_str());
            id(g_address)=id(mqtt_server).state.c_str();
            id(g_username)=id(mqtt_user).state.c_str();
            id(g_password)=id(mqtt_password).state.c_str();
            id(g_prefix)=id(mqtt_prefix).state.c_str();
            id(g_port)=std::stoi(id(mqtt_port).state.c_str());

            id(mqtt_client).set_broker_address(id(g_address));
            id(mqtt_client).set_broker_port(id(g_port));
            id(mqtt_client).set_username(id(g_username));
            id(mqtt_client).set_password(id(g_password));

自行修改这下面的内容,这里是原始mqtt的配置参数
substitutions:
mqtt_server_: mqtt.iot.org
mqtt_port_: '1883'
mqtt_user_: pi
mqtt_password_: xxxxxxx
mqtt_prefix_: homeassistant




**** Hidden Message *****

ghostist 发表于 2023-12-5 14:20:48

原生api串设备的原因是什么?我在log里也有这种情况,倒不影响设备工作
不清楚如何修复

yybl 发表于 2023-12-5 15:23:05


原生api串设备 是啥意思,我没理解。

bugensui 发表于 2023-12-5 16:08:25

esp不是原生支持ha吗,为什么要走一遍mqtt,有什么区别

sorrypqa 发表于 2023-12-5 16:31:14

什么叫做串设备?我用了40多个,没见过什么异常啊

trz0332 发表于 2023-12-5 17:45:15

sorrypqa 发表于 2023-12-5 16:31
什么叫做串设备?我用了40多个,没见过什么异常啊

做了macIP绑定就不会出问题,如果是dhcp,而且添加的时候用IP添加,路由器重启之后这个esphome-A的ip换成了ESPHOMEB的IP,这个时候就会出问题了。

sorrypqa 发表于 2023-12-5 18:21:38

trz0332 发表于 2023-12-5 17:45
做了macIP绑定就不会出问题,如果是dhcp,而且添加的时候用IP添加,路由器重启之后这个esphome-A的ip换成 ...

哦!原来是这样,我习惯所有网络设备都由DHCP分配静态IP,但不一定做mac与IP绑定

hunterfox 发表于 2023-12-5 20:31:45

到这刷到了。膜拜。思路大开:lol

pengguilian 发表于 2023-12-5 22:05:45

感谢楼主的分享!这天书般的配置文件,完全看不懂

sirakawa 发表于 2023-12-5 22:47:47

trz0332 发表于 2023-12-5 17:45
做了macIP绑定就不会出问题,如果是dhcp,而且添加的时候用IP添加,路由器重启之后这个esphome-A的ip换成 ...

原来如此- -一直靠mdns的倒是没碰上。
所有IP添加的设备都固定DHCP了;P
页: [1] 2 3
查看完整版本: ESPHOME202311月更新,可以修改mqtt参数了