Ejemplo n.º 1
0
void HoofSerial::sendData(const DataPacket& dataPacket)
{
  String output;
  const int BUFFER_SIZE = JSON_OBJECT_SIZE(1) + (JSON_ARRAY_SIZE(DATA_ARRAY_SIZE) * (JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(4)));
  
  StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();       //Build object tree in memory
  
  root["type"] = dataPacket.type;
  JsonArray& dataObj = root.createNestedArray("sample");

  for(unsigned int i = 0; i < DATA_ARRAY_SIZE; i++)
  {
    JsonObject& test = dataObj.createNestedObject();
    test["t"] = dataPacket.data[i].timeStamp; 
     
    JsonArray& obj = test.createNestedArray("f");   

    for(unsigned int j = 0; j < 4; j++)
    {
      obj.add(dataPacket.data[i].data[j]);
    }    
  }
  
  root.printTo(output);       //Generate the JSON string  
  
  this->println(output, true);      //Send over the Serial medium
}
TEST(StaticJsonBuffer_Object_Tests, ObjectDoesntGrowWhenFull) {
  StaticJsonBuffer<JSON_OBJECT_SIZE(1)> json;

  JsonObject &obj = json.createObject();
  obj["hello"];
  obj["world"];

  ASSERT_EQ(JSON_OBJECT_SIZE(1), json.size());
}
TEST(StaticJsonBuffer_Object_Tests, GrowsWithObject) {
  StaticJsonBuffer<JSON_OBJECT_SIZE(3)> json;

  JsonObject &obj = json.createObject();
  ASSERT_EQ(JSON_OBJECT_SIZE(0), json.size());

  obj["hello"];
  ASSERT_EQ(JSON_OBJECT_SIZE(1), json.size());

  obj["world"];
  ASSERT_EQ(JSON_OBJECT_SIZE(2), json.size());

  obj["world"];  // <- same value, should not grow
  ASSERT_EQ(JSON_OBJECT_SIZE(2), json.size());
}
Ejemplo n.º 4
0
void loop()
{
    if (tick >= 5) // publish to topic every 5seconds
    {
        tick = 0;

        const size_t bufferSize = JSON_OBJECT_SIZE(2) + 20;
        DynamicJsonBuffer jsonBuffer(bufferSize);

        JsonObject &root = jsonBuffer.createObject();
        root["payload"] = msgCount++;
        root["thing_id"] = thing_id;
        String json_output;

        root.printTo(json_output);
        char payload[bufferSize];

        json_output.toCharArray(payload, bufferSize);
        sprintf(payload, json_output.c_str());

        if (aws_iot.publish(aws_mqtt_thing_topic_pub, payload) == 0)
        {
            Serial.print("Publish Message:");
            Serial.println(payload);
        }
        else
        {
            Serial.println("Publish failed");
        }
    }
    vTaskDelay(1000 / portTICK_RATE_MS);
    tick++;
}
TEST_F(StaticJsonBuffer_ParseArray_Tests,
       BufferOfTheRightSizeForArrayWithNestedObject) {
  StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0)> bufferOfRightSize;
  with(bufferOfRightSize);
  whenInputIs("[{}]");
  parseMustSucceed();
}
TEST_F(StaticJsonBuffer_ParseObject_Tests,
       BufferOfTheRightSizeForObjectWithNestedObject) {
  StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0)> bufferOfRightSize;
  with(bufferOfRightSize);
  whenInputIs("{\"a\":[]}");
  parseMustSucceed();
}
TEST_F(StaticJsonBuffer_ParseArray_Tests,
       TooSmallBufferForArrayWithNestedObject) {
  StaticJsonBuffer<JSON_ARRAY_SIZE(1) + JSON_OBJECT_SIZE(0) - 1> bufferTooSmall;
  with(bufferTooSmall);
  whenInputIs("[{}]");
  parseMustFail();
}
TEST_F(StaticJsonBuffer_ParseObject_Tests,
       TooSmallBufferForObjectWithNestedObject) {
  StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_ARRAY_SIZE(0) - 1> bufferTooSmall;
  with(bufferTooSmall);
  whenInputIs("{\"a\":[]}");
  parseMustFail();
}
TEST_F(StaticJsonBuffer_ParseObject_Tests,
       BufferOfTheRightSizeForObjectWithOneValue) {
  StaticJsonBuffer<JSON_OBJECT_SIZE(1)> bufferOfRightSize;
  with(bufferOfRightSize);
  whenInputIs("{\"a\":1}");
  parseMustSucceed();
}
TEST_F(StaticJsonBuffer_ParseObject_Tests,
       TooSmallBufferForObjectWithOneValue) {
  StaticJsonBuffer<JSON_OBJECT_SIZE(1) - 1> bufferTooSmall;
  with(bufferTooSmall);
  whenInputIs("{\"a\":1}");
  parseMustFail();
}
Ejemplo n.º 11
0
  void test_with_value(T expected) {
    StaticJsonBuffer<JSON_OBJECT_SIZE(1)> jsonBuffer;

    JsonObject& jsonObject = jsonBuffer.createObject();

    jsonObject["key"] = expected;
    T actual = jsonObject["key"];

    ASSERT_EQ(expected, actual);
  }
Ejemplo n.º 12
0
namespace HomieInternals {
  const uint16_t MAX_JSON_CONFIG_FILE_SIZE = 1000;
  const uint16_t MAX_JSON_CONFIG_ARDUINOJSON_BUFFER_SIZE = JSON_OBJECT_SIZE(6) + JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(6) + JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(10);  // Max 5 elements at root, 2 elements in nested, etc... the last 10 means 10 custom settings max

  const uint8_t MAX_WIFI_SSID_LENGTH = 32 + 1;
  const uint8_t MAX_WIFI_PASSWORD_LENGTH = 64 + 1;
  const uint16_t MAX_HOSTNAME_LENGTH = 255 + 1;

  const uint8_t MAX_MQTT_CREDS_LENGTH = 32 + 1;
  const uint8_t MAX_MQTT_BASE_TOPIC_LENGTH = sizeof("shared-broker/username-lolipop/homie/sensors/");

  const uint8_t MAX_FRIENDLY_NAME_LENGTH = sizeof("My awesome friendly name of the living room");
  const uint8_t MAX_DEVICE_ID_LENGTH = sizeof("my-awesome-device-id-living-room");

  const uint8_t MAX_BRAND_LENGTH = MAX_WIFI_SSID_LENGTH - sizeof("-0123abcd") + 1;
  const uint8_t MAX_FIRMWARE_NAME_LENGTH = sizeof("my-awesome-home-firmware-name");
  const uint8_t MAX_FIRMWARE_VERSION_LENGTH = sizeof("v1.0.0-alpha+001");

  const uint8_t MAX_NODE_ID_LENGTH = sizeof("my-super-awesome-node-id");
  const uint8_t MAX_NODE_TYPE_LENGTH = sizeof("my-super-awesome-type");
  const uint8_t MAX_NODE_PROPERTY_LENGTH = sizeof("my-super-awesome-property");
}  // namespace HomieInternals
Ejemplo n.º 13
0
void HoofSerial::sendResponse(const ResponsePacket& responsePacket)
{
  String output;
  StaticJsonBuffer<JSON_OBJECT_SIZE(4)> jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();       //Build object tree in memory
  
  root["type"] = responsePacket.type;
  root["parameter"] = responsePacket.parameter.c_str();
  root["value"] = responsePacket.value.c_str();
  
  root.printTo(output);               //Generate the JSON string  
  this->println(output, false);      //Send over the Serial medium 
}
bool HttpBuyTransaction::sendForBalance(char* badge, unsigned long time)
{
    char timeString[11];
    
    snprintf(timeString, sizeof(timeString), "%lu", time);

    HashBuilder hashBuilder;
    hashBuilder.print(badge);
    hashBuilder.print(timeString);
    
    StaticJsonBuffer<JSON_OBJECT_SIZE(4)> jsonBuffer;
    JsonObject& json = jsonBuffer.createObject();
    json["Badge"] = badge;
    json["Hash"] = hashBuilder.getHash();
    json["Time"] = timeString;
    json.printTo(buffer, sizeof(buffer));

    return http.query("POST " API_PATH "/balance", buffer, sizeof(buffer));
}
bool HttpBuyTransaction::parse()
{
    StaticJsonBuffer<JSON_OBJECT_SIZE(4)+JSON_ARRAY_SIZE(2)> jsonBuffer;

    JsonObject& root = jsonBuffer.parseObject(buffer);
    if (!root.success()) {Serial.println("JSON error"); error = "JSON error"; return false;}

    melody = root["Melody"];
    if (melody == NULL) {Serial.println("No melody sent"); error = "No melody sent"; return false;}

    JsonArray& messageArray = root["Message"];
    if (!messageArray.success()) return false;
    
    messages[0] = messageArray[0];
    messages[1] = messageArray[1];

    time = root["Time"];
    if (time == NULL) {Serial.println("No time sent"); error = "No time sent"; return false;}

    hash = root["Hash"];
    if (hash == NULL) {Serial.println("No hash sent"); error = "No hash sent"; return false;}

    return true;
}
TEST_F(StaticJsonBuffer_ParseObject_Tests, TooSmallBufferForEmptyObject) {
  StaticJsonBuffer<JSON_OBJECT_SIZE(0) - 1> bufferTooSmall;
  with(bufferTooSmall);
  whenInputIs("{}");
  parseMustFail();
}
TEST(StaticJsonBuffer_Object_Tests, FailsWhenTooSmall) {
  StaticJsonBuffer<JSON_OBJECT_SIZE(0) - 1> json;

  JsonObject &object = json.createObject();
  ASSERT_FALSE(object.success());
}
TEST(StaticJsonBuffer_Object_Tests, SucceedWhenBigEnough) {
  StaticJsonBuffer<JSON_OBJECT_SIZE(0)> json;

  JsonObject &object = json.createObject();
  ASSERT_TRUE(object.success());
}
TEST_F(StaticJsonBuffer_ParseObject_Tests, BufferOfTheRightSizeForEmptyObject) {
  StaticJsonBuffer<JSON_OBJECT_SIZE(0)> bufferOfRightSize;
  with(bufferOfRightSize);
  whenInputIs("{}");
  parseMustSucceed();
}