Dear Forum, ...edit...please jump to post number 4 before wading through this!
I'm continuing on with a project in which I am trying to get up to 10 temperature readings, and act upon them. The sensors are Maxim DS18B20, and I have copied some code to allow me to take measurements; and the code works really well. Of course this code doesn't do exactly what I want so, despite not knowing exactly what I'm doing, I'm trying to modify it!
This code uses two libraries, called OneWire.h and DallasTemperature.h, and the code is shown below:
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into digital pin 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass oneWire reference to DallasTemperature library
DallasTemperature sensors(&oneWire);
int deviceCount = 0;
float tempC;
void setup(void)
{
sensors.begin(); // Start up the library
//Set all sensors' resolution to 10 bits
sensors.setResolution(10);
Serial.begin(9600);
// locate devices on the bus
Serial.print("Locating devices...");
Serial.print("Found ");
deviceCount = sensors.getDeviceCount();
Serial.print(deviceCount, DEC); //the DEC simply makes sure number is decimal
Serial.println(" devices.");
Serial.println("");
}
void loop(void)
{
// Send command to all the sensors for temperature conversion
sensors.requestTemperatures();
// Display temperature from each sensor
for (int i = 0; i < deviceCount; i++)
{
Serial.print("Sensor ");
Serial.print(i+1);
Serial.print(" : ");
tempC = sensors.getTempCByIndex(i);
Serial.print(tempC);
Serial.print((char)39);//shows nearest to degrees character I can find
Serial.print("C | ");
Serial.print(DallasTemperature::toFahrenheit(tempC));
Serial.print((char)176);//shows degrees character
//no, it doesn't! Not recognised on my serial monitor
Serial.println("F");
}
Serial.println("");
delay(1000);
}
There are a couple of niggly points, such as near to the end the author tries to print a degrees symbol
Serial.print((char)176);//shows degrees character What appears when I look at my serial monitor is a horizontally inverted question mark (mirrored '?') which, as I can't find it in the ASCII character set, presumably means an unrecognisable unicode.
What could be the solution to this, ie so it prints the correct degrees symbol?
I managed to change the resolution for every sensor down from 12 bits to 10 bits in order to save temperature conversion time, using the line
//Set all sensors' resolution to 10 bits
sensors.setResolution(10);
but really I got lost in the header files of DallasTemperature and Onewire, so it was more luck than knowledge - ultimately I'd like to be able to set the resolution for each sensor individually (or at least KNOW how to do it, as this project will run fine by setting all sensors the same.
The main aim for me is to be able to read an individual sensor's 64 bit identification number so that I can give it a useful name such as ROOMTEMP or OUTSIDE TEMP, etc. At the moment the software decides which sensor is 'sensor 1' etc., based on the command tempC = sensors.getTempCByIndex(i); but that's not ideal as I can see that, should one sensor have to be replaced, this order may change.
My questions then:
-
Does the "sensors.getTempCByIndex(i);" command always read the sensors in the same order? Lowest S/N to highest S/N?
If this is the case then a sensor substitution could change the order, so simply assigning a name to 'sensor1....sensor2, etc, would be foolhardy. -
Looking at the datasheet for DS18B20 the S/N must be quoted over the databus to get that sensor to cough up its temperature reading, so presumably the number I want is there inside one of the libraries (I have looked, but don't really know what I'm looking for). Please would someone point me towards what I should look for, where it is, and how I grab it for use in my code.
Regards, and thanks to all who give up their time to try to educate old duffers like me!
GM