From: David Woodhouse Date: Mon, 5 Aug 2024 19:41:53 +0000 (+0100) Subject: Add humidity fan controller for toilet, tweak bathroom to match X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=87a704d0ab67797cd4e4072335c7c05d64a481da;p=users%2Fdwmw2%2Fesp32-pool.git Add humidity fan controller for toilet, tweak bathroom to match Turns out WiFi reception here is even worse than in the bathroom. By switching to esp-idf framework, turning off low power mode *and* setting the preferred BSSID manually, it does actually work. Update the bathroom to match. Untested until that actually does get back on the network, or I go prod at it with a serial cable... --- diff --git a/bathroom.yaml b/bathroom.yaml index 232bac4..c057ccf 100644 --- a/bathroom.yaml +++ b/bathroom.yaml @@ -11,13 +11,14 @@ esphome: esp32: board: esp32-gateway framework: - type: arduino + type: esp-idf # Enable logging logger: level: DEBUG ota: + platform: esphome password: !secret ota_upgrade_pw external_components: @@ -27,8 +28,14 @@ external_components: components: [syslog] wifi: - ssid: !secret wifi_ssid - password: !secret wifi_pw + power_save_mode: none + networks: + - ssid: !secret wifi_ssid + password: !secret wifi_pw + bssid: !secret wndr3800_bssid + priority: 1 + - ssid: !secret wifi_ssid + password: !secret wifi_pw script: # Publish a value to Domoticz as an nvalue @@ -94,7 +101,7 @@ globals: - id: hum_threshold type: float - initial_value: '50.0' + initial_value: '70.0' restore_value: true - id: fan_delay @@ -116,8 +123,8 @@ time: } -#network: -# enable_ipv6: true +network: + enable_ipv6: true mqtt: broker: !secret mqtt_server diff --git a/toilet.yaml b/toilet.yaml new file mode 100644 index 0000000..36d2b37 --- /dev/null +++ b/toilet.yaml @@ -0,0 +1,227 @@ +# +# https://www.aliexpress.com/item/1005004099215436.html +# +# GPIO23: Onboard blue LED (used to show MQTT connectivity) +# GPIO16: Onboard relay (toilet fan) +# GPIO13: DHT22 + +esphome: + name: toilet + +esp32: + board: esp32-gateway + framework: + type: esp-idf + +# Enable logging +logger: + level: DEBUG + +ota: + platform: esphome + password: !secret ota_upgrade_pw + +external_components: + - source: + type: local + path: ../git/esphome_syslog/components + components: [syslog] + +wifi: + power_save_mode: none + networks: + - ssid: !secret wifi_ssid + password: !secret wifi_pw + bssid: !secret garden_bssid + priority: 1 + - ssid: !secret wifi_ssid + password: !secret wifi_pw + +script: + # Publish a value to Domoticz as an nvalue + - id: tell_domo_nvalue + mode: queued + parameters: + udevice: int + nvalue: int + then: + lambda: |- + id(mqtt_client).publish_json("domoticz/in", [=](JsonObject root) { + root["command"] = "udevice"; + root["idx"] = udevice; + root["nvalue"] = nvalue; + }); + + # Publish a value to Domoticz as an svalue + - id: tell_domo_svalue + mode: queued + parameters: + udevice: int + svalue: string + then: + lambda: |- + id(mqtt_client).publish_json("domoticz/in", [=](JsonObject root) { + root["command"] = "udevice"; + root["idx"] = udevice; + root["svalue"] = svalue; + }); + + # Publishto Domoticz as nvalue and svalues + - id: tell_domo_nsvalues + mode: queued + parameters: + udevice: int + nvalue: int + svalue: string + then: + lambda: |- + id(mqtt_client).publish_json("domoticz/in", [=](JsonObject root) { + root["command"] = "udevice"; + root["idx"] = udevice; + root["nvalue"] = nvalue; + root["svalue"] = svalue; + }); + +# # Enable fallback hotspot (captive portal) in case wifi connection fails +# ap: +# ssid: "Pool Fallback Hotspot" +# password: "JWWHJb38UzxY" +# +# manual_ip: !include poolup.yaml +# +#captive_portal: + +syslog: + ip_address: !secret syslog_ip + +globals: + - id: relay_off_time + type: time_t + initial_value: '0' + + - id: hum_threshold + type: float + initial_value: '70.0' + restore_value: true + + - id: fan_delay + type: int + initial_value: '600' + restore_value: true + +time: + - platform: sntp + id: sntp_time + servers: !secret ntp_servers + on_time: + - seconds: /5 + then: + lambda: |- + //ESP_LOGD("time", "off time %ld", (long)id(relay_off_time)); + if (id(relay_off_time) && id(relay_off_time) < ::time(NULL)) { + id(fan_relay).turn_off(); + } + + +network: + enable_ipv6: true + +mqtt: + broker: !secret mqtt_server + port: 1884 + discovery_prefix: ${mqtt_prefix}/homeassistant + log_topic: ${mqtt_prefix}/logs + username: "pool" + password: !secret pool_mqtt_pw + id: mqtt_client + on_connect: + then: + - light.turn_on: blue_led + - delay: 2s # Too soon and the first messages don't get through! + - lambda: |- + id(tell_domo_nvalue)->execute(900, id(fan_relay).state); + id(tell_domo_svalue)->execute(901, std::to_string(id(hum_threshold))); + + on_disconnect: + then: + - light.turn_off: blue_led + + on_json_message: + - topic: domoticz/out + then: + - lambda: |- + int idx = x["idx"]; + int nvalue = x["nvalue"].as(); + + // ESP_LOGD("on_json_message", x["name"]); + switch (idx) { + case 900: /* Fan relay */ + if (nvalue) { + id(fan_relay).turn_on(); + id(relay_off_time) = ::time(NULL) + id(fan_delay); + } else { + id(fan_relay).turn_off(); + } + break; + + case 901: /* Humidity threshold */ + id(hum_threshold) = x["svalue"].as(); + ESP_LOGD("mqtt", "Got hum_threshold %f", id(hum_threshold)); + break; + } + + +light: + - platform: binary + name: "Blue LED" + id: blue_led + output: led_output + +output: + - id: led_output + platform: gpio + pin: GPIO23 + +switch: + - platform: gpio + name: "Fan" + id: fan_relay + pin: GPIO16 + on_turn_on: + then: + - lambda: |- + id(tell_domo_nvalue)->execute(900, 1); + id(relay_off_time) = 0; + on_turn_off: + then: + - lambda: |- + id(tell_domo_nvalue)->execute(900, 0); + id(relay_off_time) = 0; + +sensor: + - platform: dht + pin: GPIO13 + model: DHT22 + temperature: + name: "Toilet Temperature" + on_value: + then: + lambda: |- + if (!isnan(x)) + id(tell_domo_svalue)->execute(899, std::to_string(x)); + humidity: + name: "Toilet Humidity" + on_value: + then: + lambda: |- + const char *hum_stat; + if (x > id(hum_threshold)) { + id(fan_relay).turn_on(); + id(relay_off_time) = ::time(NULL) + id(fan_delay); + hum_stat = "3"; // Wet + } else { + hum_stat = "2"; // Dry + } + if (!isnan(x)) + id(tell_domo_nsvalues)->execute(898, x, hum_stat); + update_interval: 10s