mandulaj / PZEM-004T-v30

Arduino library for the Updated PZEM-004T v3.0 Power and Energy meter
MIT License
266 stars 115 forks source link

Add calibration function to the library #12

Open devicter opened 5 years ago

devicter commented 5 years ago

The chip supports this. This seems to be in the documentation. It would be great.

The format of the master command for calibrating a slave (6 bytes in total):

0xF8 + 0x41 + 0x37 + 0x21 + CRC check high byte + CRC check low byte.

Correct answer: 0xF8 + 0x41 + 0x37 + 0x21 + CRC check high byte + CRC check low byte.

Error response: 0xF8 + 0xC1 + anomalous code + CRC check high byte + CRC check low byte.

It should be noted that the calibration takes from 3 to 4 seconds, after the master sends a command, if the calibration is successful, it takes 3-4 seconds to get a response from the slave.

mandulaj commented 5 years ago

I have to still play with the calibration feature. I have heard that you can even ruin your device. I will only add it when I am confident it works well. Thanks, I will put it higher up in my priority list.

RB3rg commented 4 years ago

I doubt this "calibration" works just like that. It will need a reference, a specific voltage, and current to be used as a reference. @devicter have you ever tested this calibration? If not, you can use node-red to test it however if I am correct, as @mandulaj said, you will ruin your device. Let us know the result.

mandulaj commented 4 years ago

Has anyone had time to look at this?

xavipolo commented 4 years ago

I don't think it makes sense. As RB3rg says, the calibration function should receive a parameter that modifies the readings. If you connect a 12W bulb, and the reading is 11W, you should be able to calibrate it to 12W or +1W

As documented the message must be 6 bytes 0xF8 + 0x41 + 0x37 + 0x21 + CRC check high byte + CRC check low byte.

0xF8: I think this is the slave address (0x01 by default) 0x41 is the calibration function 0x37 and 0x21 could be the adjustment value (data high and low bytes) signed?

morganflint commented 4 years ago

More probably, I imagine you'll have to feed a specific voltage and current from a precision source to the module and then call the calibration function. In that case, without knowing these parameters, the calibration function is useless for us.

Anyway, the calibration option is included in the Windows software provided by Peacefair, even in the case the calibration procedure was well documented, anybody wanting to calibrate a module could do it from that software, so I don't think the function is needed (nor desirable if, as has been said, it could ruin the module) in the library.

Nismonx commented 4 years ago

15960111613616626068608358322857 Thought this might help... Although I still uncertain if this is to be done with an know load or off load.

RB3rg commented 4 years ago

0xF8 is the broadcast address for Modbus so any device will catch it. 0x41 is the function of cal. Now comes the question, the device reads voltage and current. Calibration needs a reference, in this case, Voltage & Current though the 0x37 0x21 results (int) 14113 I can't see 14113 being the Voltage+Current to be compared.
Those guys are Chinese so 220V/50Hz is the standard, something around 220 should be pass in the calibration parameter. Another point, the calibration should be float for precision (220.32), anyone can see how to pass (float)voltage + current into 2 bytes?.

Jackaro0z commented 4 years ago

0x37 0x21 is the password. Refer to datasheet intro.

poweredscript commented 4 years ago

I tried to calibration even though it was successful. But when the power supply is disconnected and connected again, the MCU does not remember the calibration done. I'm confused, what am I missing? This is my code

//The command format of the master to calibrate the slave is (total 6 bytes):
//               0xF8 + 0x41 + 0x37 + 0x21 + CRC check high byte + CRC check low byte.
//Correct reply: 0xF8 + 0x41 + 0x37 + 0x21 + CRC check high byte + CRC check low byte. 6
//Error Reply:   0xF8 + 0xC1 + Abnormal code + CRC check high byte + CRC check low byte. 5
bool PZEM004Tv30::setCalibration(){
    uint8_t buffer[] = {0xF8, 0x41, 0x37, 0x21, 0x00, 0x00}; // Work
    //uint8_t buffer[] = {0x01, 0x41, 0x37, 0x21, 0x00, 0x00}; // Work
    uint8_t reply[6];   

    setCRC(buffer, 6);
    _serial->write(buffer, 6);

    uint16_t length = recieve(reply, 6);

    // for(int i = 0; i < sizeof(reply); i++){
        // Serial.print("0x");
       // Serial.print(reply[i] < 16 ? "0" : "");
       // Serial.println(reply[i], HEX);
    // }

    if(length == 0 || length == 5){
        return false;
    }
    return true;
}
Nismonx commented 4 years ago

@poweredscript

In what condition you had the PZEM when you attempt your calibration calibration code? In windows it always return calibration fail, have tried with/or without load. PZEM004T

Request F8 41 37 21 B7 78 //Send-Calibration request Return: F8 C1 04 A0 62 //Calibration fail

poweredscript commented 4 years ago

@Nismonx

My code is adding a function to the library. As for the program you use, I think it should have an instruction manual "the user manual of the software.txt" If you do not use voltage and current, the program requirements cannot be calibrated.

The user manual of the software

1.Before use it, please click the กพrunกฟdocument to login, after login successfully, you can use the software.

2.In all parameter setting,if the color is gray,that means it can't be amend, other color means it can be amend.

3.At present,this software is standalone version, not support Multimachine communication,that means if you want to use more than one module at the same time, you should set the address of each module in advance, then use the address to distinguish the termianl box when read or set the data.

4.Power alarm way is: the power data display become to read in the software.There is no power alarm signal output driving port in the module PCB.

5.Please be with caution to use the calibration function in the software! The module has been calibrated at least twice with the professional equipment before delivery, so the data will has problem in general. If it is must be calibrated because of the defective product,the calibrationg password is: PZEM014/6 Calibration condition: PZEM-014: VOLTAGE:220v,CURRENT:1A PZEM-016: VOLTAGE:220v,CURRENT:10A

Nismonx commented 4 years ago

@poweredscript

I was just concerned that the calibration simply may not work and clearly misconstrued the last 2 paragraphs:

"".... Calibration condition: PZEM-014: VOLTAGE:220v,CURRENT:1A PZEM-016: VOLTAGE:220v,CURRENT:10A ""

I have use a 220v pressure with a 1amp load and had the same outcome (calibration failed) but its as far as I went. The picture above was a very poor choice of mine.

Thanks for pointing this out I just realise where I gone wrong , I have a PZEM-016.

morganflint commented 4 years ago

Apparently, in the "Set parameters" option, you can see the calibration conditions for a module (but they are greyed out, so cannot be changed): CapturaPZEM2

Anyway, I wouldn't calibrate it unless I had a precision AC source and load...

RB3rg commented 4 years ago

Apparently, in the "Set parameters" option, you can see the calibration conditions for a module (but they are greyed out, so cannot be changed): CapturaPZEM2

Anyway, I wouldn't calibrate it unless I had a precision AC source and load...

@morganflint can you share this PZEM014 Master? The one I found doesn't run. Then I will have a look into this.

morganflint commented 4 years ago

Hello, although in the screen says "PZEM014Master", I downloaded and installed it as PZEM004TMaster: image

I think I downloaded it from the link that can be found at Peacefair's Aliexpress shop product page, but I'm enclosing it here just in case: 004T software+driver+user manual.zip

sirapol commented 3 years ago

Hello , Is this issues solve or not ? I still like your problem "The calibration failed!"

poweredscript commented 3 years ago

Hello , Is this issues solve or not ? I still like your problem "The calibration failed!" The problem has not been resolved.