Due to high demand expect some shipping delays at this time, orders may not ship for 3-4 business days.
0

Problems Sending String Payloads with Adafruit IO Arduino Li
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.

Problems Sending String Payloads with Adafruit IO Arduino Li

by okyang on Tue Dec 07, 2021 2:54 am

Hello,

I've been scratching my head for a while on trying to send Strings to Adafruit IO using Arduino (with the ultimate goal trying to send a base64 image that usually has over 1000 characters). I'm not an expert, but I tried a couple simple tests and ran into some interesting problems. Was wondering if it is a bug with the library or my code or something else.

Here were my findings:
1. `String` and `char *` payloads that were 100 characters in length did not show up in Adafruit IO feed, but the `save` method still returned a `1` for `true`
2. `const char *` payloads showed up as a boolean in the Adafruit IO feed

More context:
1. I am using PlatformIO
2. Tested on ESP32-CAM

Thank you :)

Code: Select all | TOGGLE FULL SIZE
#include <Arduino.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "config.h"

// Adafruit IO
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
AdafruitIO_Feed *imageFeed = io.feed(IMAGE_FEEDNAME);

bool chrPayloadTest(int payloadLen, AdafruitIO_Feed*  feed) {
  char t[payloadLen +1];
 
  for (int i=0; i<payloadLen; i++){
    t[i] = 'a';
  }
  t[payloadLen-1] = 0;

  char * test = t;
  Serial.println(test);
  return imageFeed->save(test);
}

bool constChrPayloadTest(int payloadLen, AdafruitIO_Feed*  feed) {
  char t[payloadLen +1];
 
  for (int i=0; i<payloadLen; i++){
    t[i] = 'a';
  }
  t[payloadLen-1] = 0;

  const char * test = t;
  Serial.println(test);
  return imageFeed->save(test);
}

bool strPayloadTest(int payloadLen, AdafruitIO_Feed*  feed) {
  String test;
 
  for (int i=0; i<payloadLen; i++){
    test+= 'a';
  }
  Serial.println(test);
  return imageFeed->save(test);
}

void setup() {
  Serial.begin(115200);

  Serial.print("Connecting to Adafruit IO");
  io.connect();

  while(io.status() < AIO_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

  Serial.println();
  Serial.println(io.statusText());

  io.run();

  // 1.1 - char of len 10
  Serial.println("1.1");
  Serial.println(chrPayloadTest(10, imageFeed));
  delay(5000);

  // 1.2 - char of len 100
  Serial.println("1.2");
  Serial.println(chrPayloadTest(100, imageFeed));
  delay(5000);

  // 2.1 - const char of len 10
  Serial.println("2.1");
  Serial.println(constChrPayloadTest(10, imageFeed));
  delay(5000);

  // 2.2 - const char of len 100
  Serial.println("2.2");
  Serial.println(constChrPayloadTest(100, imageFeed));
  delay(5000);

  // 3.1 - String of len 10
  Serial.println("3.1");
  Serial.println(strPayloadTest(100, imageFeed));
  delay(5000);

  // 3.2 - String of len 100
  Serial.println("3.2");
  Serial.println(strPayloadTest(10, imageFeed));
  delay(5000);

}

void loop() {

}
Attachments
Screenshot 2021-12-06 224804.png
Screenshot 2021-12-06 224804.png (27.97 KiB) Viewed 212 times

okyang
 
Posts: 1
Joined: Sat Apr 24, 2021 2:57 am

Please be positive and constructive with your questions and comments.