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 a6fd3e2

Browse files
committedJul 26, 2024·
feat: NotecardConnectionManager
1 parent f39958c commit a6fd3e2

10 files changed

+1232
-22
lines changed
 

‎.gitignore

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

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Library for handling and managing network connections by providing keep-alive fu
2020
#if defined(BOARD_HAS_ETHERNET)
2121
EthernetConnectionHandler conMan;
2222
#elif defined(BOARD_HAS_WIFI)
23-
WiFiConnectionHandler conMan("SECRET_SSID", "SECRET_PASS");
23+
WiFiConnectionHandler conMan("SECRET_WIFI_SSID", "SECRET_WIFI_PASS");
2424
#elif defined(BOARD_HAS_GSM)
2525
GSMConnectionHandler conMan("SECRET_PIN", "SECRET_APN", "SECRET_GSM_LOGIN", "SECRET_GSM_PASS");
2626
#elif defined(BOARD_HAS_NB)

‎examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
/* SECRET_ fields are in arduino_secrets.h included above
2-
* if using a WiFi board (Arduino MKR1000, MKR WiFi 1010, Nano 33 IoT, UNO
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
@@ -31,10 +42,25 @@
3142

3243
#include <Arduino_ConnectionHandler.h>
3344

34-
#if defined(BOARD_HAS_ETHERNET)
45+
#if !(defined(USE_NOTECARD) || defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_LORA) || \
46+
defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET) || defined(BOARD_HAS_CATM1_NBIOT))
47+
#error "Please check Arduino Connection Handler supported boards list: https://github.com/arduino-libraries/Arduino_ConnectionHandler/blob/master/README.md"
48+
#endif
49+
50+
#if defined(USE_NOTECARD)
51+
/* The Notecard can provide connectivity to almost any board via ESLOV (I2C)
52+
* or UART. An empty string (or the default value provided below) will not
53+
* override the Notecard's existing configuration.
54+
* Learn more at: https://dev.blues.io */
55+
#define NOTECARD_PRODUCT_UID "com.domain.you:product"
56+
#endif
57+
58+
#if defined(USE_NOTECARD)
59+
NotecardConnectionHandler conMan(NOTECARD_PRODUCT_UID);
60+
#elif defined(BOARD_HAS_ETHERNET)
3561
EthernetConnectionHandler conMan(SECRET_IP, SECRET_DNS, SECRET_GATEWAY, SECRET_NETMASK);
3662
#elif defined(BOARD_HAS_WIFI)
37-
WiFiConnectionHandler conMan(SECRET_SSID, SECRET_PASS);
63+
WiFiConnectionHandler conMan(SECRET_WIFI_SSID, SECRET_WIFI_PASS);
3864
#elif defined(BOARD_HAS_GSM)
3965
GSMConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET_GSM_PASS);
4066
#elif defined(BOARD_HAS_NB)
@@ -48,9 +74,9 @@ CellularConnectionHandler conMan(SECRET_PIN, SECRET_APN, SECRET_GSM_USER, SECRET
4874
#endif
4975

5076
void setup() {
77+
/* Initialize serial and wait up to 5 seconds for port to open */
5178
Serial.begin(9600);
52-
/* Give a few seconds for the Serial connection to be available */
53-
delay(4000);
79+
for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { }
5480
#ifndef __AVR__
5581
setDebugMessageLevel(DBG_INFO);
5682
#endif

‎examples/ConnectionHandlerDemo/arduino_secrets.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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
66
const char SECRET_APN[] = "MOBILE PROVIDER APN ADDRESS";

‎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: 12 additions & 1 deletion
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
/******************************************************************************

‎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: 1015 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
This file is part of ArduinoIoTCloud.
3+
4+
Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
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 modify or
13+
otherwise use the software for commercial activities involving the Arduino
14+
software without disclosing the source code of your own applications. To purchase
15+
a commercial license, send an email to license@arduino.cc.
16+
*/
17+
18+
//TODO: Understand how `_keep_alive` is updated and used in the `ConnectionHandler` class
19+
20+
#ifndef ARDUINO_NOTECARD_CONNECTION_HANDLER_H_
21+
#define ARDUINO_NOTECARD_CONNECTION_HANDLER_H_
22+
23+
/******************************************************************************
24+
INCLUDE
25+
******************************************************************************/
26+
27+
#if defined(USE_NOTECARD) /* Only compile if the Notecard is present */
28+
29+
#include <stdint.h>
30+
#include <Notecard.h>
31+
32+
#include "Arduino_ConnectionHandlerInterface.h"
33+
34+
/******************************************************************************
35+
CLASS DECLARATION
36+
******************************************************************************/
37+
38+
class NotecardConnectionHandler final : public ConnectionHandler
39+
{
40+
public:
41+
enum class TopicType : uint8_t {
42+
Invalid = 0,
43+
Command,
44+
Thing,
45+
Notehub = 255
46+
};
47+
48+
typedef enum {
49+
NOTECARD_ERROR_NONE = 0,
50+
NOTECARD_ERROR_NO_DATA_AVAILABLE = -1,
51+
NOTECARD_ERROR_GENERIC = -2,
52+
HOST_ERROR_OUT_OF_MEMORY = -3,
53+
} NotecardCommunicationError;
54+
55+
static const uint32_t NOTEHUB_CONN_TIMEOUT_MS = 185000;
56+
57+
NotecardConnectionHandler(
58+
const String & project_uid,
59+
bool en_hw_int = false,
60+
bool keep_alive = true,
61+
uint32_t i2c_address = NOTE_I2C_ADDR_DEFAULT,
62+
uint32_t i2c_max = NOTE_I2C_MAX_DEFAULT,
63+
TwoWire & wire = Wire,
64+
const String & notehub_url = ""
65+
);
66+
67+
NotecardConnectionHandler(
68+
const String & project_uid,
69+
HardwareSerial & serial,
70+
uint32_t speed = 9600,
71+
bool en_hw_int = false,
72+
bool keep_alive = true,
73+
const String & notehub_url = ""
74+
);
75+
76+
// Notehub Logging
77+
inline int disableNotehubLogging (void) const
78+
{
79+
Debug.print(DBG_INFO, F("Disabling Notehub logging..."));
80+
return notehubLogging(false);
81+
}
82+
inline int enableNotehubLogging (void) const
83+
{
84+
Debug.print(DBG_INFO, F("Enabling Notehub logging..."));
85+
return notehubLogging(true);
86+
}
87+
88+
// Accessors for Unique Hardware Identifiers
89+
const String & getNotecardUid(void) const {
90+
return _notecard_uid;
91+
}
92+
93+
// Identify the target topic for R/W operations
94+
TopicType getTopicType(void) const {
95+
return _topic_type;
96+
}
97+
void setTopicType(TopicType topic) {
98+
_topic_type = topic;
99+
}
100+
101+
int setWiFiCredentials (const String & ssid, const String & pass);
102+
const String & syncArduinoDeviceId (const String & device_id);
103+
int syncSecretDeviceKey (const String & secret_device_key);
104+
105+
// ConnectionHandler interface
106+
virtual bool available() override;
107+
virtual unsigned long getTime() override;
108+
virtual int read() override;
109+
virtual int write(const uint8_t *buf, size_t size) override;
110+
111+
protected:
112+
113+
virtual NetworkConnectionState update_handleInit () override;
114+
virtual NetworkConnectionState update_handleConnecting () override;
115+
virtual NetworkConnectionState update_handleConnected () override;
116+
virtual NetworkConnectionState update_handleDisconnecting() override;
117+
virtual NetworkConnectionState update_handleDisconnected () override;
118+
119+
private:
120+
121+
// Private members
122+
HardwareSerial * _serial;
123+
TwoWire * _wire;
124+
uint8_t * _inbound_buffer;
125+
uint32_t _conn_start_ms;
126+
uint32_t _i2c_address;
127+
uint32_t _i2c_max;
128+
uint32_t _uart_speed;
129+
uint32_t _inbound_buffer_index;
130+
uint32_t _inbound_buffer_size;
131+
bool _en_hw_int;
132+
TopicType _topic_type;
133+
Notecard _notecard;
134+
String _device_id;
135+
String _notecard_uid;
136+
String _notehub_url;
137+
String _project_uid;
138+
139+
// Private methods
140+
bool armInterrupt (void) const;
141+
bool configureConnection (bool connect) const;
142+
uint_fast8_t connected (void) const;
143+
J * getNote (bool pop = false) const;
144+
int initiateNotehubSync (void) const;
145+
int notehubLogging (bool enable) const;
146+
bool updateUidCache (void);
147+
};
148+
149+
#endif /* #ifdef USE_NOTECARD */
150+
151+
#endif /* ARDUINO_NOTECARD_CONNECTION_HANDLER_H_ */

0 commit comments

Comments
 (0)
Please sign in to comment.