本帖最后由 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
|