Adafruit Industries, Essential service and business: NYC – Executive Order 202.6 - Read more.
0

Last Value 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.
Please be positive and constructive with your questions and comments.

Last Value Feed

by guillaumearnx on Sat Mar 21, 2020 12:02 pm

Hello, how can I get the last value from a feed when ESP8266 starts because
Code: Select all | TOGGLE FULL SIZE
if (!strcmp((char*)onoff.lastread, "**"))
only updates when I send a new value to the feed

guillaumearnx
 
Posts: 4
Joined: Sat Nov 23, 2019 8:58 am

Re: Last Value Feed

by brubell on Tue Mar 24, 2020 10:05 am

Hi, to get a value from the feed, you'll want to use the */get topic.

If you’re using the Adafruit IO Arduino library, you can add /get support to your project in one line of code:
Code: Select all | TOGGLE FULL SIZE
 feedName->get();


Code for the Adafruit IO Arduino library can be found here: https://github.com/adafruit/Adafruit_IO ... d_read.ino

If you're using another library, we have a page in the Adafruit IO MQTT API documentation about using the /get topic: https://io.adafruit.com/api/docs/mqtt.h ... -get-topic

Let me know if you need help or get stuck anywhere.

brubell
 
Posts: 792
Joined: Fri Jul 17, 2015 10:33 pm

Re: Last Value Feed

by guillaumearnx on Tue Mar 24, 2020 11:42 am

Hi, I've tried to install the io library but when i want to upload on ESP8266, i've an error
Thanks
Image

guillaumearnx
 
Posts: 4
Joined: Sat Nov 23, 2019 8:58 am

Re: Last Value Feed

by brubell on Wed Mar 25, 2020 12:46 pm

Could you post your entire code in
Code: Select all | TOGGLE FULL SIZE
[code] tags
and the full error output below?

brubell
 
Posts: 792
Joined: Fri Jul 17, 2015 10:33 pm

Re: Last Value Feed

by guillaumearnx on Wed Mar 25, 2020 1:00 pm

Code: Select all | TOGGLE FULL SIZE
#include <AdafruitIO.h>
#include <AdafruitIO_Dashboard.h>
#include <AdafruitIO_Data.h>
#include <AdafruitIO_Definitions.h>
#include <AdafruitIO_Ethernet.h>
#include <AdafruitIO_Feed.h>
#include <AdafruitIO_FONA.h>
#include <AdafruitIO_Group.h>
#include <AdafruitIO_MQTT.h>
#include <AdafruitIO_Time.h>
#include <AdafruitIO_WiFi.h>
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

#define WIFI_SSID "**"
#define WIFI_PASS "**"
#define MQTT_SERV "io.adafruit.com"
#define MQTT_PORT 1883
#define MQTT_NAME "**"
#define MQTT_PASS "**"

int led = D0;
const char* deviceName = "ESP-TEST";
io.connect();
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, MQTT_SERV, MQTT_PORT, MQTT_NAME, MQTT_PASS);
Adafruit_MQTT_Subscribe onoff = Adafruit_MQTT_Subscribe(&mqtt, MQTT_NAME "/f/test");

void setup()
{
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(led, OUTPUT);
  WiFi.hostname(deviceName);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  WiFi.mode(WIFI_STA);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");}
  digitalWrite(LED_BUILTIN, HIGH);
  Serial.println("OK!");
  mqtt.subscribe(&onoff);
      }

void loop()
{
  MQTT_connect();
  Adafruit_MQTT_Subscribe * subscription;
  while ((subscription = mqtt.readSubscription(5000)))
  {
    onoff->get();
    if (subscription == &onoff){
      Serial.print("onoff: ");
      Serial.println((char*) onoff.lastread);
      if (!strcmp((char*) onoff.lastread, "1")){digitalWrite(led, HIGH);}
      else if (!strcmp((char*) onoff.lastread, "0")){digitalWrite(led, LOW);}
    }
  }
}


void MQTT_connect()
{
  if (mqtt.connected() && mqtt.ping())
  {
    return;
  }
  int8_t ret;
  mqtt.disconnect();
  Serial.print("Connecting to MQTT... ");
  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0)
  {
    Serial.println(mqtt.connectErrorString(ret));
    Serial.println("Retrying MQTT connection in 5 seconds...");
    mqtt.disconnect();
    delay(5000);
    retries--;
    if (retries == 0)
    {
      ESP.reset();
    }
  }
  Serial.println("MQTT Connected!");
}




In file included from C:\Users\arnou\Desktop\test\test.ino:1:0:
Multiple libraries were found for "AdafruitIO.h"
D:\Arduino\libraries\Adafruit_IO_Arduino\src/AdafruitIO.h:27:31: fatal error: ArduinoHttpClient.h: No such file or directory
Used: D:\Arduino\libraries\Adafruit_IO_Arduino
#include "ArduinoHttpClient.h"
Not used: D:\Arduino\libraries\Adafruit_IO_Arduino-master
^
compilation terminated.
exit status 1
Error compiling for board NodeMCU 1.0 (ESP-12E Module)

guillaumearnx
 
Posts: 4
Joined: Sat Nov 23, 2019 8:58 am

Please be positive and constructive with your questions and comments.