mathworks / thingspeak-arduino

ThingSpeak Communication Library for Arduino, ESP8266 and ESP32
https://thingspeak.com
431 stars 231 forks source link

Problem with ThingSpeak.readMultipleFields() no Values #80

Closed kugelkopf123 closed 3 years ago

kugelkopf123 commented 3 years ago

Just tried to read multiple fields from my private Channel but Im not able to get all Values.

I'm using the latest Version from this Library on an ESP8285 Core2.7.4

struct Sensordaten {
  float temp;
  float hum;
  float pres;
  float bat;
  int rssi;
};

Sensordaten self, ext1, ext2;
int x;
x = ThingSpeak.readMultipleFields(config.TSChannelID, config.TSRAPIKey);
    Serial.println(x);
    delay(1000);
    if (x == 200)
    {
      Serial.println("Channel read successful.");
      ext1.temp =  ThingSpeak.getFieldAsFloat(1);
      ext1.hum = ThingSpeak.getFieldAsFloat(2);
      ext2.temp = ThingSpeak.getFieldAsFloat(3);
      ext2.hum = ThingSpeak.getFieldAsFloat(4);
   }
      Serial.println("//////////////////////////////");
      Serial.println(ext1.temp);
      Serial.println(ext1.hum);
      Serial.println("//////////////////////////////");
      Serial.println(ext2.temp);
      Serial.println(ext2.hum);
      Serial.println("//////////////////////////////");
else {
Serial.print("Error: ");Serial.println(x);
}

Results:

Channel read successful.
//////////////////////////////
0.00
0.00
0.00
//////////////////////////////
0.00
0.00
0.00
//////////////////////////////

Printed the Content of "multicontent" from the Thingspeak.cpp {"created_at":"2021-04-06T20:25:06Z","entry_id":5,"field1":null,"field2":null,"field3":null,"field4":null,"field5":"27.82000","field6":"28.96680","latitude":null,"longitude":null,"elevation":null,"status":null}

In all fields from 1-6 are values:

Bildschirmfoto 2021-04-06 um 23 12 54

Have no clue.

When fetching the values via: ThingSpeak.readFloatField(NUM) there is no Problem but its slow...

Thanks!

v-c commented 3 years ago

It appears that not all fields on your channel are being updated concurrently. Instead it appears some fields are being updated, while others are not. This leads to a situation where the FEED (which is composed of many FIELDS) has one FIELD value set, but other FIELDS are null. So, when you use ThingSpeak.readMultipleFields() (which behind the scene reads the FEED), some FIELDS show up as value 0.0 (i.e., NULL).

The modification required is for your code that sends data to ThingSpeak to use ThingSpeak.setField() to set all fields and make a single call to ThingSpeak.writeFields() to update the entire feed in one operation.

kugelkopf123 commented 3 years ago

Thanks for your answer. I see. Is there a way to get all the data at the same time, like with readMultipleFields, but instead of taking the last feed, just take the data that was added last and that is not "null"? So the behaviour as known from ThingSpeak.readFieldAsFloat()?

v-c commented 3 years ago

Perhaps we can start by understanding your use case -- is the channel only being updated by a single device? If so, why are all fields not being updated concurrently?

kugelkopf123 commented 3 years ago

Thanks for your help!

No there are 3 devices that record humidity and temperature. Device 1 writes in Field 1/2 Device 2 in 3/4 Device 3 in 5/6.

From each device it is possible to read the data from the other sensors. At least that is the plan. So device 1 can display its own data and that of devices 2 and 3. Etc.

To safe Energy (battery), it's much faster to read all at once in place of read every single field. So it's a bit time critical.

v-c commented 3 years ago

Having multiple devices write to the same channel is fraught with danger of contention among them to update the channel as a single channel can only be updated once every second (or once every 15s with a free license). If your clocks across the devices are not accurately sync'ed it might look like only one or two of them is working reliably.

My recommendation is to use one device to update one channel. You can have as many devices read the same channel as you wish.