Store Dallas Temp ds1820b address in human readable format instead of print it

This is my first arduino post as I'm relatively new to the whole scene.
I have spent quite a few hours searching C/C++ articles trying to figure out how to do this one, and I confess I am not the best of C programmers.

Basically, I have found examples of "Printing" ds1820b hex addresses to the console, I just can't seem to wrap my head around actually storing that and putting it into a json doc (which I'm using arduinojson for).

I think my problem is just not understanding how the data types inside the function that I wrote are working :frowning:

Code in progress:

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

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 5

// 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);

// variable to hold device addresses
DeviceAddress Thermometer;

// return variable for device address
byte DS18B20_DeviceByteAddresses[20][8];

int deviceCount = 0;

void setup(void)
{
  // start serial port
  Serial.begin(9600);

  // Start up the library
  sensors.begin();

  // locate devices on the bus
  Serial.println("Locating devices...");
  Serial.print("Found ");
  deviceCount = sensors.getDeviceCount();
  Serial.print(deviceCount, DEC);
  Serial.println(" devices.");
  Serial.println("");
  
  Serial.println("Printing addresses...");
  for (int i = 0;  i < deviceCount;  i++)
  {
    
    // Basic output printing...
    Serial.print("Sensor ");
    Serial.print(i+1);
    Serial.print(" : ");
    sensors.getAddress(Thermometer, i);
    printAddress(Thermometer);
  }

// We assume here that we know the number of sensors attached and what they do...
  const size_t capacity = JSON_ARRAY_SIZE(3) + 3*JSON_OBJECT_SIZE(4);
  DynamicJsonDocument doc(capacity);
  int position = 1;

  sensors.getAddress(Thermometer, 0);
  JsonObject doc_0 = doc.createNestedObject();
  doc_0["sensor_name"] = "Wormbin1";
  doc_0["sensor_address"] = Temperature_storeDeviceAddress(position, Thermometer);
//  doc_0["temp_celsius"] = "30";
//  doc_0["temp_farenheit"] = "72";

//  sensors.getAddress(Thermometer, 1);
//  JsonObject doc_1 = doc.createNestedObject();
//  doc_1["sensor_name"] = "Wormbin2";
//  doc_1["sensor_address"] = ;
////  doc_1["temp_celsius"] = "30";
////  doc_1["temp_farenheit"] = "72";
//
//  sensors.getAddress(Thermometer, 2);
//  JsonObject doc_2 = doc.createNestedObject();
//  doc_2["sensor_name"] = "Plant Chamber";
//  doc_2["sensor_address"] = ;
////  doc_2["temp_celsius"] = "30";
////  doc_2["temp_farenheit"] = "72";
  
  serializeJson(doc, Serial);

  
}

void loop(void)
{}

//Attempting to store address instead of print it.
void Temperature_storeDeviceAddress(int position, DeviceAddress deviceAddress) {
  // function to convert a device address in a printable string
  for (uint8_t i = 0; i < 8; i++) {
    DS18B20_DeviceByteAddresses[position][i] = deviceAddress[i];
  }
}

// simple function to print a device address, 
void printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
  Serial.println("");
}

// Print Address
//void printAddress(DeviceAddress deviceAddress)
//{ 
//  for (uint8_t i = 0; i < 8; i++)
//  {
//    Serial.print("0x");
//    if (deviceAddress[i] < 0x10) Serial.print("0");
//    Serial.print(deviceAddress[i], HEX);
//    if (i < 7) Serial.print(", ");
//  }
//  Serial.println("");
//}

Note that the function printAddress works fine.
All I'm trying to do is take that printAddress and instead return the address and store it in that Json Document object.

The compiler error usually spits back the following:

Arduino: 1.8.13 (Windows 10), Board: "Arduino Nano, ATmega328P"

C:\Users\jthom\OneDrive\Documents\Arduino\get_ds1820b_addrs\get_ds1820b_addrs\get_ds1820b_addrs.ino: In function 'void setup()':

get_ds1820b_addrs:58:81: error: no match for 'operator=' (operand types are 'ArduinoJson6161_11::enable_if<true, ArduinoJson6161_11::MemberProxy<ArduinoJson6161_11::ObjectRef, const char*> >::type {aka ArduinoJson6161_11::MemberProxy<ArduinoJson6161_11::ObjectRef, const char*>}' and 'void')

   doc_0["sensor_address"] = Temperature_storeDeviceAddress(position, Thermometer);
 ^
In file included from C:\Users\jthom\OneDrive\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/Document/JsonDocument.hpp:9:0,

                 from C:\Users\jthom\OneDrive\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/Document/BasicJsonDocument.hpp:7,

                 from C:\Users\jthom\OneDrive\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/Document/DynamicJsonDocument.hpp:7,

                 from C:\Users\jthom\OneDrive\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson.hpp:21,

                 from C:\Users\jthom\OneDrive\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson.h:9,

                 from C:\Users\jthom\OneDrive\Documents\Arduino\get_ds1820b_addrs\get_ds1820b_addrs\get_ds1820b_addrs.ino:3:

C:\Users\jthom\OneDrive\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/Object/MemberProxy.hpp:38:27: note: candidate: ArduinoJson6161_11::MemberProxy<TParent, TStringRef>::this_type& ArduinoJson6161_11::MemberProxy<TParent, TStringRef>::operator=(const this_type&) [with TObject = ArduinoJson6161_11::ObjectRef; TStringRef = const char*; ArduinoJson6161_11::MemberProxy<TParent, TStringRef>::this_type = ArduinoJson6161_11::MemberProxy<ArduinoJson6161_11::ObjectRef, const char*>]

   FORCE_INLINE this_type &operator=(const this_type &src) {

                           ^~~~~~~~

C:\Users\jthom\OneDrive\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/Object/MemberProxy.hpp:38:27: note:   no known conversion for argument 1 from 'void' to 'const this_type& {aka const ArduinoJson6161_11::MemberProxy<ArduinoJson6161_11::ObjectRef, const char*>&}'

C:\Users\jthom\OneDrive\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/Object/MemberProxy.hpp:45:3: note: candidate: template<class TValue> typename ArduinoJson6161_11::enable_if<(! ArduinoJson6161_11::is_array<TValue>::value), ArduinoJson6161_11::MemberProxy<TParent, TStringRef>&>::type ArduinoJson6161_11::MemberProxy<TParent, TStringRef>::operator=(const TValue&) [with TValue = TValue; TObject = ArduinoJson6161_11::ObjectRef; TStringRef = const char*]

   operator=(const TValue &src) {

   ^~~~~~~~

C:\Users\jthom\OneDrive\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/Object/MemberProxy.hpp:45:3: note:   template argument deduction/substitution failed:

C:\Users\jthom\OneDrive\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/Object/MemberProxy.hpp: In substitution of 'template<class TValue> typename ArduinoJson6161_11::enable_if<(! ArduinoJson6161_11::is_array<TValue>::value), ArduinoJson6161_11::MemberProxy<ArduinoJson6161_11::ObjectRef, const char*>&>::type ArduinoJson6161_11::MemberProxy<ArduinoJson6161_11::ObjectRef, const char*>::operator=<TValue>(const TValue&) [with TValue = void]':

C:\Users\jthom\OneDrive\Documents\Arduino\get_ds1820b_addrs\get_ds1820b_addrs\get_ds1820b_addrs.ino:58:81:   required from here

C:\Users\jthom\OneDrive\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/Object/MemberProxy.hpp:45:3: error: forming reference to void

C:\Users\jthom\OneDrive\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/Object/MemberProxy.hpp:54:27: note: candidate: template<class TChar> ArduinoJson6161_11::MemberProxy<TParent, TStringRef>::this_type& ArduinoJson6161_11::MemberProxy<TParent, TStringRef>::operator=(TChar*) [with TChar = TChar; TObject = ArduinoJson6161_11::ObjectRef; TStringRef = const char*]

   FORCE_INLINE this_type &operator=(TChar *src) {

                           ^~~~~~~~

C:\Users\jthom\OneDrive\Documents\Arduino\libraries\ArduinoJson\src/ArduinoJson/Object/MemberProxy.hpp:54:27: note:   template argument deduction/substitution failed:

C:\Users\jthom\OneDrive\Documents\Arduino\get_ds1820b_addrs\get_ds1820b_addrs\get_ds1820b_addrs.ino:58:81: note:   mismatched types 'TChar*' and 'void'

   doc_0["sensor_address"] = Temperature_storeDeviceAddress(position, Thermometer);

                                                                                 ^

exit status 1

no match for 'operator=' (operand types are 'ArduinoJson6161_11::enable_if<true, ArduinoJson6161_11::MemberProxy<ArduinoJson6161_11::ObjectRef, const char*> >::type {aka ArduinoJson6161_11::MemberProxy<ArduinoJson6161_11::ObjectRef, const char*>}' and 'void')

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Lets see ifin you'll figure it out or have to be told.

Biggest clue.

no match for 'operator=' (operand types are 'ArduinoJson6161_11::enable_if<true, ArduinoJson6161_11::MemberProxy<ArduinoJson6161_11::ObjectRef, const char*> >::type {aka ArduinoJson6161_11::MemberProxy<ArduinoJson6161_11::ObjectRef, const char*>}' and 'void'

A clue

DeviceAddress Thermometer;

, is not a

const char*

.

Thank you for using code tags and posting the error message. +1

Suppose the DS18B20 identifier/address = 0x30, 0x1A, 0x3C, 0x12, 0x01, 0x28, 0x02, 0x9D
then you might use it as a json string if you want. For example the readable text: "301A3C120128029D"
You have to convert it to the text. Use either sprintf() or in a for-loop.
Please note the leading zero's when the number is 0x01 or 0x02.

sprintf format: http://www.cplusplus.com/reference/cstdio/printf/

I think it could be something like this:

char buffer[20];
sprintf( buffer, "%02X%02X%02X%02X%02X%02X%02X%02X", (int) Thermometer[0], (int) Thermometer[1], (int) Thermometer[2], (int) Thermometer[3], (int) Thermometer[4], (int) Thermometer[5], (int) Thermometer[6], (int) Thermometer[7]);

Warning: the most important part of the code above is eight times the (int) cast. You have to be careful with sprintf(), the format must match the parameters.

However, it depends what's on the other side if that is the best solution.
You might also put the separate bytes as hexadecimal readable text into a json-array.

The Serial.println() can do a lot of things, and there are multiple layers of code behind it. It is not something that you can store, unless you make your own 'print' class.

Ok thanks. I'll have a look tomorrow with a fresh mind.

john_wiki:
Ok thanks. I'll have a look tomorrow with a fresh mind.

It is amazing how often that works!

"Topic: Store Dallas Temp ds1820b address in human readable format instead of print it"

My first thought was, why??. You should store the data in the most efficient form for the processor and only convert it to "human" when you need to show it to a human.

JSON uses readable text, just like HTML code and email code does. Others use Base64 to convert binary data for JSON. The answer to your question "why??" is: "because the internet".