Skip to content

Commit 1a1c8ad

Browse files
d-a-vmcspr
authored andcommitted
make WiFi/Ethernet interface compatible with Arduino Ethernet API (esp8266#8645)
* make WiFi/Ethernet interface compatible with Arduino Ethernet API provide some minimaly adapted examples from legacy * move ethernet compat globals to EthernetCompat.h * LegacyEthernet: add UDP example * adjust comments Co-authored-by: Max Prokhorov <prokhorov.max@outlook.com>
1 parent 6a0c309 commit 1a1c8ad

File tree

7 files changed

+590
-2
lines changed

7 files changed

+590
-2
lines changed

cores/esp8266/LwipIntf.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class LwipIntf
4141
// ---- + --------- -------------
4242
// local_ip | local_ip local_ip
4343
// arg1 | gateway dns1
44-
// arg2 | netmask [Agateway
44+
// arg2 | netmask gateway
4545
// arg3 | dns1 netmask
4646
//
4747
// result stored into gateway/netmask/dns1

cores/esp8266/LwipIntfDev.h

-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ class LwipIntfDev: public LwipIntf, public RawDev
104104
}
105105

106106
// ESP8266WiFi API compatibility
107-
108107
wl_status_t status();
109108

110109
protected:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
Advanced Chat Server
3+
4+
A more advanced server that distributes any incoming messages
5+
to all connected clients but the client the message comes from.
6+
To use, telnet to your device's IP address and type.
7+
You can see the client's input in the serial monitor as well.
8+
Using an Arduino Wiznet Ethernet shield.
9+
10+
Circuit:
11+
* Ethernet Wiznet5500/Wiznet5100/ENC28J60 on esp8266
12+
13+
created 18 Dec 2009
14+
by David A. Mellis
15+
modified 9 Apr 2012
16+
by Tom Igoe
17+
redesigned to make use of operator== 25 Nov 2013
18+
by Norbert Truchsess
19+
20+
*/
21+
22+
// specific to esp8266 w/lwIP
23+
#include <EthernetCompat.h>
24+
ArduinoWiznet5500lwIP Ethernet(/*SS*/ 16); // <== adapt to your hardware
25+
// ArduinoWiznet5100lwIP Ethernet(/*SS*/ 16); // <== adapt to your hardware
26+
// ArduinoENC28J60lwIP Ethernet(/*SS*/ 16); // <== adapt to your hardware
27+
28+
29+
30+
// Enter a MAC address and IP address for your controller below.
31+
// The IP address will be dependent on your local network.
32+
// gateway and subnet are optional:
33+
byte notNeededButAllowed_mac[] = {
34+
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
35+
};
36+
byte* mac = nullptr; // automatic mac
37+
IPAddress ip(192, 168, 1, 177);
38+
IPAddress myDns(192, 168, 1, 1);
39+
IPAddress gateway(192, 168, 1, 1);
40+
IPAddress subnet(255, 255, 0, 0);
41+
42+
43+
// telnet defaults to port 23
44+
EthernetServer server(23);
45+
46+
EthernetClient clients[8];
47+
48+
void setup() {
49+
// You can use Ethernet.init(pin) to configure the CS pin
50+
// Ethernet.init(10); // Most Arduino shields
51+
// Ethernet.init(5); // MKR ETH shield
52+
// Ethernet.init(0); // Teensy 2.0
53+
// Ethernet.init(20); // Teensy++ 2.0
54+
// Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
55+
// Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
56+
// // esp8266 w/lwIP: SS set in Ethernet constructor
57+
58+
// initialize the Ethernet device
59+
Ethernet.begin(mac, ip, myDns, gateway, subnet);
60+
61+
// Open serial communications and wait for port to open:
62+
Serial.begin(9600);
63+
while (!Serial) {
64+
; // wait for serial port to connect. Needed for native USB port only
65+
}
66+
67+
// Check for Ethernet hardware present
68+
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
69+
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
70+
while (true) {
71+
delay(1); // do nothing, no point running without Ethernet hardware
72+
}
73+
}
74+
if (Ethernet.linkStatus() == LinkOFF) {
75+
Serial.println("Ethernet cable is not connected.");
76+
}
77+
78+
// start listening for clients
79+
server.begin();
80+
81+
Serial.print("Chat server address:");
82+
Serial.println(Ethernet.localIP());
83+
}
84+
85+
void loop() {
86+
// check for any new client connecting, and say hello (before any incoming data)
87+
EthernetClient newClient = server.accept();
88+
if (newClient) {
89+
for (byte i = 0; i < 8; i++) {
90+
if (!clients[i]) {
91+
Serial.print("We have a new client #");
92+
Serial.println(i);
93+
newClient.print("Hello, client number: ");
94+
newClient.println(i);
95+
// Once we "accept", the client is no longer tracked by EthernetServer
96+
// so we must store it into our list of clients
97+
clients[i] = newClient;
98+
break;
99+
}
100+
}
101+
}
102+
103+
// check for incoming data from all clients
104+
for (byte i = 0; i < 8; i++) {
105+
if (clients[i] && clients[i].available() > 0) {
106+
// read bytes from a client
107+
byte buffer[80];
108+
int count = clients[i].read(buffer, 80);
109+
// write the bytes to all other connected clients
110+
for (byte j = 0; j < 8; j++) {
111+
if (j != i && clients[j].connected()) {
112+
clients[j].write(buffer, count);
113+
}
114+
}
115+
}
116+
}
117+
118+
// stop any clients which disconnect
119+
for (byte i = 0; i < 8; i++) {
120+
if (clients[i] && !clients[i].connected()) {
121+
Serial.print("disconnect client #");
122+
Serial.println(i);
123+
clients[i].stop();
124+
}
125+
}
126+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
Chat Server
3+
4+
A simple server that distributes any incoming messages to all
5+
connected clients. To use, telnet to your device's IP address and type.
6+
You can see the client's input in the serial monitor as well.
7+
Using an Arduino Wiznet Ethernet shield.
8+
9+
Circuit:
10+
* Ethernet Wiznet5500/Wiznet5100/ENC28J60 on esp8266
11+
12+
created 18 Dec 2009
13+
by David A. Mellis
14+
modified 9 Apr 2012
15+
by Tom Igoe
16+
17+
*/
18+
19+
20+
// specific to esp8266 w/lwIP
21+
#include <EthernetCompat.h>
22+
ArduinoWiznet5500lwIP Ethernet(/*SS*/ 16); // <== adapt to your hardware
23+
// ArduinoWiznet5100lwIP Ethernet(/*SS*/ 16); // <== adapt to your hardware
24+
// ArduinoENC28J60lwIP Ethernet(/*SS*/ 16); // <== adapt to your hardware
25+
26+
27+
28+
// Enter a MAC address and IP address for your controller below.
29+
// The IP address will be dependent on your local network.
30+
// gateway and subnet are optional:
31+
byte notNeededButAllowed_mac[] = {
32+
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
33+
};
34+
byte* mac = nullptr; // automatic mac
35+
IPAddress ip(192, 168, 1, 177);
36+
IPAddress myDns(192, 168, 1, 1);
37+
IPAddress gateway(192, 168, 1, 1);
38+
IPAddress subnet(255, 255, 0, 0);
39+
40+
41+
// telnet defaults to port 23
42+
EthernetServer server(23);
43+
bool alreadyConnected = false; // whether or not the client was connected previously
44+
45+
void setup() {
46+
// You can use Ethernet.init(pin) to configure the CS pin
47+
// Ethernet.init(10); // Most Arduino shields
48+
// Ethernet.init(5); // MKR ETH shield
49+
// Ethernet.init(0); // Teensy 2.0
50+
// Ethernet.init(20); // Teensy++ 2.0
51+
// Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
52+
// Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
53+
// // esp8266 w/lwIP: SS set in Ethernet constructor
54+
55+
// initialize the ethernet device
56+
Ethernet.begin(mac, ip, myDns, gateway, subnet);
57+
58+
// Open serial communications and wait for port to open:
59+
Serial.begin(9600);
60+
while (!Serial) {
61+
; // wait for serial port to connect. Needed for native USB port only
62+
}
63+
64+
// Check for Ethernet hardware present
65+
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
66+
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
67+
while (true) {
68+
delay(1); // do nothing, no point running without Ethernet hardware
69+
}
70+
}
71+
if (Ethernet.linkStatus() == LinkOFF) {
72+
Serial.println("Ethernet cable is not connected.");
73+
}
74+
75+
// start listening for clients
76+
server.begin();
77+
78+
Serial.print("Chat server address:");
79+
Serial.println(Ethernet.localIP());
80+
}
81+
82+
void loop() {
83+
// wait for a new client:
84+
EthernetClient client = server.available();
85+
86+
// when the client sends the first byte, say hello:
87+
if (client) {
88+
if (!alreadyConnected) {
89+
// clear out the input buffer:
90+
client.flush();
91+
Serial.println("We have a new client");
92+
client.println("Hello, client!");
93+
alreadyConnected = true;
94+
}
95+
96+
if (client.available() > 0) {
97+
// read the bytes incoming from the client:
98+
char thisChar = client.read();
99+
// echo the bytes back to the client:
100+
server.write(thisChar);
101+
// echo the bytes to the server as well:
102+
Serial.write(thisChar);
103+
}
104+
}
105+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
DHCP-based IP printer
3+
4+
This sketch uses the DHCP extensions to the Ethernet library
5+
to get an IP address via DHCP and print the address obtained.
6+
using an Arduino Wiznet Ethernet shield.
7+
8+
Circuit:
9+
* Ethernet Wiznet5500/Wiznet5100/ENC28J60 on esp8266
10+
11+
created 12 April 2011
12+
modified 9 Apr 2012
13+
by Tom Igoe
14+
modified 02 Sept 2015
15+
by Arturo Guadalupi
16+
17+
*/
18+
19+
// specific to esp8266 w/lwIP
20+
#include <EthernetCompat.h>
21+
ArduinoWiznet5500lwIP Ethernet(/*SS*/ 16); // <== adapt to your hardware
22+
// ArduinoWiznet5100lwIP Ethernet(/*SS*/ 16); // <== adapt to your hardware
23+
// ArduinoENC28J60lwIP Ethernet(/*SS*/ 16); // <== adapt to your hardware
24+
25+
26+
// Enter a MAC address for your controller below.
27+
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
28+
byte notNeededButAllowed_mac[] = {
29+
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02
30+
};
31+
byte* mac = nullptr; // automatic mac
32+
33+
void setup() {
34+
// You can use Ethernet.init(pin) to configure the CS pin
35+
// Ethernet.init(10); // Most Arduino shields
36+
// Ethernet.init(5); // MKR ETH shield
37+
// Ethernet.init(0); // Teensy 2.0
38+
// Ethernet.init(20); // Teensy++ 2.0
39+
// Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
40+
// Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
41+
// // esp8266 w/lwIP: SS set in Ethernet constructor
42+
43+
44+
// Open serial communications and wait for port to open:
45+
Serial.begin(9600);
46+
while (!Serial) {
47+
; // wait for serial port to connect. Needed for native USB port only
48+
}
49+
50+
// start the Ethernet connection:
51+
Serial.println("Initialize Ethernet with DHCP:");
52+
if (Ethernet.begin(mac) == 0) {
53+
Serial.println("Failed to configure Ethernet using DHCP");
54+
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
55+
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
56+
} else if (Ethernet.linkStatus() == LinkOFF) {
57+
Serial.println("Ethernet cable is not connected.");
58+
}
59+
// no point in carrying on, so do nothing forevermore:
60+
while (true) {
61+
delay(1);
62+
}
63+
}
64+
// print your local IP address:
65+
Serial.print("My IP address: ");
66+
Serial.println(Ethernet.localIP());
67+
}
68+
69+
void loop() {
70+
switch (Ethernet.maintain()) {
71+
case 1:
72+
// renewed fail
73+
Serial.println("Error: renewed fail");
74+
break;
75+
76+
case 2:
77+
// renewed success
78+
Serial.println("Renewed success");
79+
// print your local IP address:
80+
Serial.print("My IP address: ");
81+
Serial.println(Ethernet.localIP());
82+
break;
83+
84+
case 3:
85+
// rebind fail
86+
Serial.println("Error: rebind fail");
87+
break;
88+
89+
case 4:
90+
// rebind success
91+
Serial.println("Rebind success");
92+
// print your local IP address:
93+
Serial.print("My IP address: ");
94+
Serial.println(Ethernet.localIP());
95+
break;
96+
97+
default:
98+
// nothing happened
99+
break;
100+
}
101+
}

0 commit comments

Comments
 (0)