Skip to content

Commit

Permalink
chore: Update LoRa details
Browse files Browse the repository at this point in the history
  • Loading branch information
zfields committed Aug 10, 2024
1 parent 0bfaa5d commit 401a92a
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 19 deletions.
7 changes: 7 additions & 0 deletions examples/ArduinoIoTCloud-Advanced/thingProperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions examples/ArduinoIoTCloud-Basic/thingProperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions examples/ArduinoIoTCloud-Callbacks/thingProperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions examples/ArduinoIoTCloud-Schedule/thingProperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
41 changes: 26 additions & 15 deletions src/ArduinoIoTCloudNotecard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -73,7 +74,7 @@ ArduinoIoTCloudNotecard::ArduinoIoTCloudNotecard()
,_thing(&_message_stream)
,_device(&_message_stream)
,_notecard_last_poll_ms{static_cast<uint32_t>(-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
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -471,7 +472,7 @@ void ArduinoIoTCloudNotecard::sendMessage(Message * msg)
{
switch (msg->id) {
case PropertiesUpdateCmdId:
return sendThingPropertyContainerToCloud();
sendThingPropertyContainerToCloud();
break;

default:
Expand All @@ -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<NotecardConnectionHandler *>(_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");
}
Expand All @@ -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<NotecardConnectionHandler *>(_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");
Expand Down
7 changes: 4 additions & 3 deletions src/ArduinoIoTCloudNotecard.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:

Expand All @@ -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;

Expand Down

0 comments on commit 401a92a

Please sign in to comment.