Revisión del código Para el sensor SHT11

Hola

Estoy trabajando en este código para el sensor sht11 y necesitó saber si la conversión de Temperatura y humedad esta bien echa.
La referencia de los cálculos la saque de http://itp.nyu.edu/physcomp/sensors/Code/SHT11

Sección del código para revisar.

SHT_Measure(3);
Serial.print(" T ");
retVal = retVal*0.01 - 40; //conversion a ºC
tC = retVal;
Serial.print(retVal, DEC); 
Serial.print("C");
Serial.print(" H ");
// SHT11 #1 Humidity
SHT_Measure(5);
rhLin = (retVal * 0.0405) - (retVal * 0.004 * retVal * 0.0007) - 4;
rhTrue = (tC - 25) * (retVal * 0.00008 + 0.01) + rhLin;
retVal = rhTrue;
Serial.print(retVal, DEC);
Serial.print("%");
Serial.println();

Codigo completo

int shtClk=11; // clock pin
int shtData=10; // Data pin
int ioByte;
int ackBit;
int cmdByte;
int dataByte;
int k;
long msTimer;
long logTime;
double retVal; // Return value from SHT11
//--------//
int tC;           // Temperatura en ºC
int rhLin;        // Humedad 
int rhTrue;       // humedad relatica con tC 
//--------//
int dly;
uint8_t bitmask;

void SHT_Write_Byte(void) {
pinMode(shtData, OUTPUT);
shiftOut(shtData, shtClk, MSBFIRST, ioByte);
pinMode(shtData, INPUT);
digitalWrite(shtData, LOW);
digitalWrite(shtClk, LOW);
digitalWrite(shtClk, HIGH);
ackBit = digitalRead(shtData);
digitalWrite(shtClk, LOW);
}

int shiftIn() {
int cwt;
cwt=0;
bitmask=128;

while (bitmask >= 1) {
digitalWrite(shtClk, HIGH);
cwt = cwt + bitmask * digitalRead(shtData);
digitalWrite(shtClk, LOW);
bitmask=bitmask/2;
}
return(cwt);
}

void SHT_Read_Byte(void) {
ioByte = shiftIn();
digitalWrite(shtData, ackBit);
pinMode(shtData, OUTPUT);
digitalWrite(shtClk, HIGH);
digitalWrite(shtClk, LOW);
pinMode(shtData, INPUT);
digitalWrite(shtData, LOW);
}

void SHT_Connection_Reset(void) {
shiftOut(shtData, shtClk, LSBFIRST, 255);
shiftOut(shtData, shtClk, LSBFIRST, 255);
}

void SHT_Soft_Reset(void) {
SHT_Connection_Reset();
ioByte = 30;
ackBit = 1;
SHT_Write_Byte();
delay(15);
}

void SHT_Wait(void) {
delay(5);
dly = 0;
while (dly < 600) {
if (digitalRead(shtData) == 0) dly=2600;
delay(1);
dly=dly+1;
}
}

void SHT_Start(void) {
digitalWrite(shtData, HIGH);
pinMode(shtData, OUTPUT);
digitalWrite(shtClk, HIGH);
digitalWrite(shtData, LOW);
digitalWrite(shtClk, LOW);
digitalWrite(shtClk, HIGH);
digitalWrite(shtData, HIGH);
digitalWrite(shtClk, LOW);
}

void SHT_Measure(int vSvc) {
SHT_Soft_Reset();
SHT_Start();
ioByte = vSvc;
SHT_Write_Byte();
SHT_Wait();
ackBit = 0;
SHT_Read_Byte();
int msby;
msby = ioByte;
ackBit = 1;
SHT_Read_Byte();
retVal = msby;
retVal = retVal * 0x100;
retVal = retVal + ioByte;
if (retVal <= 0) retVal = 1;
}

int SHT_Get_Status(void) {
SHT_Soft_Reset();
SHT_Start();
ioByte = 7;
SHT_Write_Byte();
SHT_Wait();
ackBit = 1;
SHT_Read_Byte();
return(ioByte);
}

void SHT_Heater(void) {
SHT_Soft_Reset();
SHT_Start();
ioByte = 6;
SHT_Write_Byte();
ioByte = 4;
SHT_Write_Byte();
ackBit = 1;
SHT_Read_Byte();
delay(500);
SHT_Soft_Reset();
SHT_Start();
ioByte = 6;
SHT_Write_Byte();
ioByte = 0;
SHT_Write_Byte();
ackBit = 1;
SHT_Read_Byte();
}
void setup()
{
pinMode(11, OUTPUT);
digitalWrite(11, HIGH);
pinMode(10, OUTPUT);
pinMode(13, OUTPUT);
Serial.begin(9600); // open serial
SHT_Connection_Reset();
}

void loop()
{
  digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);

while (Serial.available() > 1) {
cmdByte = Serial.read();
dataByte = Serial.read();
switch (cmdByte) {
case 65: // C
{
if (dataByte == 66) k=1;
break;
}
case 68:
{
k=0;
break;
}
}
}
if (k==1) {
msTimer=millis();
if (msTimer <= logTime) {
logTime = 0;
}
if (msTimer > (logTime + 1000)) {   //original  1000
logTime = millis();
// SHT11 #1 Temperature
SHT_Measure(3);
Serial.print(" T ");
retVal = retVal*0.01 - 40; //conversion a ºC
tC = retVal;
Serial.print(retVal, DEC); 
Serial.print("C");
Serial.print(" H ");
// SHT11 #1 Humidity
SHT_Measure(5);
rhLin = (retVal * 0.0405) - (retVal * 0.004 * retVal * 0.0007) - 4;
rhTrue = (tC - 25) * (retVal * 0.00008 + 0.01) + rhLin;
retVal = rhTrue;
Serial.print(retVal, DEC);
Serial.print("%");
Serial.println();
}
}
}

Los datos que recibo del sensor son

T 24C H 60% (Temeperatura en ºC y humedad Relativa)

referencias:

http://www.sensirion.com/pdf/product_information/Sample_Code_humidity_sensor_SHTxx.pdf

Yo lo estoy usando, me sirve sólo con la lineal, pues la temperatura es muy próxima a los 25 ºC y no necesitan altas precisiones. Lo que te da no se si está o no bien, pero no entiendo la cuenta rhLin = (retVal * 0.0405) - (retVal * 0.004 * retVal * 0.0007) - 4; debería ser, bueno yo lo hago así y me sale bien. rhLin = (retVal * 0.0405) - (retVal * retVal * 0.0000028) - 4;

Gracias por revisar el código
Se produjo un cambio en rhLin y agregue otro dato “Dew point” o Punto de rocío.(referencia de
http://www.sensirion.com/pdf/product_information/Sample_Code_humidity_sensor_SHTxx.pdf)

int shtClk=11; // clock pin
int shtData=10; // Data pin
int ioByte;
int ackBit;
int cmdByte;
int dataByte;
int k;
long msTimer;
long logTime;
double retVal; // Return value from SHT11
//--------//
int tC;           // Temperatura en ºC
int rhLin;        // Humedad 
int rhTrue;       // humedad relativa con tC 
float H;
float Dp;
//--------//
int dly;
uint8_t bitmask;

void SHT_Write_Byte(void) {
pinMode(shtData, OUTPUT);
shiftOut(shtData, shtClk, MSBFIRST, ioByte);
pinMode(shtData, INPUT);
digitalWrite(shtData, LOW);
digitalWrite(shtClk, LOW);
digitalWrite(shtClk, HIGH);
ackBit = digitalRead(shtData);
digitalWrite(shtClk, LOW);
}

int shiftIn() {
int cwt;
cwt=0;
bitmask=128;

while (bitmask >= 1) {
digitalWrite(shtClk, HIGH);
cwt = cwt + bitmask * digitalRead(shtData);
digitalWrite(shtClk, LOW);
bitmask=bitmask/2;
}
return(cwt);
}

void SHT_Read_Byte(void) {
ioByte = shiftIn();
digitalWrite(shtData, ackBit);
pinMode(shtData, OUTPUT);
digitalWrite(shtClk, HIGH);
digitalWrite(shtClk, LOW);
pinMode(shtData, INPUT);
digitalWrite(shtData, LOW);
}

void SHT_Connection_Reset(void) {
shiftOut(shtData, shtClk, LSBFIRST, 255);
shiftOut(shtData, shtClk, LSBFIRST, 255);
}

void SHT_Soft_Reset(void) {
SHT_Connection_Reset();
ioByte = 30;
ackBit = 1;
SHT_Write_Byte();
delay(15);
}

void SHT_Wait(void) {
delay(5);
dly = 0;
while (dly < 600) {
if (digitalRead(shtData) == 0) dly=2600;
delay(1);
dly=dly+1;
}
}

void SHT_Start(void) {
digitalWrite(shtData, HIGH);
pinMode(shtData, OUTPUT);
digitalWrite(shtClk, HIGH);
digitalWrite(shtData, LOW);
digitalWrite(shtClk, LOW);
digitalWrite(shtClk, HIGH);
digitalWrite(shtData, HIGH);
digitalWrite(shtClk, LOW);
}

void SHT_Measure(int vSvc) {
SHT_Soft_Reset();
SHT_Start();
ioByte = vSvc;
SHT_Write_Byte();
SHT_Wait();
ackBit = 0;
SHT_Read_Byte();
int msby;
msby = ioByte;
ackBit = 1;
SHT_Read_Byte();
retVal = msby;
retVal = retVal * 0x100;
retVal = retVal + ioByte;
if (retVal <= 0) retVal = 1;
}

int SHT_Get_Status(void) {
SHT_Soft_Reset();
SHT_Start();
ioByte = 7;
SHT_Write_Byte();
SHT_Wait();
ackBit = 1;
SHT_Read_Byte();
return(ioByte);
}

void SHT_Heater(void) {
SHT_Soft_Reset();
SHT_Start();
ioByte = 6;
SHT_Write_Byte();
ioByte = 4;
SHT_Write_Byte();
ackBit = 1;
SHT_Read_Byte();
delay(500);
SHT_Soft_Reset();
SHT_Start();
ioByte = 6;
SHT_Write_Byte();
ioByte = 0;
SHT_Write_Byte();
ackBit = 1;
SHT_Read_Byte();
}
void setup()
{
pinMode(11, OUTPUT);
digitalWrite(11, HIGH);
pinMode(10, OUTPUT);
pinMode(13, OUTPUT);
Serial.begin(9600); // open serial
SHT_Connection_Reset();
}

void loop()
{
  digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);

while (Serial.available() > 1) {
cmdByte = Serial.read();
dataByte = Serial.read();
switch (cmdByte) {
case 65: // C
{
if (dataByte == 66) k=1;
break;
}
case 68:
{
k=0;
break;
}
}
}
if (k==1) {
msTimer=millis();
if (msTimer <= logTime) {
logTime = 0;
}
if (msTimer > (logTime + 1000)) {   //original  1000
logTime = millis();
// SHT11 #1 Temperature
SHT_Measure(3);
Serial.print(" T ");
retVal = retVal*0.01 - 40;
tC = retVal;
Serial.print(retVal, DEC); //conversion a cº
Serial.print("C");
Serial.print(" H ");
// SHT11 #1 Humidity
SHT_Measure(5);
rhLin = (retVal * 0.0405) - (retVal * retVal * 0.0000028) - 4;
rhTrue = (tC - 25) * (retVal * 0.00008 + 0.01) + rhLin;
retVal = rhTrue;
Serial.print(retVal, DEC);
Serial.print("%");
Serial.print(" Dp ");
H = (log10(retVal)-2)/0.4343 + (17.62*tC)/(243.12+tC);
Dp = 243.12*H/(17.62-H);
Serial.print(Dp, DEC);
Serial.print("C");
Serial.println();
}
}
}