Skip to content

Commit 04af28b

Browse files
authored
Merge pull request #81 from pennam/portenta-eth
Add Ethernet support for Portenta + Ethernet Shield
2 parents 884878d + 4e68980 commit 04af28b

11 files changed

+255
-9
lines changed

Diff for: examples/ConnectionHandlerDemo/ConnectionHandlerDemo.ino

+14-1
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,26 @@
1414
* If using a MKR NB1500 you'll need a NBConnectionHandler object as follows
1515
*
1616
* NBConnectionHandler conMan(SECRET_PIN);
17+
*
18+
* If using a Portenta + Ethernet shield you'll need a EthernetConnectionHandler object as follows:
19+
*
20+
* DHCP mode
21+
* EthernetConnectionHandler conMan();
22+
*
23+
* Manual configuration
24+
* EthernetConnectionHandler conMan(SECRET_IP, SECRET_DNS, SECRET_GATEWAY, SECRET_NETMASK);
25+
*
26+
* Manual configuration will fallback on DHCP mode if SECRET_IP is invalid or equal to INADDR_NONE.
27+
*
1728
*/
1829

1930
#include "arduino_secrets.h"
2031

2132
#include <Arduino_ConnectionHandler.h>
2233

23-
#if defined(BOARD_HAS_WIFI)
34+
#if defined(BOARD_HAS_ETHERNET)
35+
EthernetConnectionHandler conMan(SECRET_IP, SECRET_DNS, SECRET_GATEWAY, SECRET_NETMASK);
36+
#elif defined(BOARD_HAS_WIFI)
2437
WiFiConnectionHandler conMan(SECRET_SSID, SECRET_PASS);
2538
#elif defined(BOARD_HAS_GSM)
2639
GSMConnectionHandler conMan(SECRET_APN, SECRET_PIN, SECRET_GSM_USER, SECRET_GSM_PASS);

Diff for: examples/ConnectionHandlerDemo/arduino_secrets.h

+5
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@ const char SECRET_GSM_PASS[] = "GSM PASSWORD";
88

99
const char SECRET_APP_EUI[] = "APP_EUI";
1010
const char SECRET_APP_KEY[] = "APP_KEY";
11+
12+
const char SECRET_IP[] = "IP ADDRESS";
13+
const char SECRET_DNS[] = "DNS ADDRESS";
14+
const char SECRET_GATEWAY[] = "GATEWAY ADDRESS";
15+
const char SECRET_NETMASK[] = "NETWORK MASK";

Diff for: keywords.txt

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ WiFiConnectionHandler KEYWORD1
1010
GSMConnectionHandler KEYWORD1
1111
NBConnectionHandler KEYWORD1
1212
LoRaConnectionHandler KEYWORD1
13+
EthernetConnectionHandler KEYWORD1
1314

1415
####################################################
1516
# Methods and Functions (KEYWORD2)

Diff for: src/Arduino_ConnectionHandler.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525
CONSTRUCTOR/DESTRUCTOR
2626
******************************************************************************/
2727

28-
ConnectionHandler::ConnectionHandler(bool const keep_alive)
28+
ConnectionHandler::ConnectionHandler(bool const keep_alive, NetworkAdapter interface)
2929
: _keep_alive{keep_alive}
30+
, _interface{interface}
3031
, _lastConnectionTickTime{millis()}
3132
, _current_net_connection_state{NetworkConnectionState::INIT}
3233
{

Diff for: src/Arduino_ConnectionHandler.h

+34-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,21 @@
4343
#define WIFI_FIRMWARE_VERSION_REQUIRED WIFI_FIRMWARE_LATEST_VERSION
4444
#endif
4545

46-
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION)
46+
#if defined(ARDUINO_PORTENTA_H7_M7)
47+
#include <WiFi.h>
48+
#include <WiFiUdp.h>
49+
#include <Ethernet.h>
50+
#include <PortentaEthernet.h>
51+
52+
#define BOARD_HAS_WIFI
53+
#define BOARD_HAS_ETHERNET
54+
#define BOARD_HAS_PORTENTA_VISION_SHIELD_ETHERNET
55+
#define NETWORK_HARDWARE_ERROR WL_NO_SHIELD
56+
#define NETWORK_IDLE_STATUS WL_IDLE_STATUS
57+
#define NETWORK_CONNECTED WL_CONNECTED
58+
#endif
59+
60+
#if defined(ARDUINO_NICLA_VISION)
4761
#include <WiFi.h>
4862
#include <WiFiUdp.h>
4963

@@ -124,6 +138,14 @@ enum class NetworkConnectionEvent {
124138
ERROR
125139
};
126140

141+
enum class NetworkAdapter {
142+
WIFI,
143+
ETHERNET,
144+
NB,
145+
GSM,
146+
LORA
147+
};
148+
127149
typedef void (*OnNetworkEventCallback)();
128150

129151
/******************************************************************************
@@ -148,12 +170,12 @@ static unsigned int const CHECK_INTERVAL_TABLE[] =
148170
class ConnectionHandler {
149171
public:
150172

151-
ConnectionHandler(bool const keep_alive);
173+
ConnectionHandler(bool const keep_alive, NetworkAdapter interface);
152174

153175

154176
NetworkConnectionState check();
155177

156-
#if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB)
178+
#if defined(BOARD_HAS_WIFI) || defined(BOARD_HAS_GSM) || defined(BOARD_HAS_NB) || defined(BOARD_HAS_ETHERNET)
157179
virtual unsigned long getTime() = 0;
158180
virtual Client &getClient() = 0;
159181
virtual UDP &getUDP() = 0;
@@ -169,6 +191,10 @@ class ConnectionHandler {
169191
return _current_net_connection_state;
170192
}
171193

194+
NetworkAdapter getInterface() {
195+
return _interface;
196+
}
197+
172198
void connect();
173199
void disconnect();
174200

@@ -180,6 +206,7 @@ class ConnectionHandler {
180206
protected:
181207

182208
bool _keep_alive;
209+
NetworkAdapter _interface;
183210

184211
virtual NetworkConnectionState update_handleInit () = 0;
185212
virtual NetworkConnectionState update_handleConnecting () = 0;
@@ -207,4 +234,8 @@ class ConnectionHandler {
207234
#include "Arduino_LoRaConnectionHandler.h"
208235
#endif
209236

237+
#if defined(BOARD_HAS_ETHERNET)
238+
#include "Arduino_EthernetConnectionHandler.h"
239+
#endif
240+
210241
#endif /* CONNECTION_HANDLER_H_ */

Diff for: src/Arduino_EthernetConnectionHandler.cpp

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
This file is part of ArduinoIoTCloud.
3+
Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
This software is released under the GNU General Public License version 3,
5+
which covers the main part of arduino-cli.
6+
The terms of this license can be found at:
7+
https://www.gnu.org/licenses/gpl-3.0.en.html
8+
You can be released from the requirements of the above licenses by purchasing
9+
a commercial license. Buying such a license is mandatory if you want to modify or
10+
otherwise use the software for commercial activities involving the Arduino
11+
software without disclosing the source code of your own applications. To purchase
12+
a commercial license, send an email to [email protected].
13+
*/
14+
15+
/******************************************************************************
16+
INCLUDE
17+
******************************************************************************/
18+
19+
#include "Arduino_EthernetConnectionHandler.h"
20+
21+
#ifdef BOARD_HAS_ETHERNET /* Only compile if the board has ethernet */
22+
23+
/******************************************************************************
24+
CTOR/DTOR
25+
******************************************************************************/
26+
27+
EthernetConnectionHandler::EthernetConnectionHandler(bool const keep_alive)
28+
: ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET}
29+
,_ip{INADDR_NONE}
30+
,_dns{INADDR_NONE}
31+
,_gateway{INADDR_NONE}
32+
,_netmask{INADDR_NONE}
33+
{
34+
35+
}
36+
37+
EthernetConnectionHandler::EthernetConnectionHandler(const IPAddress ip, const IPAddress dns, const IPAddress gateway, const IPAddress netmask, bool const keep_alive)
38+
: ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET}
39+
,_ip{ip}
40+
,_dns{dns}
41+
,_gateway{gateway}
42+
,_netmask{netmask}
43+
{
44+
45+
}
46+
47+
EthernetConnectionHandler::EthernetConnectionHandler(const char * ip, const char * dns, const char * gateway, const char * netmask, bool const keep_alive)
48+
: ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET}
49+
,_ip{INADDR_NONE}
50+
,_dns{INADDR_NONE}
51+
,_gateway{INADDR_NONE}
52+
,_netmask{INADDR_NONE}
53+
{
54+
if(!_ip.fromString(ip)) {
55+
_ip = INADDR_NONE;
56+
}
57+
if(!_dns.fromString(dns)) {
58+
_dns = INADDR_NONE;
59+
}
60+
if(!_gateway.fromString(gateway)) {
61+
_gateway = INADDR_NONE;
62+
}
63+
if(!_netmask.fromString(netmask)) {
64+
_netmask = INADDR_NONE;
65+
}
66+
}
67+
68+
/******************************************************************************
69+
PROTECTED MEMBER FUNCTIONS
70+
******************************************************************************/
71+
72+
NetworkConnectionState EthernetConnectionHandler::update_handleInit()
73+
{
74+
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
75+
Debug.print(DBG_ERROR, F("Error, ethernet shield was not found."));
76+
return NetworkConnectionState::ERROR;
77+
}
78+
return NetworkConnectionState::CONNECTING;
79+
}
80+
81+
NetworkConnectionState EthernetConnectionHandler::update_handleConnecting()
82+
{
83+
if (_ip != INADDR_NONE) {
84+
if (Ethernet.begin(nullptr, _ip, _dns, _gateway, _netmask, 15000, 4000) == 0) {
85+
Debug.print(DBG_ERROR, F("Failed to configure Ethernet, check cable connection"));
86+
return NetworkConnectionState::CONNECTING;
87+
}
88+
} else {
89+
if (Ethernet.begin(nullptr, 15000, 4000) == 0) {
90+
Debug.print(DBG_ERROR, F("Waiting Ethernet configuration from DHCP server, check cable connection"));
91+
return NetworkConnectionState::CONNECTING;
92+
}
93+
}
94+
95+
return NetworkConnectionState::CONNECTED;
96+
}
97+
98+
NetworkConnectionState EthernetConnectionHandler::update_handleConnected()
99+
{
100+
if (Ethernet.linkStatus() == LinkOFF) {
101+
Debug.print(DBG_ERROR, F("Ethernet link OFF, connection lost."));
102+
if (_keep_alive)
103+
{
104+
Debug.print(DBG_ERROR, F("Attempting reconnection"));
105+
}
106+
return NetworkConnectionState::DISCONNECTED;
107+
}
108+
return NetworkConnectionState::CONNECTED;
109+
}
110+
111+
NetworkConnectionState EthernetConnectionHandler::update_handleDisconnecting()
112+
{
113+
Ethernet.disconnect();
114+
return NetworkConnectionState::DISCONNECTED;
115+
}
116+
117+
NetworkConnectionState EthernetConnectionHandler::update_handleDisconnected()
118+
{
119+
if (_keep_alive)
120+
{
121+
return NetworkConnectionState::INIT;
122+
}
123+
else
124+
{
125+
return NetworkConnectionState::CLOSED;
126+
}
127+
}
128+
129+
#endif /* #ifdef BOARD_HAS_ETHERNET */

Diff for: src/Arduino_EthernetConnectionHandler.h

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
This file is part of ArduinoIoTCloud.
3+
Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
This software is released under the GNU General Public License version 3,
5+
which covers the main part of arduino-cli.
6+
The terms of this license can be found at:
7+
https://www.gnu.org/licenses/gpl-3.0.en.html
8+
You can be released from the requirements of the above licenses by purchasing
9+
a commercial license. Buying such a license is mandatory if you want to modify or
10+
otherwise use the software for commercial activities involving the Arduino
11+
software without disclosing the source code of your own applications. To purchase
12+
a commercial license, send an email to [email protected].
13+
*/
14+
15+
#ifndef ARDUINO_ETHERNET_CONNECTION_HANDLER_H_
16+
#define ARDUINO_ETHERNET_CONNECTION_HANDLER_H_
17+
18+
/******************************************************************************
19+
INCLUDE
20+
******************************************************************************/
21+
22+
#include "Arduino_ConnectionHandler.h"
23+
24+
#ifdef BOARD_HAS_ETHERNET /* Only compile if the board has ethernet */
25+
26+
/******************************************************************************
27+
CLASS DECLARATION
28+
******************************************************************************/
29+
30+
class EthernetConnectionHandler : public ConnectionHandler
31+
{
32+
public:
33+
34+
EthernetConnectionHandler(bool const keep_alive = true);
35+
EthernetConnectionHandler(const IPAddress ip, const IPAddress dns, const IPAddress gateway, const IPAddress netmask, bool const keep_alive = true);
36+
EthernetConnectionHandler(const char * ip, const char * dns, const char * gateway, const char * netmask, bool const keep_alive = true);
37+
38+
39+
virtual unsigned long getTime() override { return 0; }
40+
virtual Client & getClient() override{ return _eth_client; }
41+
virtual UDP & getUDP() override { return _eth_udp; }
42+
43+
44+
protected:
45+
46+
virtual NetworkConnectionState update_handleInit () override;
47+
virtual NetworkConnectionState update_handleConnecting () override;
48+
virtual NetworkConnectionState update_handleConnected () override;
49+
virtual NetworkConnectionState update_handleDisconnecting() override;
50+
virtual NetworkConnectionState update_handleDisconnected () override;
51+
52+
private:
53+
54+
IPAddress _ip;
55+
IPAddress _dns;
56+
IPAddress _gateway;
57+
IPAddress _netmask;
58+
59+
EthernetUDP _eth_udp;
60+
EthernetClient _eth_client;
61+
62+
};
63+
64+
#endif /* #ifdef BOARD_HAS_ETHERNET */
65+
66+
#endif /* ARDUINO_ETHERNET_CONNECTION_HANDLER_H_ */

Diff for: src/Arduino_GSMConnectionHandler.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ __attribute__((weak)) void mkr_gsm_feed_watchdog()
4747
******************************************************************************/
4848

4949
GSMConnectionHandler::GSMConnectionHandler(const char * pin, const char * apn, const char * login, const char * pass, bool const keep_alive)
50-
: ConnectionHandler{keep_alive}
50+
: ConnectionHandler{keep_alive, NetworkAdapter::GSM}
5151
, _pin(pin)
5252
, _apn(apn)
5353
, _login(login)

Diff for: src/Arduino_LoRaConnectionHandler.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ typedef enum
4444
CTOR/DTOR
4545
******************************************************************************/
4646
LoRaConnectionHandler::LoRaConnectionHandler(char const * appeui, char const * appkey, _lora_band const band, char const * channelMask, _lora_class const device_class)
47-
: ConnectionHandler{false}
47+
: ConnectionHandler{false, NetworkAdapter::LORA}
4848
, _appeui(appeui)
4949
, _appkey(appkey)
5050
, _band(band)

Diff for: src/Arduino_NBConnectionHandler.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ NBConnectionHandler::NBConnectionHandler(char const * pin, char const * apn, boo
5757
}
5858

5959
NBConnectionHandler::NBConnectionHandler(char const * pin, char const * apn, char const * login, char const * pass, bool const keep_alive)
60-
: ConnectionHandler{keep_alive}
60+
: ConnectionHandler{keep_alive, NetworkAdapter::NB}
6161
, _pin(pin)
6262
, _apn(apn)
6363
, _login(login)

Diff for: src/Arduino_WiFiConnectionHandler.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
******************************************************************************/
2929

3030
WiFiConnectionHandler::WiFiConnectionHandler(char const * ssid, char const * pass, bool const keep_alive)
31-
: ConnectionHandler{keep_alive}
31+
: ConnectionHandler{keep_alive, NetworkAdapter::WIFI}
3232
, _ssid{ssid}
3333
, _pass{pass}
3434
{

0 commit comments

Comments
 (0)