-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathR4850G2.YAML
533 lines (473 loc) · 20.3 KB
/
R4850G2.YAML
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
esphome:
name: can-bus-control-r4850g2
friendly_name: r4850g2
esp32:
board: esp32dev
framework:
type: arduino
logger:
level: INFO
##################################################################################################################
# Uncomment this webserver block if you want to control the device via webpage
#web_server:
# version: 3
##################################################################################################################
## If you are using Home Assistant uncomment this section
#api:
# encryption:
# key: "4taaK4Kn+NZNAyA8/RM+YaFkwiyy/gTorr4XlwfThQE="
##################################################################################################################
## uncomment this block to enable mqtt if you are using NodeRed or other MQTT software !also enable the mqtt relevant block at the bottom of this YAML to activate the ability to set Voltage and Current using mqtt!
mqtt:
broker: !secret mqtt_host
username: !secret mqtt_username
password: !secret mqtt_password
id: mqtt_client
##################################################################################################################
ota:
- platform: esphome
password: "f8ab526ee137e5bef0fd65e50a223861"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "R4875G1 Fallback Hotspot"
password: '12345678'
captive_portal:
globals:
- id: can_message_received
type: bool
restore_value: no
initial_value: "false"
canbus:
- platform: esp32_can
tx_pin: GPIO19
rx_pin: GPIO21
can_id: 0xE5F32E5
bit_rate: 125kbps
use_extended_id: true
on_frame:
- can_id: 0x1081407F
use_extended_id: true
then:
- lambda: |-
// Set the flag to true whenever a CAN message is received
id(can_message_received) = true;
if (x.size() >= 8) {
const uint8_t byte_0 = static_cast<uint8_t>(x[0]);
const uint8_t byte_1 = static_cast<uint8_t>(x[1]);
const uint32_t value = (static_cast<uint32_t>(x[4]) << 24) |
(static_cast<uint32_t>(x[5]) << 16) |
(static_cast<uint32_t>(x[6]) << 8) |
static_cast<uint32_t>(x[7]);
switch (byte_0) {
case 0x01:
switch (byte_1) {
case 0x70: id(ac_power_in_sensor).publish_state(value); break;
case 0x71: id(grid_frequency_sensor).publish_state(value); break;
case 0x72: id(input_current_sensor).publish_state(value); break;
case 0x73: id(dc_power_out_sensor).publish_state(value); break;
case 0x75: id(output_voltage_sensor).publish_state(value); break;
case 0x76: id(set_max_output_current_sensor).publish_state(value); break;
case 0x78: id(input_grid_voltage_sensor).publish_state(value); break;
case 0x7F: id(output_temperature_sensor).publish_state(value); break;
case 0x81: id(output_current_sensor).publish_state(value); break;
}
break;
}
}
# Define sensors
substitutions:
multiplier_default: '0.0009765625' # 1/1024 conversion factor
multiplier_current: '0.05' # 1/20 conversion factor
sensor:
- platform: template
name: "01 Input AC Power"
id: ac_power_in_sensor
unit_of_measurement: "W"
accuracy_decimals: 2
filters:
- multiply: ${multiplier_default}
icon: "mdi:flash"
- platform: template
name: "05 Output DC Power"
id: dc_power_out_sensor
unit_of_measurement: "W"
accuracy_decimals: 2
filters:
- multiply: ${multiplier_default}
icon: "mdi:flash"
- platform: template
name: "02 Input Grid AC Frequency"
id: grid_frequency_sensor
unit_of_measurement: "Hz"
accuracy_decimals: 1
filters:
- multiply: ${multiplier_default}
icon: "mdi:transmission-tower-import"
- platform: template
name: "03 Input AC Current"
id: input_current_sensor
unit_of_measurement: "A"
accuracy_decimals: 1
filters:
- multiply: ${multiplier_default}
icon: "mdi:current-ac"
- platform: template
name: "07 Output DC Voltage"
id: output_voltage_sensor
unit_of_measurement: "V"
accuracy_decimals: 1
filters:
- multiply: ${multiplier_default}
icon: "mdi:current-dc"
- platform: template
name: "04 Input AC Grid Voltage"
id: input_grid_voltage_sensor
unit_of_measurement: "V"
accuracy_decimals: 2
filters:
- multiply: ${multiplier_default}
icon: "mdi:sine-wave"
- platform: template
name: "08 Output Temperature"
id: output_temperature_sensor
unit_of_measurement: "°C"
accuracy_decimals: 1
filters:
- multiply: ${multiplier_default}
icon: "mdi:thermometer"
on_value:
then:
- if:
condition:
lambda: 'return id(output_temperature_sensor).state > 90.0;'
then:
- canbus.send:
use_extended_id: true
can_id: 0x108080FE
data: [1, 50, 0, 1, 0, 0, 0, 0]
- logger.log:
level: INFO
format: "Temperature exceeded 90°C. Sent CAN OFF message."
- platform: template
name: "06 Output DC Current"
id: output_current_sensor
unit_of_measurement: "A"
accuracy_decimals: 1
filters:
- multiply: ${multiplier_default}
icon: "mdi:current-dc"
- platform: template
name: "09 Max Output DC Current"
id: set_max_output_current_sensor
unit_of_measurement: "A"
accuracy_decimals: 1
filters:
- multiply: ${multiplier_current}
icon: "mdi:current-ac"
number:
- platform: template
name: "10 CAN Voltage Set"
id: can_voltage_input
min_value: 49
max_value: 58
step: 0.1
unit_of_measurement: "V"
optimistic: true
mode: BOX
restore_value: true
icon: "mdi:current-dc"
on_value:
then:
- delay: 0.01s # Small delay to ensure the value is properly updated
- lambda: |-
float voltage_value = id(can_voltage_input).state;
int scaled_value = (int)(voltage_value * 1024);
ESP_LOGI("custom", "Received voltage: %.1fV, Scaled value: %d", voltage_value, scaled_value);
uint8_t high_byte = (scaled_value >> 8) & 0xFF;
uint8_t low_byte = scaled_value & 0xFF;
ESP_LOGI("custom", "Encoded CAN message: [0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x%02X, 0x%02X]", high_byte, low_byte);
- canbus.send:
use_extended_id: true
can_id: 0x108180FE
data: !lambda |-
std::vector<uint8_t> can_data(8, 0x00);
can_data[0] = 0x01;
int scaled_value = (int)(id(can_voltage_input).state * 1024);
can_data[6] = (scaled_value >> 8) & 0xFF;
can_data[7] = scaled_value & 0xFF;
return can_data;
# Amp Setting
- platform: template
name: "12 Set Output DC Current"
id: can_amp_input
min_value: 1
max_value: 50 # Max value for the Amp input
step: 1 # 1 Amp steps
unit_of_measurement: "A"
optimistic: true
mode: BOX
restore_value: True
icon: "mdi:current-dc"
on_value:
then:
- delay: 0.01s # Small delay to ensure the value is properly updated
- lambda: |-
float amp_value = id(can_amp_input).state; // Get the current Amp value
int scaled_value = (int)(amp_value * 20); // Scale the Amp value (Amp * 20)
ESP_LOGI("custom", "Received Amp: %.1fA, Scaled value: %d", amp_value, scaled_value);
uint8_t high_byte = (scaled_value >> 8) & 0xFF;
uint8_t low_byte = scaled_value & 0xFF;
ESP_LOGI("custom", "Encoded CAN message: [0x01, 0x03, 0x00, 0x00, 0x00, 0x00, 0x%02X, 0x%02X]", high_byte, low_byte); // Log the CAN message data
- canbus.send:
use_extended_id: true
can_id: 0x108180FE
data: !lambda |-
std::vector<uint8_t> can_data(8, 0x00); // Initialize with 8 zeros
can_data[0] = 0x01; // First byte is always 0x01
can_data[1] = 0x03; // Second byte is 0x03
int scaled_value = (int)(id(can_amp_input).state * 20); // Scale the Amp value
can_data[6] = (scaled_value >> 8) & 0xFF; // Encode the high byte in the 7th byte
can_data[7] = scaled_value & 0xFF; // Encode the low byte in the 8th byte
return can_data;
# Fallback Amp Setting
- platform: template
name: "13 Set Fallback Output DC Current"
id: fallback_amp_input
min_value: 1
max_value: 50 # Adjust as needed
step: 1 # 1A steps
unit_of_measurement: "A"
optimistic: true
mode: BOX
restore_value: True
icon: "mdi:current-dc"
on_value:
then:
- delay: 0.01s # Small delay to ensure the value is properly updated
- lambda: |-
float fallback_amp_value = id(fallback_amp_input).state; // Get the fallback Amp value
int fallback_scaled_value = (int)(fallback_amp_value * 20); // Scale the Amp value (Amp * 20)
ESP_LOGI("custom", "Fallback Amp: %.1fA, Scaled value: %d", fallback_amp_value, fallback_scaled_value);
uint8_t high_byte = (fallback_scaled_value >> 8) & 0xFF; // Get high byte
uint8_t low_byte = fallback_scaled_value & 0xFF; // Get low byte
ESP_LOGI("custom", "Encoded CAN message: [0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x%02X, 0x%02X]", high_byte, low_byte); // Log the CAN message data
- canbus.send:
use_extended_id: true
can_id: 0x108180FE
data: !lambda |-
std::vector<uint8_t> can_data(8, 0x00); // Initialize with 8 zeros
can_data[0] = 0x01; // First byte is always 0x01
can_data[1] = 0x04; // Second byte is 0x04 for the Fallback Amps setting
int fallback_scaled_value = (int)(id(fallback_amp_input).state * 20); // Scale the Amp value
can_data[6] = (fallback_scaled_value >> 8) & 0xFF; // Encode the high byte in the 7th byte
can_data[7] = fallback_scaled_value & 0xFF; // Encode the low byte in the 8th byte
return can_data;
# Fallback Voltage Setting
- platform: template
name: "11 Set Fallback Output DC Voltage"
id: fallback_voltage_input
min_value: 49
max_value: 58 # Adjust as needed for your voltage range
step: 0.1 # 0.1V steps
unit_of_measurement: "V"
optimistic: true
mode: BOX
restore_value: True
icon: "mdi:sine-wave"
on_value:
then:
- delay: 0.01s # Small delay to ensure the value is properly updated
- lambda: |-
float fallback_voltage_value = id(fallback_voltage_input).state; // Get the fallback voltage value
int fallback_scaled_value = (int)(fallback_voltage_value * 1024); // Scale the voltage value (voltage * 1024)
ESP_LOGI("custom", "Fallback Voltage: %.1fV, Scaled value: %d", fallback_voltage_value, fallback_scaled_value);
uint8_t high_byte = (fallback_scaled_value >> 8) & 0xFF; // Get high byte
uint8_t low_byte = fallback_scaled_value & 0xFF; // Get low byte
ESP_LOGI("custom", "Encoded CAN message: [0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x%02X, 0x%02X]", high_byte, low_byte); // Log the CAN message data
- canbus.send:
use_extended_id: true
can_id: 0x108180FE
data: !lambda |-
std::vector<uint8_t> can_data(8, 0x00); // Initialize with 8 zeros
can_data[0] = 0x01; // First byte is always 0x01
can_data[1] = 0x01; // Second byte is 0x01 for the Fallback Voltage setting
int fallback_scaled_value = (int)(id(fallback_voltage_input).state * 1024); // Scale the voltage value
can_data[6] = (fallback_scaled_value >> 8) & 0xFF; // Encode the high byte in the 7th byte
can_data[7] = fallback_scaled_value & 0xFF; // Encode the low byte in the 8th byte
return can_data;
interval:
- interval: 30s
then:
- lambda: |-
if (id(can_message_received)) {
ESP_LOGI("can_bus", "CAN BUS OK");
id(can_message_received) = false; // Reset the flag
} else {
ESP_LOGI("can_bus", "CAN BUS inactive");
}
# Interval for sending the first CAN message every 30 seconds
- interval: 500ms
then:
- canbus.send:
use_extended_id: true
can_id: 0x108040FE
data: [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
# Interval for sending voltage CAN message every 30 seconds
- interval: 30s
then:
- canbus.send:
use_extended_id: true
can_id: 0x108080FE
data: !lambda |-
std::vector<uint8_t> can_data(8, 0x00); // Initialize with 8 zeros
can_data[0] = 0x01; // First byte is always 0x01
int scaled_value = (int)(id(can_voltage_input).state * 1024); // Scale the voltage value
can_data[6] = (scaled_value >> 8) & 0xFF; // Encode the high byte in the 7th byte
can_data[7] = scaled_value & 0xFF; // Encode the low byte in the 8th byte
return can_data;
# Interval for sending Amp setting CAN message every 10 seconds
- interval: 30s
then:
- canbus.send:
use_extended_id: true
can_id: 0x108080FE
data: !lambda |-
std::vector<uint8_t> can_data(8, 0x00); // Initialize with 8 zeros
can_data[0] = 0x01; // First byte is always 0x01
can_data[1] = 0x03; // Second byte is 0x03
int scaled_value = (int)(id(can_amp_input).state * 20); // Scale the Amp value
can_data[6] = (scaled_value >> 8) & 0xFF; // Encode the high byte in the 7th byte
can_data[7] = scaled_value & 0xFF; // Encode the low byte in the 8th byte
return can_data;
# Define a button component for turning ON
button:
- platform: template
name: "001 CAN ON Button"
icon: "mdi:button"
on_press:
then:
- lambda: |-
ESP_LOGI("custom", "CAN ON Button pressed, sending message: [0x01, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]");
- canbus.send:
use_extended_id: true
can_id: 0x108080FE
data: [0x01, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
# Define a button component for turning OFF
- platform: template
name: "002 CAN OFF Button"
icon: "mdi:button"
on_press:
then:
- lambda: |-
ESP_LOGI("custom", "CAN OFF Button pressed, sending message: [0x01, 0x32, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]");
- canbus.send:
use_extended_id: true
can_id: 0x108080FE
data: [0x01, 0x32, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]
# Define a button component for setting the fan to full speed
- platform: template
name: "Fan Full Speed Button"
icon: "mdi:button"
on_press:
then:
- lambda: |-
ESP_LOGI("custom", "Fan Full Speed Button pressed, sending message: [0x01, 0x34, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]");
- canbus.send:
use_extended_id: true
can_id: 0x108080FE
data: [0x01, 0x34, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00]
# Define a button component for setting the fan to auto mode
- platform: template
name: "Fan Auto Mode Button"
icon: "mdi:button"
on_press:
then:
- lambda: |-
ESP_LOGI("custom", "Fan Auto Mode Button pressed, sending message: [0x01, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]");
- canbus.send:
use_extended_id: true
can_id: 0x108080FE
data: [0x01, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
###################################################################################################################################
##This Block must be added to enable voltage and currrent SET topics when using the mqtt: component
## to set values simply publish a value (number) to MQTT topic to update CAN voltage or current.
text_sensor:
- platform: mqtt_subscribe
name: "CAN Voltage MQTT"
id: mqtt_voltage_input
topic: "home/canbus/voltage_set"
internal: true # This makes the sensor internal
on_value:
then:
- delay: 0.01s # Small delay to ensure the value is properly updated
- lambda: |-
// Convert MQTT message to float
float mqtt_voltage = atof(x.c_str());
// Update the number input with the new value
if (mqtt_voltage >= 49.0 && mqtt_voltage <= 58.0) {
id(can_voltage_input).publish_state(mqtt_voltage);
ESP_LOGI("custom", "MQTT Voltage Set to: %.1fV", mqtt_voltage);
} else {
ESP_LOGW("custom", "Received out-of-range voltage value: %.1fV", mqtt_voltage);
}
# Subscribe to MQTT topic to update CAN amps
- platform: mqtt_subscribe
name: "CAN Amps MQTT"
id: mqtt_amp_input
topic: "home/canbus/amp_set"
internal: true # This makes the sensor internal
on_value:
then:
- delay: 0.01s # Small delay to ensure the value is properly updated
- lambda: |-
// Convert MQTT message to float
float mqtt_amp = atof(x.c_str());
// Update the number input with the new value
if (mqtt_amp >= 1.0 && mqtt_amp <= 50.0) {
id(can_amp_input).publish_state(mqtt_amp);
ESP_LOGI("custom", "MQTT Amp Set to: %.1fA", mqtt_amp);
} else {
ESP_LOGW("custom", "Received out-of-range amp value: %.1fA", mqtt_amp);
}
# Subscribe to MQTT topic to update Fallback Voltage
- platform: mqtt_subscribe
name: "Fallback Voltage MQTT"
id: mqtt_fallback_voltage
topic: "home/canbus/fallback_voltage_set"
internal: true # This makes the sensor internal
on_value:
then:
- delay: 0.01s # Small delay to ensure the value is properly updated
- lambda: |-
// Convert MQTT message to float
float fallback_voltage = atof(x.c_str());
// Update the number input with the new value
if (fallback_voltage >= 49.0 && fallback_voltage <= 58.0) {
id(fallback_voltage_input).publish_state(fallback_voltage);
ESP_LOGI("custom", "Fallback Voltage Set to: %.1fV", fallback_voltage);
} else {
ESP_LOGW("custom", "Received out-of-range fallback voltage value: %.1fV", fallback_voltage);
}
# Subscribe to MQTT topic to update Fallback Amps
- platform: mqtt_subscribe
name: "Fallback Amps MQTT"
id: mqtt_fallback_amp
topic: "home/canbus/fallback_amp_set"
internal: true # This makes the sensor internal
on_value:
then:
- delay: 0.01s # Small delay to ensure the value is properly updated
- lambda: |-
// Convert MQTT message to float
float fallback_amp = atof(x.c_str());
// Update the number input with the new value
if (fallback_amp >= 1.0 && fallback_amp <= 50.0) {
id(fallback_amp_input).publish_state(fallback_amp);
ESP_LOGI("custom", "Fallback Amps Set to: %.1fA", fallback_amp);
} else {
ESP_LOGW("custom", "Received out-of-range fallback amp value: %.1fA", fallback_amp);
}
###############################################################################################################################