### Retrieve Network Configuration and Status Source: https://context7.com/vjmuzik/nativeethernet/llms.txt Demonstrates how to initialize Ethernet via DHCP and print network configuration details, MAC address, hardware status, and link status to the Serial monitor. ```cpp #include byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; void setup() { Serial.begin(9600); while (!Serial) { ; } if (Ethernet.begin(mac) == 0) { Serial.println("DHCP failed"); while (true) { delay(1); } } // Get network configuration Serial.println("Network Configuration:"); Serial.println("======================"); Serial.print("IP Address: "); Serial.println(Ethernet.localIP()); Serial.print("Subnet Mask: "); Serial.println(Ethernet.subnetMask()); Serial.print("Gateway: "); Serial.println(Ethernet.gatewayIP()); Serial.print("DNS Server: "); Serial.println(Ethernet.dnsServerIP()); Serial.print("DHCP Server: "); Serial.println(Ethernet.dhcpServerIP()); // Get MAC address byte currentMAC[6]; Ethernet.MACAddress(currentMAC); Serial.print("MAC Address: "); for (int i = 0; i < 6; i++) { if (currentMAC[i] < 16) Serial.print("0"); Serial.print(currentMAC[i], HEX); if (i < 5) Serial.print(":"); } Serial.println(); // Hardware status Serial.print("Hardware: "); if (Ethernet.hardwareStatus() == EthernetW5500) { Serial.println("Native Ethernet (FNET)"); } // Link status Serial.print("Link Status: "); Serial.println(Ethernet.linkStatus() == LinkON ? "Connected" : "Disconnected"); } void loop() { // Modify network settings at runtime // Ethernet.setLocalIP(IPAddress(192, 168, 1, 200)); // Ethernet.setSubnetMask(IPAddress(255, 255, 255, 0)); // Ethernet.setGatewayIP(IPAddress(192, 168, 1, 1)); // Ethernet.setDnsServerIP(IPAddress(8, 8, 8, 8)); } ``` -------------------------------- ### Create a TCP Server Source: https://context7.com/vjmuzik/nativeethernet/llms.txt Initializes a server listening on a specific port. Use available() to accept incoming client connections and handle data streams. ```cpp #include byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192, 168, 1, 177); EthernetServer server(80); void setup() { Serial.begin(9600); while (!Serial) { ; } Ethernet.begin(mac, ip); server.begin(); Serial.print("Server is at "); Serial.println(Ethernet.localIP()); } void loop() { EthernetClient client = server.available(); if (client) { Serial.println("New client connected"); boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); // HTTP request ends with blank line if (c == '\n' && currentLineIsBlank) { // Send HTTP response client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connection: close"); client.println(); client.println(""); client.println(""); client.println("

Hello from Teensy 4.1!

"); client.print("

Analog 0: "); client.print(analogRead(0)); client.println("

"); break; } if (c == '\n') { currentLineIsBlank = true; } else if (c != '\r') { currentLineIsBlank = false; } } } delay(1); client.stop(); Serial.println("Client disconnected"); } } ``` -------------------------------- ### Ethernet.begin - Initialize with Static IP Source: https://context7.com/vjmuzik/nativeethernet/llms.txt Initializes the Ethernet interface with a manually configured static IP address, DNS server, gateway, and subnet mask. This method does not return a value and blocks until the link is established. ```APIDOC ## Ethernet.begin (Static IP) ### Description Initializes the Ethernet interface with a manually configured static IP address, DNS server, gateway, and subnet mask. This method does not return a value and blocks until the link is established. ### Method `Ethernet.begin(mac, ip, dns, gateway, subnet)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```cpp #include byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192, 168, 1, 177); IPAddress myDns(192, 168, 1, 1); IPAddress gateway(192, 168, 1, 1); IPAddress subnet(255, 255, 0, 0); void setup() { Serial.begin(9600); while (!Serial) { ; } // Initialize with full network configuration Ethernet.begin(mac, ip, myDns, gateway, subnet); if (Ethernet.hardwareStatus() == EthernetNoHardware) { Serial.println("Ethernet shield was not found."); while (true) { delay(1); } } if (Ethernet.linkStatus() == LinkOFF) { Serial.println("Ethernet cable is not connected."); } Serial.print("Server IP: "); Serial.println(Ethernet.localIP()); Serial.print("Gateway: "); Serial.println(Ethernet.gatewayIP()); Serial.print("Subnet: "); Serial.println(Ethernet.subnetMask()); } void loop() { } ``` ### Response This method does not return a value. It blocks until the link is established. #### Success Response None explicitly defined, but the method proceeds to network configuration. #### Response Example None. ``` -------------------------------- ### Create a TCP Client Source: https://context7.com/vjmuzik/nativeethernet/llms.txt Establishes a connection to a remote server using a hostname or IP address. Returns 1 on success and 0 on failure. ```cpp #include byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; char server[] = "www.google.com"; EthernetClient client; void setup() { Serial.begin(9600); while (!Serial) { ; } if (Ethernet.begin(mac) == 0) { Serial.println("Failed to configure Ethernet using DHCP"); while (true) { delay(1); } } Serial.print("My IP: "); Serial.println(Ethernet.localIP()); delay(1000); Serial.print("Connecting to "); Serial.println(server); if (client.connect(server, 80)) { Serial.print("Connected to "); Serial.println(client.remoteIP()); // Send HTTP GET request client.println("GET /search?q=arduino HTTP/1.1"); client.println("Host: www.google.com"); client.println("Connection: close"); client.println(); } else { Serial.println("Connection failed"); } } void loop() { // Read and print incoming data if (client.available()) { char c = client.read(); Serial.write(c); } // Check if server disconnected if (!client.connected()) { Serial.println("\nDisconnected"); client.stop(); while (true) { delay(1); } } } ``` -------------------------------- ### Initialize Ethernet with Static IP Source: https://context7.com/vjmuzik/nativeethernet/llms.txt Configures the Ethernet interface using a manual static IP, DNS, gateway, and subnet mask. ```cpp #include byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192, 168, 1, 177); IPAddress myDns(192, 168, 1, 1); IPAddress gateway(192, 168, 1, 1); IPAddress subnet(255, 255, 0, 0); void setup() { Serial.begin(9600); while (!Serial) { ; } // Initialize with full network configuration Ethernet.begin(mac, ip, myDns, gateway, subnet); if (Ethernet.hardwareStatus() == EthernetNoHardware) { Serial.println("Ethernet shield was not found."); while (true) { delay(1); } } if (Ethernet.linkStatus() == LinkOFF) { Serial.println("Ethernet cable is not connected."); } Serial.print("Server IP: "); Serial.println(Ethernet.localIP()); Serial.print("Gateway: "); Serial.println(Ethernet.gatewayIP()); Serial.print("Subnet: "); Serial.println(Ethernet.subnetMask()); } void loop() { } ``` -------------------------------- ### Initialize Ethernet with DHCP Source: https://context7.com/vjmuzik/nativeethernet/llms.txt Configures the Ethernet interface using DHCP. Requires calling Ethernet.maintain() in the loop for lease renewal. ```cpp #include byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; void setup() { Serial.begin(9600); while (!Serial) { ; } Serial.println("Initialize Ethernet with DHCP:"); if (Ethernet.begin(mac) == 0) { Serial.println("Failed to configure Ethernet using DHCP"); if (Ethernet.hardwareStatus() == EthernetNoHardware) { Serial.println("Ethernet shield was not found."); while (true) { delay(1); } } if (Ethernet.linkStatus() == LinkOFF) { Serial.println("Ethernet cable is not connected."); } } else { Serial.print("DHCP assigned IP: "); Serial.println(Ethernet.localIP()); } } void loop() { // Call maintain() periodically to handle DHCP lease renewal Ethernet.maintain(); } ``` -------------------------------- ### Enable mDNS Service Discovery Source: https://context7.com/vjmuzik/nativeethernet/llms.txt Configures mDNS to allow network discovery of the device by hostname. Requires the NativeEthernet library. ```cpp #include uint8_t mac[6]; EthernetServer server(443, true); // TLS-enabled server void teensyMAC(uint8_t *mac) { for(uint8_t by=0; by<2; by++) mac[by]=(HW_OCOTP_MAC1 >> ((1-by)*8)) & 0xFF; for(uint8_t by=0; by<4; by++) mac[by+2]=(HW_OCOTP_MAC0 >> ((3-by)*8)) & 0xFF; } void setup() { teensyMAC(mac); Serial.begin(9600); Ethernet.begin(mac); // Initialize mDNS with hostname and number of services MDNS.begin("Teensy41", 2); // Optionally set a custom service name // MDNS.setServiceName("My_Teensy_Server"); // Advertise HTTPS service on port 443 MDNS.addService("_https._tcp", 443); // For HTTP, use: // MDNS.addService("_http._tcp", 80); server.begin(); Serial.print("IP address: "); Serial.println(Ethernet.localIP()); Serial.println("Access via: https://Teensy41.local"); } void loop() { EthernetClient client = server.available(); if (client) { Serial.println("New client"); boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); if (c == '\n' && currentLineIsBlank) { client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connection: close"); client.println(); client.println("

mDNS Server

"); client.close(); break; } if (c == '\n') currentLineIsBlank = true; else if (c != '\r') currentLineIsBlank = false; } } delay(1); client.stop(); } } ``` -------------------------------- ### Ethernet.begin - Initialize with DHCP Source: https://context7.com/vjmuzik/nativeethernet/llms.txt Initializes the Ethernet interface using DHCP to automatically obtain an IP address. The method accepts a MAC address and optional timeout parameters. Returns 1 on success and 0 if DHCP configuration fails. ```APIDOC ## Ethernet.begin (DHCP) ### Description Initializes the Ethernet interface using DHCP to automatically obtain an IP address. The method accepts a MAC address and optional timeout parameters. Returns 1 on success and 0 if DHCP configuration fails. ### Method `Ethernet.begin(mac)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```cpp #include byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; void setup() { Serial.begin(9600); while (!Serial) { ; } Serial.println("Initialize Ethernet with DHCP:"); if (Ethernet.begin(mac) == 0) { Serial.println("Failed to configure Ethernet using DHCP"); if (Ethernet.hardwareStatus() == EthernetNoHardware) { Serial.println("Ethernet shield was not found."); while (true) { delay(1); } } if (Ethernet.linkStatus() == LinkOFF) { Serial.println("Ethernet cable is not connected."); } } else { Serial.print("DHCP assigned IP: "); Serial.println(Ethernet.localIP()); } } void loop() { // Call maintain() periodically to handle DHCP lease renewal Ethernet.maintain(); } ``` ### Response #### Success Response (1) Returns 1 on successful DHCP configuration. #### Error Response (0) Returns 0 if DHCP configuration fails. #### Response Example None explicitly defined, but success is indicated by a return value of 1. ``` -------------------------------- ### Create Secure HTTPS Server Source: https://context7.com/vjmuzik/nativeethernet/llms.txt Initialize an EthernetServer with TLS support enabled. Custom certificates can be provided for production environments using setSRVCert and setSRVKey. ```cpp #include uint8_t mac[6]; void teensyMAC(uint8_t *mac) { for(uint8_t by=0; by<2; by++) mac[by]=(HW_OCOTP_MAC1 >> ((1-by)*8)) & 0xFF; for(uint8_t by=0; by<4; by++) mac[by+2]=(HW_OCOTP_MAC0 >> ((3-by)*8)) & 0xFF; } // Create TLS-enabled server on port 443 EthernetServer server(443, true); // For custom certificates: // const char* serverCert = "-----BEGIN CERTIFICATE-----\n..."; // const char* serverKey = "-----BEGIN PRIVATE KEY-----\n..."; void setup() { teensyMAC(mac); Serial.begin(9600); while (!Serial) { ; } if (Ethernet.begin(mac) == 0) { Serial.println("DHCP failed"); while (true) { delay(1); } } // Optional: Set custom certificates // server.setSRVCert(serverCert, strlen(serverCert)); // server.setSRVKey(serverKey, strlen(serverKey)); server.begin(); Serial.print("HTTPS Server at: "); Serial.println(Ethernet.localIP()); } void loop() { EthernetClient client = server.available(); if (client) { Serial.println("New TLS client"); boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); if (c == '\n' && currentLineIsBlank) { client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connection: close"); client.println("Refresh: 5"); client.println(); client.println(""); client.println("

Secure Teensy Server

"); client.print("

Uptime: "); client.print(millis() / 1000); client.println(" seconds

"); client.close(); break; } if (c == '\n') currentLineIsBlank = true; else if (c != '\r') currentLineIsBlank = false; } } delay(1); client.stop(); Serial.println("Client disconnected"); } } ``` -------------------------------- ### Configure Ethernet Stack Memory Source: https://context7.com/vjmuzik/nativeethernet/llms.txt Adjust heap size, socket buffer sizes, and socket counts before initializing the Ethernet interface. These settings must be applied before calling Ethernet.begin(). ```cpp #include byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // Custom heap buffer (must be declared before setup) DMAMEM uint8_t customHeap[128 * 1024]; // 128KB custom heap void setup() { Serial.begin(9600); while (!Serial) { ; } // Configure stack heap size (default is 64KB) // Option 1: Just change the size, let library allocate Ethernet.setStackHeap(128 * 1024); // Option 2: Provide your own buffer // Ethernet.setStackHeap(customHeap, sizeof(customHeap)); // Configure socket buffer size (default is 2KB per socket) Ethernet.setSocketSize(4 * 1024); // 4KB per socket // Configure number of sockets (default is 8, max is 8) Ethernet.setSocketNum(4); // Use only 4 sockets // Now initialize Ethernet if (Ethernet.begin(mac) == 0) { Serial.println("DHCP failed"); while (true) { delay(1); } } Serial.print("IP: "); Serial.println(Ethernet.localIP()); } void loop() { Ethernet.maintain(); } ``` -------------------------------- ### Establish Secure HTTPS Connection Source: https://context7.com/vjmuzik/nativeethernet/llms.txt Enables TLS encryption by passing true as the third argument to the connect() method. Requires a secure port, typically 443. ```cpp #include uint8_t mac[6]; EthernetClient client; char server[] = "pjrc.com"; // Extract Teensy's unique MAC address from hardware void teensyMAC(uint8_t *mac) { for(uint8_t by=0; by<2; by++) mac[by]=(HW_OCOTP_MAC1 >> ((1-by)*8)) & 0xFF; for(uint8_t by=0; by<4; by++) mac[by+2]=(HW_OCOTP_MAC0 >> ((3-by)*8)) & 0xFF; } void setup() { teensyMAC(mac); Serial.begin(9600); while (!Serial) { ; } if (Ethernet.begin(mac) == 0) { Serial.println("Failed to configure Ethernet using DHCP"); while (true) { delay(1); } } Serial.print("IP: "); Serial.println(Ethernet.localIP()); delay(1000); } void loop() { // Connect with TLS encryption (port 443, tls=true) if (client.connect(server, 443, true)) { Serial.println("Connected with TLS"); client.println("GET /index.html HTTP/1.1"); client.println("Host: www.pjrc.com"); client.println("User-Agent: arduino-ethernet"); client.println("Connection: close"); client.println(); // Read response while (client.connected() || client.available()) { if (client.available()) { char c = client.read(); Serial.write(c); } } client.close(); client.stop(); } else { Serial.println("TLS connection failed"); } delay(10000); } ``` -------------------------------- ### Resolve Hostnames with DNSClient Source: https://context7.com/vjmuzik/nativeethernet/llms.txt Perform manual DNS lookups to resolve domain names to IP addresses. Requires an active Ethernet connection and a configured DNS server. ```cpp #include #include byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; DNSClient dns; void setup() { Serial.begin(9600); while (!Serial) { ; } if (Ethernet.begin(mac) == 0) { Serial.println("DHCP failed"); while (true) { delay(1); } } Serial.print("My IP: "); Serial.println(Ethernet.localIP()); Serial.print("DNS Server: "); Serial.println(Ethernet.dnsServerIP()); // Initialize DNS client with DNS server dns.begin(Ethernet.dnsServerIP()); // Resolve hostname IPAddress resolvedIP; const char* hostname = "www.google.com"; Serial.print("Resolving "); Serial.print(hostname); Serial.println("..."); int result = dns.getHostByName(hostname, resolvedIP, 5000); if (result == 1) { Serial.print("Resolved to: "); Serial.println(resolvedIP); } else { Serial.print("DNS lookup failed with error: "); Serial.println(result); } } void loop() { } ``` -------------------------------- ### Implement UDP Communication with EthernetUDP Source: https://context7.com/vjmuzik/nativeethernet/llms.txt Uses EthernetUDP to send and receive datagrams. Requires NativeEthernet.h and NativeEthernetUdp.h headers. ```cpp #include #include byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; unsigned int localPort = 8888; const char timeServer[] = "time.nist.gov"; const int NTP_PACKET_SIZE = 48; byte packetBuffer[NTP_PACKET_SIZE]; EthernetUDP Udp; void sendNTPpacket(const char* address) { memset(packetBuffer, 0, NTP_PACKET_SIZE); packetBuffer[0] = 0b11100011; // LI, Version, Mode packetBuffer[1] = 0; // Stratum packetBuffer[2] = 6; // Polling Interval packetBuffer[3] = 0xEC; // Peer Clock Precision packetBuffer[12] = 49; packetBuffer[13] = 0x4E; packetBuffer[14] = 49; packetBuffer[15] = 52; Udp.beginPacket(address, 123); Udp.write(packetBuffer, NTP_PACKET_SIZE); Udp.endPacket(); } void setup() { Serial.begin(9600); while (!Serial) { ; } if (Ethernet.begin(mac) == 0) { Serial.println("Failed to configure Ethernet using DHCP"); while (true) { delay(1); } } Udp.begin(localPort); Serial.print("Local IP: "); Serial.println(Ethernet.localIP()); } void loop() { sendNTPpacket(timeServer); delay(1000); if (Udp.parsePacket()) { Udp.read(packetBuffer, NTP_PACKET_SIZE); unsigned long highWord = word(packetBuffer[40], packetBuffer[41]); unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]); unsigned long secsSince1900 = highWord << 16 | lowWord; const unsigned long seventyYears = 2208988800UL; unsigned long epoch = secsSince1900 - seventyYears; Serial.print("Unix time: "); Serial.println(epoch); Serial.print("UTC: "); Serial.print((epoch % 86400L) / 3600); Serial.print(":"); Serial.print((epoch % 3600) / 60); Serial.print(":"); Serial.println(epoch % 60); } delay(10000); Ethernet.maintain(); } ``` -------------------------------- ### Monitor Ethernet Link Status Source: https://context7.com/vjmuzik/nativeethernet/llms.txt Checks the current physical link status of the Ethernet connection. ```cpp #include void setup() { Serial.begin(9600); } void loop() { auto link = Ethernet.linkStatus(); Serial.print("Link status: "); switch (link) { case Unknown: Serial.println("Unknown"); break; case LinkON: Serial.println("ON"); break; case LinkOFF: Serial.println("OFF"); break; } delay(1000); } ``` -------------------------------- ### Ethernet.linkStatus - Monitor Link Status Source: https://context7.com/vjmuzik/nativeethernet/llms.txt Returns the current Ethernet link status as an EthernetLinkStatus enum value: LinkON when connected, LinkOFF when disconnected, or Unknown if the status cannot be determined. ```APIDOC ## Ethernet.linkStatus ### Description Returns the current Ethernet link status as an `EthernetLinkStatus` enum value: `LinkON` when connected, `LinkOFF` when disconnected, or `Unknown` if the status cannot be determined. ### Method `Ethernet.linkStatus()` ### Parameters None ### Request Example ```cpp #include void setup() { Serial.begin(9600); } void loop() { auto link = Ethernet.linkStatus(); Serial.print("Link status: "); switch (link) { case Unknown: Serial.println("Unknown"); break; case LinkON: Serial.println("ON"); break; case LinkOFF: Serial.println("OFF"); break; } delay(1000); } ``` ### Response #### Success Response An `EthernetLinkStatus` enum value: - `Unknown`: The status cannot be determined. - `LinkON`: The Ethernet link is active. - `LinkOFF`: The Ethernet link is disconnected. #### Response Example ``` Link status: ON ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.