Temp sensor device address into variable

I'm trying to get the actual device address of multiple temp sensors. I found a sub the gets the address but it prints to the screen. I want to return it in a variable that I am going to concat to send to a url. How to I get this sub to return the device id xxx in a variable instead of serial.print to the screen. Sorry I'm a little new to this.
string xxx = Serial.print(deviceAddress[i], HEX); doesn't seem to work.

Thank you in advance for any help.

void returnDeviceAddress(DeviceAddress deviceAddress)
{ 
  for (uint8_t i = 0; i < 8; i++)
  {
    if (deviceAddress[i] < 0x10) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
  return deviceAddress(xxxx);
}

Might be able to help if we knew what kinds of sensors... DS18B20, one of the BME/BMP series etc. etc.

First, if you want that your function returns anything, it should be declared with return type, not void. The function below returns String

String returnDeviceAddress(DeviceAddress deviceAddress)
{ 
 String dev_addr;
  for (uint8_t i = 0; i < 8; i++)
  {
    if (deviceAddress[i] < 0x10) {
       Serial.print("0");
       dev_addr += String("0");
     }
   
    Serial.print(deviceAddress[i], HEX);
    dev_addr += String(deviceAddress[i], HEX);
  }
  return dev_addr
}

Sorry, they are DS18B20

The DS18B20 has an example code for reading from multiple sensors.

1. The DS18B20 Digital Temperature Sensor contains a 64-bit (8-byte) identification/ROM Code (Fig-1) which can be read and put in a named unsigned long long myAddress (uint64_t myAddress) variable.


Figure-1:

2. The example sketch is:

#include<OneWire.h>
OneWire ds(5);    //Signal in is DPin-10.

union  myData
{
  unsigned long long myAddress;//hold 64-bit ROM Code
  byte myAddressArray[8];//to hold 64-bit ROM Codes; lower byte first
};
myData data;

void setup()
{
  Serial.begin(9600);
  ds.reset();
  ds.search(data.myAddressArray);
  for (int i = 0; i < 8; i++)
  {
    byte y = data.myAddressArray[i];
    if (y < 0x10)
    {
      Serial.print('0'); //show leading zero
    }
   }
    Serial.print(y, HEX);//Lower byte first);
}

void loop(){}

This was exactly what I needed.. Thank you so much

Thanks to all the other answers they helped as well.

Perfect. Thank you.

Why have you preferred the above instead of the following when the leading 0 is a single character?

Serial.print('0');

The question is not for me, this is not my code

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.