Skip to content

Calculation error due to using uint16_t = 65535? 66.5kW = 0.5kW! #199

@ilya7zz

Description

@ilya7zz

When the active power (W) and accumulated power (Wh) measurements are exceeded, the buffer overflows.
For example:
65.535 kWh is the limit after which it starts counting from zero.
i.e. 65.537 kWh = 0.001 kWh
6553.5 W is the limit after which it starts counting from zero.
6553.7 W = 0.1 W

bool PR_alarm;
uint8_t result;
float U_PR, I_PR, P_PR, PPR, PR_F, PR_PF; 
void PZEM() {
  result = node.readInputRegisters(0x0000, 10);
  if (result == node.ku8MBSuccess) {
  U_PR = (node.getResponseBuffer(0x00)/10.0f); // Voltage
  I_PR = (node.getResponseBuffer(0x01)/1000.000f); // Current
  P_PR = (node.getResponseBuffer(0x03)/10.0f); // Power
  PPR = (node.getResponseBuffer(0x05)/1000.0f); // Power consumption max:9999.99
  PR_F = (node.getResponseBuffer(0x07)/10.0f); // Frequency 45 – 65 Hz
  PR_PF = (node.getResponseBuffer(0x08)/100.0f); // Power Factor
  PR_alarm = (node.getResponseBuffer(0x09)); // Active power threshold
  sendSerial();
}

void sendSerial() {
  Serial.print("U_PR: " + String(U_PR,1) + " V,  ");
  Serial.print("I_PR: " + String(I_PR,3) + " A,  ");
  Serial.print("P_PR: " + String(P_PR,1) + " W,  ");
  Serial.print("PPR: " + String(PPR,3) + " kW*h,  ");
  Serial.print("PR_F: " + String(PR_F,1) + " Hz,  ");
  Serial.print("PR_PF: " + String(PR_PF) + " cos φ,  ");    // Power factor (cos φ)
  Serial.println("PR_alarm: " + String(PR_alarm,0)); // Active power threshold
}

I really liked this library, I ask you to improve it!
Or please advise me how to fix this error ;)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions