Quote:
Originally Posted by PaulTR
Went ahead and just bent back the plastic piece, and hosted the APK from the source code on Dropbox since it's not available online anymore. Not sure if it's reading correctly, but at least it's reading. Getting 1.13...E9k, which is definitely not within a normal range.
|
Disclaimer: I am totally new to electronics and Arduino, so I may be talking complete nonsense here.
I stripped one side of one of the female-female wires (just cut off the white plastic), and put a little solder on each tip to keep it together (to prevent the ends from fraying).
Once I was set up, I also got the value you got (1.13...E9k). After some digging, it looked like it's because the Arduino side is sending a 4-byte
float:
Code:
int val = analogRead( sensorPin );
float voltage = (val * 5.0) / 1024.0;
float tempCelcius = voltage * 100;
float tempKelvin = tempCelcius + 273.15;
byte * b = (byte *) &tempKelvin;
usb.write(b, 4);
while the Android side is reading it into a 4 byte
int:
Code:
int i = 0;
while (i < bytes) {
if (bytes >= 4) {
int intbits = ((buffer[i + 3]) << 24)
| ((buffer[i + 2] & 0xff) << 16)
| ((buffer[i + 1] & 0xff) << 8)
| ((buffer[i] & 0xff));
mHandler.obtainMessage(NEW_TEMPERATURE,
new Temperature(intbits)).sendToTarget();
}
i += 4;
So I did all my calculations in
float, but cast to
int just before sending to USB.
Code:
float tempKelvin = tempCelcius + 273.15;
int Kelvin = (int) tempKelvin;
byte * b = (byte *) &Kelvin;
usb.write(b, sizeof(Kelvin);
and I had to update the Android side, since
int on Arduino (the board I have) is only 2 bytes:
Code:
while (i < bytes) {
if (bytes >= 2) {
int int_bits = 0;
int_bits = ((buffer[i + 1] & 0xff) << 8)
| ((buffer[i] & 0xff));
mHandler.obtainMessage(NEW_TEMPERATURE,
new Temperature(int_bits)).sendToTarget();
}
i += 2;
My values were then much more reasonable, but still not realistic (80 °C

)
Some more digging, and I realized that the algorithms in the book to calculate the temperature from the sensor pin reading are not consistent: the notes in the book gives one algorithm, and the code gives another. A bit of Googling and I think (note disclaimer above) the code's algorithm is for the LM35 sensor, whereas the notes' algorithm is for TMP36 (which is the one included in the K000008 Kit). Anyway, I just grabbed
this algorithm for TMP36
Code:
int rawvoltage= analogRead(outputpin);
float volts= rawvoltage/205.0;
float celsiustemp= 100.0 * volts - 50;
(don't forget to add 273.15 to celciustemp if you want to send the temperature in Kelvin)
After plugging in the above (and sending a 2-byte int instead of the 4-byte float), it's working fine.
I've lost some accuracy since I'm throwing away the fractional part of the temperature, but I don't know how to parse floats on the Android side: how many bits are for the mantissa? do I do byte swapping like in the case of the int? I'd be very grateful if anyone can advise me on this.
ps: I know this is somewhat of a thread necro, but hopefully the above can be of some assistance to someone else.
pps: I'm a total newb to this stuff, so please post any corrections to the above.