Unverified Commit 8673da25 authored by Juraj Andrássy's avatar Juraj Andrássy Committed by GitHub

Ethernet legacy API compatibility layer (#2147)

With example from the Arduino Ethernet library
parent fa58b698
/*
Web client
This sketch connects to a website (http://www.google.com)
using an Arduino WIZnet Ethernet shield.
Circuit:
Ethernet shield attached to default SPI pins
created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe, based on work by Adrian McEwen
*/
#include <SPI.h>
#include <EthernetCompat.h>
ArduinoWiznet5500lwIP Ethernet(D10, SPI, D2);
//ArduinoWiznet5100lwIP Ethernet(D10, SPI, D2);
//ArduinoENC28J60lwIP Ethernet(D10, SPI, D2);
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
char server[] = "www.google.com"; // name address for Google (using DNS)
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);
IPAddress myDns(192, 168, 0, 1);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
// Variables to measure the speed
unsigned long beginMicros, endMicros;
unsigned long byteCount = 0;
bool printWebData = true; // set to false for better speed measurement
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection:
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// 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.");
}
// try to configure using IP address instead of DHCP:
Ethernet.begin(mac, ip, myDns);
} else {
Serial.print(" DHCP assigned IP ");
Serial.println(Ethernet.localIP());
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.print("connecting to ");
Serial.print(server);
Serial.println("...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.print("connected to ");
Serial.println(client.remoteIP());
// Make a HTTP request:
client.println("GET /search?q=arduino HTTP/1.1");
client.println("Host: www.google.com");
client.println("Connection: close");
client.println();
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
beginMicros = micros();
}
void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
int len = client.available();
if (len > 0) {
byte buffer[80];
if (len > 80) {
len = 80;
}
client.read(buffer, len);
if (printWebData) {
Serial.write(buffer, len); // show in the serial monitor (slows some boards)
}
byteCount = byteCount + len;
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
endMicros = micros();
Serial.println();
Serial.println("disconnecting.");
client.stop();
Serial.print("Received ");
Serial.print(byteCount);
Serial.print(" bytes in ");
float seconds = (float)(endMicros - beginMicros) / 1000000.0;
Serial.print(seconds, 4);
float rate = (float) byteCount / seconds / 1000.0;
Serial.print(", rate = ");
Serial.print(rate);
Serial.print(" kbytes/second");
Serial.println();
// do nothing forevermore:
while (true) {
delay(1);
}
}
}
#pragma once
#include <LwipIntfDev.h>
#include <W5100lwIP.h>
#include <W5500lwIP.h>
#include <ENC28J60lwIP.h>
#include <WiFiUdp.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
using EthernetUDP = WiFiUDP;
using EthernetClient = WiFiClient;
using EthernetServer = WiFiServer;
enum HardwareStatus {
EthernetNoHardware,
EthernetHardwareFound,
};
template<class RawDev>
class ArduinoEthernet : public LwipIntfDev<RawDev> {
public:
ArduinoEthernet(int8_t cs = SS, SPIClass &spi = SPI, int8_t intr = -1) :
LwipIntfDev<RawDev>(cs, spi, intr) {
_hardwareStatus = EthernetNoHardware;
}
// Arduino-Ethernet API compatibility, order can be either:
// mac, ip, gateway, netmask, dns (esp8266 or natural order)
// mac, ip, dns, gateway, netmask (Arduino legacy)
boolean begin(const uint8_t *macAddress, IPAddress local_ip = IPADDR_NONE, IPAddress arg1 = IPADDR_NONE, IPAddress arg2 = IPADDR_NONE, IPAddress arg3 = IPADDR_NONE) {
if (local_ip.isSet() && local_ip.isV4()) {
// setting auto values using arduino ordering of parameters
if (arg1 == IPADDR_NONE) { // else dns or gw
arg1 = local_ip;
arg1[3] = 1;
}
if (arg2 == IPADDR_NONE) { // else gw or mask
arg2 = local_ip;
arg2[3] = 1;
}
// if arg2 is mask (esp ordering), let DNS IP unconfigured
if (arg3 == IPADDR_NONE && arg2[0] != 255) { // else mask or dns
arg3 = IPAddress(255, 255, 255, 0);
}
}
bool ret = true;
if (local_ip.isSet()) {
ret = LwipIntfDev<RawDev>::config(local_ip, arg1, arg2, arg3);
}
if (ret) {
ret = LwipIntfDev<RawDev>::begin(macAddress);
if (!local_ip.isSet()) {
// Arduino API waits for DHCP answer
while (!LwipIntfDev<RawDev>::connected()) {
delay(100);
}
}
}
if (ret) {
_hardwareStatus = EthernetHardwareFound;
}
return ret;
}
void end() {
ip_addr_copy(LwipIntfDev<RawDev>::_netif.ip_addr, ip_addr_any); // to allow DHCP at next begin
LwipIntfDev<RawDev>::end();
}
void MACAddress(uint8_t *mac) {
LwipIntfDev<RawDev>::macAddress(mac);
}
IPAddress dnsServerIP() const {
return LwipIntfDev<RawDev>::dnsIP(0);
}
void setDnsServerIP(const IPAddress dnsIP) {
LwipIntfDev<RawDev>::setDNS(dnsIP);
}
HardwareStatus hardwareStatus() const {
return _hardwareStatus;
}
int maintain() const {
return 0;
}
protected:
HardwareStatus _hardwareStatus;
};
using ArduinoWiznet5500lwIP = ArduinoEthernet<Wiznet5500>;
using ArduinoWiznet5100lwIP = ArduinoEthernet<Wiznet5100>;
using ArduinoENC28J60lwIP = ArduinoEthernet<ENC28J60>;
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment