LowPowerLab Forum

Hardware support => RF - Range - Antennas - RFM69 library => Topic started by: mkmk on February 29, 2020, 10:02:49 AM

Title: Improve RFM69 readTemperature
Post by: mkmk on February 29, 2020, 10:02:49 AM
Hi everybody

A proposal for a better temperature reading function.
My starting point was: testing 2 RFM69HCW using library vers. 1.3.
One was the master, the other the slave who only sends an ACK.
To play around I also displayed the temperature of the master and noticed, that if the slave does not answer for a long time the temperature increases up to 3 degree.
As soon as the slave starts to answer again, the temperature decreases and after a few seconds it shows the correct value.

Instead of the the original function:
Code: [Select]
uint8_t RFM69::readTemperature(uint8_t calFactor) { // returns centigrade
    setMode(RF69_MODE_STANDBY);
    writeReg(REG_TEMP1, RF_TEMP1_MEAS_START);
    while ((readReg(REG_TEMP1) & RF_TEMP1_MEAS_RUNNING));
    return ~readReg(REG_TEMP2) + COURSE_TEMP_COEF + calFactor; // 'complement' corrects the slope, rising temp = rising val
} // COURSE_TEMP_COEF puts reading in the ballpark, user can add additional correction

I use this one (sorry, I addapt the code to my requirements, but the essential point should be clear):
Code: [Select]
int8_t MyRfm69::ReadTemperature(int8_t calFactor) { // returns centigrade
    uint8_t op_mode;

    op_mode = this->mode;
    if (this->mode != RF69_MODE_STANDBY){
        this->SetMode(RF69_MODE_STANDBY);
    }
    this->WriteReg(REG_TEMP1, RF_TEMP1_MEAS_START);
    while ((this->ReadReg(REG_TEMP1) & RF_TEMP1_MEAS_RUNNING));
    int8_t val = ~this->ReadReg(REG_TEMP2) + COURSE_TEMP_COEF + calFactor; // 'complement' corrects the slope, rising temp = rising val
    if (op_mode != this->mode)
        this->SetMode(op_mode);
    return val;
}

Please note, that uint8_t should also be changed to int8_t