Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f779ebf

Browse files
committedAug 6, 2024·
feat: NotecardConnectionManager
1 parent f39958c commit f779ebf

13 files changed

+1431
-34
lines changed
 

‎.github/workflows/compile-examples.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ jobs:
3535
- name: MKRNB
3636
- name: MKRWAN
3737
- name: Arduino_Cellular
38+
- name: Blues Wireless Notecard
3839
ARDUINOCORE_MBED_STAGING_PATH: extras/ArduinoCore-mbed
3940
ARDUINOCORE_API_STAGING_PATH: extras/ArduinoCore-API
4041
SKETCHES_REPORTS_PATH: sketches-reports
@@ -135,7 +136,7 @@ jobs:
135136
# Install ESP8266 platform via Boards Manager
136137
- name: esp8266:esp8266
137138
source-url: https://arduino.esp8266.com/stable/package_esp8266com_index.json
138-
version: 2.5.0
139+
version: 3.1.2
139140
- board:
140141
platform-name: esp32:esp32
141142
platforms: |

‎.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode/

‎README.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,30 @@ Arduino Library for network connections management
66
[![Spell Check status](https://github.com/arduino-libraries/Arduino_ConnectionHandler/actions/workflows/spell-check.yml/badge.svg)](https://github.com/arduino-libraries/Arduino_ConnectionHandler/actions/workflows/spell-check.yml)
77

88
Library for handling and managing network connections by providing keep-alive functionality and automatic reconnection in case of connection-loss. It supports the following boards:
9-
* **WiFi**: [`MKR 1000`](https://store.arduino.cc/arduino-mkr1000-wifi), [`MKR WiFi 1010`](https://store.arduino.cc/arduino-mkr-wifi-1010), [`Nano 33 IoT`](https://store.arduino.cc/arduino-nano-33-iot), [`Portenta H7`](https://store.arduino.cc/products/portenta-h7), [`Nano RP2040 Connect`](https://store.arduino.cc/products/arduino-nano-rp2040-connect), [`Nicla Vision`](https://store.arduino.cc/products/nicla-vision), [`OPTA WiFi`](https://store.arduino.cc/products/opta-wifi), [`GIGA R1 WiFi`](https://store.arduino.cc/products/giga-r1-wifi), [`Portenta C33`](https://store.arduino.cc/products/portenta-c33), [`UNO R4 WiFi`](https://store.arduino.cc/products/uno-r4-wifi), [`Nano ESP32`](https://store.arduino.cc/products/nano-esp32), [`ESP8266`](https://github.com/esp8266/Arduino/releases/tag/2.5.0), [`ESP32`](https://github.com/espressif/arduino-esp32)
10-
* **GSM**: [`MKR GSM 1400`](https://store.arduino.cc/arduino-mkr-gsm-1400-1415)
9+
10+
* **Wi-Fi**: [`MKR 1000`](https://store.arduino.cc/arduino-mkr1000-wifi), [`MKR WiFi 1010`](https://store.arduino.cc/arduino-mkr-wifi-1010), [`Nano 33 IoT`](https://store.arduino.cc/arduino-nano-33-iot), [`Portenta H7`](https://store.arduino.cc/products/portenta-h7), [`Nano RP2040 Connect`](https://store.arduino.cc/products/arduino-nano-rp2040-connect), [`Nicla Vision`](https://store.arduino.cc/products/nicla-vision), [`OPTA WiFi`](https://store.arduino.cc/products/opta-wifi), [`GIGA R1 WiFi`](https://store.arduino.cc/products/giga-r1-wifi), [`Portenta C33`](https://store.arduino.cc/products/portenta-c33), [`UNO R4 WiFi`](https://store.arduino.cc/products/uno-r4-wifi), [`Nano ESP32`](https://store.arduino.cc/products/nano-esp32), [`ESP8266`](https://github.com/esp8266/Arduino/releases/tag/2.5.0), [`ESP32`](https://github.com/espressif/arduino-esp32),
11+
[`Notecard`](https://shop.blues.com/collections/notecard)
12+
* **GSM**: [`MKR GSM 1400`](https://store.arduino.cc/arduino-mkr-gsm-1400-1415), [`Notecard`](https://shop.blues.com/collections/notecard)
13+
* **LTE**: [`Notecard`](https://shop.blues.com/collections/notecard)
1114
* **5G**: [`MKR NB 1500`](https://store.arduino.cc/arduino-mkr-nb-1500-1413)
12-
* **LoRa**: [`MKR WAN 1300/1310`](https://store.arduino.cc/mkr-wan-1310)
15+
* **LoRa**: [`MKR WAN 1300/1310`](https://store.arduino.cc/mkr-wan-1310), [`Notecard`](https://shop.blues.com/collections/notecard)
1316
* **Ethernet**: [`Portenta H7`](https://store.arduino.cc/products/portenta-h7) + [`Vision Shield Ethernet`](https://store.arduino.cc/products/arduino-portenta-vision-shield-ethernet), [`Max Carrier`](https://store.arduino.cc/products/portenta-max-carrier), [`Breakout`](https://store.arduino.cc/products/arduino-portenta-breakout), [`Portenta Machine Control`](https://store.arduino.cc/products/arduino-portenta-machine-control), [`OPTA WiFi`](https://store.arduino.cc/products/opta-wifi), [`OPTA RS485`](https://store.arduino.cc/products/opta-rs485), [`OPTA Lite`](https://store.arduino.cc/products/opta-lite), [`Portenta C33`](https://store.arduino.cc/products/portenta-c33) + [`Vision Shield Ethernet`](https://store.arduino.cc/products/arduino-portenta-vision-shield-ethernet)
17+
* **Satellite**: [`Notecard`](https://shop.blues.com/collections/notecard)
18+
19+
#### More About Notecard Connectivity
20+
21+
The Notecard is a wireless, secure abstraction for device connectivity, that can
22+
be used to enable _ANY*_ device with I2C, or UART, to connect to the Arduino IoT
23+
Cloud via cellular, LoRa, satellite or Wi-Fi (including the devices listed
24+
above)!
25+
26+
As a result, the STM32 architecture has now been added to this library. If you
27+
have an STM32 device, you are now able to connect it to the Arduino IoT Cloud by
28+
using a Notecard to provide a secure communication channel.
29+
30+
> \*_Unfortunately, the AVR architecture is not supported by the Arduino IoT
31+
> Cloud library. Therefore, those devices are ineligible for use with the
32+
> Notecard._
1433
1534
### How-to-use
1635

@@ -20,7 +39,7 @@ Library for handling and managing network connections by providing keep-alive fu
2039
#if defined(BOARD_HAS_ETHERNET)
2140
EthernetConnectionHandler conMan;
2241
#elif defined(BOARD_HAS_WIFI)
23-
WiFiConnectionHandler conMan("SECRET_SSID", "SECRET_PASS");
42+
WiFiConnectionHandler conMan("SECRET_WIFI_SSID", "SECRET_WIFI_PASS");
2443
#elif defined(BOARD_HAS_GSM)
2544
GSMConnectionHandler conMan("SECRET_PIN", "SECRET_APN", "SECRET_GSM_LOGIN", "SECRET_GSM_PASS");
2645
#elif defined(BOARD_HAS_NB)
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/* SECRET_ fields are in `arduino_secrets.h` (included below)
2+
*
3+
* If using a Host + Notecard connected over I2C you'll need a
4+
* NotecardConnectionHandler object as follows:
5+
*
6+
* NotecardConnectionHandler conMan(NOTECARD_PRODUCT_UID);
7+
*
8+
* If using a Host + Notecard connected over Serial you'll need a
9+
* NotecardConnectionHandler object as follows:
10+
*
11+
* NotecardConnectionHandler conMan(NOTECARD_PRODUCT_UID, UART_INTERFACE);
12+
*/
13+
14+
#include <Notecard.h> // MUST include this first to enable Notecard support
15+
#include <Arduino_ConnectionHandler.h>
16+
17+
#include "arduino_secrets.h"
18+
19+
/* Uncomment the following line to use this example in a manner that is more
20+
* compatible with LoRa.
21+
*/
22+
// #define USE_NOTE_LORA
23+
24+
#ifndef USE_NOTE_LORA
25+
#define CONN_TOGGLE_MS 60000
26+
#else
27+
#define CONN_TOGGLE_MS 300000
28+
#endif
29+
30+
/* The Notecard can provide connectivity to almost any board via ESLOV (I2C)
31+
* or UART. An empty string (or the default value provided below) will not
32+
* override the Notecard's existing configuration.
33+
* Learn more at: https://dev.blues.io */
34+
#define NOTECARD_PRODUCT_UID "com.domain.you:product"
35+
36+
/* Uncomment the following line to use the Notecard over UART */
37+
// #define UART_INTERFACE Serial1
38+
39+
#ifndef UART_INTERFACE
40+
NotecardConnectionHandler conMan(NOTECARD_PRODUCT_UID);
41+
#else
42+
NotecardConnectionHandler conMan(NOTECARD_PRODUCT_UID, UART_INTERFACE);
43+
#endif
44+
45+
bool attemptConnect = false;
46+
uint32_t lastConnToggleMs = 0;
47+
48+
void setup() {
49+
/* Initialize serial debug port and wait up to 5 seconds for port to open */
50+
Serial.begin(9600);
51+
for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { }
52+
53+
/* Set the debug message level:
54+
* - DBG_ERROR: Only show error messages
55+
* - DBG_WARNING: Show warning and error messages
56+
* - DBG_INFO: Show info, warning, and error messages
57+
* - DBG_DEBUG: Show debug, info, warning, and error messages
58+
* - DBG_VERBOSE: Show all messages
59+
*/
60+
setDebugMessageLevel(DBG_INFO);
61+
62+
/* Add callbacks to the ConnectionHandler object to get notified of network
63+
* connection events. */
64+
conMan.addCallback(NetworkConnectionEvent::CONNECTED, onNetworkConnect);
65+
conMan.addCallback(NetworkConnectionEvent::DISCONNECTED, onNetworkDisconnect);
66+
conMan.addCallback(NetworkConnectionEvent::ERROR, onNetworkError);
67+
68+
/* First call to `check()` initializes the connection to the Notecard */
69+
conMan.check();
70+
71+
#ifndef USE_NOTE_LORA
72+
/* Set the Wi-Fi credentials for the Notecard */
73+
String ssid = SECRET_WIFI_SSID;
74+
if (ssid.length() > 0 && ssid != "NETWORK NAME") {
75+
conMan.setWiFiCredentials(SECRET_WIFI_SSID, SECRET_WIFI_PASS);
76+
}
77+
#endif
78+
79+
/* Confirm Interface */
80+
Serial.print("Network Adapter Interface: ");
81+
if (NetworkAdapter::NOTECARD == conMan.getInterface()) {
82+
Serial.print("Notecard ");
83+
Serial.print(conMan.getNotecardUid());
84+
#ifndef UART_INTERFACE
85+
Serial.println(" (via I2C)");
86+
#else
87+
Serial.println(" (via UART)");
88+
#endif
89+
} else {
90+
Serial.println("ERROR: Unexpected Interface");
91+
while(1);
92+
}
93+
}
94+
95+
void loop() {
96+
/* Toggle the connection every `CONN_TOGGLE_MS` milliseconds */
97+
if ((millis() - lastConnToggleMs) > CONN_TOGGLE_MS) {
98+
Serial.println("Toggling connection...");
99+
if (attemptConnect) {
100+
conMan.connect();
101+
} else {
102+
conMan.disconnect();
103+
}
104+
attemptConnect = !attemptConnect;
105+
lastConnToggleMs = millis();
106+
}
107+
108+
/* The following code keeps on running connection workflows on our
109+
* ConnectionHandler object, hence allowing reconnection in case of failure
110+
* and notification of connect/disconnect event if enabled (see
111+
* addConnectCallback/addDisconnectCallback) NOTE: any use of delay() within
112+
* the loop or methods called from it will delay the execution of .update(),
113+
* which might not guarantee the correct functioning of the ConnectionHandler
114+
* object.
115+
*/
116+
conMan.check();
117+
}
118+
119+
void onNetworkConnect() {
120+
Serial.println(">>>> CONNECTED to network");
121+
}
122+
123+
void onNetworkDisconnect() {
124+
Serial.println(">>>> DISCONNECTED from network");
125+
}
126+
127+
void onNetworkError() {
128+
Serial.println(">>>> ERROR");
129+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* A complete list of supported boards with WiFi is available here:
2+
* https://github.com/arduino-libraries/Arduino_ConnectionHandler/blob/master/README.md
3+
*/
4+
const char SECRET_WIFI_SSID[] = "NETWORK NAME";
5+
const char SECRET_WIFI_PASS[] = "NETWORK PASSWORD";

‎examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino

Lines changed: 95 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1-
/* SECRET_ fields are in arduino_secrets.h included above
2-
* if using a WiFi board (Arduino MKR1000, MKR WiFi 1010, Nano 33 IoT, UNO
1+
/* SECRET_ fields are in `arduino_secrets.h` (included below)
2+
*
3+
* If using a Host + Notecard connected over I2C you'll need a
4+
* NotecardConnectionHandler object as follows
5+
*
6+
* NotecardConnectionHandler conMan(NOTECARD_PRODUCT_UID);
7+
*
8+
* If using a Host + Notecard connected over Serial you'll need a
9+
* NotecardConnectionHandler object as follows
10+
*
11+
* NotecardConnectionHandler conMan(NOTECARD_PRODUCT_UID, Serial);
12+
*
13+
* If using a WiFi board (Arduino MKR1000, MKR WiFi 1010, Nano 33 IoT, UNO
314
* WiFi Rev 2 or ESP8266/32), create a WiFiConnectionHandler object by adding
4-
* Network Name (SECRET_SSID) and password (SECRET_PASS) in the arduino_secrets.h
5-
* file (or Secrets tab in Create Web Editor).
15+
* Network Name (SECRET_WIFI_SSID) and password (SECRET_WIFI_PASS) in the
16+
* arduino_secrets.h file (or Secrets tab in Create Web Editor).
617
*
7-
* WiFiConnectionHandler conMan(SECRET_SSID, SECRET_PASS);
18+
* WiFiConnectionHandler conMan(SECRET_WIFI_SSID, SECRET_WIFI_PASS);
819
*
920
* If using a MKR GSM 1400 or other GSM boards supporting the same API you'll
1021
* need a GSMConnectionHandler object as follows
@@ -27,14 +38,31 @@
2738
*
2839
*/
2940

41+
#include <Arduino_ConnectionHandler.h>
42+
3043
#include "arduino_secrets.h"
3144

32-
#include <Arduino_ConnectionHandler.h>
45+
#define CONN_TOGGLE_MS 60000
46+
47+
#if !(defined(USE_NOTECARD) || defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_LORA) || \
48+
defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET) || defined(BOARD_HAS_CATM1_NBIOT))
49+
#error "Please check Arduino Connection Handler supported boards list: https://github.com/arduino-libraries/Arduino_ConnectionHandler/blob/master/README.md"
50+
#endif
3351

34-
#if defined(BOARD_HAS_ETHERNET)
52+
#if defined(USE_NOTECARD)
53+
/* The Notecard can provide connectivity to almost any board via ESLOV (I2C)
54+
* or UART. An empty string (or the default value provided below) will not
55+
* override the Notecard's existing configuration.
56+
* Learn more at: https://dev.blues.io */
57+
#define NOTECARD_PRODUCT_UID "com.domain.you:product"
58+
#endif
59+
60+
#if defined(USE_NOTECARD)
61+
NotecardConnectionHandler conMan(NOTECARD_PRODUCT_UID);
62+
#elif defined(BOARD_HAS_ETHERNET)
3563
EthernetConnectionHandler conMan(SECRET_IP, SECRET_DNS, SECRET_GATEWAY, SECRET_NETMASK);
3664
#elif defined(BOARD_HAS_WIFI)
37-
WiFiConnectionHandler conMan(SECRET_SSID, SECRET_PASS);
65+
WiFiConnectionHandler conMan(SECRET_WIFI_SSID, SECRET_WIFI_PASS);
3866
#elif defined(BOARD_HAS_GSM)
3967
GSMConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS);
4068
#elif defined(BOARD_HAS_NB)
@@ -47,19 +75,76 @@ CatM1ConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GS
4775
CellularConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS);
4876
#endif
4977

78+
bool attemptConnect = false;
79+
uint32_t lastConnToggleMs = 0;
80+
5081
void setup() {
82+
/* Initialize serial debug port and wait up to 5 seconds for port to open */
5183
Serial.begin(9600);
52-
/* Give a few seconds for the Serial connection to be available */
53-
delay(4000);
84+
for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { }
85+
5486
#ifndef __AVR__
87+
/* Set the debug message level:
88+
* - DBG_ERROR: Only show error messages
89+
* - DBG_WARNING: Show warning and error messages
90+
* - DBG_INFO: Show info, warning, and error messages
91+
* - DBG_DEBUG: Show debug, info, warning, and error messages
92+
* - DBG_VERBOSE: Show all messages
93+
*/
5594
setDebugMessageLevel(DBG_INFO);
5695
#endif
96+
97+
/* Add callbacks to the ConnectionHandler object to get notified of network
98+
* connection events. */
5799
conMan.addCallback(NetworkConnectionEvent::CONNECTED, onNetworkConnect);
58100
conMan.addCallback(NetworkConnectionEvent::DISCONNECTED, onNetworkDisconnect);
59101
conMan.addCallback(NetworkConnectionEvent::ERROR, onNetworkError);
102+
103+
Serial.print("Network Adapter Interface: ");
104+
switch (conMan.getInterface()) {
105+
case NetworkAdapter::WIFI:
106+
Serial.println("Wi-Fi");
107+
break;
108+
case NetworkAdapter::ETHERNET:
109+
Serial.println("Ethernet");
110+
break;
111+
case NetworkAdapter::NB:
112+
Serial.println("Narrowband");
113+
break;
114+
case NetworkAdapter::GSM:
115+
Serial.println("GSM");
116+
break;
117+
case NetworkAdapter::LORA:
118+
Serial.println("LoRa");
119+
break;
120+
case NetworkAdapter::CATM1:
121+
Serial.println("Category M1");
122+
break;
123+
case NetworkAdapter::CELL:
124+
Serial.println("Cellular");
125+
break;
126+
case NetworkAdapter::NOTECARD:
127+
Serial.println("Notecard");
128+
break;
129+
default:
130+
Serial.println("Unknown");
131+
break;
132+
}
60133
}
61134

62135
void loop() {
136+
/* Toggle the connection every `CONN_TOGGLE_MS` milliseconds */
137+
if ((millis() - lastConnToggleMs) > CONN_TOGGLE_MS) {
138+
Serial.println("Toggling connection...");
139+
if (attemptConnect) {
140+
conMan.connect();
141+
} else {
142+
conMan.disconnect();
143+
}
144+
attemptConnect = !attemptConnect;
145+
lastConnToggleMs = millis();
146+
}
147+
63148
/* The following code keeps on running connection workflows on our
64149
* ConnectionHandler object, hence allowing reconnection in case of failure
65150
* and notification of connect/disconnect event if enabled (see
@@ -68,7 +153,6 @@ void loop() {
68153
* which might not guarantee the correct functioning of the ConnectionHandler
69154
* object.
70155
*/
71-
72156
conMan.check();
73157
}
74158

‎examples/ConnectionHandlerDemo/arduino_secrets.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// Required for WiFiConnectionHandler
2-
const char SECRET_SSID[] = "NETWORK NAME";
3-
const char SECRET_PASS[] = "NETWORK PASSWORD";
2+
const char SECRET_WIFI_SSID[] = "NETWORK NAME";
3+
const char SECRET_WIFI_PASS[] = "NETWORK PASSWORD";
44

55
// Required for GSMConnectionHandler
6-
const char SECRET_APN[] = "MOBILE PROVIDER APN ADDRESS";
7-
const char SECRET_PIN[] = "0000"; // Required for NBConnectionHandler
8-
const char SECRET_GSM_USER[] = "GSM USERNAME";
9-
const char SECRET_GSM_PASS[] = "GSM PASSWORD";
6+
const char SECRET_APN[] = "MOBILE PROVIDER APN ADDRESS";
7+
const char SECRET_PIN[] = "0000"; // Required for NBConnectionHandler
8+
const char SECRET_GSM_USER[] = "GSM USERNAME";
9+
const char SECRET_GSM_PASS[] = "GSM PASSWORD";
1010

1111
// Required for LoRaConnectionHandler
1212
const char SECRET_APP_EUI[] = "APP_EUI";

‎library.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name=Arduino_ConnectionHandler
22
version=0.9.0
33
author=Ubi de Feo, Cristian Maglie, Andrea Catozzi, Alexander Entinger et al.
44
maintainer=Arduino <info@arduino.cc>
5-
sentence=Arduino Library for network connection management (WiFi, GSM, NB, [Ethernet])
5+
sentence=Arduino Library for network connection management (WiFi, GSM, NB, [Ethernet], Notecard)
66
paragraph=Originally part of ArduinoIoTCloud
77
category=Communication
88
url=https://github.com/arduino-libraries/Arduino_ConnectionHandler
9-
architectures=samd,esp32,esp8266,mbed,megaavr,mbed_nano,mbed_portenta,mbed_nicla,mbed_opta,mbed_giga,renesas_portenta,renesas_uno,mbed_edge
10-
depends=Arduino_DebugUtils, WiFi101, WiFiNINA, MKRGSM, MKRNB, MKRWAN
9+
architectures=samd,esp32,esp8266,mbed,megaavr,mbed_nano,mbed_portenta,mbed_nicla,mbed_opta,mbed_giga,renesas_portenta,renesas_uno,mbed_edge,stm32
10+
depends=Arduino_DebugUtils, WiFi101, WiFiNINA, MKRGSM, MKRNB, MKRWAN, Blues Wireless Notecard (>=1.6.0)

‎src/Arduino_ConnectionHandler.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
#include <Arduino.h>
3030
#include "Arduino_ConnectionHandlerDefinitions.h"
3131

32+
#if defined(USE_NOTECARD)
33+
#include "Arduino_NotecardConnectionHandler.h"
34+
#else
35+
3236
#if defined(BOARD_HAS_WIFI)
3337
#include "Arduino_WiFiConnectionHandler.h"
3438
#endif
@@ -57,4 +61,6 @@
5761
#include "Arduino_CellularConnectionHandler.h"
5862
#endif
5963

64+
#endif // USE_NOTECARD
65+
6066
#endif /* CONNECTION_HANDLER_H_ */

‎src/Arduino_ConnectionHandlerDefinitions.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,16 @@
2121
INCLUDES
2222
******************************************************************************/
2323

24+
#if defined __has_include
25+
#if __has_include (<Notecard.h>)
26+
#define USE_NOTECARD
27+
#endif
28+
#endif
29+
2430
#include <Arduino.h>
2531

32+
#ifndef USE_NOTECARD
33+
2634
#ifdef ARDUINO_SAMD_MKR1000
2735
#define BOARD_HAS_WIFI
2836
#define NETWORK_HARDWARE_ERROR WL_NO_SHIELD
@@ -136,6 +144,8 @@
136144
#define NETWORK_HARDWARE_ERROR
137145
#endif
138146

147+
#endif // USE_NOTECARD
148+
139149
/******************************************************************************
140150
TYPEDEFS
141151
******************************************************************************/
@@ -163,7 +173,8 @@ enum class NetworkAdapter {
163173
GSM,
164174
LORA,
165175
CATM1,
166-
CELL
176+
CELL,
177+
NOTECARD
167178
};
168179

169180
/******************************************************************************
@@ -173,7 +184,7 @@ enum class NetworkAdapter {
173184
static unsigned int const CHECK_INTERVAL_TABLE[] =
174185
{
175186
/* INIT */ 100,
176-
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
187+
#if defined(USE_NOTECARD) || defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
177188
/* CONNECTING */ 4000,
178189
#else
179190
/* CONNECTING */ 500,

‎src/Arduino_ConnectionHandlerInterface.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,17 @@ class ConnectionHandler {
4848

4949
NetworkConnectionState check();
5050

51-
#if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET) || defined(BOARD_HAS_CATM1_NBIOT)
51+
#if not defined(BOARD_HAS_LORA)
5252
virtual unsigned long getTime() = 0;
53-
virtual Client &getClient() = 0;
54-
virtual UDP &getUDP() = 0;
5553
#endif
5654

57-
#if defined(BOARD_HAS_LORA)
58-
virtual int write(const uint8_t *buf, size_t size) = 0;
59-
virtual int read() = 0;
55+
#if defined(USE_NOTECARD) || defined(BOARD_HAS_LORA)
6056
virtual bool available() = 0;
57+
virtual int read() = 0;
58+
virtual int write(const uint8_t *buf, size_t size) = 0;
59+
#else
60+
virtual Client &getClient() = 0;
61+
virtual UDP &getUDP() = 0;
6162
#endif
6263

6364
NetworkConnectionState getStatus() __attribute__((deprecated)) {
@@ -87,7 +88,6 @@ class ConnectionHandler {
8788
virtual NetworkConnectionState update_handleDisconnecting() = 0;
8889
virtual NetworkConnectionState update_handleDisconnected () = 0;
8990

90-
9191
private:
9292

9393
unsigned long _lastConnectionTickTime;

‎src/Arduino_NotecardConnectionHandler.cpp

Lines changed: 887 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
/*
2+
This file is part of ArduinoIoTCloud.
3+
4+
Copyright 2024 Blues (http://www.blues.com/)
5+
6+
This software is released under the GNU General Public License version 3,
7+
which covers the main part of arduino-cli.
8+
The terms of this license can be found at:
9+
https://www.gnu.org/licenses/gpl-3.0.en.html
10+
11+
You can be released from the requirements of the above licenses by purchasing
12+
a commercial license. Buying such a license is mandatory if you want to
13+
modify or otherwise use the software for commercial activities involving the
14+
Arduino software without disclosing the source code of your own applications.
15+
To purchase a commercial license, send an email to license@arduino.cc.
16+
*/
17+
18+
#ifndef ARDUINO_NOTECARD_CONNECTION_HANDLER_H_
19+
#define ARDUINO_NOTECARD_CONNECTION_HANDLER_H_
20+
21+
/******************************************************************************
22+
INCLUDE
23+
******************************************************************************/
24+
25+
#include <stdint.h>
26+
27+
#include <Arduino.h>
28+
#include <Notecard.h>
29+
#include <Wire.h>
30+
31+
#include "Arduino_ConnectionHandlerInterface.h"
32+
33+
/******************************************************************************
34+
DEFINES
35+
******************************************************************************/
36+
37+
#define NOTECARD_CONNECTION_HANDLER_VERSION_MAJOR 1
38+
#define NOTECARD_CONNECTION_HANDLER_VERSION_MINOR 0
39+
#define NOTECARD_CONNECTION_HANDLER_VERSION_PATCH 0
40+
41+
#define NOTECARD_CONNECTION_HANDLER_VERSION NOTE_C_STRINGIZE(NOTECARD_CONNECTION_HANDLER_VERSION_MAJOR) "." NOTE_C_STRINGIZE(NOTECARD_CONNECTION_HANDLER_VERSION_MINOR) "." NOTE_C_STRINGIZE(NOTECARD_CONNECTION_HANDLER_VERSION_PATCH)
42+
43+
/******************************************************************************
44+
CLASS DECLARATION
45+
******************************************************************************/
46+
47+
/**
48+
* @brief The NotecardConnectionHandler class
49+
*
50+
* The NotecardConnectionHandler class is a concrete implementation of the
51+
* ConnectionHandler interface that provides connectivity to the Arduino IoT
52+
* Cloud using a Notecard.
53+
*/
54+
class NotecardConnectionHandler final : public ConnectionHandler
55+
{
56+
public:
57+
/**
58+
* @brief The type of topic to be used for R/W operations
59+
*
60+
* The Notecard uses topics to identify the target of a read or write
61+
* operation. The TopicType enum defines the valid types of topics.
62+
*
63+
* @par
64+
* - Command - used to interact with the Arduino IoT Cloud.
65+
* - Thing - used to send application data to the Arduino IoT Cloud.
66+
*/
67+
enum class TopicType : uint8_t {
68+
Invalid = 0,
69+
Command,
70+
Thing,
71+
};
72+
73+
/**
74+
* @brief The error codes for communicating with the Notecard
75+
*
76+
* The NotecardCommunicationError enum defines the error codes that can be
77+
* returned by the NotecardConnectionHandler class.
78+
*
79+
* @par
80+
* - NOTECARD_ERROR_NONE - No error occurred.
81+
* - NOTECARD_ERROR_NO_DATA_AVAILABLE - No data is available.
82+
* - NOTECARD_ERROR_GENERIC - A generic error occurred.
83+
* - HOST_ERROR_OUT_OF_MEMORY - The host is out of memory.
84+
*/
85+
typedef enum {
86+
NOTECARD_ERROR_NONE = 0,
87+
NOTECARD_ERROR_NO_DATA_AVAILABLE = -1,
88+
NOTECARD_ERROR_GENERIC = -2,
89+
HOST_ERROR_OUT_OF_MEMORY = -3,
90+
} NotecardCommunicationError;
91+
92+
/**
93+
* @brief The default timeout for the Notecard to connect to Notehub
94+
*/
95+
static const uint32_t NOTEHUB_CONN_TIMEOUT_MS = 185000;
96+
97+
/**
98+
* @brief The I2C constructor for the Notecard
99+
*
100+
* @param project_uid[in] The project UID of the related Notehub account
101+
* @param keep_alive[in] Keep the connection alive if connection to Notehub drops
102+
* @param i2c_address[in] The I2C address of the Notecard
103+
* @param i2c_max[in] The maximum I2C transaction size (MTU)
104+
* @param wire[in] The I2C bus to use
105+
*/
106+
NotecardConnectionHandler(
107+
const String & project_uid,
108+
bool keep_alive = true,
109+
uint32_t i2c_address = NOTE_I2C_ADDR_DEFAULT,
110+
uint32_t i2c_max = NOTE_I2C_MAX_DEFAULT,
111+
TwoWire & wire = Wire
112+
);
113+
114+
/**
115+
* @brief The UART constructor for the Notecard
116+
*
117+
* @param project_uid[in] The project UID of the related Notehub account
118+
* @param serial[in] The serial port to use
119+
* @param keep_alive[in] Keep the connection alive if connection to Notehub drops
120+
* @param baud[in] The baud rate of the serial port
121+
*/
122+
NotecardConnectionHandler(
123+
const String & project_uid,
124+
HardwareSerial & serial,
125+
bool keep_alive = true,
126+
uint32_t baud = 9600
127+
);
128+
129+
/**
130+
* @brief Disable hardware interrupts
131+
*
132+
* When hardware interrupts are disabled, the `NotecardConnectionHandler`
133+
* must be polled for incoming data. This is necessary when the host
134+
* microcontroller is unable to use the ATTN pin of the Notecard.
135+
*/
136+
inline void disableHardwareInterrupts (void) {
137+
_en_hw_int = false;
138+
}
139+
140+
/**
141+
* @brief Enable hardware interrupts
142+
*
143+
* Hardware interrupts allow the `NotecardConnectionHandler` to leverage the
144+
* ATTN pin of the Notecard. This improves the responsiveness of the
145+
* `NotecardConnectionHandler` by eliminating the need for the host
146+
* microcontroller to poll the Notecard for incoming data.
147+
*/
148+
inline void enableHardwareInterrupts (void) {
149+
_en_hw_int = true;
150+
}
151+
152+
/**
153+
* @brief Get the Arduino IoT Cloud Device ID
154+
*
155+
* @return The Arduino IoT Cloud Device ID
156+
*/
157+
inline const String & getDeviceId(void) {
158+
check(); // Ensure the connection to the Notecard is initialized
159+
return _device_id;
160+
}
161+
162+
/**
163+
* @brief Get the Notecard Device ID
164+
*
165+
* @return The Notecard Device ID
166+
*/
167+
inline const String & getNotecardUid(void) {
168+
check(); // Ensure the connection to the Notecard is initialized
169+
return _notecard_uid;
170+
}
171+
172+
/**
173+
* @brief Get the topic type for R/W operations
174+
*
175+
* @return The current topic type
176+
*/
177+
TopicType getTopicType(void) const {
178+
return _topic_type;
179+
}
180+
181+
/**
182+
* @brief Set the topic type for R/W operations
183+
*
184+
* @param topic[in] The topic type to set
185+
* @par
186+
* - TopicType::Command - used to interact with the Arduino IoT Cloud.
187+
* - TopicType::Thing - used to send application data to the Arduino IoT Cloud.
188+
*
189+
* @see TopicType
190+
*/
191+
void setTopicType(TopicType topic) {
192+
_topic_type = topic;
193+
}
194+
195+
/**
196+
* @brief Set the WiFi credentials to be used by the Notecard
197+
*
198+
* @param ssid[in] The SSID of the WiFi network
199+
* @param pass[in] The password of the WiFi network
200+
*
201+
* @return 0 if successful, otherwise an error code
202+
*
203+
* @note This method is only applicable when using a Wi-Fi capable Notecard,
204+
* and is unnecessary when using a Notecard with cellular connectivity.
205+
* If the Notecard is not Wi-Fi capable, this method will be a no-op.
206+
*
207+
* @see NotecardCommunicationError
208+
*/
209+
int setWiFiCredentials (const String & ssid, const String & pass);
210+
211+
// ConnectionHandler interface
212+
virtual bool available() override;
213+
virtual unsigned long getTime() override;
214+
virtual int read() override;
215+
virtual int write(const uint8_t *buf, size_t size) override;
216+
217+
protected:
218+
219+
virtual NetworkConnectionState update_handleInit () override;
220+
virtual NetworkConnectionState update_handleConnecting () override;
221+
virtual NetworkConnectionState update_handleConnected () override;
222+
virtual NetworkConnectionState update_handleDisconnecting() override;
223+
virtual NetworkConnectionState update_handleDisconnected () override;
224+
225+
private:
226+
227+
// Private members
228+
Notecard _notecard;
229+
//TODO: Remove if not needed
230+
String _device_id;
231+
String _notecard_uid;
232+
String _project_uid;
233+
HardwareSerial * _serial;
234+
TwoWire * _wire;
235+
uint8_t * _inbound_buffer;
236+
uint32_t _conn_start_ms;
237+
uint32_t _i2c_address;
238+
uint32_t _i2c_max;
239+
uint32_t _inbound_buffer_index;
240+
uint32_t _inbound_buffer_size;
241+
uint32_t _uart_baud;
242+
bool _en_hw_int;
243+
TopicType _topic_type;
244+
245+
// Private methods
246+
bool armInterrupt (void) const;
247+
bool configureConnection (bool connect) const;
248+
uint_fast8_t connected (void) const;
249+
J * getNote (bool pop = false) const;
250+
int initiateNotehubSync (void) const;
251+
bool updateUidCache (void);
252+
};
253+
254+
#endif /* ARDUINO_NOTECARD_CONNECTION_HANDLER_H_ */

0 commit comments

Comments
 (0)
Please sign in to comment.