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 ![]()
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.