adafruit / Adafruit_IO_Arduino

Arduino library to access Adafruit IO from WiFi, cellular, and ethernet modules.
Other
210 stars 107 forks source link

Incorrect handling of data->feedName() when feed has group #119

Closed blaskovicz closed 4 years ago

blaskovicz commented 4 years ago

Thank you for providing this excellent data warehouse and associated apis!

I found that when using a feed scoped to a group that's longer than the 20 character limit, the feedName() function call overflows incorrectly into the data.

1) Create a feed group, eg: coop-command. 2) Create a feed that will cause the group name + . + feed to be > 20; eg: leds_on_off 3) Create a dashboard with an on/off switch whose texts are "ON" and "OFF" 4) Create and run a sketch with the following code:

#include <AdafruitIO_WiFi.h>

bool ledsOn = false;
AdafruitIO_WiFi io(ENV_AIO_USERNAME, ENV_AIO_KEY, ENV_WIFI_SSID, ENV_WIFI_PASS);
AdafruitIO_Feed *ledOnOffFeed = io.feed("coop-command.leds_on_off");
void handleAdafruitMessage(AdafruitIO_Data *data)
{
  String dataFeed = data->feedName();
  String dataString = data->toString();
  Serial.println("[adafruit] " + String(data->feedName()) + " <- " + dataString);

 if (dataFeed == "coop-command.leds_on_off" && (dataString == "ON" || dataString == "OFF"))
  {
    if (dataString == "ON")
    {
      ledsOn = true;
    }
    else
    {
      ledsOn = false;
    }

    return;
  }

  Serial.println("[adafruit] malformed message, ignored.");
}
void connectAdafruitIO()
{
  // connect to io.adafruit.com
  Serial.print("[adafruit] connecting");
  io.connect();

  // set up a message handlers for the feeds
  ledOnOffFeed->onMessage(handleAdafruitMessage);

  // wait for a connection
  while (io.status() < AIO_CONNECTED)
  {
    Serial.print(".");
    delay(500);
  }

  // we are connected
  Serial.println();
  Serial.println(io.statusText());

  // get initial states
  ledOnOffFeed->get();
}

void serialInit()
{
  // set up serial monitor and wait for it to open
  Serial.begin(9600);

  do
  {
    delay(100);
  } while (!Serial);

  Serial.println();
}

void setup()
{
  serialInit();
  connectAdafruitIO();
}

void loop() {
  io.run();
}

5) toggle the on / off switch and observe the overflow in the serial monitor:

[adafruit] coop-command.leds_onON,,,
 <- ON
[adafruit] malformed message, ignored.
blaskovicz commented 4 years ago

I just looked on the adafruit.io site and it seems like this should be 128.128 \0, max, so 258. Does that sound right?

brentru commented 4 years ago

@blaskovicz This looks right, thanks for filing the issue. Would you want to create a pull request for this issue?

blaskovicz commented 4 years ago

Does the max csv length need to get updated as well https://github.com/adafruit/Adafruit_IO_Arduino/blob/ce83ba999056639728de655fbad49bfb4c935cfd/src/AdafruitIO_Definitions.h#L130?

brentru commented 4 years ago

Not sure, I'd need to dig further into this and may by the end-of-week, may be worth taking a sep. issue out for the CSV length.