DS1822 Programming (Assigning name for each sensor)

Hello!

I found this code from hacktronics.com

//  This Arduino sketch reads DS18B20 "1-Wire" digital
//  temperature sensors.
//  Copyright (c) 2010 Mark McComb, hacktronics LLC
//  License: http://www.opensource.org/licenses/mit-license.php (Go crazy)
//  Tutorial:
//  http://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html

#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into pin 3 on the Arduino
#define ONE_WIRE_BUS 3

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

// Assign the addresses of your 1-Wire temp sensors.
// See the tutorial on how to obtain these addresses:
// http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html

DeviceAddress insideThermometer = { 0x28, 0x94, 0xE2, 0xDF, 0x02, 0x00, 0x00, 0xFE };
DeviceAddress outsideThermometer = { 0x28, 0x6B, 0xDF, 0xDF, 0x02, 0x00, 0x00, 0xC0 };
DeviceAddress dogHouseThermometer = { 0x28, 0x59, 0xBE, 0xDF, 0x02, 0x00, 0x00, 0x9F };

void setup(void)
{
  // start serial port
  Serial.begin(9600);
  // Start up the library
  sensors.begin();
  // set the resolution to 10 bit (good enough?)
  sensors.setResolution(insideThermometer, 10);
  sensors.setResolution(outsideThermometer, 10);
  sensors.setResolution(dogHouseThermometer, 10);
}

void printTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == -127.00) {
    Serial.print("Error getting temperature");
  } else {
    Serial.print("C: ");
    Serial.print(tempC);
    Serial.print(" F: ");
    Serial.print(DallasTemperature::toFahrenheit(tempC));
  }
}

void loop(void)
{ 
  delay(2000);
  Serial.print("Getting temperatures...\n\r");
  sensors.requestTemperatures();
  
  Serial.print("Inside temperature is: ");
  printTemperature(insideThermometer);
  Serial.print("\n\r");
  Serial.print("Outside temperature is: ");
  printTemperature(outsideThermometer);
  Serial.print("\n\r");
  Serial.print("Dog House temperature is: ");
  printTemperature(dogHouseThermometer);
  Serial.print("\n\r\n\r");
}

Could you teach me on how to modify the code so that I could use a for loop for printing the DeviceAddress name and temperature reading?

Can I modify the code so that I could use a for loop for printing the DeviceAddress name and temperature reading?

Of course you can.

By using an array?

arduinoTime:
By using an array?

Several options...
using class it is possible. Then you make array of this class and loop through the whole array.

class Device {
public:
    DeviceAddress da;
    float tempC;
    void printTemperature();
};

xmart:
Several options...
using class it is possible. Then you make array of this class and loop through the whole array.

Can I use function instead of class? Also can I store the device address in an array?

Example:
DeviceAddress insideThermometer = { 0x28, 0x94, 0xE2, 0xDF, 0x02, 0x00, 0x00, 0xFE };
DeviceAddress outsideThermometer = { 0x28, 0x6B, 0xDF, 0xDF, 0x02, 0x00, 0x00, 0xC0 };
DeviceAddress dogHouseThermometer = { 0x28, 0x59, 0xBE, 0xDF, 0x02, 0x00, 0x00, 0x9F };

to

thermometer[inside] = insideThermometer;
thermometer[outside] = outsideThermometer;
thermometer[dogHouse] = dogHouseThermometer;

Sorry, I don't know how to use class =(

This is why I want to modify the code:
I'll be using different numbers of temperature sensors for each pin and I will assign a name on all of them. Then I will use a for loop to check all pins (1 to 13) and display the temperature sensor name and reading.

I don't know how to use class and I don't know much about two-dimensional arrays (but I'm reading/trying) so, please please please, could you tell me what to research and do?

I found this code on bildr.org and it prints the address of all temperature sensor it detects and the temperature reading but it doesn't assign names on the temperature sensor.

What code (hacktronics or bildr) will be easier for me to modify so I could execute what I have in mind?

Since you allude to Hacktronics, I am assuming you are aware that these devices operate over one wire, but even that might be a bit rash of me

Then I will use a for loop to check all pins (1 to 13)

as it's very hard to understand what you are trying to do, but I think you might be trying to re-invent the wheel - possibly twice.

I think there are essentially two ways of reading the DS18B20 over one wire. The first is by calling the predetermined address and reading it, as exemplified by Hacktronics. The other is by trawling the address as part of the process.

I'll be using different numbers of temperature sensors for each pin

It's possible that your problem is that you intend to be regularly varying the number of sensors on the pin, and want to do that without altering the programme. If that is your intention, the Hacktronics method is not for you. If that is not your intention, I fail to see why you have a problem, and the Hacktronics method is all you need..

so, please please please, could you tell me what to research and do?

I submit the first thing to do is understand what the sensor is all about, which principally means understanding how many wires a one-wire system needs. The Hacktronics tutorial probably has all you need - certainly enough to make a decision about its suitability.

Sorry, I don't know how to use class

I haven't the faintest idea of what class is, much less how to use it and, for this exercise, I bet you don't need to know either. The same goes for two-dimensional arrays. All you need to do is read the sensors, and present the data in a readable form to whatever needs it. So, if that is actually what you want, Hacktronics has what you need .

Nick_Pyner:
It's possible that your problem is that you intend to be regularly varying the number of sensors on the pin, and want to do that without altering the programme. If that is your intention, the Hacktronics method is not for you.

Yes, this is what I need. Could you give me an advise on what method to use so I could solve my problem?

Every sensor they make, has a unique identifier code. Those three long identifiers in that code example, are the ID numbers of the sensors that the guy who wrote that example owned. Your sensors will have different ID numbers ( unless you got his, somehow ), and you will have to figure out what they are.

arduinoTime:

Nick_Pyner:
It's possible that your problem is that you intend to be regularly varying the number of sensors on the pin, and want to do that without altering the programme. If that is your intention, the Hacktronics method is not for you.

Yes, this is what I need. Could you give me an advise on what method to use so I could solve my problem?

OK. IF you are certain that that is what you want to do, that's a decision made, and the Hacktronics code is not for you. What you want is a variation that uses the command.

getTempCByIndex

Whereby you need not know the sensor addresses, the programme sniffs them for you each time round the loop. I don't think it's a good idea, I don't understand it, and I have never used it, but then I have never needed to, and it does work for other people.

http://www.ucontrolit.tv/184/

Assuming that your DS1822s behave like DS18B20s, getTempCByIndex will get you part way to your goal if you're prepared to use multiple pins. You can hard code the information that the dog house sensor is on say pin 6.

The problem comes when you want to take advantage of one wire and put multiple sensors on a single pin. These sensors will presumably be in different places and you'll want a descriptive name associated with each. Sadly, the arduino has no way to know what the physical layout of the sensors is.

It's a common mistake to suppose that the sensor that responds to getTempCByIndex(0) is the physically closest to the Arduino. It isn't. Worse yet, if you ever switch out a sensor, the order that the sensors respond in may change.

You really need some way to let the Arduino know what sensors are where. That's going to be tricky if you don't want to have to change the code.

Good way to keep track of them is to give each one a name in the sketch. Like K1,K2,D1,L1,L2,L3 etc. Then write them on a sheet of paper along with the location. If you then move them, it's just a matter of changing the location on the paper.

Nick_Pyner:
getTempCByIndex
http://www.ucontrolit.tv/184/

Thank you! It solved one of my problems.
Code:

/*
Tested on Arduino 1.0.3
UcontrolIt.TV
Bulding Block 2 - Sketch 1 Single Sensor to Serial with High Low and C/F
 
Add two more libraries: OneWire and DallasTemperature
 
Mark Johnson
uControlIt.tv
December 2012
 
License: GNU General Public License
 
*/
 
// Load Libraries for DS1820 and OneWire
#include <OneWire.h>
#include <DallasTemperature.h>
 
// DS1820 Data wire is plugged into pin 4 on the Arduino
#define ONE_WIRE_BUS_A 3
#define ONE_WIRE_BUS_B 2
 
// Setup oneWire instance to communicate with devices
OneWire oneWireA(ONE_WIRE_BUS_A);
OneWire oneWireB(ONE_WIRE_BUS_B);

// Pass oneWire reference to Dallas Temperature.
DallasTemperature sensorA(&oneWireA);
DallasTemperature sensorB(&oneWireB);

// Indicates the sensor for each pin minus 1 because it stars with zero
int a = 1;
int b = 0;

 
void setup()
{
   Serial.begin(9600);
   // Start the OneWire library
   sensorA.begin();
   sensorB.begin();
}
 
void loop()
{
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  sensorA.requestTemperatures(); // Send the command to get temperatures
  sensorB.requestTemperatures(); // Send the command to get temperatures

  for(int i = 0; i <= a; i++){
    Serial.print("Pin A, Sensor "); Serial.println(i);
    Serial.print("Current Temp: ");
    Serial.print(sensorA.getTempCByIndex(i));
    Serial.println("C");
    delay (500);
  }

  for(int i = 0; i <= b; i++){
    Serial.print("Pin B, Sensor "); Serial.println(i);
    Serial.print("Current Temp: ");
    Serial.print(sensorB.getTempCByIndex(i));
    Serial.println("C");
    delay (500);
  }
}

Display:

Pin A, Sensor 0
Current Temp: 27.62C
Pin A, Sensor 1
Current Temp: 27.50C
Pin B, Sensor 0
Current Temp: 27.56C

Is it possible to declare only one oneWire reference to DallasTemperature (use sensors instead of sensorA and sensorB) so that I could shorten the code? Also, what can I do to the sensors unique ID to name each sensor? (Pin A, Sensor 0 will be recognised as Inside Thermometer)

OneWire class is initialized for a pin, if you use more pins then you have to use as many instances as many pins (without changes inside of lib). It is able to put everything on the only bus, it supports large number of sensors.

...so that I could shorten the code?

Yes and also to save other resources: memory, pins...if it worth.

Also, what can I do to the sensors unique ID to name each sensor? (Pin A, Sensor 0 will be recognised as Inside Thermometer)

? ...to change a name?