Skip to content

Commit

Permalink
Fix example with static_cast to avoid ambiguity
Browse files Browse the repository at this point in the history
  • Loading branch information
MathewHDYT committed Aug 1, 2023
1 parent 5ff6a7f commit 35d02b0
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,11 @@ void loop() {
Serial.println("Sending telemetry data...");
#endif
// Uploads new telemetry to ThingsBoard using MQTT.
// See https://thingsboard.io/docs/reference/mqtt-api/#telemetry-upload-api
// for more details
tb.sendTelemetryData(TEMPERATURE_KEY, random(10, 31));
tb.sendTelemetryData(HUMIDITY_KEY, random(40, 90));
// See https://thingsboard.io/docs/reference/mqtt-api/#telemetry-upload-api for more details.
// Static_cast, helps the Telemetry constructor which type is wanted exactly, because if this is not given
// conversion between float and bool can cause compile time failure with ambiguity which constructor is exactly wanted
tb.sendTelemetryData(TEMPERATURE_KEY, static_cast<int>(random(10, 31)));
tb.sendTelemetryData(HUMIDITY_KEY, static_cast<int>(random(40, 90)));

tb.loop();
}
16 changes: 10 additions & 6 deletions examples/0001-arduino_send_batch/0001-arduino_send_batch.ino
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,11 @@ void loop() {
#endif

const uint8_t data_items = 2U;
// Static_cast, helps the Telemetry constructor which type is wanted exactly, because if this is not given
// conversion between float and bool can cause compile time failure with ambiguity which constructor is exactly wanted
Telemetry data[data_items] = {
{ TEMPERATURE_KEY, 42.2 },
{ HUMIDITY_KEY, 80 },
{ TEMPERATURE_KEY, static_cast<float>(42.2) },
{ HUMIDITY_KEY, static_cast<int>(80) },
};

/* For C++98 compiler, shipped with Arduino IDE version 1.6.6 or less:
Expand All @@ -205,15 +207,17 @@ void loop() {
Serial.println("Sending attributes data...");
#endif

const int attribute_items = 2;
const uint8_t attribute_items = 2;
// Static_cast, helps the Telemetry constructor which type is wanted exactly, because if this is not given
// conversion between float and bool can cause compile time failure with ambiguity which constructor is exactly wanted
Attribute attributes[attribute_items] = {
{ DEVICE_TYPE_KEY, SENSOR_VALUE },
{ ACTIVE_KEY, true },
{ DEVICE_TYPE_KEY, static_cast<char*>(SENSOR_VALUE) },
{ ACTIVE_KEY, static_cast<bool>(true) },
};

/* For C++98 compiler, shipped with Arduino IDE version 1.6.6 or less:
Attribute attributes[data_items] = {
Attribute attributes[attribute_items] = {
Attribute( DEVICE_TYPE_KEY, SENSOR_VALUE ),
Attribute( ACTIVE_KEY, true ),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,22 +283,23 @@ void loop() {
}
#endif

// Uploads new telemetry to ThingsBoard using HTTP.
// See https://thingsboard.io/docs/reference/http-api/#telemetry-upload-api
// for more details
// Uploads new telemetry to ThingsBoard using MQTT.
// See https://thingsboard.io/docs/reference/mqtt-api/#telemetry-upload-api for more details.
// Static_cast, helps the Telemetry constructor which type is wanted exactly, because if this is not given
// conversion between float and bool can cause compile time failure with ambiguity which constructor is exactly wanted
#if THINGSBOARD_ENABLE_PROGMEM
Serial.println(F("Sending temperature data..."));
#else
Serial.println("Sending temperature data...");
#endif
tb.sendTelemetryData(TEMPERATURE_KEY, random(10, 31));
tb.sendTelemetryData(TEMPERATURE_KEY, static_cast<int>(random(10, 31)));

#if THINGSBOARD_ENABLE_PROGMEM
Serial.println(F("Sending humidity data..."));
#else
Serial.println("Sending humidity data...");
#endif
tb.sendTelemetryData(HUMIDITY_KEY, random(40, 90));
tb.sendTelemetryData(HUMIDITY_KEY, static_cast<int>(random(40, 90)));

#if !USING_HTTPS
tb.loop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,22 +205,23 @@ void loop() {
}
}

// Uploads new telemetry to ThingsBoard using HTTP.
// See https://thingsboard.io/docs/reference/http-api/#telemetry-upload-api
// for more details
// Uploads new telemetry to ThingsBoard using MQTT.
// See https://thingsboard.io/docs/reference/mqtt-api/#telemetry-upload-api for more details.
// Static_cast, helps the Telemetry constructor which type is wanted exactly, because if this is not given
// conversion between float and bool can cause compile time failure with ambiguity which constructor is exactly wanted
#if THINGSBOARD_ENABLE_PROGMEM
Serial.println(F("Sending temperature data..."));
#else
Serial.println("Sending temperature data...");
#endif
tb.sendTelemetryData(TEMPERATURE_KEY, random(10, 31));
tb.sendTelemetryData(TEMPERATURE_KEY, static_cast<int>(random(10, 31)));

#if THINGSBOARD_ENABLE_PROGMEM
Serial.println(F("Sending humidity data..."));
#else
Serial.println("Sending humidity data...");
#endif
tb.sendTelemetryData(HUMIDITY_KEY, random(40, 90));
tb.sendTelemetryData(HUMIDITY_KEY, static_cast<int>(random(40, 90)));

tb.loop();
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,20 +184,21 @@ void loop() {
#endif
}

// Uploads new telemetry to ThingsBoard using HTTP.
// See https://thingsboard.io/docs/reference/http-api/#telemetry-upload-api
// for more details
// Uploads new telemetry to ThingsBoard using MQTT.
// See https://thingsboard.io/docs/reference/mqtt-api/#telemetry-upload-api for more details.
// Static_cast, helps the Telemetry constructor which type is wanted exactly, because if this is not given
// conversion between float and bool can cause compile time failure with ambiguity which constructor is exactly wanted
#if THINGSBOARD_ENABLE_PROGMEM
Serial.println(F("Sending temperature data..."));
#else
Serial.println("Sending temperature data...");
#endif
tb.sendTelemetryData(TEMPERATURE_KEY, random(10, 31));
tb.sendTelemetryData(TEMPERATURE_KEY, static_cast<int>(random(10, 31)));

#if THINGSBOARD_ENABLE_PROGMEM
Serial.println(F("Sending humidity data..."));
#else
Serial.println("Sending humidity data...");
#endif
tb.sendTelemetryData(HUMIDITY_KEY, random(40, 90));
tb.sendTelemetryData(HUMIDITY_KEY, static_cast<int>(random(40, 90)));
}
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,12 @@ void loop() {
#else
Serial.println("Sending telemetry...");
#endif
tb.sendTelemetryData(TEMPERATURE_KEY, 22);
tb.sendTelemetryData(HUMIDITY_KEY, 42.5);
// Uploads new telemetry to ThingsBoard using MQTT.
// See https://thingsboard.io/docs/reference/mqtt-api/#telemetry-upload-api for more details.
// Static_cast, helps the Telemetry constructor which type is wanted exactly, because if this is not given
// conversion between float and bool can cause compile time failure with ambiguity which constructor is exactly wanted
tb.sendTelemetryData(TEMPERATURE_KEY, static_cast<int>(22));
tb.sendTelemetryData(HUMIDITY_KEY, static_cast<float>(42.5));
}
}

Expand Down

0 comments on commit 35d02b0

Please sign in to comment.