diff --git a/examples/ArduinoIoTCloud-Advanced/thingProperties.h b/examples/ArduinoIoTCloud-Advanced/thingProperties.h index 77c397e0..53fb8746 100644 --- a/examples/ArduinoIoTCloud-Advanced/thingProperties.h +++ b/examples/ArduinoIoTCloud-Advanced/thingProperties.h @@ -11,6 +11,10 @@ #define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" #endif +#if defined(BOARD_HAS_LORA) + #define THING_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" +#endif + #if defined(USE_NOTECARD) /* The Notecard can provide connectivity to almost any board via ESLOV (I2C) * or UART. An empty string (or the default value provided below) will not @@ -31,6 +35,9 @@ void initProperties() { ArduinoCloud.setBoardId(BOARD_ID); ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY); #endif +#if defined(BOARD_HAS_LORA) + ArduinoCloud.setThingId(THING_ID); +#endif #if defined(USE_NOTECARD) || defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET) || defined(BOARD_HAS_CATM1_NBIOT) ArduinoCloud.addProperty(switchButton, Permission::Write).onUpdate(onSwitchButtonChange); ArduinoCloud.addProperty(location, Permission::Read).publishOnChange(0.0f); diff --git a/examples/ArduinoIoTCloud-Basic/thingProperties.h b/examples/ArduinoIoTCloud-Basic/thingProperties.h index a4718828..5f933ece 100644 --- a/examples/ArduinoIoTCloud-Basic/thingProperties.h +++ b/examples/ArduinoIoTCloud-Basic/thingProperties.h @@ -11,6 +11,10 @@ #define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" #endif +#if defined(BOARD_HAS_LORA) + #define THING_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" +#endif + #if defined(USE_NOTECARD) /* The Notecard can provide connectivity to almost any board via ESLOV (I2C) * or UART. An empty string (or the default value provided below) will not @@ -30,6 +34,9 @@ void initProperties() { ArduinoCloud.setBoardId(BOARD_ID); ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY); #endif +#if defined(BOARD_HAS_LORA) + ArduinoCloud.setThingId(THING_ID); +#endif #if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET) || defined(BOARD_HAS_CATM1_NBIOT) ArduinoCloud.addProperty(led, Permission::Write).onUpdate(onLedChange); ArduinoCloud.addProperty(potentiometer, Permission::Read).publishOnChange(10); diff --git a/examples/ArduinoIoTCloud-Callbacks/thingProperties.h b/examples/ArduinoIoTCloud-Callbacks/thingProperties.h index 62b7dc77..3e0c34a9 100644 --- a/examples/ArduinoIoTCloud-Callbacks/thingProperties.h +++ b/examples/ArduinoIoTCloud-Callbacks/thingProperties.h @@ -11,6 +11,10 @@ #define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" #endif +#if defined(BOARD_HAS_LORA) + #define THING_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" +#endif + #if defined(USE_NOTECARD) /* The Notecard can provide connectivity to almost any board via ESLOV (I2C) * or UART. An empty string (or the default value provided below) will not @@ -24,6 +28,9 @@ void initProperties() { ArduinoCloud.setBoardId(BOARD_ID); ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY); #endif +#if defined(BOARD_HAS_LORA) + ArduinoCloud.setThingId(THING_ID); +#endif } #if defined(USE_NOTECARD) diff --git a/examples/ArduinoIoTCloud-Notecard/ArduinoIoTCloud-Notecard.ino b/examples/ArduinoIoTCloud-Notecard/ArduinoIoTCloud-Notecard.ino index c63adad8..5e828b1a 100644 --- a/examples/ArduinoIoTCloud-Notecard/ArduinoIoTCloud-Notecard.ino +++ b/examples/ArduinoIoTCloud-Notecard/ArduinoIoTCloud-Notecard.ino @@ -44,7 +44,7 @@ void setup() { /* Initialize Arduino IoT Cloud library */ #ifndef ATTN_PIN ArduinoCloud.begin(ArduinoIoTPreferredConnection); - ArduinoCloud.setNotecardPollInterval(3000); // default: 1000ms, min: 250ms + ArduinoCloud.setNotecardPollingInterval(3000); // default: 1000ms, min: 250ms #else ArduinoCloud.begin(ArduinoIoTPreferredConnection, ATTN_PIN); #endif diff --git a/examples/ArduinoIoTCloud-Schedule/thingProperties.h b/examples/ArduinoIoTCloud-Schedule/thingProperties.h index e9a1e65c..368566e5 100644 --- a/examples/ArduinoIoTCloud-Schedule/thingProperties.h +++ b/examples/ArduinoIoTCloud-Schedule/thingProperties.h @@ -11,6 +11,10 @@ #define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" #endif +#if defined(BOARD_HAS_LORA) + #define THING_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" +#endif + #if defined(USE_NOTECARD) /* The Notecard can provide connectivity to almost any board via ESLOV (I2C) * or UART. An empty string (or the default value provided below) will not @@ -35,6 +39,9 @@ void initProperties() { ArduinoCloud.setBoardId(BOARD_ID); ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY); #endif +#if defined(BOARD_HAS_LORA) + ArduinoCloud.setThingId(THING_ID); +#endif #if defined(USE_NOTECARD) || defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET) || defined(BOARD_HAS_CATM1_NBIOT) ArduinoCloud.addProperty(switchButton, Permission::Write); ArduinoCloud.addProperty(oneShot, Permission::ReadWrite); diff --git a/src/ArduinoIoTCloudNotecard.cpp b/src/ArduinoIoTCloudNotecard.cpp index ef662c58..18c2dc4c 100644 --- a/src/ArduinoIoTCloudNotecard.cpp +++ b/src/ArduinoIoTCloudNotecard.cpp @@ -43,7 +43,8 @@ * CONSTANTS ******************************************************************************/ -static size_t const CBOR_NOTE_MSG_MAX_SIZE = 255; +static size_t const LORA_CBOR_MSG_MAX_SIZE = 242; +static size_t const LORA_PAYLOAD_MAX_SIZE = 236; static size_t const DEFAULT_READ_INTERVAL_MS = 1000; // 1 second static size_t const FAILSAFE_READ_INTERVAL_MS = 15000; // 15 seconds @@ -73,7 +74,7 @@ ArduinoIoTCloudNotecard::ArduinoIoTCloudNotecard() ,_thing(&_message_stream) ,_device(&_message_stream) ,_notecard_last_poll_ms{static_cast(-DEFAULT_READ_INTERVAL_MS)} - ,_notecard_poll_interval_ms{DEFAULT_READ_INTERVAL_MS} + ,_notecard_polling_interval_ms{DEFAULT_READ_INTERVAL_MS} ,_interrupt_pin{-1} ,_data_available{false} #if OTA_ENABLED @@ -278,7 +279,7 @@ bool ArduinoIoTCloudNotecard::available(void) const bool interrupts_enabled = (_interrupt_pin >= 0); const uint32_t now_ms = ::millis(); - bool check_data = ((now_ms - _notecard_last_poll_ms) > _notecard_poll_interval_ms); + bool check_data = ((now_ms - _notecard_last_poll_ms) > _notecard_polling_interval_ms); if (interrupts_enabled) { check_data = (_data_available || ((now_ms - _notecard_last_poll_ms) > FAILSAFE_READ_INTERVAL_MS)); } @@ -354,8 +355,8 @@ void ArduinoIoTCloudNotecard::pollNotecard(void) { /* Decode available data. */ if (available()) { - size_t note_len = CBOR_NOTE_MSG_MAX_SIZE; - uint8_t note_buf[CBOR_NOTE_MSG_MAX_SIZE]; + size_t note_len = LORA_CBOR_MSG_MAX_SIZE; + uint8_t note_buf[LORA_CBOR_MSG_MAX_SIZE]; fetchIncomingBytes(note_buf, note_len); processMessage(note_buf, note_len); } @@ -471,7 +472,7 @@ void ArduinoIoTCloudNotecard::sendMessage(Message * msg) { switch (msg->id) { case PropertiesUpdateCmdId: - return sendThingPropertyContainerToCloud(); + sendThingPropertyContainerToCloud(); break; default: @@ -482,17 +483,23 @@ void ArduinoIoTCloudNotecard::sendMessage(Message * msg) void ArduinoIoTCloudNotecard::sendCommandMsgToCloud(Message * msg_) { - size_t bytes_encoded = CBOR_NOTE_MSG_MAX_SIZE; - uint8_t data[CBOR_NOTE_MSG_MAX_SIZE]; + size_t bytes_encoded = LORA_CBOR_MSG_MAX_SIZE; + uint8_t data[LORA_CBOR_MSG_MAX_SIZE]; CBORMessageEncoder encoder; NotecardConnectionHandler *notecard_connection = reinterpret_cast(_connection); if (encoder.encode(msg_, data, bytes_encoded) == Encoder::Status::Complete) { - if (bytes_encoded > 0) { + if (LORA_PAYLOAD_MAX_SIZE < bytes_encoded) { + DEBUG_WARNING("Encoded %d bytes for Command Message. Exceeds maximum payload size of %d bytes, and cannot be sent to cloud.", bytes_encoded, LORA_PAYLOAD_MAX_SIZE); + } else if (bytes_encoded > 0) { + DEBUG_DEBUG("Encoded %d bytes for Command Message", bytes_encoded); notecard_connection->setTopicType(NotecardConnectionHandler::TopicType::Command); - notecard_connection->write(data, bytes_encoded); + if (notecard_connection->write(data, bytes_encoded)) { + DEBUG_ERROR("Failed to send Command Message to cloud"); + } + } else { + DEBUG_DEBUG("No bytes encoded for Command Message"); } - DEBUG_DEBUG("Encoded %d bytes for Command Message", bytes_encoded); } else { DEBUG_ERROR("Failed to encode Command Message"); } @@ -501,15 +508,19 @@ void ArduinoIoTCloudNotecard::sendCommandMsgToCloud(Message * msg_) void ArduinoIoTCloudNotecard::sendThingPropertyContainerToCloud() { int bytes_encoded = 0; - uint8_t data[CBOR_NOTE_MSG_MAX_SIZE]; + uint8_t data[LORA_CBOR_MSG_MAX_SIZE]; NotecardConnectionHandler *notecard_connection = reinterpret_cast(_connection); // Check if any property needs encoding and send them to the cloud if (CBOREncoder::encode(_thing.getPropertyContainer(), data, sizeof(data), bytes_encoded, _thing.getPropertyContainerIndex(), USE_LIGHT_PAYLOADS) == CborNoError) { - if (bytes_encoded > 0) { - notecard_connection->setTopicType(NotecardConnectionHandler::TopicType::Thing); - notecard_connection->write(data, bytes_encoded); + if (LORA_PAYLOAD_MAX_SIZE < bytes_encoded) { + DEBUG_ERROR("Encoded %d bytes for Thing properties. Exceeds maximum encoded payload size of %d bytes, and cannot sync with cloud.", bytes_encoded, LORA_PAYLOAD_MAX_SIZE); + } else if (bytes_encoded > 0) { DEBUG_DEBUG("Encoded %d bytes for Thing properties", bytes_encoded); + notecard_connection->setTopicType(NotecardConnectionHandler::TopicType::Thing); + if (notecard_connection->write(data, bytes_encoded)) { + DEBUG_ERROR("Failed to sync Thing properties with cloud"); + } } } else { DEBUG_ERROR("Failed to encode Thing properties"); diff --git a/src/ArduinoIoTCloudNotecard.h b/src/ArduinoIoTCloudNotecard.h index 10ccf134..b778816c 100644 --- a/src/ArduinoIoTCloudNotecard.h +++ b/src/ArduinoIoTCloudNotecard.h @@ -81,7 +81,8 @@ class ArduinoIoTCloudNotecard : public ArduinoIoTCloudClass /** * @brief Set the Notecard polling interval. * - * The interval at which the Notecard is polled for new data. + * The interval at which the Notecard is polled (via I2C/UART) for new data. + * This is not a network transaction, but a local polling of the Notecard. * * @param interval_ms The interval in milliseconds. * @par @@ -91,7 +92,7 @@ class ArduinoIoTCloudNotecard : public ArduinoIoTCloudClass * @note The Notecard poll interval is ignored if an interrupt pin is * provided to the `begin()` function. */ - inline void setNotecardPollInterval(uint32_t interval_ms) { _notecard_poll_interval_ms = ((interval_ms < 250) ? 250 : interval_ms); } + inline void setNotecardPollingInterval(uint32_t interval_ms) { _notecard_polling_interval_ms = ((interval_ms < 250) ? 250 : interval_ms); } private: @@ -111,7 +112,7 @@ class ArduinoIoTCloudNotecard : public ArduinoIoTCloudClass // Notecard member variables uint32_t _notecard_last_poll_ms; - uint32_t _notecard_poll_interval_ms; + uint32_t _notecard_polling_interval_ms; int _interrupt_pin; volatile bool _data_available;