3 devices, 1 bucket, variable sensor numbers


#1

I have three ESP8266, they have 1-4 dallas/1-wire sensors on them. I am trying to write code that works on all three and maybe more generically. I can use the websetup to define wifi and device id, so that is good.

But what I want is to graph all these temps on one line graph on the thinger.io dashboard.

So I figured out I need all the data to be in 1 bucket.

It looks like I can have each of the devices write to the same bucket, so that is good.

But what I need is somehow to change the variable name, so I can keep that data separate and not have to code in different names.

this is what I have so far, but it really is not elegant is wont work above 4.

thing["Temperatures"] >> [](pson & out) {
    int numberOfSensors = sensors.getDeviceCount() - 1;
    sensors.requestTemperatures();
    for (int i = 0; i <= numberOfSensors; i++) {
      if (i == 0)
      {
        out["Temp1"] = sensors.getTempFByIndex(i);
      }
      if (i == 1)
      {
        out["Temp2"] = sensors.getTempFByIndex(i);
      }
      if (i == 2)
      {
        out["Temp3"] = sensors.getTempFByIndex(i);
      }
      if (i == 3)
      {
        out["Temp4"] = sensors.getTempFByIndex(i);
      }

    }
    out["sensorCount"] = numberOfSensors + 1;
  };

Ideally, I get the count of the sensors and then loop over them, but I cannot do this:

thing["Temperatures"] >> [](pson & out) {
    int numberOfSensors = sensors.getDeviceCount() - 1;
    sensors.requestTemperatures();
    for (int i = 0; i <= numberOfSensors; i++) {
       out[WiFi.getMAC()+"Temp"+1] = sensors.getTempFByIndex(i);
    }
    out["sensorCount"] = numberOfSensors + 1;
  };

Maybe you can see what I want to do here? I want some device specific variable to be appended to the key name so I can tell the diff between each of the temps in the bucket. But I get errors about const char and the like.

Any ideas on how I can do this? Otherwise, I will have to put a big set of ifs and update the code each time I add in a new device.


#2

Hi,

You cant do this, as the out[""] is expecting a const char, just like a literal strin. At most, you can initialize an array in the setup that contains the strings you want, and then refer then with an index in your loop, i.e.

// dummy var for testing
const int numberOfSensors = 5;
String names[numberOfSensors];

void setup() {
  Serial.begin(115200);

  // initialize names
  for(int i=0; i<numberOfSensors;i++){
      names[i] += "Temp-";
      names[i] += i;
  }

  // initialize resource
  thing["Temperatures"] >> [](pson & out) {
    for (int i = 0; i<numberOfSensors; i++) {
       out[names[i].c_str()] = 0;
    }
    out["sensorCount"] = numberOfSensors;
  };
}

void loop() {
  thing.handle();
}

Hope it helps! :slight_smile: