This commit is contained in:
@@ -1,51 +0,0 @@
|
||||
esphome:
|
||||
name: bath_light
|
||||
platform: ESP8266
|
||||
board: esp01_1m
|
||||
|
||||
wifi:
|
||||
ssid: !secret wifi_24_name
|
||||
password: !secret wifi_24_pass
|
||||
|
||||
# Enable fallback hotspot (captive portal) in case wifi connection fails
|
||||
ap:
|
||||
ssid: "BathLight Fallback Hotspot"
|
||||
password: !secret esphome_captive_pass
|
||||
|
||||
logger:
|
||||
|
||||
api:
|
||||
password: !secret esphome_pass
|
||||
|
||||
ota:
|
||||
password: !secret esphome_pass
|
||||
|
||||
binary_sensor:
|
||||
- platform: gpio
|
||||
pin:
|
||||
number: GPIO14
|
||||
mode: INPUT_PULLUP
|
||||
inverted: True
|
||||
id: button_1
|
||||
on_press:
|
||||
then:
|
||||
- light.toggle: light_1
|
||||
|
||||
- platform: status
|
||||
name: "Bath status"
|
||||
|
||||
output:
|
||||
- platform: gpio
|
||||
pin: GPIO12
|
||||
id: relay_1
|
||||
|
||||
light:
|
||||
- platform: binary
|
||||
name: "bath"
|
||||
id: light_1
|
||||
output: relay_1
|
||||
|
||||
status_led:
|
||||
pin:
|
||||
number: GPIO13
|
||||
inverted: yes
|
||||
@@ -1,40 +0,0 @@
|
||||
# Need to turn off logging because senseair uses uart and have conflicts with logging
|
||||
logger:
|
||||
baud_rate: 0
|
||||
|
||||
esphome:
|
||||
name: senseair
|
||||
platform: ESP8266
|
||||
board: d1_mini
|
||||
|
||||
wifi:
|
||||
ssid: !secret wifi_24_name
|
||||
password: !secret wifi_24_pass
|
||||
|
||||
# Enable fallback hotspot (captive portal) in case wifi connection fails
|
||||
ap:
|
||||
ssid: "Senseair Fallback Hotspot"
|
||||
password: !secret esphome_captive_pass
|
||||
|
||||
captive_portal:
|
||||
|
||||
# Enable Home Assistant API
|
||||
api:
|
||||
password: !secret esphome_pass
|
||||
|
||||
ota:
|
||||
password: !secret esphome_pass
|
||||
|
||||
# senseair communication uart
|
||||
uart:
|
||||
rx_pin: D7
|
||||
tx_pin: D8
|
||||
baud_rate: 9600
|
||||
|
||||
# hass sensor =)
|
||||
sensor:
|
||||
- platform: senseair
|
||||
co2:
|
||||
name: "SenseAir CO2 Value"
|
||||
update_interval: 60s
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
#include "esphome.h"
|
||||
#include <Bounce2.h>
|
||||
|
||||
#define CounterNum 2
|
||||
#define COUNTER_COLD 0
|
||||
#define COUNTER_HOT 1
|
||||
|
||||
static const char *TAG = "watercounter.sensor";
|
||||
|
||||
class WaterCountersSensor : public Component, public Sensor, public CustomAPIDevice {
|
||||
protected:
|
||||
int CounterPin[CounterNum] = {D1,D2};
|
||||
float CounterValues[CounterNum] = {};
|
||||
Bounce CounterBouncer[CounterNum] = {};
|
||||
|
||||
public:
|
||||
Sensor *cold_sensor = new Sensor();
|
||||
Sensor *hot_sensor = new Sensor();
|
||||
bool initReadValues = false;
|
||||
|
||||
globals::GlobalsComponent<float> *counter_cold_value;
|
||||
globals::GlobalsComponent<float> *counter_hot_value;
|
||||
|
||||
void setup() override {
|
||||
register_service(&WaterCountersSensor::on_set_counter_values, "set_counter_values",
|
||||
{"cold_value", "hot_value"});
|
||||
|
||||
// init flash variables
|
||||
counter_cold_value = new globals::GlobalsComponent<float>();
|
||||
App.register_component(counter_cold_value);
|
||||
counter_cold_value->set_restore_value(1547432152);
|
||||
|
||||
counter_hot_value = new globals::GlobalsComponent<float>();
|
||||
App.register_component(counter_hot_value);
|
||||
counter_hot_value->set_restore_value(2087162808);
|
||||
|
||||
ESP_LOGD(TAG, "Initializing counters.");
|
||||
for (int i=0; i<CounterNum; i++) {
|
||||
pinMode(CounterPin[i], INPUT_PULLUP);
|
||||
CounterBouncer[i].attach(CounterPin[i]);
|
||||
CounterBouncer[i].interval(100);
|
||||
}
|
||||
ESP_LOGD(TAG, "Initialize done.");
|
||||
}
|
||||
|
||||
void loop() override {
|
||||
if (is_connected() && !initReadValues) {
|
||||
ESP_LOGD(TAG, "Reading counters from fs.");
|
||||
for (int i=0; i<CounterNum; i++) {
|
||||
readCounterFromFile(i);
|
||||
sendCounterValue(i);
|
||||
}
|
||||
initReadValues = true;
|
||||
}
|
||||
for (int i=0; i<CounterNum; i++) {
|
||||
boolean changed = CounterBouncer[i].update();
|
||||
if ( changed ) {
|
||||
int value = CounterBouncer[i].read();
|
||||
if ( value == LOW) {
|
||||
ESP_LOGD(TAG, "Got counter %d 'tick'.", i);
|
||||
ESP_LOGD(TAG, "Incrementing value. Old: %.2f", CounterValues[i]);
|
||||
CounterValues[i] = CounterValues[i] + 0.01;
|
||||
ESP_LOGD(TAG, "New: %.2f", CounterValues[i]);
|
||||
sendCounterValue(i);
|
||||
writeFile(i);
|
||||
printState(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// void update() override {
|
||||
// for (int i=0; i<CounterNum; i++) {
|
||||
// sendCounterValue(i);
|
||||
// }
|
||||
// }
|
||||
|
||||
// --------------- HASS service call handling ----------------------------
|
||||
void on_set_counter_values(float cold_value, float hot_value)
|
||||
{
|
||||
ESP_LOGD(TAG, "Saving values: cold - %.2f, hot - %.2f", cold_value, hot_value);
|
||||
|
||||
CounterValues[COUNTER_COLD] = cold_value;
|
||||
CounterValues[COUNTER_HOT] = hot_value;
|
||||
|
||||
for (int i=0; i<CounterNum; i++) {
|
||||
writeFile(i);
|
||||
sendCounterValue(i);
|
||||
}
|
||||
|
||||
ESP_LOGD(TAG, "Save success.");
|
||||
}
|
||||
|
||||
// --------------- functions ----------------------------
|
||||
void readCounterFromFile(int counterId) {
|
||||
if (counterId == COUNTER_COLD) {
|
||||
CounterValues[COUNTER_COLD] = counter_cold_value->value();
|
||||
}else if (counterId == COUNTER_HOT) {
|
||||
CounterValues[COUNTER_HOT] = counter_hot_value->value();
|
||||
}
|
||||
}
|
||||
|
||||
void writeFile(int counterId) {
|
||||
if (counterId == COUNTER_COLD) {
|
||||
counter_cold_value->value() = CounterValues[COUNTER_COLD];
|
||||
}else if (counterId == COUNTER_HOT) {
|
||||
counter_hot_value->value() = CounterValues[COUNTER_HOT];
|
||||
}
|
||||
ESP_LOGD(TAG, "Saving done.");
|
||||
}
|
||||
|
||||
void sendCounterValue(int counterId) {
|
||||
if (counterId == COUNTER_COLD) {
|
||||
ESP_LOGD(TAG, "Publishing cold state.");
|
||||
float value = CounterValues[counterId];
|
||||
cold_sensor->publish_state(value);
|
||||
}else if (counterId == COUNTER_HOT) {
|
||||
ESP_LOGD(TAG, "Publishing hot state.");
|
||||
float value = CounterValues[counterId];
|
||||
hot_sensor->publish_state(value);
|
||||
}
|
||||
}
|
||||
|
||||
String countrName(int Id) {
|
||||
switch(Id) {
|
||||
case COUNTER_HOT:
|
||||
return "hot";
|
||||
case COUNTER_COLD:
|
||||
return "cold";
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
void printState(int counterId) {
|
||||
ESP_LOGD(TAG, "Counter[%d]=%.2f", counterId, CounterValues[counterId]);
|
||||
}
|
||||
};
|
||||
@@ -2,10 +2,6 @@
|
||||
substitutions:
|
||||
devicename: watercontrol
|
||||
upper_devicename: Watercontrol
|
||||
pin_cold: D1
|
||||
init_cold: '216.08'
|
||||
pin_hot: D2
|
||||
init_hot: '112.3'
|
||||
water_open_pin: D5
|
||||
water_close_pin: D3
|
||||
ultrasonic_echo_pin: D6
|
||||
@@ -19,6 +15,10 @@ esphome:
|
||||
name: $devicename
|
||||
platform: ESP8266
|
||||
board: d1_mini
|
||||
libraries:
|
||||
- "Bounce2"
|
||||
includes:
|
||||
- water_counters.h
|
||||
|
||||
wifi:
|
||||
ssid: !secret wifi_24_name
|
||||
@@ -43,58 +43,25 @@ globals:
|
||||
type: bool
|
||||
restore_value: yes
|
||||
initial_value: 'true'
|
||||
- id: counter_cold_value
|
||||
type: float
|
||||
restore_value: yes
|
||||
initial_value: $init_cold
|
||||
- id: counter_hot_value
|
||||
type: float
|
||||
restore_value: yes
|
||||
initial_value: $init_hot
|
||||
|
||||
binary_sensor:
|
||||
- platform: gpio
|
||||
id: pulse_cold
|
||||
pin:
|
||||
number: $pin_cold
|
||||
mode: INPUT_PULLUP
|
||||
filters:
|
||||
- delayed_on: 1s
|
||||
- delayed_off: 1s
|
||||
on_press:
|
||||
then:
|
||||
- lambda: 'id(counter_cold_value) += 0.01;'
|
||||
|
||||
- platform: gpio
|
||||
id: pulse_hot
|
||||
pin:
|
||||
number: $pin_hot
|
||||
mode: INPUT_PULLUP
|
||||
filters:
|
||||
- delayed_on: 1s
|
||||
- delayed_off: 1s
|
||||
on_press:
|
||||
then:
|
||||
- lambda: 'id(counter_hot_value) += 0.01;'
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
sensor:
|
||||
- platform: uptime
|
||||
name: "$upper_devicename Uptime"
|
||||
|
||||
- platform: template
|
||||
name: "counter_hot"
|
||||
accuracy_decimals: 2
|
||||
#------------------------------------------------------------------------------
|
||||
- platform: custom
|
||||
lambda: |-
|
||||
return id(counter_hot_value);
|
||||
update_interval: 60s
|
||||
|
||||
- platform: template
|
||||
name: "counter_cold"
|
||||
accuracy_decimals: 2
|
||||
lambda: |-
|
||||
return id(counter_cold_value);
|
||||
update_interval: 60s
|
||||
auto my_sensor = new WaterCountersSensor();
|
||||
App.register_component(my_sensor);
|
||||
return {my_sensor->cold_sensor, my_sensor->hot_sensor};
|
||||
sensors:
|
||||
- name: "Counter Cold"
|
||||
accuracy_decimals: 2
|
||||
- name: "Counter Hot"
|
||||
accuracy_decimals: 2
|
||||
|
||||
#------------------------------------------------------------------------------
|
||||
- platform: ultrasonic
|
||||
trigger_pin: $ultrasonic_trig_pin
|
||||
echo_pin: $ultrasonic_echo_pin
|
||||
@@ -130,13 +97,4 @@ switch:
|
||||
- switch.turn_on: water_close
|
||||
- delay: 5s
|
||||
- switch.turn_off: water_close
|
||||
- lambda: 'id(water_state) = false;'
|
||||
|
||||
- platform: template
|
||||
name: "Reset"
|
||||
turn_on_action:
|
||||
- lambda: 'id(counter_cold_value) = 216.08;'
|
||||
- lambda: 'id(counter_hot_value) = 112.3;'
|
||||
turn_off_action:
|
||||
- lambda: 'id(counter_cold_value) = 216.08;'
|
||||
- lambda: 'id(counter_hot_value) = 112.3;'
|
||||
- lambda: 'id(water_state) = false;'
|
||||
Reference in New Issue
Block a user