Skip to content

Commit

Permalink
Merge pull request #10 from augustynski-lukasz/amiplus-patch
Browse files Browse the repository at this point in the history
Amiplus-patch for tarrifs T1, T2, T3, T4
  • Loading branch information
SzczepanLeon authored Jul 13, 2024
2 parents 1ad30e4 + b3262b7 commit d5e2bef
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions driver_amiplus.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@ struct Amiplus: Driver
std::map<std::string, double> ret_val{};

add_to_map(ret_val, "total_energy_consumption_kwh", this->get_total_energy_consumption_kwh(telegram));
add_to_map(ret_val, "total_energy_consumption_t1_kwh", this->get_total_energy_consumption_t_kwh(1, telegram));
add_to_map(ret_val, "total_energy_consumption_t2_kwh", this->get_total_energy_consumption_t_kwh(2, telegram));
add_to_map(ret_val, "total_energy_consumption_t3_kwh", this->get_total_energy_consumption_t_kwh(3, telegram));
add_to_map(ret_val, "total_energy_consumption_t4_kwh", this->get_total_energy_consumption_t_kwh(4, telegram));
add_to_map(ret_val, "current_power_consumption_kw", this->get_current_power_consumption_kw(telegram));
add_to_map(ret_val, "total_energy_production_kwh", this->get_total_energy_production_kwh(telegram));
add_to_map(ret_val, "total_energy_production_t1_kwh", this->get_total_energy_production_t_kwh(1, telegram));
add_to_map(ret_val, "total_energy_production_t2_kwh", this->get_total_energy_production_t_kwh(2, telegram));
add_to_map(ret_val, "total_energy_production_t3_kwh", this->get_total_energy_production_t_kwh(3, telegram));
add_to_map(ret_val, "total_energy_production_t4_kwh", this->get_total_energy_production_t_kwh(4, telegram));
add_to_map(ret_val, "current_power_production_kw", this->get_current_power_production_kw(telegram));
add_to_map(ret_val, "voltage_at_phase_1_v", this->get_voltage_at_phase_v(1, telegram));
add_to_map(ret_val, "voltage_at_phase_2_v", this->get_voltage_at_phase_v(2, telegram));
Expand Down Expand Up @@ -55,6 +63,48 @@ struct Amiplus: Driver
return ret_val;
};

esphome::optional<double> get_total_energy_consumption_t_kwh(uint8_t tarrif, std::vector<unsigned char> &telegram) {
esphome::optional<double> ret_val{};
uint32_t usage = 0;
size_t i = 11;
uint32_t total_register = tarrif < 4 ? 0x8E0003 | ((tarrif*16) << 8) : 0x8E801003;
while (i < telegram.size()) {
uint32_t c = tarrif < 4 ?
(((uint32_t)telegram[i+0] << 16) | ((uint32_t)telegram[i+1] << 8 ) | ((uint32_t)telegram[i+2]) )
: (((uint32_t)telegram[i+0] << 24) | ((uint32_t)telegram[i+1] << 16 ) | ((uint32_t)telegram[i+2] << 8) | ((uint32_t)telegram[i+3]) );
if (c == total_register) {
i += tarrif < 4 ? 3 : 4;
usage = bcd_2_int(telegram, i, 6);
ret_val = usage / 1000.0;
ESP_LOGVV(TAG, "Found register '%X' with '%d'->'%f'", total_register, usage, ret_val.value());
break;
}
i++;
}
return ret_val;
};

esphome::optional<double> get_total_energy_production_t_kwh(uint8_t tarrif, std::vector<unsigned char> &telegram) {
esphome::optional<double> ret_val{};
uint32_t usage = 0;
size_t i = 11;
uint64_t total_register = tarrif < 4 ? 0x8E00833C | ((tarrif*16) << 16) : 0x8E8010833C;
while (i < telegram.size()) {
uint64_t c = tarrif < 4 ?
(((uint32_t)telegram[i+0] << 24) | ((uint32_t)telegram[i+1] << 16 ) | ((uint32_t)telegram[i+2] << 8) | ((uint32_t)telegram[i+3]) )
: (((uint64_t)telegram[i+0] << 32) | ((uint32_t)telegram[i+1] << 24 ) | ((uint32_t)telegram[i+2] << 16) | ((uint32_t)telegram[i+3] << 8) | ((uint32_t)telegram[i+4]));
if (c == total_register) {
i += tarrif < 4 ? 4 : 5;
usage = bcd_2_int(telegram, i, 6);
ret_val = usage / 1000.0;
ESP_LOGVV(TAG, "Found register '%X' with '%d'->'%f'", total_register, usage, ret_val.value());
break;
}
i++;
}
return ret_val;
};

esphome::optional<double> get_total_energy_consumption_kwh(std::vector<unsigned char> &telegram) {
esphome::optional<double> ret_val{};
uint32_t usage = 0;
Expand Down Expand Up @@ -93,6 +143,7 @@ struct Amiplus: Driver
return ret_val;
};


esphome::optional<double> get_total_energy_production_kwh(std::vector<unsigned char> &telegram) {
esphome::optional<double> ret_val{};
uint32_t usage = 0;
Expand Down

0 comments on commit d5e2bef

Please sign in to comment.