Skip to content

Commit 058595d

Browse files
authored
Merge pull request #190 from leonardocavagnis/eth_lib_fix_main
Ethernet library: add missing functions
2 parents e90fa20 + 7b6f193 commit 058595d

File tree

26 files changed

+1214
-295
lines changed

26 files changed

+1214
-295
lines changed

libraries/Ethernet/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Ethernet Library for Arduino
2+
3+
Provides Ethernet connectivity for Arduino boards using the Arduino Zephyr core, together with a shield or carrier featuring an Ethernet connector.
4+
5+
📖 For more information about this library please read the documentation [here](http://www.arduino.cc/en/Reference/Ethernet).
6+
7+
## License
8+
9+
Copyright (c) 2025 Arduino SA. All rights reserved.
10+
11+
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Link Status
3+
This sketch prints the ethernet link status. When the
4+
ethernet cable is connected the link status should go to "ON".
5+
*/
6+
7+
#include <ZephyrEthernet.h>
8+
9+
void setup() {
10+
Serial.begin(9600);
11+
}
12+
13+
void loop() {
14+
auto link = Ethernet.linkStatus();
15+
Serial.print("Link status: ");
16+
switch (link) {
17+
case Unknown:
18+
Serial.println("Unknown");
19+
break;
20+
case LinkON:
21+
Serial.println("ON");
22+
break;
23+
case LinkOFF:
24+
Serial.println("OFF");
25+
break;
26+
}
27+
delay(1000);
28+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
Telnet client
3+
4+
This sketch connects to a telnet server.
5+
You need a telnet server to test this.
6+
*/
7+
8+
#include "ZephyrClient.h"
9+
#include "ZephyrEthernet.h"
10+
11+
// The IP address will be dependent on your local network:
12+
IPAddress ip(192, 168, 1, 177);
13+
14+
// Example: To get the IP address of telehack.com (an example telnet server), run in a terminal:
15+
// ping telehack.com
16+
// or
17+
// nslookup telehack.com
18+
// Then use the returned IP address in the code.
19+
20+
// Enter the IP address of the server you're connecting to:
21+
IPAddress server(1, 1, 1, 1);
22+
int port = 23; // Telnet port
23+
24+
// Initialize the Ethernet client library
25+
// with the IP address and port of the server
26+
// that you want to connect to (port 23 is default for telnet;
27+
// if you're using Processing's ChatServer, use port 10002):
28+
ZephyrClient client;
29+
30+
void setup() {
31+
// Open serial communications and wait for port to open:
32+
Serial.begin(9600);
33+
while (!Serial) {
34+
; // wait for serial port to connect. Needed for native USB port only
35+
}
36+
37+
// Check for Ethernet hardware present
38+
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
39+
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
40+
while (true) {
41+
delay(1); // do nothing, no point running without Ethernet hardware
42+
}
43+
}
44+
45+
// in Zephyr system check if Ethernet is ready before proceeding to initialize
46+
while (Ethernet.linkStatus() != LinkON) {
47+
Serial.println("Waiting for link on");
48+
delay(100);
49+
}
50+
51+
// start the Ethernet connection:
52+
Ethernet.begin(ip);
53+
54+
// give the Ethernet shield a second to initialize:
55+
delay(1000);
56+
Serial.println("connecting...");
57+
58+
// if you get a connection, report back via serial:
59+
if (client.connect(server, port)) {
60+
Serial.println("connected");
61+
} else {
62+
// if you didn't get a connection to the server:
63+
Serial.println("connection failed");
64+
}
65+
}
66+
67+
void loop() {
68+
// if there are incoming bytes available
69+
// from the server, read them and print them:
70+
if (client.available()) {
71+
char c = client.read();
72+
Serial.print(c);
73+
}
74+
75+
// as long as there are bytes in the serial queue,
76+
// read them and send them out the socket if it's open:
77+
while (Serial.available() > 0) {
78+
char inChar = Serial.read();
79+
if (client.connected()) {
80+
client.print(inChar);
81+
}
82+
}
83+
84+
// if the server's disconnected, stop the client:
85+
if (!client.connected()) {
86+
Serial.println();
87+
Serial.println("disconnecting.");
88+
client.stop();
89+
// do nothing:
90+
while (true) {
91+
delay(1);
92+
}
93+
}
94+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
UDPSendReceiveString:
3+
This sketch receives UDP message strings, prints them to the serial port
4+
and sends an "acknowledge" string back to the sender
5+
6+
A Processing sketch is included at the end of file that can be used to send
7+
and received messages for testing with a computer.
8+
*/
9+
10+
#include <ZephyrEthernet.h>
11+
#include <ZephyrUDP.h>
12+
13+
// The IP address will be dependent on your local network:
14+
IPAddress ip(192, 168, 1, 177);
15+
16+
unsigned int localPort = 8888; // local port to listen on
17+
18+
// buffers for receiving and sending data
19+
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; // buffer to hold incoming packet,
20+
char ReplyBuffer[] = "acknowledged"; // a string to send back
21+
22+
// An EthernetUDP instance to let us send and receive packets over UDP
23+
ZephyrUDP Udp;
24+
25+
void setup() {
26+
// Open serial communications and wait for port to open:
27+
Serial.begin(9600);
28+
while (!Serial) {
29+
; // wait for serial port to connect. Needed for native USB port only
30+
}
31+
32+
// in Zephyr system check if Ethernet is ready before proceeding to initialize
33+
while (Ethernet.linkStatus() != LinkON) {
34+
Serial.println("Waiting for link on");
35+
delay(100);
36+
}
37+
38+
// start the Ethernet
39+
Ethernet.begin(ip);
40+
41+
// Check for Ethernet hardware present
42+
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
43+
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
44+
while (true) {
45+
delay(1); // do nothing, no point running without Ethernet hardware
46+
}
47+
}
48+
if (Ethernet.linkStatus() == LinkOFF) {
49+
Serial.println("Ethernet cable is not connected.");
50+
}
51+
52+
// start UDP
53+
Udp.begin(localPort);
54+
}
55+
56+
void loop() {
57+
// if there's data available, read a packet
58+
int packetSize = Udp.parsePacket();
59+
if (packetSize) {
60+
Serial.print("Received packet of size ");
61+
Serial.println(packetSize);
62+
Serial.print("From ");
63+
IPAddress remote = Udp.remoteIP();
64+
for (int i = 0; i < 4; i++) {
65+
Serial.print(remote[i], DEC);
66+
if (i < 3) {
67+
Serial.print(".");
68+
}
69+
}
70+
Serial.print(", port ");
71+
Serial.println(Udp.remotePort());
72+
73+
// read the packet into packetBufffer
74+
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
75+
Serial.println("Contents:");
76+
Serial.println(packetBuffer);
77+
78+
// send a reply to the IP address and port that sent us the packet we received
79+
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
80+
Udp.write(ReplyBuffer);
81+
Udp.endPacket();
82+
}
83+
delay(10);
84+
}
85+
86+
87+
/*
88+
Processing sketch to run with this example
89+
=====================================================
90+
91+
// Processing UDP example to send and receive string data from Arduino
92+
// press any key to send the "Hello Arduino" message
93+
94+
95+
import hypermedia.net.*;
96+
97+
UDP udp; // define the UDP object
98+
99+
100+
void setup() {
101+
udp = new UDP( this, 6000 ); // create a new datagram connection on port 6000
102+
//udp.log( true ); // <-- printout the connection activity
103+
udp.listen( true ); // and wait for incoming message
104+
}
105+
106+
void draw()
107+
{
108+
}
109+
110+
void keyPressed() {
111+
String ip = "192.168.1.177"; // the remote IP address
112+
int port = 8888; // the destination port
113+
114+
udp.send("Hello World", ip, port ); // the message to send
115+
116+
}
117+
118+
void receive( byte[] data ) { // <-- default handler
119+
//void receive( byte[] data, String ip, int port ) { // <-- extended handler
120+
121+
for(int i=0; i < data.length; i++)
122+
print(char(data[i]));
123+
println();
124+
}
125+
*/
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
3+
Udp NTP Client
4+
5+
Get the time from a Network Time Protocol (NTP) time server
6+
Demonstrates use of UDP sendPacket and ReceivePacket
7+
For more on NTP time servers and the messages needed to communicate with them,
8+
see http://en.wikipedia.org/wiki/Network_Time_Protocol
9+
10+
*/
11+
12+
#include <ZephyrEthernet.h>
13+
#include <ZephyrUDP.h>
14+
15+
unsigned int localPort = 8888; // local port to listen for UDP packets
16+
17+
const char timeServer[] = "time.nist.gov"; // time.nist.gov NTP server
18+
19+
const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
20+
21+
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
22+
23+
// A UDP instance to let us send and receive packets over UDP
24+
ZephyrUDP Udp;
25+
26+
void setup() {
27+
// Open serial communications and wait for port to open:
28+
Serial.begin(9600);
29+
while (!Serial) {
30+
; // wait for serial port to connect. Needed for native USB port only
31+
}
32+
33+
// in Zephyr system check if Ethernet is ready before proceeding to initialize
34+
while (Ethernet.linkStatus() != LinkON) {
35+
Serial.println("Waiting for link on");
36+
delay(100);
37+
}
38+
39+
// start Ethernet and UDP
40+
if (Ethernet.begin() == 0) {
41+
Serial.println("Failed to configure Ethernet using DHCP");
42+
// Check for Ethernet hardware present
43+
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
44+
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
45+
} else if (Ethernet.linkStatus() == LinkOFF) {
46+
Serial.println("Ethernet cable is not connected.");
47+
}
48+
// no point in carrying on, so do nothing forevermore:
49+
while (true) {
50+
delay(1);
51+
}
52+
}
53+
Udp.begin(localPort);
54+
}
55+
56+
void loop() {
57+
// send an NTP packet to a time server
58+
sendNTPpacket(timeServer);
59+
60+
// wait to see if a reply is available
61+
delay(1000);
62+
if (Udp.parsePacket()) {
63+
// We've received a packet, read the data from it
64+
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
65+
66+
// the timestamp starts at byte 40 of the received packet and is four bytes,
67+
// or two words, long. First, extract the two words:
68+
69+
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
70+
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
71+
// combine the four bytes (two words) into a long integer
72+
// this is NTP time (seconds since Jan 1 1900):
73+
unsigned long secsSince1900 = highWord << 16 | lowWord;
74+
Serial.print("Seconds since Jan 1 1900 = ");
75+
Serial.println(secsSince1900);
76+
77+
// now convert NTP time into everyday time:
78+
Serial.print("Unix time = ");
79+
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
80+
const unsigned long seventyYears = 2208988800UL;
81+
// subtract seventy years:
82+
unsigned long epoch = secsSince1900 - seventyYears;
83+
// print Unix time:
84+
Serial.println(epoch);
85+
86+
87+
// print the hour, minute and second:
88+
Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
89+
Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
90+
Serial.print(':');
91+
if (((epoch % 3600) / 60) < 10) {
92+
// In the first 10 minutes of each hour, we'll want a leading '0'
93+
Serial.print('0');
94+
}
95+
Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
96+
Serial.print(':');
97+
if ((epoch % 60) < 10) {
98+
// In the first 10 seconds of each minute, we'll want a leading '0'
99+
Serial.print('0');
100+
}
101+
Serial.println(epoch % 60); // print the second
102+
}
103+
// wait ten seconds before asking for the time again
104+
delay(10000);
105+
}
106+
107+
// send an NTP request to the time server at the given address
108+
void sendNTPpacket(const char* address) {
109+
// set all bytes in the buffer to 0
110+
memset(packetBuffer, 0, NTP_PACKET_SIZE);
111+
// Initialize values needed to form NTP request
112+
// (see URL above for details on the packets)
113+
packetBuffer[0] = 0b11100011; // LI, Version, Mode
114+
packetBuffer[1] = 0; // Stratum, or type of clock
115+
packetBuffer[2] = 6; // Polling Interval
116+
packetBuffer[3] = 0xEC; // Peer Clock Precision
117+
// 8 bytes of zero for Root Delay & Root Dispersion
118+
packetBuffer[12] = 49;
119+
packetBuffer[13] = 0x4E;
120+
packetBuffer[14] = 49;
121+
packetBuffer[15] = 52;
122+
123+
// all NTP fields have been given values, now
124+
// you can send a packet requesting a timestamp:
125+
Udp.beginPacket(address, 123); // NTP requests are to port 123
126+
Udp.write(packetBuffer, NTP_PACKET_SIZE);
127+
Udp.endPacket();
128+
}

0 commit comments

Comments
 (0)