Skip to content

Commit

Permalink
minor: Fix CM160 current amps reading
Browse files Browse the repository at this point in the history
  • Loading branch information
zuckschwerdt committed Mar 13, 2023
1 parent 147bc5c commit 2a9ce36
Showing 1 changed file with 4 additions and 14 deletions.
18 changes: 4 additions & 14 deletions src/devices/oregon_scientific.c
Original file line number Diff line number Diff line change
Expand Up @@ -811,32 +811,22 @@ static int oregon_scientific_v3_decode(r_device *decoder, bitbuffer_t *bitbuffer
return 1;
}
else if ((msg[0] == 0x20) || (msg[0] == 0x21) || (msg[0] == 0x22) || (msg[0] == 0x23) || (msg[0] == 0x24)) { // Owl CM160 Readings

msg[0] = msg[0] & 0x0F;

if (validate_os_checksum(decoder, msg, 22) != 0)
return DECODE_FAIL_MIC;

int id = msg[1] & 0x0F;

//cm160 current_watts rework & total energy added from https://github.com/magellannh/rtl-wx/blob/dbaf3924815903c47b230c65841d18b263421854/src/rtl-433fm-decode.c
unsigned int current_amps = swap_nibbles(msg[3]) + (msg[4]<<8);
unsigned int current_watts = (current_amps / (0.27*230) * 1000); //Assuming device is running in 230V country
//Alternate formula = (current_amps * 0.07) * 230 - https://github.com/cornetp/eagle-owl/blob/master/src/cm160.c
//One Ampere is defined as the current that flows with electric charge of one Coulomb per second.
unsigned int current_amps = swap_nibbles(msg[3]) | ((msg[4] >> 4) << 8);
unsigned int current_watts = current_amps * 0.07 * 230; // Assuming device is running in 230V country

double total_amps = ((uint64_t)swap_nibbles(msg[10]) << 36) | ((uint64_t)swap_nibbles(msg[9]) << 28) |
(swap_nibbles(msg[8]) << 20) | (swap_nibbles(msg[7]) << 12) |
(swap_nibbles(msg[6]) << 4) | (msg[5]&0xf);

//fprintf(stderr, "Raw KWH Total: %x (%d) - %7.4f \n",(int) total, (int) total, total);

double total_kWh = (total_amps * 230 ) / 3600.0 / 1000.0; //Assuming device is running in 230V country
//Not 100% sure about the formula = (amp * volt) / time per hour / kilo
//(amp * volt) = watt
// watt / 1 hour time (60 * 60) = Wh
// Wh / 1000 = kWh
//result compares to municpal box kWh count in Cape Town South Africa
double total_kWh = total_amps * 230.0 / 3600.0 / 1000.0; // Assuming device is running in 230V country
// result compares to municpal box kWh count in Cape Town South Africa

/* clang-format off */
data = data_make(
Expand Down

0 comments on commit 2a9ce36

Please sign in to comment.