-
Notifications
You must be signed in to change notification settings - Fork 37
Add Ethernet support for Portenta + Ethernet Shield #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
7ab21ac
Add EthernetConnectionHandler
pennam d47a687
Enable Ethernet support for Portenta H7. Needs Vision shield
pennam 9cb7f99
Remove MAC address configuration
pennam 37758d3
Add function to get the hardware network interface type used for conn…
pennam a3af901
Handle automatic retries on connect/disconnect
pennam 349cee2
Add CTOR and logic for static IP configuration
pennam 328bd83
Use more detailed define to enable ethernet support
pennam 9b74bb6
Add missing include file <PortentaEthernet.h>
pennam dafc31b
Remove useless call to Ethernet.end()
pennam 8f0b594
Simplify debug prints
pennam a3fee7e
Drop #if !defined(__AVR__) statements
pennam 1026b98
Simplify ethernet support for portenta based boards
pennam 1f3b9a5
Rename parameter and internal variables from subnet to netmask
pennam b53dd95
Add constructor to handle string parameters
pennam 2462ec0
Use const attribute for IPAddress parameters
pennam 4e68980
Extend example adding ethernet connection handler
pennam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
This file is part of ArduinoIoTCloud. | ||
Copyright 2020 ARDUINO SA (http://www.arduino.cc/) | ||
This software is released under the GNU General Public License version 3, | ||
which covers the main part of arduino-cli. | ||
The terms of this license can be found at: | ||
https://www.gnu.org/licenses/gpl-3.0.en.html | ||
You can be released from the requirements of the above licenses by purchasing | ||
a commercial license. Buying such a license is mandatory if you want to modify or | ||
otherwise use the software for commercial activities involving the Arduino | ||
software without disclosing the source code of your own applications. To purchase | ||
a commercial license, send an email to [email protected]. | ||
*/ | ||
|
||
/****************************************************************************** | ||
INCLUDE | ||
******************************************************************************/ | ||
|
||
#include "Arduino_EthernetConnectionHandler.h" | ||
|
||
#ifdef BOARD_HAS_ETHERNET /* Only compile if the board has ethernet */ | ||
|
||
/****************************************************************************** | ||
CTOR/DTOR | ||
******************************************************************************/ | ||
|
||
EthernetConnectionHandler::EthernetConnectionHandler(bool const keep_alive) | ||
: ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET} | ||
,_ip{INADDR_NONE} | ||
,_dns{INADDR_NONE} | ||
,_gateway{INADDR_NONE} | ||
,_subnet{INADDR_NONE} | ||
{ | ||
|
||
} | ||
|
||
EthernetConnectionHandler::EthernetConnectionHandler(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet, bool const keep_alive) | ||
: ConnectionHandler{keep_alive, NetworkAdapter::ETHERNET} | ||
,_ip{ip} | ||
,_dns{dns} | ||
,_gateway{gateway} | ||
,_subnet{subnet} | ||
{ | ||
|
||
} | ||
|
||
/****************************************************************************** | ||
PROTECTED MEMBER FUNCTIONS | ||
******************************************************************************/ | ||
|
||
NetworkConnectionState EthernetConnectionHandler::update_handleInit() | ||
{ | ||
if (Ethernet.hardwareStatus() == EthernetNoHardware) { | ||
#if !defined(__AVR__) | ||
Debug.print(DBG_ERROR, F("Error, ethernet shield was not found.")); | ||
#endif | ||
return NetworkConnectionState::ERROR; | ||
} | ||
return NetworkConnectionState::CONNECTING; | ||
} | ||
|
||
NetworkConnectionState EthernetConnectionHandler::update_handleConnecting() | ||
{ | ||
if (_ip != INADDR_NONE) { | ||
if (Ethernet.begin(nullptr, _ip, _dns, _gateway, _subnet, 15000, 4000) == 0) { | ||
#if !defined(__AVR__) | ||
Debug.print(DBG_ERROR, F("Failed to configure Ethernet, check cable connection")); | ||
#endif | ||
return NetworkConnectionState::CONNECTING; | ||
} | ||
} else { | ||
if (Ethernet.begin(nullptr, 15000, 4000) == 0) { | ||
#if !defined(__AVR__) | ||
Debug.print(DBG_ERROR, F("Waiting Ethernet configuration from DHCP server, check cable connection")); | ||
#endif | ||
return NetworkConnectionState::CONNECTING; | ||
} | ||
} | ||
return NetworkConnectionState::CONNECTED; | ||
} | ||
|
||
NetworkConnectionState EthernetConnectionHandler::update_handleConnected() | ||
{ | ||
if (Ethernet.linkStatus() == LinkOFF) { | ||
#if !defined(__AVR__) | ||
Debug.print(DBG_VERBOSE, F("Ethernet.status(): %d"), Ethernet.status()); | ||
Debug.print(DBG_ERROR, F("Connection lost.")); | ||
#endif | ||
if (_keep_alive) | ||
{ | ||
#if !defined(__AVR__) | ||
Debug.print(DBG_ERROR, F("Attempting reconnection")); | ||
#endif | ||
} | ||
return NetworkConnectionState::DISCONNECTED; | ||
} | ||
return NetworkConnectionState::CONNECTED; | ||
} | ||
|
||
NetworkConnectionState EthernetConnectionHandler::update_handleDisconnecting() | ||
{ | ||
Ethernet.disconnect(); | ||
return NetworkConnectionState::DISCONNECTED; | ||
} | ||
|
||
NetworkConnectionState EthernetConnectionHandler::update_handleDisconnected() | ||
{ | ||
Ethernet.end(); | ||
if (_keep_alive) | ||
{ | ||
return NetworkConnectionState::INIT; | ||
} | ||
else | ||
{ | ||
return NetworkConnectionState::CLOSED; | ||
} | ||
} | ||
|
||
#endif /* #ifdef BOARD_HAS_ETHERNET */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
This file is part of ArduinoIoTCloud. | ||
Copyright 2020 ARDUINO SA (http://www.arduino.cc/) | ||
This software is released under the GNU General Public License version 3, | ||
which covers the main part of arduino-cli. | ||
The terms of this license can be found at: | ||
https://www.gnu.org/licenses/gpl-3.0.en.html | ||
You can be released from the requirements of the above licenses by purchasing | ||
a commercial license. Buying such a license is mandatory if you want to modify or | ||
otherwise use the software for commercial activities involving the Arduino | ||
software without disclosing the source code of your own applications. To purchase | ||
a commercial license, send an email to [email protected]. | ||
*/ | ||
|
||
#ifndef ARDUINO_ETHERNET_CONNECTION_HANDLER_H_ | ||
#define ARDUINO_ETHERNET_CONNECTION_HANDLER_H_ | ||
|
||
/****************************************************************************** | ||
INCLUDE | ||
******************************************************************************/ | ||
|
||
#include "Arduino_ConnectionHandler.h" | ||
|
||
#ifdef BOARD_HAS_ETHERNET /* Only compile if the board has ethernet */ | ||
|
||
/****************************************************************************** | ||
CLASS DECLARATION | ||
******************************************************************************/ | ||
|
||
class EthernetConnectionHandler : public ConnectionHandler | ||
{ | ||
public: | ||
|
||
EthernetConnectionHandler(bool const keep_alive = true); | ||
EthernetConnectionHandler(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet, bool const keep_alive = true); | ||
|
||
|
||
virtual unsigned long getTime() override { return 0; } | ||
virtual Client & getClient() override{ return _eth_client; } | ||
virtual UDP & getUDP() override { return _eth_udp; } | ||
|
||
|
||
protected: | ||
|
||
virtual NetworkConnectionState update_handleInit () override; | ||
virtual NetworkConnectionState update_handleConnecting () override; | ||
virtual NetworkConnectionState update_handleConnected () override; | ||
virtual NetworkConnectionState update_handleDisconnecting() override; | ||
virtual NetworkConnectionState update_handleDisconnected () override; | ||
|
||
private: | ||
|
||
IPAddress _ip; | ||
IPAddress _dns; | ||
IPAddress _gateway; | ||
IPAddress _subnet; | ||
|
||
EthernetUDP _eth_udp; | ||
EthernetClient _eth_client; | ||
|
||
}; | ||
|
||
#endif /* #ifdef BOARD_HAS_ETHERNET */ | ||
|
||
#endif /* ARDUINO_ETHERNET_CONNECTION_HANDLER_H_ */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is not explicitly targeted on this PR. But since AVR is not and will not be supported on Arduino IoT Cloud (its not technically feasible), you can drop all those
#if !defined(__AVR__)
statements.