|
| 1 | +/** |
| 2 | + * LegacyEthernetTest from NetApiHelpers library |
| 3 | + * modified for the EthernetESP32 library. |
| 4 | + */ |
| 5 | + |
| 6 | +#include <EthernetESP32.h> |
| 7 | +#include <MacAddress.h> |
| 8 | + |
| 9 | +byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF }; |
| 10 | + |
| 11 | +W5500Driver driver; |
| 12 | +//ENC28J60Driver driver; |
| 13 | +//EMACDriver driver(ETH_PHY_LAN8720); |
| 14 | + |
| 15 | +void setup() { |
| 16 | + |
| 17 | + Serial.begin(115200); |
| 18 | + delay(500); |
| 19 | + while (!Serial); |
| 20 | + Serial.println("START"); |
| 21 | + Serial.println(); |
| 22 | + |
| 23 | + Ethernet.init(driver); |
| 24 | + |
| 25 | + Serial.println("Attempting to connect with DHCP ..."); |
| 26 | + testEthernet(true); |
| 27 | + |
| 28 | + if (Ethernet.hardwareStatus() == EthernetNoHardware) { |
| 29 | + Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :("); |
| 30 | + while (true) { |
| 31 | + delay(1); // do nothing, no point running without Ethernet hardware |
| 32 | + } |
| 33 | + } |
| 34 | + Serial.println("Checking link ..."); |
| 35 | + if (Ethernet.linkStatus() == LinkOFF) { |
| 36 | + Serial.println("\tretry..."); |
| 37 | + delay(500); |
| 38 | + } |
| 39 | + switch (Ethernet.linkStatus()) { |
| 40 | + case LinkOFF: |
| 41 | + Serial.println("\tEthernet cable is not connected."); |
| 42 | + break; |
| 43 | + case LinkON: |
| 44 | + Serial.println("\tEthernet cable is connected."); |
| 45 | + break; |
| 46 | + default: |
| 47 | + Serial.println("\tLink state unknown."); |
| 48 | + break; |
| 49 | + } |
| 50 | + Serial.println(); |
| 51 | + |
| 52 | + IPAddress ip = Ethernet.localIP(); |
| 53 | + IPAddress gw = Ethernet.gatewayIP(); |
| 54 | + IPAddress mask = Ethernet.subnetMask(); |
| 55 | + IPAddress dns = Ethernet.dnsServerIP(); |
| 56 | + |
| 57 | + const char *serverName = "www.google.com"; |
| 58 | + Serial.print("Resolve IP for "); |
| 59 | + Serial.println(serverName); |
| 60 | + IPAddress resolvedIP; |
| 61 | + if (Ethernet.hostByName("www.google.com", resolvedIP) == 1) { |
| 62 | + Serial.print("\t"); |
| 63 | + Serial.println(resolvedIP); |
| 64 | + } else { |
| 65 | + Serial.println("\t...ERROR"); |
| 66 | + } |
| 67 | + Serial.println(); |
| 68 | + |
| 69 | + Ethernet.end(); |
| 70 | + |
| 71 | +// IPAddress ip(192, 168, 1, 177); |
| 72 | +// IPAddress gw(192, 168, 1, 1); |
| 73 | +// IPAddress dns(192, 168, 1, 1); |
| 74 | +// IPAddress mask(255, 255, 255, 0); |
| 75 | + |
| 76 | + Serial.print("Configuring static IP "); |
| 77 | + ip[3] = 177; |
| 78 | + Serial.println(ip); |
| 79 | + Ethernet.begin(mac, ip, dns, gw, mask); |
| 80 | + if (Ethernet.dnsServerIP() == mask) { |
| 81 | + Serial.println("ERROR: begin() has wrong ordering of parameters"); |
| 82 | + while (true) { |
| 83 | + delay(1); |
| 84 | + } |
| 85 | + } |
| 86 | + testEthernet(false); |
| 87 | + if (ip != Ethernet.localIP()) { |
| 88 | + Serial.println("ERROR: Static IP was not used."); |
| 89 | + while (true) { |
| 90 | + delay(1); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + IPAddress dnsIP2(8, 8, 8, 8); |
| 95 | + Ethernet.setDnsServerIP(dnsIP2); |
| 96 | + if (Ethernet.dnsServerIP() != dnsIP2) { |
| 97 | + Serial.print("ERROR: DNS IP setter or getter error. dnsServerIP() returned "); |
| 98 | + Serial.println(Ethernet.dnsServerIP()); |
| 99 | + Serial.println(); |
| 100 | + } |
| 101 | + Ethernet.setDnsServerIP(dns); |
| 102 | + |
| 103 | + Ethernet.end(); |
| 104 | + |
| 105 | + Serial.print("Configuring with automatic gateway, DNS and mask with static IP "); // <------- |
| 106 | + ip[3] = 178; |
| 107 | + Serial.println(ip); |
| 108 | + Ethernet.begin(mac, ip); |
| 109 | + testEthernet(false); |
| 110 | + if (ip != Ethernet.localIP()) { |
| 111 | + Serial.println("ERROR: Static IP was not used."); |
| 112 | + while (true) { |
| 113 | + delay(1); |
| 114 | + } |
| 115 | + } |
| 116 | + IPAddress autoIP(ip); |
| 117 | + autoIP[3] = 1; |
| 118 | + IPAddress defaultMask(255, 255, 255, 0); |
| 119 | + if (Ethernet.gatewayIP() == autoIP && Ethernet.dnsServerIP() == autoIP && Ethernet.subnetMask() == defaultMask) { |
| 120 | + Serial.println("Automatic config values are OK"); |
| 121 | + } else { |
| 122 | + Serial.println("ERROR: Automatic config values are wrong"); |
| 123 | + if (Ethernet.gatewayIP() != autoIP) { |
| 124 | + Serial.print("\tgateway IP Address: "); |
| 125 | + Serial.println(Ethernet.gatewayIP()); |
| 126 | + } |
| 127 | + if (Ethernet.subnetMask() != defaultMask) { |
| 128 | + Serial.print("\tsubnet IP mask: "); |
| 129 | + Serial.println(Ethernet.subnetMask()); |
| 130 | + } |
| 131 | + if (Ethernet.dnsServerIP() != autoIP) { |
| 132 | + Serial.print("\tDNS server: "); |
| 133 | + Serial.println(Ethernet.dnsServerIP()); |
| 134 | + } |
| 135 | + } |
| 136 | + Serial.println(); |
| 137 | + |
| 138 | + Ethernet.end(); |
| 139 | + |
| 140 | + Serial.println("Attempting to connect with DHCP again ..."); |
| 141 | + testEthernet(true); |
| 142 | + |
| 143 | + if (ip == Ethernet.localIP()) { |
| 144 | + Serial.println("ERROR: The IP didn't change from static IP to DHCP assigned IP."); |
| 145 | + } else { |
| 146 | + Serial.println("Check on router the hostname and MAC address."); |
| 147 | + } |
| 148 | + Serial.println(); |
| 149 | + |
| 150 | + Serial.println("END"); |
| 151 | +} |
| 152 | + |
| 153 | +void loop() { |
| 154 | + delay(1); |
| 155 | +} |
| 156 | + |
| 157 | +void testEthernet(bool dhcp) { |
| 158 | + |
| 159 | + if (dhcp) { |
| 160 | + Ethernet.setHostname("arduino"); |
| 161 | + if (!Ethernet.begin(mac)) { |
| 162 | + Serial.println("\t...ERROR"); |
| 163 | + while (true) { |
| 164 | + delay(1); |
| 165 | + } |
| 166 | + } else { |
| 167 | + Serial.println("\t...success"); |
| 168 | + } |
| 169 | + } |
| 170 | + Serial.println(); |
| 171 | + |
| 172 | + printEthernetStatus(); |
| 173 | + Serial.println(); |
| 174 | + |
| 175 | + Serial.print("Attempt to connect to port 80 on "); |
| 176 | + Serial.println(Ethernet.gatewayIP()); |
| 177 | + EthernetClient client; |
| 178 | + if (client.connect(Ethernet.gatewayIP(), 80)) { |
| 179 | + Serial.println("\t...success"); |
| 180 | + } else { |
| 181 | + Serial.println("\t...ERROR"); |
| 182 | + } |
| 183 | + client.stop(); |
| 184 | + Serial.println(); |
| 185 | +} |
| 186 | + |
| 187 | +void printEthernetStatus() { |
| 188 | + |
| 189 | + byte mac[6]; |
| 190 | + Ethernet.MACAddress(mac); |
| 191 | + Serial.print("MAC: "); |
| 192 | + Serial.println(MacAddress(mac)); |
| 193 | + if (mac[0] & 1) { // unicast bit is set |
| 194 | + Serial.println("\t is the ordering of the MAC address bytes reversed?"); |
| 195 | + } |
| 196 | + |
| 197 | + Serial.print("IP Address: "); |
| 198 | + Serial.println(Ethernet.localIP()); |
| 199 | + |
| 200 | + Serial.print("gateway IP Address: "); |
| 201 | + Serial.println(Ethernet.gatewayIP()); |
| 202 | + |
| 203 | + Serial.print("subnet IP mask: "); |
| 204 | + Serial.println(Ethernet.subnetMask()); |
| 205 | + |
| 206 | + Serial.print("DNS server: "); |
| 207 | + IPAddress dns = Ethernet.dnsServerIP(); |
| 208 | + if (dns == INADDR_NONE) { |
| 209 | + Serial.println("not set"); |
| 210 | + } else { |
| 211 | + Serial.println(dns); |
| 212 | + } |
| 213 | +} |
0 commit comments