I'm working on making my code more dynamic and I'm storing all my ds18b20 sensor data in a two dimensional array. Everything is working perfectly except one part.
I'm defining my data like this...
typedef struct {
DeviceAddress addr;
char name[8];
float temp;
float setpoint;
} Sensor;
Sensor sensor_list[] = {
{ { 0x28, 0xFF, 0x46, 0x82, 0x58, 0x16, 0x04, 0x9A }, "zone0", 45.00, 44.44 },
{ { 0x28, 0xFF, 0x8F, 0x59, 0x58, 0x16, 0x04, 0xDE }, "zone1", 45.00, 44.44 }
};
I'm able to update that temperature (in the 3rd column in the array) with no problem. I can read my sensors without issue. The one thing I'm having an issue with is parsing my MQTT topics that come in to grab that setpoint. If I do...
void callback(char* topic, byte* payload, unsigned int length) {
for (int i=0;i<length;i++) {
payload[length] = '\0';
cstring = (char *) payload;
}
//Serial.println(topic); //This is the topic
// Serial.println(cstring); //this is the payload
request = atof(cstring);
if (strstr(topic, "setpoint") != NULL) {
for (unsigned i = 0; i < sizeof(sensor_list) / sizeof(Sensor); i++) {
setpoint = atof(cstring);
if (strstr(topic, "1") != NULL) {
sensor_list[i].setpoint = setpoint;
}
}
}
}
It updates EVERY row of the array with the setpoint it receives from MQTT. Pretty much every version of this code that I've tried either updates every row or zero rows.
Can someone help me understand my logic error?