mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-18 23:03:34 +03:00
make WiFi/Ethernet interface compatible with Arduino Ethernet API (#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>
This commit is contained in:
@ -0,0 +1,105 @@
|
||||
/*
|
||||
Chat Server
|
||||
|
||||
A simple server that distributes any incoming messages to all
|
||||
connected clients. To use, telnet to your device's IP address and type.
|
||||
You can see the client's input in the serial monitor as well.
|
||||
Using an Arduino Wiznet Ethernet shield.
|
||||
|
||||
Circuit:
|
||||
* Ethernet Wiznet5500/Wiznet5100/ENC28J60 on esp8266
|
||||
|
||||
created 18 Dec 2009
|
||||
by David A. Mellis
|
||||
modified 9 Apr 2012
|
||||
by Tom Igoe
|
||||
|
||||
*/
|
||||
|
||||
|
||||
// specific to esp8266 w/lwIP
|
||||
#include <EthernetCompat.h>
|
||||
ArduinoWiznet5500lwIP Ethernet(/*SS*/ 16); // <== adapt to your hardware
|
||||
// ArduinoWiznet5100lwIP Ethernet(/*SS*/ 16); // <== adapt to your hardware
|
||||
// ArduinoENC28J60lwIP Ethernet(/*SS*/ 16); // <== adapt to your hardware
|
||||
|
||||
|
||||
|
||||
// Enter a MAC address and IP address for your controller below.
|
||||
// The IP address will be dependent on your local network.
|
||||
// gateway and subnet are optional:
|
||||
byte notNeededButAllowed_mac[] = {
|
||||
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
|
||||
};
|
||||
byte* mac = nullptr; // automatic mac
|
||||
IPAddress ip(192, 168, 1, 177);
|
||||
IPAddress myDns(192, 168, 1, 1);
|
||||
IPAddress gateway(192, 168, 1, 1);
|
||||
IPAddress subnet(255, 255, 0, 0);
|
||||
|
||||
|
||||
// telnet defaults to port 23
|
||||
EthernetServer server(23);
|
||||
bool alreadyConnected = false; // whether or not the client was connected previously
|
||||
|
||||
void setup() {
|
||||
// You can use Ethernet.init(pin) to configure the CS pin
|
||||
// Ethernet.init(10); // Most Arduino shields
|
||||
// Ethernet.init(5); // MKR ETH shield
|
||||
// Ethernet.init(0); // Teensy 2.0
|
||||
// Ethernet.init(20); // Teensy++ 2.0
|
||||
// Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
|
||||
// Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
|
||||
// // esp8266 w/lwIP: SS set in Ethernet constructor
|
||||
|
||||
// initialize the ethernet device
|
||||
Ethernet.begin(mac, ip, myDns, gateway, subnet);
|
||||
|
||||
// Open serial communications and wait for port to open:
|
||||
Serial.begin(9600);
|
||||
while (!Serial) {
|
||||
; // wait for serial port to connect. Needed for native USB port only
|
||||
}
|
||||
|
||||
// Check for Ethernet hardware present
|
||||
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
|
||||
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
|
||||
while (true) {
|
||||
delay(1); // do nothing, no point running without Ethernet hardware
|
||||
}
|
||||
}
|
||||
if (Ethernet.linkStatus() == LinkOFF) {
|
||||
Serial.println("Ethernet cable is not connected.");
|
||||
}
|
||||
|
||||
// start listening for clients
|
||||
server.begin();
|
||||
|
||||
Serial.print("Chat server address:");
|
||||
Serial.println(Ethernet.localIP());
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// wait for a new client:
|
||||
EthernetClient client = server.available();
|
||||
|
||||
// when the client sends the first byte, say hello:
|
||||
if (client) {
|
||||
if (!alreadyConnected) {
|
||||
// clear out the input buffer:
|
||||
client.flush();
|
||||
Serial.println("We have a new client");
|
||||
client.println("Hello, client!");
|
||||
alreadyConnected = true;
|
||||
}
|
||||
|
||||
if (client.available() > 0) {
|
||||
// read the bytes incoming from the client:
|
||||
char thisChar = client.read();
|
||||
// echo the bytes back to the client:
|
||||
server.write(thisChar);
|
||||
// echo the bytes to the server as well:
|
||||
Serial.write(thisChar);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user