-
Notifications
You must be signed in to change notification settings - Fork 0
/
watermeter-cam-standalone.yml
207 lines (179 loc) · 4.76 KB
/
watermeter-cam-standalone.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
esphome:
name: watermeter-cam-standalone
friendly_name: watermeter-cam-standalone
on_boot:
then:
- lambda: |-
id(camera).add_image_callback([](std::shared_ptr<esp32_camera::CameraImage> image) {
if (image->was_requested_by(esp32_camera::WEB_REQUESTER)) {
id(image_data).length = image->get_data_length();
id(image_data).data = image->get_data_buffer();
ESP_LOGD("main", "got requested image, len %d", id(image_data).length);
}
});
esp32:
board: esp32dev
framework:
type: arduino
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "XXXXX"
ota:
password: "XXXXX"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Watermeter-Cam Fallback"
password: "XXXXX"
captive_portal:
time:
- platform: sntp
id: sntp_time
timezone: Europe/Vilnius
servers:
- 0.pool.ntp.org
- 1.pool.ntp.org
- 2.pool.ntp.org
# Just loading dependencies, will issue request manually via HTTPClient
http_request:
globals:
- id: image_data
type: esp32_camera::CameraImageData
- id: stored_value
type: float
restore_value: true
- id: updated_at
type: time_t
restore_value: true
esp32_camera:
name: "Camera"
id: camera
external_clock:
pin: GPIO0
frequency: 20MHz
i2c_pins:
sda: GPIO26
scl: GPIO27
data_pins: [GPIO5, GPIO18, GPIO19, GPIO21, GPIO36, GPIO39, GPIO34, GPIO35]
vsync_pin: GPIO25
href_pin: GPIO23
pixel_clock_pin: GPIO22
power_down_pin: GPIO32
idle_framerate: 0.25 fps
resolution: 640x480
jpeg_quality: 10
output:
- platform: ledc
pin: GPIO4
id: onboard_led_output
light:
- platform: monochromatic
id: onboard_led
output: onboard_led_output
name: "Flashlight"
esp32_camera_web_server:
- port: 8080
mode: stream
- port: 8081
mode: snapshot
web_server:
port: 80
sensor:
- platform: template
id: value
name: "Value"
lambda: |-
return id(stored_value);
device_class: water
state_class: total_increasing
unit_of_measurement: "m³"
text_sensor:
- platform: template
id: status
name: "Status"
- platform: template
name: "Value Last Update"
id: value_last_update
lambda: |-
char str[20];
time_t curr_time = id(updated_at);
strftime(str, sizeof(str), "%Y-%m-%d %H:%M:%S", localtime(&curr_time));
return std::string(str);
text:
- platform: template
id: digitizer_url
name: "Digitizer URL"
optimistic: true
mode: text
restore_value: true
number:
- platform: template
id: reading_interval
name: "Reading interval (seconds)"
min_value: 10
max_value: 3600
step: 10
initial_value: 60
optimistic: true
restore_value: true
mode: box
interval:
- interval: 1sec
then:
if:
condition:
not:
script.is_running: digitize
then:
- script.execute: digitize
script:
- id: digitize
then:
- light.turn_on:
id: onboard_led
transition_length: 0s
brightness: 80%
- delay: 5s
- lambda: id(camera).request_image(esp32_camera::WEB_REQUESTER);
- wait_until:
condition:
lambda: return id(image_data).length > 0;
- lambda: |-
WiFiClient wifiClient;
WiFiClientSecure wifiClientSecure;
wifiClientSecure.setInsecure();
HTTPClient http;
http.setConnectTimeout(1000);
http.setTimeout(5000);
std::string url = id(digitizer_url).state;
if (url.rfind("https://", 0) == 0) {
http.begin(wifiClientSecure, url.c_str());
} else {
http.begin(wifiClient, url.c_str());
}
int http_code = http.POST(id(image_data).data, id(image_data).length);
std::string http_response = http.getString().c_str();
ESP_LOGD("HTTPClient", "completed; URL: %s; Code: %d, Result: %s", url.c_str(), http_code, http_response.c_str());
if (http_code == 200) {
json::parse_json(http_response, [](JsonObject root) {
id(stored_value) = root["value"];
id(updated_at) = id(sntp_time).now().timestamp;
id(value).update();
id(value_last_update).update();
id(status).publish_state("OK");
});
} else {
id(status).publish_state("Error: " + http_response);
}
http.end();
- lambda: id(image_data) = {};
- light.turn_off:
id: onboard_led
transition_length: 0s
- delay: !lambda |-
return id(reading_interval).state * 1000;