debsahu / ESP-MQTT-AWS-IoT-Core

Arduino examples of connecting ESP8266/ESP32 to AWS IOT Core
MIT License
118 stars 50 forks source link

Adding More Fields to state_reported in sendData #8

Closed ashajjar closed 5 years ago

ashajjar commented 5 years ago

Hi

Thank you for the code it is very useful. It works out of the box for me. However when I try to change the function void sendData(void) by adding extra fields to the reported state as follows :

void sendData(void)
{
  DynamicJsonDocument jsonBuffer(JSON_OBJECT_SIZE(3) + 1000);
  JsonObject root = jsonBuffer.to<JsonObject>();
  JsonObject state = root.createNestedObject("state");
  JsonObject state_reported = state.createNestedObject("reported");
/// the new fields
  state_reported["humidity"] = humidity;
  state_reported["temperature"] = temperature;
  state_reported["heatIndex"] = heatIndex;
  state_reported["gasReading"] = gasReading;
  state_reported["now"] = now;

  Serial.printf("Sending  [%s]: ", MQTT_PUB_TOPIC);
  serializeJson(root, Serial);
  Serial.println();
  char shadow[measureJson(root) + 1];
  serializeJson(root, shadow, sizeof(shadow));
  if (!client.publish(MQTT_PUB_TOPIC, shadow, false))
    pubSubErr(client.state());
}

The code above actually prints something like this to the serial monitor, but never updates the shadow state:

Sending  [$aws/things/HomeEnvThing/shadow/update]: {"state":{"reported":{"humidity":34,"temperature":30,"heatIndex":29.07598,"gasReading":8,"now":1556807522}}}

Any suggestions ?

Thanks again :smiley:

ashajjar commented 5 years ago

Adding this flag to PlatformIO ini file solved the issue :

build_flags = -DMQTT_MAX_PACKET_SIZE=256
ashajjar commented 5 years ago

The issue is related to the payload maximum size specified by PubSub Library

debsahu commented 5 years ago

PIO FTW!

There are other methods to send large data as well on PubSubClient see https://github.com/knolleary/pubsubclient/issues/517