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

10 vues (au cours des 30 derniers jours)
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 commentaires
Ardish Morawala
Ardish Morawala le 12 Sep 2022
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.
dpb
dpb le 12 Sep 2022
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...

Connectez-vous pour commenter.

Réponses (3)

Ardish Morawala
Ardish Morawala le 12 Sep 2022
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.

Image Analyst
Image Analyst le 12 Sep 2022
If you can get your data into a double image, you can use
imshow(yourTemperatureImage, []);

Christopher Stapels
Christopher Stapels le 12 Sep 2022
Modifié(e) : Christopher Stapels le 12 Sep 2022
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 commentaire
Ardish Morawala
Ardish Morawala le 15 Sep 2022
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

Connectez-vous pour commenter.

Communautés

Plus de réponses dans  ThingSpeak Community

Catégories

En savoir plus sur Instrument Control Toolbox dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by