]> www.infradead.org Git - users/dwmw2/esp32-pool.git/commitdiff
Add humidity fan controller for toilet, tweak bathroom to match
authorDavid Woodhouse <dwmw@amazon.co.uk>
Mon, 5 Aug 2024 19:41:53 +0000 (20:41 +0100)
committerDavid Woodhouse <dwmw@amazon.co.uk>
Mon, 5 Aug 2024 19:42:25 +0000 (20:42 +0100)
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...

bathroom.yaml
toilet.yaml [new file with mode: 0644]

index 232bac49ad349031d4613564736a91e2a22187ce..c057ccfa1d6358bf2e6d6eb91f3122159db2a2a3 100644 (file)
@@ -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 (file)
index 0000000..36d2b37
--- /dev/null
@@ -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<int>();
+
+            // 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<float>();
+                  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