I'm auto-detecting ds18b20 1-wire sensors which are plugged into my arduino.
Everything is working as I had hoped with 2 exceptions:
- I cannot get the sensor address from the pointer into the struct.
- During this process, I need to also convert the sensor address from the typical 0x00 0x00 type hex address into its 28-xxxxx type address so I can do some other stuff with it. I can't figure out how to write a function to do that.
Here's what I'm working with...(with comments in the code).
I'm a noob with C, so detailed answers are really helpful.
typedef struct {
DeviceAddress addr;
char name[8];
float temp;
float setpoint;
int state;
int act;
int id;
} Sensor;
struct Node {
Sensor *sensor;
Node *next;
};
Node *list = nullptr;
char *cstring;
void discoverOneWireDevices(void) {
uint8_t i;
char buffer[4];
DeviceAddress addr;
Node *nodePtr;
Sensor *sensorPtr;
//Looking for 1 wire devices
ds.reset_search();
while (ds.search(addr)) {
//Print what we found for reference.
Serial.println("\n\rFound \'1-Wire\' device with address:");
for (i = 0; i < 8; i++) {
Serial.print("0x");
if (addr[i] < 16) {
Serial.print('0');
}
Serial.print(addr[i], HEX);
if (i < 7) {
Serial.print(", ");
}
}
if (OneWire::crc8(addr, 7) != addr[7]) {
Serial.println("CRC is not valid, skipping!");
continue;
}
//build my pointer
sensorPtr = new Sensor;
memcpy(sensorPtr->addr, addr, sizeof(DeviceAddress));
//I have this to prove that I can override the preset temperature (45) below and I know that I'm putting data in the Struct
sensorPtr->temp = 22.0;
sensorPtr->id = finder;
nodePtr = new Node;
nodePtr->sensor = sensorPtr;
nodePtr->next = list;
list = nodePtr;
finder++;
}
//My default Struct that will store my 5 sensors
Sensor sensor_list[] = {
{ { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, "Zone0", 45.00, 44.44, 0, 0, 0 },
{ { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, "Zone1", 45.00, 44.44, 0, 0, 1 },
{ { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, "Zone2", 45.00, 44.44, 0, 0, 2 },
{ { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, "Zone3", 45.00, 44.44, 0, 0, 3 },
{ { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, "Zone4", 45.00, 44.44, 0, 0, 4 }
};
Serial.println("Finished finding");
//Put the sensors into the struct
for (Node *node = list; node != nullptr; node = node->next) {
int i = node->sensor->id;
float newTemp = sensorPtr->temp;
//my feeble attempts to get the the addresses into my struct
//DeviceAddress addr = {node->sensor->addr};
// sensor_list[i].addr = {node->sensor->addr};
sensor_list[i].act = 1;
sensor_list[i].temp = newTemp;
// Serial.print("Address:");
// Serial.println(sensor_list[i].addr);
//This shows me that the act and temp changes even though the address doesn't work
Serial.print("Active:");
Serial.println(sensor_list[i].act);
Serial.print("Starting Temp:");
Serial.println(sensor_list[i].temp);
}
}