Bug in API response, Reading multiple Fields with ESP8266 Arduino Library.

2 views (last 30 days)
Hey,
I am currently programming something in ArduinoIDE for an ESP8266 to read my sensor data that is already uploaded to TS.
For this, I use the function:
ThingSpeak.readMultipleFields(CHANNELID,READKEY).
Unfortunately, I found that the server no longer returns the correct data.
Here some debug Log from the Arduino library right after caling ThingSpeak.readMultipleFields(CHANNELID,READKEY):
ts::readRaw (channelNumber: 13xxxxx readAPIKey: H89OOxxYFXxxxxxx suffixURL: "/feeds/last.txt?status=true&location=true")
Connect to default ThingSpeak: api.thingspeak.com:80...Success.
GET "/channels/13xxxxx/feeds/last.txt?status=true&location=true"
Got Status of 200
Content Length: 210
Found end of header
Response: "{"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}"
Read: "{"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}"
disconnected.
As you can see, field 4 returns "null".
When reading the same Channel and field with the function:
ThingSpeak.readFloatField(CHANNELID,FIELD ID,READKEY)
I get:
ts::readStringField(channelNumber: 13xxxxx readAPIKey: H89OOxxYFXxxxxxx field: 4)
ts::readRaw (channelNumber: 13xxxxx readAPIKey: H89OOxxYFXxxxxxx suffixURL: "/fields/4/last")
Connect to default ThingSpeak: api.thingspeak.com:80...Success.
GET "/channels/13xxxxx/fields/4/last"
Got Status of 200
Content Length: 8
Found end of header
Response: "29.94141"
Read: "29.94141"
disconnected.
And this is the right value.
Im using the latest Version from TS arduino Library. I already opend an issue on Github but there is no reaction on it.
Does anyone have any ideas?
Greetings!
  1 Comment
Beata-Anita Gerger
Beata-Anita Gerger on 19 Apr 2024

Hallo, dasselbe Problem habe ich auch! "Thingspeak libary" = Katastrophe.

Funktioniert aber "httpGET-Abfrage hervorragend.

Ich mache auch "last" alle 15 Minuten und falls es ein neuer Wert geschrieben wurde, bekomme ich immer den neusten Wert in Arduino angezeigt

Serial.println("Reading from Thingspeak"); HTTPClient http;

String url= "http://API.thingspeak.com/channels/Chanelnummer/fields/4/last.json?api_key=apikey=reults=2" http.begin(url); int httpResponseCode = http.GET();

if (httpResponseCode == 200) { String payload = http.getString(); Serial.println(payload);

int field4ValueIndex = payload.indexOf("\"field4\":\"")+10;
String field4Value = payload.substring(field4ValueIndex, payload.indexOf("\"", field4ValueIndex));
} else {
Serial.println("Error Reading from Thingspeak")

unsigned Long timeout = millis; if (millis() - timeout > 60000) { return; }

Es funktioniert auch auf Display, "Display.print" oder Serial, "Serial.print" Ich verwende libary: #include "HTTPClient.h" #include ArdunioJson.h //Vers.5.13

Viel Glück!

Sign in to comment.

Accepted Answer

Christopher Stapels
Christopher Stapels on 12 Apr 2021
I think you are comparing two different operations as well. The first is a read of the latest feed entry, or all of the field values. The second is a call to the last entry for the field, which may not return the last feed, it might return a field entry from an older feed. The field call is looking for the last non-null entry. As Vinod says, comparing to the full export will likely show the issue.
  2 Comments
Chris Nas
Chris Nas on 12 Apr 2021
Edited: Chris Nas on 12 Apr 2021
Thanks for your reply!
Do I understand it correctly. That the function: readMultipleFields() only displays the data that was added during the last write?
For example: I write two values at 12:01:01 a value in field 1 a value in field 2.
There are also values in fields 3-6, but these are older and are not rewritten during the write process.
If I now try to read out all fields via ReadMultipleFields(), I only get the first two fields, because these are the only ones with the timestamp 12:01:01 during the last write process.
Is there a way to apply the behaviour that ReadFieldAsFloat() has to readMultipleFields()? So simply the last values that were not "null" are returned?
Christopher Stapels
Christopher Stapels on 14 Apr 2021
You will need to use multiple calls to read a filed get that behavior. Read multiple fields uses a single feed entry (common timestamp) read and extracts the data from multiple fields.
You might be able to use the readraw command to read multiple feed entries and then parse the results yourself.

Sign in to comment.

More Answers (1)

Vinod
Vinod on 12 Apr 2021
I think looking at the response at one point in time is probably leading you to incorrect conclusions because the channel was updated during the period you switched from retrieving the feed vs/ retrieving just the field. I recommend you download the data from the "Import/Export Data" tab of the channel and confirm the library is giving you the correct reading.

Communities

More Answers in the  ThingSpeak Community

Categories

Find more on Write Data to Channel in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!