0

JSON not posting to adafruit.io feed
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

JSON not posting to adafruit.io feed

by jbdamask on Mon Apr 08, 2019 11:47 am

Hi,
This seems trivial but I'm struggling. I'm using a Feather HUZZAH ESP32, the latest version of ArduinoJson and the latest version of Adafruit-IO-Arduino. My code shows three ways of posting to my feed:

1 String created from ArduinoJSON object; sent as character array
2 Pre-formatted string
3 Pre-formatted string using single quotes

When run, the save() function returns success but only the string that used single quotes actually posts, as can be seen by the message handler (and verified by watching my feed and the adafruit io monitor).

Any idea what I'm doing wrong?

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

int count = 0;

#define IO_LOOP_DELAY 5000
unsigned long lastUpdate = 0;

// set up the 'stuff' feed
AdafruitIO_Feed *stuff = io.feed("stuff");


void setup() {
  Serial.begin(115200);
  while(! Serial);
  Serial.print("Connecting to Adafruit IO");
  io.connect();
  stuff->onMessage(handleMessage);
  while(io.status() < AIO_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println();
  Serial.println(io.statusText());
  stuff->get();
}

void loop() {
  io.run();
  if (millis() > (lastUpdate + IO_LOOP_DELAY)) {
    publish();
    count++;
    lastUpdate = millis();
  }
}

void publish(){
  String sJson;
  char *cJson;
  const int capacity = JSON_OBJECT_SIZE(2);
  StaticJsonDocument<capacity> root;
  root["thing_name"] = "feather_esp32";
  root["state"] = 1;
  serializeJson(root, sJson);
  cJson = &sJson[0u]; 
  Serial.print("1) Sending JSON object serialized to char array -> ");
  Serial.println(cJson);
  Serial.println(stuff->save(cJson) ? "Success" : "Fail");

  String sJson2;   
  sJson2 = "{\"thing_name\":\"feather_esp32\":,\"state\":1}";
  Serial.print("2) Sending pre-formatted JSON string -> ");
  Serial.println(sJson2);
  Serial.println(stuff->save(sJson2) ? "Success" : "Fail");

  String sJson3;
  sJson3 = "{\'thing_name\':\'feather_esp32\':,\'state\':1}";
  Serial.print("3) Sending pre-formatted JSON string single quotes -> ");
  Serial.println(sJson3);
  Serial.println(stuff->save(sJson3) ? "Success" : "Fail");

  Serial.println("");
}


void handleMessage(AdafruitIO_Data *data) {
  Serial.print("received <- ");
  Serial.println(data->value());
}


Serial monitor:
Code: Select all | TOGGLE FULL SIZE
1) Sending JSON object serialized to string -> {"thing_name":"feather_esp32","state":1}
Success
2) Sending pre-formatted JSON string -> {"thing_name":"feather_esp32":,"state":1}
Success
3) Sending pre-formatted JSON string single quotes -> {'thing_name':'feather_esp32':,'state':1}
Success

received <- {'thing_name':'feather_esp32':,'state':1}


Thanks in advance

jbdamask
 
Posts: 9
Joined: Sun Feb 07, 2016 4:44 pm

Re: JSON not posting to adafruit.io feed

by adafruit_support_carter on Tue Apr 09, 2019 8:23 pm

If you go to your dashboard, do all three show up in your feed?

adafruit_support_carter
 
Posts: 11467
Joined: Tue Nov 29, 2016 2:45 pm

Re: JSON not posting to adafruit.io feed

by jbdamask on Wed Apr 10, 2019 1:24 pm

Nope. Just the third one (ie string with single quotes)

jbdamask
 
Posts: 9
Joined: Sun Feb 07, 2016 4:44 pm

Re: JSON not posting to adafruit.io feed

by adafruit_support_carter on Wed Apr 10, 2019 2:39 pm

Hmmm. Maybe it's a rate limit thing. Are you using the free tier? It has a rate limit of 30 posts per minute. You are posting 3 things every 5 seconds. So you may be exceeding that.

adafruit_support_carter
 
Posts: 11467
Joined: Tue Nov 29, 2016 2:45 pm

Re: JSON not posting to adafruit.io feed

by jbdamask on Thu Apr 11, 2019 6:06 pm

weird...it seems to be a refresh thing. When I checked the feed today I see all my test data from the last few days.
It's fine to assume user error and close this but yeah...weird
thanks

jbdamask
 
Posts: 9
Joined: Sun Feb 07, 2016 4:44 pm

Re: JSON not posting to adafruit.io feed

by adafruit_support_carter on Thu Apr 11, 2019 6:18 pm

OK. The other thing I was thinking might be happening was how you are calling io.run(). Since that sort of drives everything, it may only fire the callback messages at that point. So your three calls to save() send data up. But then when it loops to the top and calls io.run() it only fires a single callback. Which is what your serial monitor output looks like.

But if it's working, then no worries. Glad it is and thanks for letting us know.

adafruit_support_carter
 
Posts: 11467
Joined: Tue Nov 29, 2016 2:45 pm

Please be positive and constructive with your questions and comments.