Hello Lukapple,
I had a look on my code how to retrieve the temperatur in Celsius and Fahrenheit from the received bit stream.
So there is no need to reinvent the wheel.
The code is in C#.
// retrieve the measured temperature as an int from the received '0' and '1' in the char array 'outArray'
int gotValue = 0;
int shiftValue = 1;
for (int i = 0 ; i < valueEndPos - valueStartPos + 1; i++ )
{
if (outArray[valueEndPos - i] == '1')
{
gotValue += (shiftValue << i);
}
else
{
if (outArray[valueEndPos - i] != '0')
{
gotValue = 9999; // gotValue = 9999 if there is at least one ? in the array
break;
}
}
}
// Some calculations to transform the measured values which are valid for
// the range from -50°C to +70°C in a form that can be easily displayed
// I'm sure there are more elegant methods to do this task
//Here the int e.measuredValue holds the temperature (gotValue) from the code above
int measuredValue = e.measuredValue / 2; // remove last digit, then one bit = 0.1 degree Celcius
measuredValue = measuredValue & 4095; // new remove leading 1 s
//if ((3595 < measuredValue) && (measuredValue < 4096)) // Negative readings are valid from -0.1 to -50 degree Celsius
if ((3700 < measuredValue) && (measuredValue < 4096)) // Negative readings are valid from -0.1 to -39 degree Celsius
{
measuredValue = 4096 - measuredValue;
degreeCelsiusString = "-";
degreeCelsiusSign = false; // sign is "-"
measuredValuePlus50 = 500 - measuredValue; // // to have only positive values we add 50 degree Celsius
} // then -50°C = 0, 0°C = 50, 70°C = 120
else // now it is easy to compare with certain thresholds
{
if ((measuredValue >= 0) && (measuredValue < 701)) // Positive readings are valid from 0 to +70 degree Celsius
{
degreeCelsiusString = "+";
degreeCelsiusSign = true; // sign is "+"
measuredValuePlus50 = measuredValue + 500; // add eqiv. 50 °C
}
else
{
degreeCelsiusString = "???";
}
}
if (degreeCelsiusString != "???")
{
// calculate celsius value
//decimalValue = ((double)measuredValue / 10);
decimalValue = degreeCelsiusSign ? (double)measuredValue / 10 : -(double)measuredValue / 10;
decimalValuePlus50 = ((double)measuredValuePlus50 / 10); // by adding 50 degree Celsius the valid range -50 - + 70
// is now 0 - 120
degreeCelsiusString = decimalValue.ToString("f1") + " °C";
// calculate fahrenheit from celsius
fahrenheitValue = decimalValue * 1.8;
if (degreeCelsiusSign == false)
{ fahrenheitValue = fahrenheitValue * -1; }
fahrenheitValue += 32;
if (fahrenheitValue >= 0)
{
degreeFahrenheitString = "+" + fahrenheitValue.ToString("f1") + " °F";
}
else
{
degreeFahrenheitString = fahrenheitValue.ToString("f1") + " °F";
}
}
else
{
decimalValue = InValidValue;
}
outString = outString + bitString.Substring(0, 9) + " " + bitString.Substring(9, 15)
+ " " + bitString.Substring(24, 5) + " Measured Value: "
+ degreeCelsiusString + " (+50 = " + decimalValuePlus50.ToString("f1") + ") "
+ degreeFahrenheitString + " " + " Time: "
+ e.ReadTime.Hour + ":" + e.ReadTime.Minute + ":" + e.ReadTime.Second
+ " Repetitions needed: " + e.repCount
+ " Failed Bit-Count: " + e.failedBitsCount
+ " Eliminated Noise Spikes: " + e.eliminatedSpikesCount;
_Print_Debug(outString + "\r\n");