-
Notifications
You must be signed in to change notification settings - Fork 13.3k
lwIP on ethernet: examples #8395
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 all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
33cbbc0
ethernet: examples
d-a-v 8faa326
remove duplicate
d-a-v 203b8b4
Merge branch 'ethexamples' of github.com:d-a-v/Arduino into ethexamples
d-a-v ad179df
Merge branch 'master' into ethexamples
d-a-v 35ca866
styling
d-a-v 969a460
fix comment restyle + comment eth.setdefault()
d-a-v 95b6601
comment and add comments about eth.setDefault()
d-a-v 2b7082e
update comments when using interface::setDefault()
d-a-v 0c7df34
Merge branch 'master' into ethexamples
d-a-v d1350fe
repair bad merge
d-a-v b549ab6
Merge branch 'master' into ethexamples
d-a-v 8768c2b
Merge branch 'master' into ethexamples
d-a-v b5fbc65
Merge branch 'master' into ethexamples
d-a-v 9040c65
Merge branch 'master' into ethexamples
d-a-v 897a51e
fix default interface case
d-a-v eeaa8e7
Merge branch 'ethexamples' of github.com:d-a-v/Arduino into ethexamples
d-a-v 86ab737
factorize
d-a-v f29d826
Merge branch 'master' into ethexamples
d-a-v 17c115e
change comment
d-a-v 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
This sketch establishes a TCP connection to a "quote of the day" service. | ||
It sends a "hello" message, and then prints received data. | ||
|
||
This is Ethernet version of: | ||
https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiClient/WiFiClient.ino | ||
*/ | ||
|
||
#include <LwipEthernet.h> | ||
|
||
Wiznet5500lwIP eth(/*SS*/ 16); // <== adapt to your hardware | ||
|
||
const char* host = "djxmmx.net"; | ||
const uint16_t port = 17; | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
|
||
Serial.println("\nEthernet\n"); | ||
|
||
// 1. Currently when no default is set, esp8266-Arduino uses the first | ||
// DHCP client interface receiving a valid address and gateway to | ||
// become the new lwIP default interface. | ||
// 2. Otherwise - when using static addresses - lwIP for every packets by | ||
// defaults selects automatically the best suited output interface | ||
// matching the destination address. If several interfaces match, | ||
// the first one is picked. On esp8266/Arduno: WiFi interfaces are | ||
// checked first. | ||
// 3. Or, use `::setDefault()` to force routing through this interface. | ||
// eth.setDefault(); // default route set through this interface | ||
|
||
if (!ethInitDHCP(eth)) { | ||
Serial.printf("no hardware found\n"); | ||
while (1) { | ||
delay(1000); | ||
} | ||
} | ||
|
||
while (!eth.connected()) { | ||
Serial.printf("."); | ||
delay(1000); | ||
} | ||
|
||
Serial.printf("Ethernet: IP Address: %s\n", | ||
eth.localIP().toString().c_str()); | ||
} | ||
|
||
void loop() { | ||
|
||
Serial.print("connecting to "); | ||
Serial.print(host); | ||
Serial.print(':'); | ||
Serial.println(port); | ||
|
||
// Use WiFiClient class to create TCP connections | ||
// (this class could have been named TCPClient) | ||
WiFiClient client; | ||
if (!client.connect(host, port)) { | ||
Serial.println("connection failed"); | ||
delay(5000); | ||
return; | ||
} | ||
|
||
// This will send a string to the server | ||
Serial.println("sending data to server"); | ||
if (client.connected()) { client.println("hello from ESP8266"); } | ||
|
||
// wait for data to be available | ||
unsigned long timeout = millis(); | ||
while (client.available() == 0) { | ||
if (millis() - timeout > 5000) { | ||
Serial.println(">>> Client Timeout !"); | ||
client.stop(); | ||
delay(60000); | ||
return; | ||
} | ||
} | ||
|
||
// Read all the lines of the reply from server and print them to Serial | ||
Serial.println("receiving from remote server"); | ||
client.sendAll(Serial); // this peer closes once all data are sent | ||
|
||
// Close the connection | ||
Serial.println(); | ||
Serial.println("closing connection"); | ||
client.stop(); | ||
|
||
delay(300000); // execute once every 5 minutes, don't flood remote service | ||
} |
92 changes: 92 additions & 0 deletions
92
libraries/lwIP_Ethernet/examples/EthClientStatic/EthClientStatic.ino
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,92 @@ | ||
/* | ||
This sketch establishes a TCP connection to a "quote of the day" service. | ||
It sends a "hello" message, and then prints received data. | ||
|
||
This is Ethernet version of: | ||
https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiClient/WiFiClient.ino | ||
*/ | ||
|
||
#include <LwipEthernet.h> | ||
|
||
#define LOCAL_IP IPAddress(192, 168, 0, 233) | ||
#define LOCAL_GW IPAddress(192, 168, 0, 254) // <== adapt to your network | ||
#define LOCAL_MASK IPAddress(255, 255, 255, 0) | ||
#define DNS IPAddress(8, 8, 8, 8) | ||
|
||
Wiznet5500lwIP eth(/*SS*/ 16); // <== adapt to your hardware | ||
|
||
const char* host = "djxmmx.net"; | ||
const uint16_t port = 17; | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
|
||
Serial.println("\nEthernet\n"); | ||
|
||
// 1. Currently when no default is set, esp8266-Arduino uses the first | ||
// DHCP client interface receiving a valid address and gateway to | ||
// become the new lwIP default interface. | ||
// 2. Otherwise - when using static addresses - lwIP for every packets by | ||
// defaults selects automatically the best suited output interface | ||
// matching the destination address. If several interfaces match, | ||
// the first one is picked. On esp8266/Arduno: WiFi interfaces are | ||
// checked first. | ||
// 3. Or, use `::setDefault()` to force routing through this interface. | ||
// eth.setDefault(true); // default route set through this interface | ||
|
||
if (!ethInitStatic(eth, LOCAL_IP, LOCAL_GW, LOCAL_MASK, DNS)) { | ||
// enabling debug message will show the real cause | ||
Serial.printf("no hardware found or bad network configuration\n"); | ||
while (1) { | ||
delay(1000); | ||
} | ||
} | ||
|
||
Serial.printf("Ethernet: IP Address: %s\n", | ||
eth.localIP().toString().c_str()); | ||
} | ||
|
||
void loop() { | ||
|
||
Serial.print("connecting to "); | ||
Serial.print(host); | ||
Serial.print(':'); | ||
Serial.println(port); | ||
|
||
// Use WiFiClient class to create TCP connections | ||
// (this class could have been named TCPClient) | ||
WiFiClient client; | ||
if (!client.connect(host, port)) { | ||
Serial.println("connection failed"); | ||
delay(5000); | ||
return; | ||
} | ||
|
||
// This will send a string to the server | ||
Serial.println("sending data to server"); | ||
if (client.connected()) { | ||
client.println("hello from ESP8266"); | ||
} | ||
|
||
// wait for data to be available | ||
unsigned long timeout = millis(); | ||
while (client.available() == 0) { | ||
if (millis() - timeout > 5000) { | ||
Serial.println(">>> Client Timeout !"); | ||
client.stop(); | ||
delay(60000); | ||
return; | ||
} | ||
} | ||
|
||
// Read all the lines of the reply from server and print them to Serial | ||
Serial.println("receiving from remote server"); | ||
client.sendAll(Serial); // this peer closes once all data are sent | ||
|
||
// Close the connection | ||
Serial.println(); | ||
Serial.println("closing connection"); | ||
client.stop(); | ||
|
||
delay(600000); // execute once every 10 minutes, don't flood remote service | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.