How can I upload and plot temperature values that are both above zero (e.g., ambient) and below zero (e.g., in a freezer)?

I wish to upload and plot on channel chart both positive and negative temperatures from DS18B20 sensor. While I can see the positive temperatures properly on the chart, the negative temperatures also show as above zero. The code snippet is as below:
if(temp_data & 0x80)
{
temp_data = (~temp_data)+1;
sprintf(esp_buff, "GET /update?api_key=%s&field1=%d", API_WRITE_KEY, -temp_data);
}
else
{
sprintf(esp_buff, "GET /update?api_key=%s&field1=%d", API_WRITE_KEY, temp_data);
}
//sprintf(esp_buff, "GET /update?api_key=%s&field1=%d", API_WRITE_KEY, temp_data);
ESP8266_Send(esp_buff);
It can be seen that the last two readings which are -6 and -9 deg C,show as +6 and +9 deg C. what should I do to make them come below the zero line?

5 Comments

Looks as though you're munging on an 8-bit data value and masking off the sign bit -- should be reading into a signed int8 variable or doing a typecast to a double of the signed input instead before this where retrieve the data. Then you wouldn't have need for all the above machinations.
Hello DB. I am reading the raw bytes from DS18B20 into a 16 bit signed variableas shown below. In the program to upload the data to Thingspeak, I am using only the integer part, hence >>4. This variable too is signed 8 bit.
So, I did use signed 8 bit variable. I tried two methods. First, the statement outside the if-else section. I typecast (float) temp_data thinking that the float will automatically convert the signed data to a -ve integer (e.g., 0xFF78 to -8.50). But this did not work. To confirm where the problem lies, I exported the channel data to a CSV file. Here,I observed that the negative readings were actually being shown as positive. So the problem is somewhere in conversion coding. Please see the feeds.csv file attached. Lines 8, 9, 10 show data as 6, 11, 14, which should be -6, -11, -14.
Please advise with the correct code statements,I will be much obliged.
int16_t rawtemp; // signed temperature data from DS18B20
int8_t temp_data = rawtemp >> 4; //use only signed integer part of data
#ifdef SEND_DEMO //we are using SEND only - integer temperature data
memset(esp_buff, 0, 150);
/*if(temp_data & 0x80) //commented out this section since not working as desired
{
temp_data = (~temp_data)+1;
sprintf(esp_buff, "GET /update?api_key=%s&field1=%d", API_WRITE_KEY, temp_data);
}
else
{
sprintf(esp_buff, "GET /update?api_key=%s&field1=%d", API_WRITE_KEY, temp_data);
} */
//using the following statement.
sprintf(esp_buff, "GET /update?api_key=%s&field1=%.1f", API_WRITE_KEY, (float)temp_data);
ESP8266_Send(esp_buff);
Can you send the esp_buff to the serial monitor to see what you are sending to ThingSpeak? ThingSpeak is actually quite good at number interpretation, I suspect you may be failing to send the data that you wish to send.
You could also send rawtemp and do the conversion with MATLAB inside of ThingSpeak.
Hello Christopher. you are right. I did check the esp_buff on the serial monitor. It shows positive temperature reading
even though the sensor is in the freezer and the LCD display shows negative. So I am not sending the data properly as required.
This is the line of code showing sprintf().
if(temp_msb & 0x80)
{
rawtemp = ~rawtemp+1; // 2's complement if negative
}
temp_data = rawtemp>>4; // use integer part
if(temp_msb & 0x80)
{
sprintf(esp_buff, "GET /update?api_key=%s&field1=%d", API_WRITE_KEY, temp_data);
}
I tried many combinations as shown on various C languages tutorials on sprintf(). None work. I can confirmthe readings are all positive when I export the channel data in a csv format. Iam now at a loss.
Can you please advise how to use MATLAB's conversion routines? I am not a good software developer, but I do know C sufficiently well.
I backed out of the thread because I have no Arduino experience nor one at hand to play with to learn about, but the poking around I did indicated there are at least a couple of libraries that can be used to interact with the one-wire device...it appeared with those one just reads a buffer and magic happens...

Sign in to comment.

Answers (3)

Hello DB. I am not an Arduino fan. I use the Arduino hardware but do the programming with Atmel Studio 7. I do not use Arduino IDE. So I do not use Arduino libraries. But I observed that the sprintf statement does not give output as negative values, even though the LCD shows negative readings. Can you spot any issues in my use of the sprintf function? Also, can you please guide me how to use MATLAB's data conversion functions to generate correct output from signed hex integer? Thanks.
If you can get your data into a double image, you can use
imshow(yourTemperatureImage, []);
I think the library answer seems best.
Here is an example using the dallas one wire (that has some other dependancies built in )
This article seems to indicate that you can use arduino libraries in Atmel Studio.
If you wanted to fix the code above, I would start by writing rawtemp to thingspeak and seeing if the negative values are encoded in there. Then you can fix your code to translate rawtemp appropriately.
sprintf(esp_buff, "GET /update?api_key=%s&field1=%d&field2=%d", API_WRITE_KEY, temp_data,rawtemp);

1 Comment

Hello Christopher. I am sorry for the delay responding to your advice, I was caught upin some othr work. As suggested by you and others, I simulated thenegative temperature as sent by DS18B20 in the Atmel Studio debugger. I was happy to see that the sprintf statement (for -ve data) was properly working, it showed me proper negative temperature. But once again, when I put the system online, I got only positive readings. Then I checked the actual raw data just before processing it for the sprintf statement (Refer debug statements incode). I caught the bug here. I found that I was mistakenly using the already processed data used by the LCD display routine just before the routine toupload toThingspeak. This was a mistake. I then edited the code to copy the DS18B20 data to another variable and processed it.This worked. Now the temperature is showing negative readings as desired. It was sheer overlook on my part not to have used the proper data.
Thank you all to advise me and suggest the way to solve the problem.
int16_t ts_temperature; //set variable for temperature upload to Thingspeak -ADDED
int8_t temp_msb; //variable to check sign
int16_t temp_data; //variable to hold integer part of ts_temperature
ts_temperature = rawtemp; //copy to upload to TS - USED THIS DATA
memset(debug_buff, 0, 10);
sprintf(debug_buff, "%X", ts_temperature); //debug- read temp
uart_puts(debug_buff); //THIS SHOWED THAT WRONG DATA WAS BEING USED
uart_puts("\r\n");
...............//FURTHER CODE

Sign in to comment.

Communities

More Answers in the  ThingSpeak Community

Categories

Asked:

on 10 Sep 2022

Commented:

on 15 Sep 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!