DS18B20 serial print sensor name

Hi,

I couldn't find a solution anywhere so I hope someone might be able to help. I have three DS18B20 sensors with a assigned name for the address. I have a separate function that checks if the sensor temperature exceeds a set temperature which is called in the loop (by the defined name). All works, except one thing. I want the code to output the sensor name if it exceeds the temperature. Right now the code is running, but instead of the name it prints "29555" and then the ALARM 1 or 2 message (or nothing at all). Can this be achieved? And if yes, how?

Thanks in advance!

#include <DallasTemperature.h>      //Temp sensor library
#include <OneWire.h>                //Temp sensor library

// Temperatuur sensoren setup
#define TEMP_BUS 8  // Temperatuur sensoren op bus 8
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(TEMP_BUS);
// OneWire referentie
DallasTemperature sensors(&oneWire);
//Adressen Thermometers
DeviceAddress RELAIS = { 0x28, 0xFF, 0x74, 0x4E, 0x51, 0x17, 0x4, 0xFC };
DeviceAddress MOTOR = { 0x28, 0xFF, 0x5, 0x55, 0x51, 0x17, 0x4, 0xF0 };
DeviceAddress ESC = { 0x28, 0xFF, 0xD, 0x28, 0x51, 0x17, 0x4, 0xFB };


void setup()
{
  Serial.begin(9600);
    //Temp sensoren
  sensors.begin();
  // Resolutie temp-sensoren op 12-bits
  sensors.setResolution(ESC, 9);
  sensors.setResolution(MOTOR, 9);
  sensors.setResolution(RELAIS, 9);
}
void CheckTemperature(DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);

      const int HighTemp = 15;
  const int MaxTemp = 20;
 

  sensors.requestTemperatures();
  if(tempC >= HighTemp && tempC < MaxTemp){
    Serial.println();
    Serial.println('deviceAddress'); // Print the sensor name as called in loop
    Serial.print("ALARM 1");
    } 
   else if(tempC >= MaxTemp){
    Serial.println('deviceAddress'); // Print the sensor name as called in loop
Serial.print("ALARM 2");
  }
}

void loop()
{
  CheckTemperature(RELAIS);
  CheckTemperature(ESC);
  CheckTemperature(MOTOR);

  Serial.println();
  delay(10000);
}

Names like RELAIS only exist at compile time; they're long forgotten at runtime so you can't expect to be able to use them directly. I'd be inclined to declare an array of structs that let you specify the sensor address and a name for each sensor.

Easier though would be to pass a second parameter to your CheckTemperature function that contains the name you want printed. You should pass that parameter as a string using double quotes:

CheckTemperature(RELAIS,"Relais");

Obviously you will need to fix the CheckTemperature function to comply, but the compiler will tell you that.

fabiandelete:
Can this be achieved? And if yes, how?

yes, look into structs.

struct Device{
  DeviceAddress address;
  char* friendlyName;
}

You can't select any random data type to print. Serial.print and .println do not know what the data type DeviceAddress is or how to print it. Do you know what the data type is?

The data type is uint8_t [8]. I know because I looked it up in the library header file. Since it is not a null terminated char array (equivocally uint8_t is a char) but the array is not null terminated so it will not work like char[]).

Two options:

  1. Copy each element into a char[9] array then print the char[] array
  2. Just print each of the values directly;

Pseudo code for option 2:
for (x=0; x<7; x++)
{
Serial.print("0x")
Serial.print(DeviceAddress[x ])
}
Serial.println()

This is dealers choice, both will work.

Thanks for the fast help!

I'm Trying it with the method Wildbill posted. I get the first part (adapting the call). But I get lost in adapting the function. The code isn't willing to print the second argument (right now i get the error " 'X' is not a type.". What am I doing wrong?

#include <DallasTemperature.h>      //Temp sensor library
#include <OneWire.h>                //Temp sensor library

// Temperatuur sensoren setup
#define TEMP_BUS 8  // Temperatuur sensoren op bus 8
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(TEMP_BUS);
// OneWire referentie
DallasTemperature sensors(&oneWire);
//Adressen Thermometers
DeviceAddress RELAIS = { 0x28, 0xFF, 0x74, 0x4E, 0x51, 0x17, 0x4, 0xFC };
DeviceAddress MOTOR = { 0x28, 0xFF, 0x5, 0x55, 0x51, 0x17, 0x4, 0xF0 };
DeviceAddress ESC = { 0x28, 0xFF, 0xD, 0x28, 0x51, 0x17, 0x4, 0xFB };
int ThrottleSet = 50;
int PowerMax = 2000;
int PowerMin = 1000;
int PowerRange = PowerMax - PowerMin;
int x=0;
int X=0;

void setup()
{
  Serial.begin(9600);
    //Temp sensoren
  sensors.begin();
  // Resolutie temp-sensoren op 12-bits
  sensors.setResolution(ESC, 9);
  sensors.setResolution(MOTOR, 9);
  sensors.setResolution(RELAIS, 9);
}
void CheckTemperature(DeviceAddress deviceAddress , X)
{
  float tempC = sensors.getTempC(deviceAddress);

      const int HighTemp = 15;
  const int MaxTemp = 20;
  int PowerMax = 2000;
  int PowerMin = 1000;

  sensors.requestTemperatures();
  if(tempC >= HighTemp && tempC < MaxTemp){
    PowerMax = PowerMin + PowerRange/2;
    Serial.println();
    Serial.println(X); // Print the sensor name as called in loop
    Serial.print("ALARM 1");
  }

   else if(tempC >= MaxTemp){
    PowerMax = PowerMin;
    Serial.println(X); // Print the sensor name as called in loop
Serial.print("ALARM 2");
  }
}

void loop()
{
  CheckTemperature(RELAIS,"RELAIS");
  CheckTemperature(ESC,"ESC");
  CheckTemperature(MOTOR,"MOTOR");

  Serial.println();
  delay(10000);
}

Got It!

Combining Wildbill's and BulldogLowell's posts made it work. Thanks for the help!

For anyone in the future, this is the code now:

#include <DallasTemperature.h>      //Temp sensor library
#include <OneWire.h>                //Temp sensor library

// Temperatuur sensoren setup
#define TEMP_BUS 8  // Temperatuur sensoren op bus 8
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(TEMP_BUS);
// OneWire referentie
DallasTemperature sensors(&oneWire);
//Adressen Thermometers
DeviceAddress RELAIS = { 0x28, 0xFF, 0x74, 0x4E, 0x51, 0x17, 0x4, 0xFC };
DeviceAddress MOTOR = { 0x28, 0xFF, 0x5, 0x55, 0x51, 0x17, 0x4, 0xF0 };
DeviceAddress ESC = { 0x28, 0xFF, 0xD, 0x28, 0x51, 0x17, 0x4, 0xFB };
int ThrottleSet = 50;
int PowerMax = 2000;
int PowerMin = 1000;
int PowerRange = PowerMax - PowerMin;
int x=0;
int X=0;

void setup()
{
  Serial.begin(9600);
    //Temp sensoren
  sensors.begin();
  // Resolutie temp-sensoren op 12-bits
  sensors.setResolution(ESC, 9);
  sensors.setResolution(MOTOR, 9);
  sensors.setResolution(RELAIS, 9);
}
void CheckTemperature(DeviceAddress deviceAddress , char* z)
{
  float tempC = sensors.getTempC(deviceAddress);

      const int HighTemp = 15;
  const int MaxTemp = 20;
  int PowerMax = 2000;
  int PowerMin = 1000;

  sensors.requestTemperatures();
  if(tempC >= HighTemp && tempC < MaxTemp){
    PowerMax = PowerMin + PowerRange/2;
    Serial.println();
    Serial.println(z); // Print the sensor name as called in loop
    Serial.print("ALARM 1");
  }

   else if(tempC >= MaxTemp){
    PowerMax = PowerMin;
    Serial.println();
    Serial.println(z); // Print the sensor name as called in loop
Serial.print("ALARM 2");
  }
}

void loop()
{
  CheckTemperature(RELAIS,"RELAIS");
  CheckTemperature(ESC,"ESC");
  CheckTemperature(MOTOR,"MOTOR");

  Serial.println();
  delay(10000);
}
1 Like