Skip to content

Commit b738506

Browse files
committed
Merge lwIP_Ethernet library from PR esp8266#8317
1 parent 3be70a4 commit b738506

File tree

10 files changed

+870
-5
lines changed

10 files changed

+870
-5
lines changed

Diff for: cores/esp8266/LwipIntfDev.h

+11-5
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class LwipIntfDev: public LwipIntf, public RawDev
6464
return IPAddress(ip4_addr_get_u32(ip_2_ip4(&_netif.gw)));
6565
}
6666

67-
void setDefault();
67+
void setDefault(bool deflt = true);
6868

6969
bool connected()
7070
{
@@ -187,10 +187,10 @@ boolean LwipIntfDev<RawDev>::begin(const uint8_t* macAddress, const uint16_t mtu
187187
return false;
188188
}
189189

190-
_netif.flags |= NETIF_FLAG_UP;
191-
192190
if (localIP().v4() == 0)
193191
{
192+
// IP not set, starting DHCP
193+
_netif.flags |= NETIF_FLAG_UP;
194194
switch (dhcp_start(&_netif))
195195
{
196196
case ERR_OK:
@@ -204,6 +204,12 @@ boolean LwipIntfDev<RawDev>::begin(const uint8_t* macAddress, const uint16_t mtu
204204
return false;
205205
}
206206
}
207+
else
208+
{
209+
// IP is set, static config
210+
netif_set_link_up(&_netif);
211+
netif_set_up(&_netif);
212+
}
207213

208214
_started = true;
209215

@@ -386,9 +392,9 @@ err_t LwipIntfDev<RawDev>::handlePackets()
386392
}
387393

388394
template <class RawDev>
389-
void LwipIntfDev<RawDev>::setDefault()
395+
void LwipIntfDev<RawDev>::setDefault(bool deflt)
390396
{
391-
_default = true;
397+
_default = deflt;
392398
if (connected())
393399
{
394400
netif_set_default(&_netif);
+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
This sketch establishes a TCP connection to a "quote of the day" service.
3+
It sends a "hello" message, and then prints received data.
4+
5+
This is Ethernet version of:
6+
https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiClient/WiFiClient.ino
7+
*/
8+
9+
#include <LwipEthernet.h>
10+
11+
Wiznet5500lwIP eth(/*SS*/16); // <== adapt to your hardware
12+
13+
const char* host = "djxmmx.net";
14+
const uint16_t port = 17;
15+
16+
void setup() {
17+
Serial.begin(115200);
18+
19+
Serial.println("\nEthernet\n");
20+
21+
eth.setDefault(true); // default route set through this interface
22+
if (!ethInitDHCP(eth)) {
23+
Serial.printf("no hardware found\n");
24+
while (1) {
25+
delay(1000);
26+
}
27+
}
28+
29+
while (!eth.connected()) {
30+
Serial.printf(".");
31+
delay(1000);
32+
}
33+
34+
Serial.printf("Ethernet: IP Address: %s\n",
35+
eth.localIP().toString().c_str());
36+
}
37+
38+
void loop() {
39+
40+
Serial.print("connecting to ");
41+
Serial.print(host);
42+
Serial.print(':');
43+
Serial.println(port);
44+
45+
// Use WiFiClient class to create TCP connections
46+
// (this class could have been named TCPClient)
47+
WiFiClient client;
48+
if (!client.connect(host, port)) {
49+
Serial.println("connection failed");
50+
delay(5000);
51+
return;
52+
}
53+
54+
// This will send a string to the server
55+
Serial.println("sending data to server");
56+
if (client.connected()) {
57+
client.println("hello from ESP8266");
58+
}
59+
60+
// wait for data to be available
61+
unsigned long timeout = millis();
62+
while (client.available() == 0) {
63+
if (millis() - timeout > 5000) {
64+
Serial.println(">>> Client Timeout !");
65+
client.stop();
66+
delay(60000);
67+
return;
68+
}
69+
}
70+
71+
// Read all the lines of the reply from server and print them to Serial
72+
Serial.println("receiving from remote server");
73+
client.sendAll(Serial); // this peer closes once all data are sent
74+
75+
// Close the connection
76+
Serial.println();
77+
Serial.println("closing connection");
78+
client.stop();
79+
80+
delay(600000); // execute once every 10 minutes, don't flood remote service
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
This sketch establishes a TCP connection to a "quote of the day" service.
3+
It sends a "hello" message, and then prints received data.
4+
5+
This is Ethernet version of:
6+
https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiClient/WiFiClient.ino
7+
*/
8+
9+
#include <LwipEthernet.h>
10+
11+
#define LOCAL_IP IPAddress(192,168,0,233)
12+
#define LOCAL_GW IPAddress(192,168,0,254) // <== adapt to your network
13+
#define LOCAL_MASK IPAddress(255,255,255,0)
14+
#define DNS IPAddress(8,8,8,8)
15+
16+
Wiznet5500lwIP eth(/*SS*/16); // <== adapt to your hardware
17+
18+
const char* host = "djxmmx.net";
19+
const uint16_t port = 17;
20+
21+
void setup() {
22+
Serial.begin(115200);
23+
24+
Serial.println("\nEthernet\n");
25+
26+
eth.setDefault(true); // default route set through this interface
27+
if (!ethInitStatic(eth, LOCAL_IP, LOCAL_GW, LOCAL_MASK, DNS)) {
28+
// enabling debug message will show the real cause
29+
Serial.printf("no hardware found or bad network configuration\n");
30+
while (1) {
31+
delay(1000);
32+
}
33+
}
34+
35+
Serial.printf("Ethernet: IP Address: %s\n",
36+
eth.localIP().toString().c_str());
37+
}
38+
39+
void loop() {
40+
41+
Serial.print("connecting to ");
42+
Serial.print(host);
43+
Serial.print(':');
44+
Serial.println(port);
45+
46+
// Use WiFiClient class to create TCP connections
47+
// (this class could have been named TCPClient)
48+
WiFiClient client;
49+
if (!client.connect(host, port)) {
50+
Serial.println("connection failed");
51+
delay(5000);
52+
return;
53+
}
54+
55+
// This will send a string to the server
56+
Serial.println("sending data to server");
57+
if (client.connected()) {
58+
client.println("hello from ESP8266");
59+
}
60+
61+
// wait for data to be available
62+
unsigned long timeout = millis();
63+
while (client.available() == 0) {
64+
if (millis() - timeout > 5000) {
65+
Serial.println(">>> Client Timeout !");
66+
client.stop();
67+
delay(60000);
68+
return;
69+
}
70+
}
71+
72+
// Read all the lines of the reply from server and print them to Serial
73+
Serial.println("receiving from remote server");
74+
client.sendAll(Serial); // this peer closes once all data are sent
75+
76+
// Close the connection
77+
Serial.println();
78+
Serial.println("closing connection");
79+
client.stop();
80+
81+
delay(600000); // execute once every 10 minutes, don't flood remote service
82+
}

0 commit comments

Comments
 (0)