Skip to content

Commit

Permalink
Merge lwIP_Ethernet library from PR esp8266#8317
Browse files Browse the repository at this point in the history
  • Loading branch information
dok-net committed Oct 9, 2021
1 parent 3be70a4 commit b738506
Show file tree
Hide file tree
Showing 10 changed files with 870 additions and 5 deletions.
16 changes: 11 additions & 5 deletions cores/esp8266/LwipIntfDev.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class LwipIntfDev: public LwipIntf, public RawDev
return IPAddress(ip4_addr_get_u32(ip_2_ip4(&_netif.gw)));
}

void setDefault();
void setDefault(bool deflt = true);

bool connected()
{
Expand Down Expand Up @@ -187,10 +187,10 @@ boolean LwipIntfDev<RawDev>::begin(const uint8_t* macAddress, const uint16_t mtu
return false;
}

_netif.flags |= NETIF_FLAG_UP;

if (localIP().v4() == 0)
{
// IP not set, starting DHCP
_netif.flags |= NETIF_FLAG_UP;
switch (dhcp_start(&_netif))
{
case ERR_OK:
Expand All @@ -204,6 +204,12 @@ boolean LwipIntfDev<RawDev>::begin(const uint8_t* macAddress, const uint16_t mtu
return false;
}
}
else
{
// IP is set, static config
netif_set_link_up(&_netif);
netif_set_up(&_netif);
}

_started = true;

Expand Down Expand Up @@ -386,9 +392,9 @@ err_t LwipIntfDev<RawDev>::handlePackets()
}

template <class RawDev>
void LwipIntfDev<RawDev>::setDefault()
void LwipIntfDev<RawDev>::setDefault(bool deflt)
{
_default = true;
_default = deflt;
if (connected())
{
netif_set_default(&_netif);
Expand Down
81 changes: 81 additions & 0 deletions libraries/lwIP_Ethernet/examples/EthClient/EthClient.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
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");

eth.setDefault(true); // 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(600000); // execute once every 10 minutes, don't flood remote service
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
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");

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
}
Loading

0 comments on commit b738506

Please sign in to comment.