Arduino - Getting multiple last values from feed ?

Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Locked
User avatar
Tornix
 
Posts: 2
Joined: Sat Nov 26, 2022 10:57 am

Arduino - Getting multiple last values from feed ?

Post by Tornix »

Hi there!

I've been looking for this feature through the doc, forums and examples but could'nt find an answer.

On Arduino, i'm using the following code in order to recover last feed's value:

Code: Select all

void setup() {
  Serial.begin(115200);
  io.connect();
  feed->onMessage(handleMessageFeed);
  
  
  while(io.status() < AIO_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
;
  
  feed->get();

 }
 
 void loop() {
  io.run();
  feed->get();
  Serial.print("Value = ");Serial.println(Value);
  delay(5000);
}

void handleMessageFeed(AdafruitIO_Data *data) {
  Value= data->toInt();
}
This works quite well for the last value.
==> I am currently trying to get back a fixed number of lasts values. For instance, reading last 5 values from a feed and deliver those into an array or multiple variables.

Is there a way to do so? Hopefully with the same library?

Thanks a lot!
Tornix

User avatar
jwcooper
 
Posts: 1004
Joined: Tue May 01, 2012 9:08 pm

Re: Arduino - Getting multiple last values from feed ?

Post by jwcooper »

I think in this case, you'd need to use the API directly (I'm not 100% on this though).

You can see the API docs for this particular option here:
https://io.adafruit.com/api/docs/#get-feed-data

You'd want to use the 'limit' option set to 5 to get the last 5 data.

User avatar
Tornix
 
Posts: 2
Joined: Sat Nov 26, 2022 10:57 am

Re: Arduino - Getting multiple last values from feed ?

Post by Tornix »

Hi!

Thanks for your valuable input!

I found an example with the library ESP8266HTTPClient.h.
Example is BasicHTTPSClient.ino as following :

Code: Select all

/**
   BasicHTTPSClient.ino

    Created on: 20.08.2018
*/

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>

#include <WiFiClientSecureBearSSL.h>
// Fingerprint for demo URL, expires on June 2, 2021, needs to be updated well before this date


ESP8266WiFiMulti WiFiMulti;
String payload;

void setup() {

  Serial.begin(115200);
  // Serial.setDebugOutput(true);

  Serial.println();
  Serial.println();
  Serial.println();

  for (uint8_t t = 4; t > 0; t--) {
    Serial.printf("[SETUP] WAIT %d...\n", t);
    Serial.flush();
    delay(1000);
  }

  WiFi.mode(WIFI_STA);
  WiFiMulti.addAP("SSID", "WiFi_password");
}

void loop() {
  // wait for WiFi connection
  if ((WiFiMulti.run() == WL_CONNECTED)) {

    std::unique_ptr<BearSSL::WiFiClientSecure> client(new BearSSL::WiFiClientSecure);

    //client->setFingerprint(fingerprint);
    // Or, if you happy to ignore the SSL certificate, then use the following line instead:
     client->setInsecure();

    HTTPClient https;

    Serial.print("[HTTPS] begin...\n");
    if (https.begin(*client, "https://io.adafruit.com/api/v2/USERNAME/feeds/group_name.feed_name/data/")) {  // HTTPS

      Serial.print("[HTTPS] GET...\n");
      // start connection and send HTTP header
      int httpCode = https.GET();

      // httpCode will be negative on error
      if (httpCode > 0) {
        // HTTP header has been send and Server response header has been handled
        Serial.printf("[HTTPS] GET... code: %d\n", httpCode);

        // file found at server
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
          payload = https.getString();
          Serial.println(payload);
        }
      } else {
        Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
      }

      https.end();
    } else {
      Serial.printf("[HTTPS] Unable to connect\n");
    }
  }

  Serial.println("Wait 10s before next round...");
  delay(10000);
}
 
It works well and deliver me a huuuuuge JSON-style (I guess?) string with last 100 points including the whole data (coords, ids, times, expiration dates, etc...).

I guess next step is about decoding the JSON answer..! I will be looking that way.

Again, thank you very much!

User avatar
jwcooper
 
Posts: 1004
Joined: Tue May 01, 2012 9:08 pm

Re: Arduino - Getting multiple last values from feed ?

Post by jwcooper »

You can tack on a '.csv' at the end of /data and you can get the results in csv format, probably a lot easier to parse on a lower powered device. ;)

Example:

Code: Select all

curl -H "X-AIO-Key: {aio_key}" https://io.adafruit.com/api/v2/{username}/feeds/{feed-key}/data.csv?limit=5

Locked
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.

Return to “Internet of Things: Adafruit IO and Wippersnapper”