Skip to content

Commit c228c8b

Browse files
committed
feat: Notecard example
1 parent 541a918 commit c228c8b

File tree

10 files changed

+141
-18
lines changed

10 files changed

+141
-18
lines changed

examples/ArduinoIoTCloud-Advanced/ArduinoIoTCloud-Advanced.ino

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ void setup() {
1717
Serial.begin(9600);
1818
for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { }
1919

20+
/* Specify the level of detail for debug messages */
21+
setDebugMessageLevel(DBG_INFO);
22+
2023
/* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */
2124
initProperties();
2225

2326
/* Initialize Arduino IoT Cloud library */
2427
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
2528

26-
setDebugMessageLevel(DBG_INFO);
2729
ArduinoCloud.printDebugInfo();
2830
}
2931

examples/ArduinoIoTCloud-Basic/ArduinoIoTCloud-Basic.ino

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ void setup() {
2525
Serial.begin(9600);
2626
for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { }
2727

28+
/* Specify the level of detail for debug messages */
29+
setDebugMessageLevel(DBG_INFO);
30+
2831
/* Configure LED pin as an output */
2932
pinMode(LED_BUILTIN, OUTPUT);
3033

@@ -34,7 +37,6 @@ void setup() {
3437
/* Initialize Arduino IoT Cloud library */
3538
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
3639

37-
setDebugMessageLevel(DBG_INFO);
3840
ArduinoCloud.printDebugInfo();
3941
}
4042

examples/ArduinoIoTCloud-Callbacks/ArduinoIoTCloud-Callbacks.ino

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ void setup() {
3333
Serial.begin(9600);
3434
for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { }
3535

36+
/* Specify the level of detail for debug messages */
37+
setDebugMessageLevel(DBG_INFO);
38+
3639
/* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */
3740
initProperties();
3841

@@ -51,7 +54,6 @@ void setup() {
5154
ArduinoCloud.addCallback(ArduinoIoTCloudEvent::SYNC, doThisOnSync);
5255
ArduinoCloud.addCallback(ArduinoIoTCloudEvent::DISCONNECT, doThisOnDisconnect);
5356

54-
setDebugMessageLevel(DBG_INFO);
5557
ArduinoCloud.printDebugInfo();
5658
}
5759

examples/ArduinoIoTCloud-DeferredOTA/ArduinoIoTCloud-DeferredOTA.ino

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ void setup() {
5858
Serial.begin(9600);
5959
for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { }
6060

61+
/* Specify the level of detail for debug messages */
62+
setDebugMessageLevel(DBG_INFO);
63+
6164
/* Configure LED pin as an output */
6265
pinMode(LED_BUILTIN, OUTPUT);
6366

@@ -70,7 +73,6 @@ void setup() {
7073
/* Setup OTA callback */
7174
ArduinoCloud.onOTARequestCb(onOTARequestCallback);
7275

73-
setDebugMessageLevel(DBG_INFO);
7476
ArduinoCloud.printDebugInfo();
7577
}
7678

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
This sketch demonstrates how to exchange data between your board and the Arduino IoT Cloud.
3+
4+
* Connect a potentiometer (or other analog sensor) to A0.
5+
* When the potentiometer (or sensor) value changes the data is sent to the Cloud.
6+
* When you flip the switch in the Cloud dashboard the onboard LED lights gets turned ON or OFF.
7+
8+
IMPORTANT:
9+
This sketch works with Notecard, WiFi, GSM, NB, Ethernet and Lora enabled boards supported by Arduino IoT Cloud.
10+
On a LoRa board, if it is configured as a class A device (default and preferred option),
11+
values from Cloud dashboard are received only after a value is sent to Cloud.
12+
13+
The full list of compatible boards can be found here:
14+
- https://github.com/arduino-libraries/ArduinoIoTCloud#what
15+
*/
16+
17+
#include <Notecard.h>
18+
#include "thingProperties.h"
19+
20+
#if !defined(LED_BUILTIN) && !defined(ARDUINO_NANO_ESP32)
21+
static int const LED_BUILTIN = 2;
22+
#endif
23+
24+
/*
25+
* Choose an interrupt capable pin to reduce polling and improve
26+
* the overall responsiveness of the ArduinoIoTCloud library
27+
*/
28+
// #define ATTN_PIN 9
29+
30+
void setup() {
31+
/* Initialize serial and wait up to 5 seconds for port to open */
32+
Serial.begin(9600);
33+
for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { }
34+
35+
/* Specify the level of detail for debug messages */
36+
setDebugMessageLevel(DBG_INFO);
37+
38+
/* Configure LED pin as an output */
39+
pinMode(LED_BUILTIN, OUTPUT);
40+
41+
/* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */
42+
initProperties();
43+
44+
/* Initialize Arduino IoT Cloud library */
45+
#ifndef ATTN_PIN
46+
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
47+
ArduinoCloud.setNotecardPollInterval(3000); // default: 1000ms, min: 250ms
48+
#else
49+
ArduinoCloud.begin(ArduinoIoTPreferredConnection, ATTN_PIN);
50+
#endif
51+
52+
ArduinoCloud.printDebugInfo();
53+
}
54+
55+
void loop() {
56+
ArduinoCloud.update();
57+
potentiometer = analogRead(A0);
58+
seconds = millis() / 1000;
59+
}
60+
61+
/*
62+
* 'onLedChange' is called when the "led" property of your Thing changes
63+
*/
64+
void onLedChange() {
65+
Serial.print("LED set to ");
66+
Serial.println(led);
67+
digitalWrite(LED_BUILTIN, led);
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <Arduino_ConnectionHandler.h>
2+
3+
/* A complete list of supported boards with WiFi is available here:
4+
* https://github.com/arduino-libraries/ArduinoIoTCloud/#what
5+
*/
6+
#define SECRET_WIFI_SSID "YOUR_WIFI_NETWORK_NAME"
7+
#define SECRET_WIFI_PASS "YOUR_WIFI_PASSWORD"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <ArduinoIoTCloud.h>
2+
#include <Arduino_ConnectionHandler.h>
3+
#include "arduino_secrets.h"
4+
5+
/* The Notecard can provide connectivity to almost any board via ESLOV (I2C)
6+
* or UART. An empty string (or the default value provided below) will not
7+
* override the Notecard's existing configuration.
8+
* Learn more at: https://dev.blues.io */
9+
#define NOTECARD_PRODUCT_UID "com.domain.you:product"
10+
11+
/* Uncomment the following line to use the Notecard over UART */
12+
// #define UART_INTERFACE Serial1
13+
14+
void onLedChange();
15+
16+
bool led;
17+
int potentiometer;
18+
int seconds;
19+
20+
void initProperties() {
21+
ArduinoCloud.addProperty(led, 1, Permission::ReadWrite).onUpdate(onLedChange);
22+
ArduinoCloud.addProperty(potentiometer, 2, Permission::Read).publishOnChange(10);
23+
ArduinoCloud.addProperty(seconds, 3, Permission::Read).publishEvery(5 * MINUTES);
24+
25+
if (strncmp(SECRET_WIFI_SSID, "YOUR_WIFI_NETWORK_NAME", sizeof(SECRET_WIFI_SSID))) {
26+
ArduinoIoTPreferredConnection.setWiFiCredentials(SECRET_WIFI_SSID, SECRET_WIFI_PASS);
27+
}
28+
}
29+
30+
#ifndef UART_INTERFACE
31+
NotecardConnectionHandler ArduinoIoTPreferredConnection(NOTECARD_PRODUCT_UID);
32+
#else
33+
NotecardConnectionHandler ArduinoIoTPreferredConnection(NOTECARD_PRODUCT_UID, UART_INTERFACE);
34+
#endif
35+

examples/ArduinoIoTCloud-Schedule/ArduinoIoTCloud-Schedule.ino

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ void setup() {
1717
Serial.begin(9600);
1818
for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { }
1919

20+
/* Specify the level of detail for debug messages */
21+
setDebugMessageLevel(DBG_INFO);
22+
2023
/* Configure LED pin as an output */
2124
pinMode(LED_BUILTIN, OUTPUT);
2225

@@ -26,7 +29,6 @@ void setup() {
2629
/* Initialize Arduino IoT Cloud library */
2730
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
2831

29-
setDebugMessageLevel(DBG_INFO);
3032
ArduinoCloud.printDebugInfo();
3133

3234
/* Setup one shot schedule example */

src/ArduinoIoTCloudNotecard.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ ArduinoIoTCloudNotecard::ArduinoIoTCloudNotecard()
7272
,_message_stream(std::bind(&ArduinoIoTCloudNotecard::sendMessage, this, std::placeholders::_1))
7373
,_thing(&_message_stream)
7474
,_device(&_message_stream)
75-
,_notecard_last_read_ms{static_cast<uint32_t>(-DEFAULT_READ_INTERVAL_MS)}
76-
,_notecard_read_interval_ms{DEFAULT_READ_INTERVAL_MS}
75+
,_notecard_last_poll_ms{static_cast<uint32_t>(-DEFAULT_READ_INTERVAL_MS)}
76+
,_notecard_poll_interval_ms{DEFAULT_READ_INTERVAL_MS}
7777
,_interrupt_pin{-1}
7878
,_data_available{false}
7979
#if OTA_ENABLED
@@ -278,15 +278,15 @@ bool ArduinoIoTCloudNotecard::available(void)
278278
const bool interrupts_enabled = (_interrupt_pin >= 0);
279279
const uint32_t now_ms = ::millis();
280280

281-
bool check_data = ((now_ms - _notecard_last_read_ms) > _notecard_read_interval_ms);
281+
bool check_data = ((now_ms - _notecard_last_poll_ms) > _notecard_poll_interval_ms);
282282
if (interrupts_enabled) {
283-
check_data = (_data_available || ((now_ms - _notecard_last_read_ms) > FAILSAFE_READ_INTERVAL_MS));
283+
check_data = (_data_available || ((now_ms - _notecard_last_poll_ms) > FAILSAFE_READ_INTERVAL_MS));
284284
}
285285

286286
if (check_data) {
287287
result = _connection->available();
288288
_data_available = ::digitalRead(_interrupt_pin);
289-
_notecard_last_read_ms = now_ms;
289+
_notecard_last_poll_ms = now_ms;
290290
} else {
291291
result = false;
292292
}

src/ArduinoIoTCloudNotecard.h

+11-8
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,27 @@ class ArduinoIoTCloudNotecard : public ArduinoIoTCloudClass
7171
* @param interrupt_pin The interrupt pin to use for the Notecard.
7272
*
7373
* @note The interrupt pin is optional and only required if you want to
74-
* eliminate the need to poll the Notecard for new data.
74+
* eliminate the need to poll the Notecard for new data. The pin only needs
75+
* to be specified, and will otherwise be configured by the library.
7576
*
7677
* @return 1 on success, 0 on failure.
7778
*/
7879
int begin(ConnectionHandler &connection, int interrupt_pin = -1);
7980

8081
/**
81-
* @brief Set the Notecard read interval.
82+
* @brief Set the Notecard polling interval.
8283
*
83-
* The interval at which the Notecard is polled for new data. The default
84-
* interval is 1000ms, and the minimum interval is 250ms.
84+
* The interval at which the Notecard is polled for new data.
8585
*
8686
* @param interval_ms The interval in milliseconds.
87+
* @par
88+
* - Minimum: 250ms
89+
* - Default: 1000ms
8790
*
88-
* @note The Notecard read interval is ignored if an interrupt pin is
91+
* @note The Notecard poll interval is ignored if an interrupt pin is
8992
* provided to the `begin()` function.
9093
*/
91-
inline void setNotecardReadInterval(uint32_t interval_ms) { _notecard_read_interval_ms = ((interval_ms < 250) ? 250 : interval_ms); }
94+
inline void setNotecardPollInterval(uint32_t interval_ms) { _notecard_poll_interval_ms = ((interval_ms < 250) ? 250 : interval_ms); }
9295

9396
private:
9497

@@ -107,8 +110,8 @@ class ArduinoIoTCloudNotecard : public ArduinoIoTCloudClass
107110
ArduinoCloudDevice _device;
108111

109112
// Notecard member variables
110-
uint32_t _notecard_last_read_ms;
111-
uint32_t _notecard_read_interval_ms;
113+
uint32_t _notecard_last_poll_ms;
114+
uint32_t _notecard_poll_interval_ms;
112115
int _interrupt_pin;
113116
volatile bool _data_available;
114117

0 commit comments

Comments
 (0)