aboutsummaryrefslogtreecommitdiff
path: root/libraries/Ethernet/examples
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/Ethernet/examples')
-rw-r--r--libraries/Ethernet/examples/BarometricPressureWebServer/BarometricPressureWebServer.ino222
-rw-r--r--libraries/Ethernet/examples/ChatServer/ChatServer.ino79
-rw-r--r--libraries/Ethernet/examples/CosmClient/CosmClient.ino161
-rw-r--r--libraries/Ethernet/examples/CosmClientString/CosmClientString.ino146
-rw-r--r--libraries/Ethernet/examples/DhcpAddressPrinter/DhcpAddressPrinter.ino59
-rw-r--r--libraries/Ethernet/examples/DhcpChatServer/DhcpChatServer.ino87
-rw-r--r--libraries/Ethernet/examples/PachubeClient/PachubeClient.ino163
-rw-r--r--libraries/Ethernet/examples/PachubeClientString/PachubeClientString.ino152
-rw-r--r--libraries/Ethernet/examples/TelnetClient/TelnetClient.ino93
-rw-r--r--libraries/Ethernet/examples/TwitterClient/TwitterClient.ino136
-rw-r--r--libraries/Ethernet/examples/UDPSendReceiveString/UDPSendReceiveString.ino118
-rw-r--r--libraries/Ethernet/examples/UdpNtpClient/UdpNtpClient.ino141
-rw-r--r--libraries/Ethernet/examples/WebClient/WebClient.ino88
-rw-r--r--libraries/Ethernet/examples/WebClientRepeating/WebClientRepeating.ino111
-rw-r--r--libraries/Ethernet/examples/WebServer/WebServer.ino100
15 files changed, 1856 insertions, 0 deletions
diff --git a/libraries/Ethernet/examples/BarometricPressureWebServer/BarometricPressureWebServer.ino b/libraries/Ethernet/examples/BarometricPressureWebServer/BarometricPressureWebServer.ino
new file mode 100644
index 0000000..bfbcb6d
--- /dev/null
+++ b/libraries/Ethernet/examples/BarometricPressureWebServer/BarometricPressureWebServer.ino
@@ -0,0 +1,222 @@
+/*
+ SCP1000 Barometric Pressure Sensor Display
+
+ Serves the output of a Barometric Pressure Sensor as a web page.
+ Uses the SPI library. For details on the sensor, see:
+ http://www.sparkfun.com/commerce/product_info.php?products_id=8161
+ http://www.vti.fi/en/support/obsolete_products/pressure_sensors/
+
+ This sketch adapted from Nathan Seidle's SCP1000 example for PIC:
+ http://www.sparkfun.com/datasheets/Sensors/SCP1000-Testing.zip
+
+ Circuit:
+ SCP1000 sensor attached to pins 6,7, and 11 - 13:
+ DRDY: pin 6
+ CSB: pin 7
+ MOSI: pin 11
+ MISO: pin 12
+ SCK: pin 13
+
+ created 31 July 2010
+ by Tom Igoe
+ */
+
+#include <Ethernet.h>
+// the sensor communicates using SPI, so include the library:
+#include <SPI.h>
+
+
+// assign a MAC address for the ethernet controller.
+// fill in your address here:
+byte mac[] = {
+ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
+// assign an IP address for the controller:
+IPAddress ip(192,168,1,20);
+IPAddress gateway(192,168,1,1);
+IPAddress subnet(255, 255, 255, 0);
+
+
+// Initialize the Ethernet server library
+// with the IP address and port you want to use
+// (port 80 is default for HTTP):
+EthernetServer server(80);
+
+
+//Sensor's memory register addresses:
+const int PRESSURE = 0x1F; //3 most significant bits of pressure
+const int PRESSURE_LSB = 0x20; //16 least significant bits of pressure
+const int TEMPERATURE = 0x21; //16 bit temperature reading
+
+// pins used for the connection with the sensor
+// the others you need are controlled by the SPI library):
+const int dataReadyPin = 6;
+const int chipSelectPin = 7;
+
+float temperature = 0.0;
+long pressure = 0;
+long lastReadingTime = 0;
+
+void setup() {
+ // start the SPI library:
+ SPI.begin();
+
+ // start the Ethernet connection and the server:
+ Ethernet.begin(mac, ip);
+ server.begin();
+
+ // initalize the data ready and chip select pins:
+ pinMode(dataReadyPin, INPUT);
+ pinMode(chipSelectPin, OUTPUT);
+
+ Serial.begin(9600);
+
+ //Configure SCP1000 for low noise configuration:
+ writeRegister(0x02, 0x2D);
+ writeRegister(0x01, 0x03);
+ writeRegister(0x03, 0x02);
+
+ // give the sensor and Ethernet shield time to set up:
+ delay(1000);
+
+ //Set the sensor to high resolution mode tp start readings:
+ writeRegister(0x03, 0x0A);
+
+}
+
+void loop() {
+ // check for a reading no more than once a second.
+ if (millis() - lastReadingTime > 1000){
+ // if there's a reading ready, read it:
+ // don't do anything until the data ready pin is high:
+ if (digitalRead(dataReadyPin) == HIGH) {
+ getData();
+ // timestamp the last time you got a reading:
+ lastReadingTime = millis();
+ }
+ }
+
+ // listen for incoming Ethernet connections:
+ listenForEthernetClients();
+}
+
+
+void getData() {
+ Serial.println("Getting reading");
+ //Read the temperature data
+ int tempData = readRegister(0x21, 2);
+
+ // convert the temperature to celsius and display it:
+ temperature = (float)tempData / 20.0;
+
+ //Read the pressure data highest 3 bits:
+ byte pressureDataHigh = readRegister(0x1F, 1);
+ pressureDataHigh &= 0b00000111; //you only needs bits 2 to 0
+
+ //Read the pressure data lower 16 bits:
+ unsigned int pressureDataLow = readRegister(0x20, 2);
+ //combine the two parts into one 19-bit number:
+ pressure = ((pressureDataHigh << 16) | pressureDataLow)/4;
+
+ Serial.print("Temperature: ");
+ Serial.print(temperature);
+ Serial.println(" degrees C");
+ Serial.print("Pressure: " + String(pressure));
+ Serial.println(" Pa");
+}
+
+void listenForEthernetClients() {
+ // listen for incoming clients
+ EthernetClient client = server.available();
+ if (client) {
+ Serial.println("Got a client");
+ // an http request ends with a blank line
+ boolean currentLineIsBlank = true;
+ while (client.connected()) {
+ if (client.available()) {
+ char c = client.read();
+ // if you've gotten to the end of the line (received a newline
+ // character) and the line is blank, the http request has ended,
+ // so you can send a reply
+ if (c == '\n' && currentLineIsBlank) {
+ // send a standard http response header
+ client.println("HTTP/1.1 200 OK");
+ client.println("Content-Type: text/html");
+ client.println();
+ // print the current readings, in HTML format:
+ client.print("Temperature: ");
+ client.print(temperature);
+ client.print(" degrees C");
+ client.println("<br />");
+ client.print("Pressure: " + String(pressure));
+ client.print(" Pa");
+ client.println("<br />");
+ break;
+ }
+ if (c == '\n') {
+ // you're starting a new line
+ currentLineIsBlank = true;
+ }
+ else if (c != '\r') {
+ // you've gotten a character on the current line
+ currentLineIsBlank = false;
+ }
+ }
+ }
+ // give the web browser time to receive the data
+ delay(1);
+ // close the connection:
+ client.stop();
+ }
+}
+
+
+//Send a write command to SCP1000
+void writeRegister(byte registerName, byte registerValue) {
+ // SCP1000 expects the register name in the upper 6 bits
+ // of the byte:
+ registerName <<= 2;
+ // command (read or write) goes in the lower two bits:
+ registerName |= 0b00000010; //Write command
+
+ // take the chip select low to select the device:
+ digitalWrite(chipSelectPin, LOW);
+
+ SPI.transfer(registerName); //Send register location
+ SPI.transfer(registerValue); //Send value to record into register
+
+ // take the chip select high to de-select:
+ digitalWrite(chipSelectPin, HIGH);
+}
+
+
+//Read register from the SCP1000:
+unsigned int readRegister(byte registerName, int numBytes) {
+ byte inByte = 0; // incoming from the SPI read
+ unsigned int result = 0; // result to return
+
+ // SCP1000 expects the register name in the upper 6 bits
+ // of the byte:
+ registerName <<= 2;
+ // command (read or write) goes in the lower two bits:
+ registerName &= 0b11111100; //Read command
+
+ // take the chip select low to select the device:
+ digitalWrite(chipSelectPin, LOW);
+ // send the device the register you want to read:
+ int command = SPI.transfer(registerName);
+ // send a value of 0 to read the first byte returned:
+ inByte = SPI.transfer(0x00);
+
+ result = inByte;
+ // if there's more than one byte returned,
+ // shift the first byte then get the second byte:
+ if (numBytes > 1){
+ result = inByte << 8;
+ inByte = SPI.transfer(0x00);
+ result = result |inByte;
+ }
+ // take the chip select high to de-select:
+ digitalWrite(chipSelectPin, HIGH);
+ // return the result:
+ return(result);
+}
diff --git a/libraries/Ethernet/examples/ChatServer/ChatServer.ino b/libraries/Ethernet/examples/ChatServer/ChatServer.ino
new file mode 100644
index 0000000..d50e5a6
--- /dev/null
+++ b/libraries/Ethernet/examples/ChatServer/ChatServer.ino
@@ -0,0 +1,79 @@
+/*
+ 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 shield attached to pins 10, 11, 12, 13
+ * Analog inputs attached to pins A0 through A5 (optional)
+
+ created 18 Dec 2009
+ by David A. Mellis
+ modified 9 Apr 2012
+ by Tom Igoe
+
+ */
+
+#include <SPI.h>
+#include <Ethernet.h>
+
+// 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 mac[] = {
+ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
+IPAddress ip(192,168,1, 177);
+IPAddress gateway(192,168,1, 1);
+IPAddress subnet(255, 255, 0, 0);
+
+
+// telnet defaults to port 23
+EthernetServer server(23);
+boolean alreadyConnected = false; // whether or not the client was connected previously
+
+void setup() {
+ // initialize the ethernet device
+ Ethernet.begin(mac, ip, gateway, subnet);
+ // start listening for clients
+ server.begin();
+ // Open serial communications and wait for port to open:
+ Serial.begin(9600);
+ while (!Serial) {
+ ; // wait for serial port to connect. Needed for Leonardo only
+ }
+
+
+ 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) {
+ // clead 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);
+ }
+ }
+}
+
+
+
diff --git a/libraries/Ethernet/examples/CosmClient/CosmClient.ino b/libraries/Ethernet/examples/CosmClient/CosmClient.ino
new file mode 100644
index 0000000..ec74278
--- /dev/null
+++ b/libraries/Ethernet/examples/CosmClient/CosmClient.ino
@@ -0,0 +1,161 @@
+/*
+ Cosm sensor client
+
+ This sketch connects an analog sensor to Cosm (http://www.cosm.com)
+ using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
+ the Adafruit Ethernet shield, either one will work, as long as it's got
+ a Wiznet Ethernet module on board.
+
+ This example has been updated to use version 2.0 of the cosm.com API.
+ To make it work, create a feed with a datastream, and give it the ID
+ sensor1. Or change the code below to match your feed.
+
+
+ Circuit:
+ * Analog sensor attached to analog in 0
+ * Ethernet shield attached to pins 10, 11, 12, 13
+
+ created 15 March 2010
+ updated 14 May 2012
+ by Tom Igoe with input from Usman Haque and Joe Saavedra
+
+http://arduino.cc/en/Tutorial/CosmClient
+ This code is in the public domain.
+
+ */
+
+#include <SPI.h>
+#include <Ethernet.h>
+
+#define APIKEY "YOUR API KEY GOES HERE" // replace your Cosm api key here
+#define FEEDID 00000 // replace your feed ID
+#define USERAGENT "My Project" // user agent is the project name
+
+// assign a MAC address for the ethernet controller.
+// Newer Ethernet shields have a MAC address printed on a sticker on the shield
+// fill in your address here:
+byte mac[] = {
+ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
+
+// fill in an available IP address on your network here,
+// for manual configuration:
+IPAddress ip(10,0,1,20);
+
+// initialize the library instance:
+EthernetClient client;
+
+// 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(216,52,233,121); // numeric IP for api.cosm.com
+char server[] = "api.cosm.com"; // name address for cosm API
+
+unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
+boolean lastConnected = false; // state of the connection last time through the main loop
+const unsigned long postingInterval = 10L*1000L; // delay between updates to cosm.com
+ // the "L" is needed to use long type numbers
+
+
+void setup() {
+ // start serial port:
+ Serial.begin(9600);
+ // start the Ethernet connection:
+ if (Ethernet.begin(mac) == 0) {
+ Serial.println("Failed to configure Ethernet using DHCP");
+ // DHCP failed, so use a fixed IP address:
+ Ethernet.begin(mac, ip);
+ }
+}
+
+void loop() {
+ // read the analog sensor:
+ int sensorReading = analogRead(A0);
+
+ // if there's incoming data from the net connection.
+ // send it out the serial port. This is for debugging
+ // purposes only:
+ if (client.available()) {
+ char c = client.read();
+ Serial.print(c);
+ }
+
+ // if there's no net connection, but there was one last time
+ // through the loop, then stop the client:
+ if (!client.connected() && lastConnected) {
+ Serial.println();
+ Serial.println("disconnecting.");
+ client.stop();
+ }
+
+ // if you're not connected, and ten seconds have passed since
+ // your last connection, then connect again and send data:
+ if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
+ sendData(sensorReading);
+ }
+ // store the state of the connection for next time through
+ // the loop:
+ lastConnected = client.connected();
+}
+
+// this method makes a HTTP connection to the server:
+void sendData(int thisData) {
+ // if there's a successful connection:
+ if (client.connect(server, 80)) {
+ Serial.println("connecting...");
+ // send the HTTP PUT request:
+ client.print("PUT /v2/feeds/");
+ client.print(FEEDID);
+ client.println(".csv HTTP/1.1");
+ client.println("Host: api.cosm.com");
+ client.print("X-ApiKey: ");
+ client.println(APIKEY);
+ client.print("User-Agent: ");
+ client.println(USERAGENT);
+ client.print("Content-Length: ");
+
+ // calculate the length of the sensor reading in bytes:
+ // 8 bytes for "sensor1," + number of digits of the data:
+ int thisLength = 8 + getLength(thisData);
+ client.println(thisLength);
+
+ // last pieces of the HTTP PUT request:
+ client.println("Content-Type: text/csv");
+ client.println("Connection: close");
+ client.println();
+
+ // here's the actual content of the PUT request:
+ client.print("sensor1,");
+ client.println(thisData);
+
+ }
+ else {
+ // if you couldn't make a connection:
+ Serial.println("connection failed");
+ Serial.println();
+ Serial.println("disconnecting.");
+ client.stop();
+ }
+ // note the time that the connection was made or attempted:
+ lastConnectionTime = millis();
+}
+
+
+// This method calculates the number of digits in the
+// sensor reading. Since each digit of the ASCII decimal
+// representation is a byte, the number of digits equals
+// the number of bytes:
+
+int getLength(int someValue) {
+ // there's at least one byte:
+ int digits = 1;
+ // continually divide the value by ten,
+ // adding one to the digit count for each
+ // time you divide, until you're at 0:
+ int dividend = someValue /10;
+ while (dividend > 0) {
+ dividend = dividend /10;
+ digits++;
+ }
+ // return the number of digits:
+ return digits;
+}
+
diff --git a/libraries/Ethernet/examples/CosmClientString/CosmClientString.ino b/libraries/Ethernet/examples/CosmClientString/CosmClientString.ino
new file mode 100644
index 0000000..e619924
--- /dev/null
+++ b/libraries/Ethernet/examples/CosmClientString/CosmClientString.ino
@@ -0,0 +1,146 @@
+/*
+ Cosm sensor client with Strings
+
+ This sketch connects an analog sensor to Cosm (http://www.cosm.com)
+ using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
+ the Adafruit Ethernet shield, either one will work, as long as it's got
+ a Wiznet Ethernet module on board.
+
+ This example has been updated to use version 2.0 of the Cosm.com API.
+ To make it work, create a feed with two datastreams, and give them the IDs
+ sensor1 and sensor2. Or change the code below to match your feed.
+
+ This example uses the String library, which is part of the Arduino core from
+ version 0019.
+
+ Circuit:
+ * Analog sensor attached to analog in 0
+ * Ethernet shield attached to pins 10, 11, 12, 13
+
+ created 15 March 2010
+ updated 14 May 2012
+ by Tom Igoe with input from Usman Haque and Joe Saavedra
+
+ http://arduino.cc/en/Tutorial/CosmClientString
+ This code is in the public domain.
+
+ */
+
+#include <SPI.h>
+#include <Ethernet.h>
+
+
+#define APIKEY "YOUR API KEY GOES HERE" // replace your Cosm api key here
+#define FEEDID 00000 // replace your feed ID
+#define USERAGENT "My Project" // user agent is the project name
+
+// assign a MAC address for the ethernet controller.
+// fill in your address here:
+ byte mac[] = {
+ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
+
+// fill in an available IP address on your network here,
+// for manual configuration:
+IPAddress ip(10,0,1,20);
+
+// initialize the library instance:
+EthernetClient client;
+
+// 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(216,52,233,121); // numeric IP for api.cosm.com
+char server[] = "api.cosm.com"; // name address for Cosm API
+
+unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
+boolean lastConnected = false; // state of the connection last time through the main loop
+const unsigned long postingInterval = 10L*1000L; // delay between updates to Cosm.com
+ // the "L" is needed to use long type numbers
+void setup() {
+ // start serial port:
+ Serial.begin(9600);
+ // give the ethernet module time to boot up:
+ delay(1000);
+ // start the Ethernet connection:
+ if (Ethernet.begin(mac) == 0) {
+ Serial.println("Failed to configure Ethernet using DHCP");
+ // DHCP failed, so use a fixed IP address:
+ Ethernet.begin(mac, ip);
+ }
+}
+
+void loop() {
+ // read the analog sensor:
+ int sensorReading = analogRead(A0);
+ // convert the data to a String to send it:
+
+ String dataString = "sensor1,";
+ dataString += sensorReading;
+
+ // you can append multiple readings to this String if your
+ // Cosm feed is set up to handle multiple values:
+ int otherSensorReading = analogRead(A1);
+ dataString += "\nsensor2,";
+ dataString += otherSensorReading;
+
+ // if there's incoming data from the net connection.
+ // send it out the serial port. This is for debugging
+ // purposes only:
+ if (client.available()) {
+ char c = client.read();
+ Serial.print(c);
+ }
+
+ // if there's no net connection, but there was one last time
+ // through the loop, then stop the client:
+ if (!client.connected() && lastConnected) {
+ Serial.println();
+ Serial.println("disconnecting.");
+ client.stop();
+ }
+
+ // if you're not connected, and ten seconds have passed since
+ // your last connection, then connect again and send data:
+ if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
+ sendData(dataString);
+ }
+ // store the state of the connection for next time through
+ // the loop:
+ lastConnected = client.connected();
+}
+
+// this method makes a HTTP connection to the server:
+void sendData(String thisData) {
+ // if there's a successful connection:
+ if (client.connect(server, 80)) {
+ Serial.println("connecting...");
+ // send the HTTP PUT request:
+ client.print("PUT /v2/feeds/");
+ client.print(FEEDID);
+ client.println(".csv HTTP/1.1");
+ client.println("Host: api.cosm.com");
+ client.print("X-ApiKey: ");
+ client.println(APIKEY);
+ client.print("User-Agent: ");
+ client.println(USERAGENT);
+ client.print("Content-Length: ");
+ client.println(thisData.length());
+
+ // last pieces of the HTTP PUT request:
+ client.println("Content-Type: text/csv");
+ client.println("Connection: close");
+ client.println();
+
+ // here's the actual content of the PUT request:
+ client.println(thisData);
+ }
+ else {
+ // if you couldn't make a connection:
+ Serial.println("connection failed");
+ Serial.println();
+ Serial.println("disconnecting.");
+ client.stop();
+ }
+ // note the time that the connection was made or attempted:
+ lastConnectionTime = millis();
+}
+
diff --git a/libraries/Ethernet/examples/DhcpAddressPrinter/DhcpAddressPrinter.ino b/libraries/Ethernet/examples/DhcpAddressPrinter/DhcpAddressPrinter.ino
new file mode 100644
index 0000000..5eaaf24
--- /dev/null
+++ b/libraries/Ethernet/examples/DhcpAddressPrinter/DhcpAddressPrinter.ino
@@ -0,0 +1,59 @@
+/*
+ DHCP-based IP printer
+
+ This sketch uses the DHCP extensions to the Ethernet library
+ to get an IP address via DHCP and print the address obtained.
+ using an Arduino Wiznet Ethernet shield.
+
+ Circuit:
+ * Ethernet shield attached to pins 10, 11, 12, 13
+
+ created 12 April 2011
+ modified 9 Apr 2012
+ by Tom Igoe
+
+ */
+
+#include <SPI.h>
+#include <Ethernet.h>
+
+// Enter a MAC address for your controller below.
+// Newer Ethernet shields have a MAC address printed on a sticker on the shield
+byte mac[] = {
+ 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
+
+// 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;
+
+void setup() {
+ // Open serial communications and wait for port to open:
+ Serial.begin(9600);
+ // this check is only needed on the Leonardo:
+ while (!Serial) {
+ ; // wait for serial port to connect. Needed for Leonardo only
+ }
+
+ // start the Ethernet connection:
+ if (Ethernet.begin(mac) == 0) {
+ Serial.println("Failed to configure Ethernet using DHCP");
+ // no point in carrying on, so do nothing forevermore:
+ for(;;)
+ ;
+ }
+ // print your local IP address:
+ Serial.print("My IP address: ");
+ for (byte thisByte = 0; thisByte < 4; thisByte++) {
+ // print the value of each byte of the IP address:
+ Serial.print(Ethernet.localIP()[thisByte], DEC);
+ Serial.print(".");
+ }
+ Serial.println();
+}
+
+void loop() {
+
+}
+
+
diff --git a/libraries/Ethernet/examples/DhcpChatServer/DhcpChatServer.ino b/libraries/Ethernet/examples/DhcpChatServer/DhcpChatServer.ino
new file mode 100644
index 0000000..09cbd43
--- /dev/null
+++ b/libraries/Ethernet/examples/DhcpChatServer/DhcpChatServer.ino
@@ -0,0 +1,87 @@
+/*
+ DHCP 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.
+
+ THis version attempts to get an IP address using DHCP
+
+ Circuit:
+ * Ethernet shield attached to pins 10, 11, 12, 13
+
+ created 21 May 2011
+ modified 9 Apr 2012
+ by Tom Igoe
+ Based on ChatServer example by David A. Mellis
+
+ */
+
+#include <SPI.h>
+#include <Ethernet.h>
+
+// 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 mac[] = {
+ 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
+IPAddress ip(192,168,1, 177);
+IPAddress gateway(192,168,1, 1);
+IPAddress subnet(255, 255, 0, 0);
+
+// telnet defaults to port 23
+EthernetServer server(23);
+boolean gotAMessage = false; // whether or not you got a message from the client yet
+
+void setup() {
+ // Open serial communications and wait for port to open:
+ Serial.begin(9600);
+ // this check is only needed on the Leonardo:
+ while (!Serial) {
+ ; // wait for serial port to connect. Needed for Leonardo only
+ }
+
+
+ // start the Ethernet connection:
+ Serial.println("Trying to get an IP address using DHCP");
+ if (Ethernet.begin(mac) == 0) {
+ Serial.println("Failed to configure Ethernet using DHCP");
+ // initialize the ethernet device not using DHCP:
+ Ethernet.begin(mac, ip, gateway, subnet);
+ }
+ // print your local IP address:
+ Serial.print("My IP address: ");
+ ip = Ethernet.localIP();
+ for (byte thisByte = 0; thisByte < 4; thisByte++) {
+ // print the value of each byte of the IP address:
+ Serial.print(ip[thisByte], DEC);
+ Serial.print(".");
+ }
+ Serial.println();
+ // start listening for clients
+ server.begin();
+
+}
+
+void loop() {
+ // wait for a new client:
+ EthernetClient client = server.available();
+
+ // when the client sends the first byte, say hello:
+ if (client) {
+ if (!gotAMessage) {
+ Serial.println("We have a new client");
+ client.println("Hello, client!");
+ gotAMessage = true;
+ }
+
+ // 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.print(thisChar);
+ }
+}
+
diff --git a/libraries/Ethernet/examples/PachubeClient/PachubeClient.ino b/libraries/Ethernet/examples/PachubeClient/PachubeClient.ino
new file mode 100644
index 0000000..dfd2d40
--- /dev/null
+++ b/libraries/Ethernet/examples/PachubeClient/PachubeClient.ino
@@ -0,0 +1,163 @@
+/*
+ Pachube sensor client
+
+ This sketch connects an analog sensor to Pachube (http://www.pachube.com)
+ using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
+ the Adafruit Ethernet shield, either one will work, as long as it's got
+ a Wiznet Ethernet module on board.
+
+ This example has been updated to use version 2.0 of the Pachube.com API.
+ To make it work, create a feed with a datastream, and give it the ID
+ sensor1. Or change the code below to match your feed.
+
+
+ Circuit:
+ * Analog sensor attached to analog in 0
+ * Ethernet shield attached to pins 10, 11, 12, 13
+
+ created 15 March 2010
+ modified 9 Apr 2012
+ by Tom Igoe with input from Usman Haque and Joe Saavedra
+
+http://arduino.cc/en/Tutorial/PachubeClient
+ This code is in the public domain.
+
+ */
+
+#include <SPI.h>
+#include <Ethernet.h>
+
+#define APIKEY "YOUR API KEY GOES HERE" // replace your pachube api key here
+#define FEEDID 00000 // replace your feed ID
+#define USERAGENT "My Project" // user agent is the project name
+
+// assign a MAC address for the ethernet controller.
+// Newer Ethernet shields have a MAC address printed on a sticker on the shield
+// fill in your address here:
+byte mac[] = {
+ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
+
+// fill in an available IP address on your network here,
+// for manual configuration:
+IPAddress ip(10,0,1,20);
+// initialize the library instance:
+EthernetClient client;
+
+// 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(216,52,233,122); // numeric IP for api.pachube.com
+//char server[] = "api.pachube.com"; // name address for pachube API
+
+unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
+boolean lastConnected = false; // state of the connection last time through the main loop
+const unsigned long postingInterval = 10*1000; //delay between updates to Pachube.com
+
+void setup() {
+ // Open serial communications and wait for port to open:
+ Serial.begin(9600);
+ while (!Serial) {
+ ; // wait for serial port to connect. Needed for Leonardo only
+ }
+
+
+ // start the Ethernet connection:
+ if (Ethernet.begin(mac) == 0) {
+ Serial.println("Failed to configure Ethernet using DHCP");
+ // DHCP failed, so use a fixed IP address:
+ Ethernet.begin(mac, ip);
+ }
+}
+
+void loop() {
+ // read the analog sensor:
+ int sensorReading = analogRead(A0);
+
+ // if there's incoming data from the net connection.
+ // send it out the serial port. This is for debugging
+ // purposes only:
+ if (client.available()) {
+ char c = client.read();
+ Serial.print(c);
+ }
+
+ // if there's no net connection, but there was one last time
+ // through the loop, then stop the client:
+ if (!client.connected() && lastConnected) {
+ Serial.println();
+ Serial.println("disconnecting.");
+ client.stop();
+ }
+
+ // if you're not connected, and ten seconds have passed since
+ // your last connection, then connect again and send data:
+ if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
+ sendData(sensorReading);
+ }
+ // store the state of the connection for next time through
+ // the loop:
+ lastConnected = client.connected();
+}
+
+// this method makes a HTTP connection to the server:
+void sendData(int thisData) {
+ // if there's a successful connection:
+ if (client.connect(server, 80)) {
+ Serial.println("connecting...");
+ // send the HTTP PUT request:
+ client.print("PUT /v2/feeds/");
+ client.print(FEEDID);
+ client.println(".csv HTTP/1.1");
+ client.println("Host: api.pachube.com");
+ client.print("X-PachubeApiKey: ");
+ client.println(APIKEY);
+ client.print("User-Agent: ");
+ client.println(USERAGENT);
+ client.print("Content-Length: ");
+
+ // calculate the length of the sensor reading in bytes:
+ // 8 bytes for "sensor1," + number of digits of the data:
+ int thisLength = 8 + getLength(thisData);
+ client.println(thisLength);
+
+ // last pieces of the HTTP PUT request:
+ client.println("Content-Type: text/csv");
+ client.println("Connection: close");
+ client.println();
+
+ // here's the actual content of the PUT request:
+ client.print("sensor1,");
+ client.println(thisData);
+
+ }
+ else {
+ // if you couldn't make a connection:
+ Serial.println("connection failed");
+ Serial.println();
+ Serial.println("disconnecting.");
+ client.stop();
+ }
+ // note the time that the connection was made or attempted:
+ lastConnectionTime = millis();
+}
+
+
+// This method calculates the number of digits in the
+// sensor reading. Since each digit of the ASCII decimal
+// representation is a byte, the number of digits equals
+// the number of bytes:
+
+int getLength(int someValue) {
+ // there's at least one byte:
+ int digits = 1;
+ // continually divide the value by ten,
+ // adding one to the digit count for each
+ // time you divide, until you're at 0:
+ int dividend = someValue /10;
+ while (dividend > 0) {
+ dividend = dividend /10;
+ digits++;
+ }
+ // return the number of digits:
+ return digits;
+}
+
diff --git a/libraries/Ethernet/examples/PachubeClientString/PachubeClientString.ino b/libraries/Ethernet/examples/PachubeClientString/PachubeClientString.ino
new file mode 100644
index 0000000..2a96e9f
--- /dev/null
+++ b/libraries/Ethernet/examples/PachubeClientString/PachubeClientString.ino
@@ -0,0 +1,152 @@
+/*
+ Cosm sensor client with Strings
+
+ This sketch connects an analog sensor to Cosm (http://www.cosm.com)
+ using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
+ the Adafruit Ethernet shield, either one will work, as long as it's got
+ a Wiznet Ethernet module on board.
+
+ This example has been updated to use version 2.0 of the Cosm.com API.
+ To make it work, create a feed with two datastreams, and give them the IDs
+ sensor1 and sensor2. Or change the code below to match your feed.
+
+ This example uses the String library, which is part of the Arduino core from
+ version 0019.
+
+ Circuit:
+ * Analog sensor attached to analog in 0
+ * Ethernet shield attached to pins 10, 11, 12, 13
+
+ created 15 March 2010
+ modified 9 Apr 2012
+ by Tom Igoe with input from Usman Haque and Joe Saavedra
+
+ http://arduino.cc/en/Tutorial/CosmClientString
+ This code is in the public domain.
+
+ */
+
+#include <SPI.h>
+#include <Ethernet.h>
+
+
+/#define APIKEY "YOUR API KEY GOES HERE" // replace your Cosm api key here
+#define FEEDID 00000 // replace your feed ID
+#define USERAGENT "My Project" // user agent is the project name
+
+
+// assign a MAC address for the ethernet controller.
+// fill in your address here:
+ byte mac[] = {
+ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
+
+// fill in an available IP address on your network here,
+// for manual configuration:
+IPAddress ip(10,0,1,20);
+
+// initialize the library instance:
+EthernetClient client;
+
+// 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(216,52,233,121); // numeric IP for api.cosm.com
+//char server[] = "api.cosm.com"; // name address for Cosm API
+
+unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
+boolean lastConnected = false; // state of the connection last time through the main loop
+const unsigned long postingInterval = 10*1000; //delay between updates to Cosm.com
+
+void setup() {
+ // Open serial communications and wait for port to open:
+ Serial.begin(9600);
+ while (!Serial) {
+ ; // wait for serial port to connect. Needed for Leonardo only
+ }
+
+
+ // give the ethernet module time to boot up:
+ delay(1000);
+ // start the Ethernet connection:
+ if (Ethernet.begin(mac) == 0) {
+ Serial.println("Failed to configure Ethernet using DHCP");
+ // DHCP failed, so use a fixed IP address:
+ Ethernet.begin(mac, ip);
+ }
+}
+
+void loop() {
+ // read the analog sensor:
+ int sensorReading = analogRead(A0);
+ // convert the data to a String to send it:
+
+ String dataString = "sensor1,";
+ dataString += sensorReading;
+
+ // you can append multiple readings to this String if your
+ // Cosm feed is set up to handle multiple values:
+ int otherSensorReading = analogRead(A1);
+ dataString += "\nsensor2,";
+ dataString += otherSensorReading;
+
+ // if there's incoming data from the net connection.
+ // send it out the serial port. This is for debugging
+ // purposes only:
+ if (client.available()) {
+ char c = client.read();
+ Serial.print(c);
+ }
+
+ // if there's no net connection, but there was one last time
+ // through the loop, then stop the client:
+ if (!client.connected() && lastConnected) {
+ Serial.println();
+ Serial.println("disconnecting.");
+ client.stop();
+ }
+
+ // if you're not connected, and ten seconds have passed since
+ // your last connection, then connect again and send data:
+ if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
+ sendData(dataString);
+ }
+ // store the state of the connection for next time through
+ // the loop:
+ lastConnected = client.connected();
+}
+
+// this method makes a HTTP connection to the server:
+void sendData(String thisData) {
+ // if there's a successful connection:
+ if (client.connect(server, 80)) {
+ Serial.println("connecting...");
+ // send the HTTP PUT request:
+ client.print("PUT /v2/feeds/");
+ client.print(FEEDID);
+ client.println(".csv HTTP/1.1");
+ client.println("Host: api.cosm.com");
+ client.print("X-CosmApiKey: ");
+ client.println(APIKEY);
+ client.print("User-Agent: ");
+ client.println(USERAGENT);
+ client.print("Content-Length: ");
+ client.println(thisData.length());
+
+ // last pieces of the HTTP PUT request:
+ client.println("Content-Type: text/csv");
+ client.println("Connection: close");
+ client.println();
+
+ // here's the actual content of the PUT request:
+ client.println(thisData);
+ }
+ else {
+ // if you couldn't make a connection:
+ Serial.println("connection failed");
+ Serial.println();
+ Serial.println("disconnecting.");
+ client.stop();
+ }
+ // note the time that the connection was made or attempted:
+ lastConnectionTime = millis();
+}
+
diff --git a/libraries/Ethernet/examples/TelnetClient/TelnetClient.ino b/libraries/Ethernet/examples/TelnetClient/TelnetClient.ino
new file mode 100644
index 0000000..3457125
--- /dev/null
+++ b/libraries/Ethernet/examples/TelnetClient/TelnetClient.ino
@@ -0,0 +1,93 @@
+/*
+ Telnet client
+
+ This sketch connects to a a telnet server (http://www.google.com)
+ using an Arduino Wiznet Ethernet shield. You'll need a telnet server
+ to test this with.
+ Processing's ChatServer example (part of the network library) works well,
+ running on port 10002. It can be found as part of the examples
+ in the Processing application, available at
+ http://processing.org/
+
+ Circuit:
+ * Ethernet shield attached to pins 10, 11, 12, 13
+
+ created 14 Sep 2010
+ modified 9 Apr 2012
+ by Tom Igoe
+
+ */
+
+#include <SPI.h>
+#include <Ethernet.h>
+
+// Enter a MAC address and IP address for your controller below.
+// The IP address will be dependent on your local network:
+byte mac[] = {
+ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
+IPAddress ip(192,168,1,177);
+
+// Enter the IP address of the server you're connecting to:
+IPAddress server(1,1,1,1);
+
+// Initialize the Ethernet client library
+// with the IP address and port of the server
+// that you want to connect to (port 23 is default for telnet;
+// if you're using Processing's ChatServer, use port 10002):
+EthernetClient client;
+
+void setup() {
+ // start the Ethernet connection:
+ Ethernet.begin(mac, ip);
+ // Open serial communications and wait for port to open:
+ Serial.begin(9600);
+ while (!Serial) {
+ ; // wait for serial port to connect. Needed for Leonardo only
+ }
+
+
+ // give the Ethernet shield a second to initialize:
+ delay(1000);
+ Serial.println("connecting...");
+
+ // if you get a connection, report back via serial:
+ if (client.connect(server, 10002)) {
+ Serial.println("connected");
+ }
+ else {
+ // if you didn't get a connection to the server:
+ Serial.println("connection failed");
+ }
+}
+
+void loop()
+{
+ // if there are incoming bytes available
+ // from the server, read them and print them:
+ if (client.available()) {
+ char c = client.read();
+ Serial.print(c);
+ }
+
+ // as long as there are bytes in the serial queue,
+ // read them and send them out the socket if it's open:
+ while (Serial.available() > 0) {
+ char inChar = Serial.read();
+ if (client.connected()) {
+ client.print(inChar);
+ }
+ }
+
+ // if the server's disconnected, stop the client:
+ if (!client.connected()) {
+ Serial.println();
+ Serial.println("disconnecting.");
+ client.stop();
+ // do nothing:
+ while(true);
+ }
+}
+
+
+
+
diff --git a/libraries/Ethernet/examples/TwitterClient/TwitterClient.ino b/libraries/Ethernet/examples/TwitterClient/TwitterClient.ino
new file mode 100644
index 0000000..9fee1fe
--- /dev/null
+++ b/libraries/Ethernet/examples/TwitterClient/TwitterClient.ino
@@ -0,0 +1,136 @@
+/*
+ Twitter Client with Strings
+
+ This sketch connects to Twitter using an Ethernet shield. It parses the XML
+ returned, and looks for <text>this is a tweet</text>
+
+ You can use the Arduino Ethernet shield, or the Adafruit Ethernet shield,
+ either one will work, as long as it's got a Wiznet Ethernet module on board.
+
+ This example uses the DHCP routines in the Ethernet library which is part of the
+ Arduino core from version 1.0 beta 1
+
+ This example uses the String library, which is part of the Arduino core from
+ version 0019.
+
+ Circuit:
+ * Ethernet shield attached to pins 10, 11, 12, 13
+
+ created 21 May 2011
+ modified 9 Apr 2012
+ by Tom Igoe
+
+ This code is in the public domain.
+
+ */
+#include <SPI.h>
+#include <Ethernet.h>
+
+
+// Enter a MAC address and IP address for your controller below.
+// The IP address will be dependent on your local network:
+byte mac[] = {
+ 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };
+IPAddress ip(192,168,1,20);
+
+// initialize the library instance:
+EthernetClient client;
+
+const unsigned long requestInterval = 60000; // delay between requests
+
+char serverName[] = "api.twitter.com"; // twitter URL
+
+boolean requested; // whether you've made a request since connecting
+unsigned long lastAttemptTime = 0; // last time you connected to the server, in milliseconds
+
+String currentLine = ""; // string to hold the text from server
+String tweet = ""; // string to hold the tweet
+boolean readingTweet = false; // if you're currently reading the tweet
+
+void setup() {
+ // reserve space for the strings:
+ currentLine.reserve(256);
+ tweet.reserve(150);
+
+ // Open serial communications and wait for port to open:
+ Serial.begin(9600);
+ while (!Serial) {
+ ; // wait for serial port to connect. Needed for Leonardo only
+ }
+
+
+ // attempt a DHCP connection:
+ Serial.println("Attempting to get an IP address using DHCP:");
+ if (!Ethernet.begin(mac)) {
+ // if DHCP fails, start with a hard-coded address:
+ Serial.println("failed to get an IP address using DHCP, trying manually");
+ Ethernet.begin(mac, ip);
+ }
+ Serial.print("My address:");
+ Serial.println(Ethernet.localIP());
+ // connect to Twitter:
+ connectToServer();
+}
+
+
+
+void loop()
+{
+ if (client.connected()) {
+ if (client.available()) {
+ // read incoming bytes:
+ char inChar = client.read();
+
+ // add incoming byte to end of line:
+ currentLine += inChar;
+
+ // if you get a newline, clear the line:
+ if (inChar == '\n') {
+ currentLine = "";
+ }
+ // if the current line ends with <text>, it will
+ // be followed by the tweet:
+ if ( currentLine.endsWith("<text>")) {
+ // tweet is beginning. Clear the tweet string:
+ readingTweet = true;
+ tweet = "";
+ }
+ // if you're currently reading the bytes of a tweet,
+ // add them to the tweet String:
+ if (readingTweet) {
+ if (inChar != '<') {
+ tweet += inChar;
+ }
+ else {
+ // if you got a "<" character,
+ // you've reached the end of the tweet:
+ readingTweet = false;
+ Serial.println(tweet);
+ // close the connection to the server:
+ client.stop();
+ }
+ }
+ }
+ }
+ else if (millis() - lastAttemptTime > requestInterval) {
+ // if you're not connected, and two minutes have passed since
+ // your last connection, then attempt to connect again:
+ connectToServer();
+ }
+}
+
+void connectToServer() {
+ // attempt to connect, and wait a millisecond:
+ Serial.println("connecting to server...");
+ if (client.connect(serverName, 80)) {
+ Serial.println("making HTTP request...");
+ // make HTTP GET request to twitter:
+ client.println("GET /1/statuses/user_timeline.xml?screen_name=arduino&count=1 HTTP/1.1");
+ client.println("HOST: api.twitter.com");
+ client.println("Connection: close");
+ client.println();
+ }
+ // note the time of this connect attempt:
+ lastAttemptTime = millis();
+}
+
diff --git a/libraries/Ethernet/examples/UDPSendReceiveString/UDPSendReceiveString.ino b/libraries/Ethernet/examples/UDPSendReceiveString/UDPSendReceiveString.ino
new file mode 100644
index 0000000..4d4045c
--- /dev/null
+++ b/libraries/Ethernet/examples/UDPSendReceiveString/UDPSendReceiveString.ino
@@ -0,0 +1,118 @@
+/*
+ UDPSendReceive.pde:
+ This sketch receives UDP message strings, prints them to the serial port
+ and sends an "acknowledge" string back to the sender
+
+ A Processing sketch is included at the end of file that can be used to send
+ and received messages for testing with a computer.
+
+ created 21 Aug 2010
+ by Michael Margolis
+
+ This code is in the public domain.
+ */
+
+
+#include <SPI.h> // needed for Arduino versions later than 0018
+#include <Ethernet.h>
+#include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
+
+
+// Enter a MAC address and IP address for your controller below.
+// The IP address will be dependent on your local network:
+byte mac[] = {
+ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
+IPAddress ip(192, 168, 1, 177);
+
+unsigned int localPort = 8888; // local port to listen on
+
+// buffers for receiving and sending data
+char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
+char ReplyBuffer[] = "acknowledged"; // a string to send back
+
+// An EthernetUDP instance to let us send and receive packets over UDP
+EthernetUDP Udp;
+
+void setup() {
+ // start the Ethernet and UDP:
+ Ethernet.begin(mac,ip);
+ Udp.begin(localPort);
+
+ Serial.begin(9600);
+}
+
+void loop() {
+ // if there's data available, read a packet
+ int packetSize = Udp.parsePacket();
+ if(packetSize)
+ {
+ Serial.print("Received packet of size ");
+ Serial.println(packetSize);
+ Serial.print("From ");
+ IPAddress remote = Udp.remoteIP();
+ for (int i =0; i < 4; i++)
+ {
+ Serial.print(remote[i], DEC);
+ if (i < 3)
+ {
+ Serial.print(".");
+ }
+ }
+ Serial.print(", port ");
+ Serial.println(Udp.remotePort());
+
+ // read the packet into packetBufffer
+ Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
+ Serial.println("Contents:");
+ Serial.println(packetBuffer);
+
+ // send a reply, to the IP address and port that sent us the packet we received
+ Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
+ Udp.write(ReplyBuffer);
+ Udp.endPacket();
+ }
+ delay(10);
+}
+
+
+/*
+ Processing sketch to run with this example
+ =====================================================
+
+ // Processing UDP example to send and receive string data from Arduino
+ // press any key to send the "Hello Arduino" message
+
+
+ import hypermedia.net.*;
+
+ UDP udp; // define the UDP object
+
+
+ void setup() {
+ udp = new UDP( this, 6000 ); // create a new datagram connection on port 6000
+ //udp.log( true ); // <-- printout the connection activity
+ udp.listen( true ); // and wait for incoming message
+ }
+
+ void draw()
+ {
+ }
+
+ void keyPressed() {
+ String ip = "192.168.1.177"; // the remote IP address
+ int port = 8888; // the destination port
+
+ udp.send("Hello World", ip, port ); // the message to send
+
+ }
+
+ void receive( byte[] data ) { // <-- default handler
+ //void receive( byte[] data, String ip, int port ) { // <-- extended handler
+
+ for(int i=0; i < data.length; i++)
+ print(char(data[i]));
+ println();
+ }
+ */
+
+
diff --git a/libraries/Ethernet/examples/UdpNtpClient/UdpNtpClient.ino b/libraries/Ethernet/examples/UdpNtpClient/UdpNtpClient.ino
new file mode 100644
index 0000000..93ffe39
--- /dev/null
+++ b/libraries/Ethernet/examples/UdpNtpClient/UdpNtpClient.ino
@@ -0,0 +1,141 @@
+/*
+
+ Udp NTP Client
+
+ Get the time from a Network Time Protocol (NTP) time server
+ Demonstrates use of UDP sendPacket and ReceivePacket
+ For more on NTP time servers and the messages needed to communicate with them,
+ see http://en.wikipedia.org/wiki/Network_Time_Protocol
+
+ created 4 Sep 2010
+ by Michael Margolis
+ modified 9 Apr 2012
+ by Tom Igoe
+
+ This code is in the public domain.
+
+ */
+
+#include <SPI.h>
+#include <Ethernet.h>
+#include <EthernetUdp.h>
+
+// 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 };
+
+unsigned int localPort = 8888; // local port to listen for UDP packets
+
+IPAddress timeServer(192, 43, 244, 18); // time.nist.gov NTP server
+
+const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message
+
+byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
+
+// A UDP instance to let us send and receive packets over UDP
+EthernetUDP Udp;
+
+void setup()
+{
+ // Open serial communications and wait for port to open:
+ Serial.begin(9600);
+ while (!Serial) {
+ ; // wait for serial port to connect. Needed for Leonardo only
+ }
+
+
+ // start Ethernet and UDP
+ if (Ethernet.begin(mac) == 0) {
+ Serial.println("Failed to configure Ethernet using DHCP");
+ // no point in carrying on, so do nothing forevermore:
+ for(;;)
+ ;
+ }
+ Udp.begin(localPort);
+}
+
+void loop()
+{
+ sendNTPpacket(timeServer); // send an NTP packet to a time server
+
+ // wait to see if a reply is available
+ delay(1000);
+ if ( Udp.parsePacket() ) {
+ // We've received a packet, read the data from it
+ Udp.read(packetBuffer,NTP_PACKET_SIZE); // read the packet into the buffer
+
+ //the timestamp starts at byte 40 of the received packet and is four bytes,
+ // or two words, long. First, esxtract the two words:
+
+ unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
+ unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
+ // combine the four bytes (two words) into a long integer
+ // this is NTP time (seconds since Jan 1 1900):
+ unsigned long secsSince1900 = highWord << 16 | lowWord;
+ Serial.print("Seconds since Jan 1 1900 = " );
+ Serial.println(secsSince1900);
+
+ // now convert NTP time into everyday time:
+ Serial.print("Unix time = ");
+ // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
+ const unsigned long seventyYears = 2208988800UL;
+ // subtract seventy years:
+ unsigned long epoch = secsSince1900 - seventyYears;
+ // print Unix time:
+ Serial.println(epoch);
+
+
+ // print the hour, minute and second:
+ Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
+ Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
+ Serial.print(':');
+ if ( ((epoch % 3600) / 60) < 10 ) {
+ // In the first 10 minutes of each hour, we'll want a leading '0'
+ Serial.print('0');
+ }
+ Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
+ Serial.print(':');
+ if ( (epoch % 60) < 10 ) {
+ // In the first 10 seconds of each minute, we'll want a leading '0'
+ Serial.print('0');
+ }
+ Serial.println(epoch %60); // print the second
+ }
+ // wait ten seconds before asking for the time again
+ delay(10000);
+}
+
+// send an NTP request to the time server at the given address
+unsigned long sendNTPpacket(IPAddress& address)
+{
+ // set all bytes in the buffer to 0
+ memset(packetBuffer, 0, NTP_PACKET_SIZE);
+ // Initialize values needed to form NTP request
+ // (see URL above for details on the packets)
+ packetBuffer[0] = 0b11100011; // LI, Version, Mode
+ packetBuffer[1] = 0; // Stratum, or type of clock
+ packetBuffer[2] = 6; // Polling Interval
+ packetBuffer[3] = 0xEC; // Peer Clock Precision
+ // 8 bytes of zero for Root Delay & Root Dispersion
+ packetBuffer[12] = 49;
+ packetBuffer[13] = 0x4E;
+ packetBuffer[14] = 49;
+ packetBuffer[15] = 52;
+
+ // all NTP fields have been given values, now
+ // you can send a packet requesting a timestamp:
+ Udp.beginPacket(address, 123); //NTP requests are to port 123
+ Udp.write(packetBuffer,NTP_PACKET_SIZE);
+ Udp.endPacket();
+}
+
+
+
+
+
+
+
+
+
+
diff --git a/libraries/Ethernet/examples/WebClient/WebClient.ino b/libraries/Ethernet/examples/WebClient/WebClient.ino
new file mode 100644
index 0000000..40523a4
--- /dev/null
+++ b/libraries/Ethernet/examples/WebClient/WebClient.ino
@@ -0,0 +1,88 @@
+/*
+ Web client
+
+ This sketch connects to a website (http://www.google.com)
+ using an Arduino Wiznet Ethernet shield.
+
+ Circuit:
+ * Ethernet shield attached to pins 10, 11, 12, 13
+
+ 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 <Ethernet.h>
+
+// 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);
+
+// 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;
+
+void setup() {
+ // Open serial communications and wait for port to open:
+ Serial.begin(9600);
+ while (!Serial) {
+ ; // wait for serial port to connect. Needed for Leonardo only
+ }
+
+ // start the Ethernet connection:
+ if (Ethernet.begin(mac) == 0) {
+ Serial.println("Failed to configure Ethernet using DHCP");
+ // no point in carrying on, so do nothing forevermore:
+ // try to congifure using IP address instead of DHCP:
+ Ethernet.begin(mac, ip);
+ }
+ // give the Ethernet shield a second to initialize:
+ delay(1000);
+ Serial.println("connecting...");
+
+ // if you get a connection, report back via serial:
+ if (client.connect(server, 80)) {
+ Serial.println("connected");
+ // 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 {
+ // kf you didn't get a connection to the server:
+ Serial.println("connection failed");
+ }
+}
+
+void loop()
+{
+ // if there are incoming bytes available
+ // from the server, read them and print them:
+ if (client.available()) {
+ char c = client.read();
+ Serial.print(c);
+ }
+
+ // if the server's disconnected, stop the client:
+ if (!client.connected()) {
+ Serial.println();
+ Serial.println("disconnecting.");
+ client.stop();
+
+ // do nothing forevermore:
+ while(true);
+ }
+}
+
diff --git a/libraries/Ethernet/examples/WebClientRepeating/WebClientRepeating.ino b/libraries/Ethernet/examples/WebClientRepeating/WebClientRepeating.ino
new file mode 100644
index 0000000..650f74e
--- /dev/null
+++ b/libraries/Ethernet/examples/WebClientRepeating/WebClientRepeating.ino
@@ -0,0 +1,111 @@
+/*
+ Repeating Web client
+
+ This sketch connects to a a web server and makes a request
+ using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
+ the Adafruit Ethernet shield, either one will work, as long as it's got
+ a Wiznet Ethernet module on board.
+
+ This example uses DNS, by assigning the Ethernet client with a MAC address,
+ IP address, and DNS address.
+
+ Circuit:
+ * Ethernet shield attached to pins 10, 11, 12, 13
+
+ created 19 Apr 2012
+ by Tom Igoe
+
+ http://arduino.cc/en/Tutorial/WebClientRepeating
+ This code is in the public domain.
+
+ */
+
+#include <SPI.h>
+#include <Ethernet.h>
+
+// assign a MAC address for the ethernet controller.
+// fill in your address here:
+byte mac[] = {
+ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
+// fill in an available IP address on your network here,
+// for manual configuration:
+IPAddress ip(10,0,0,20);
+
+// fill in your Domain Name Server address here:
+IPAddress myDns(1,1,1,1);
+
+// initialize the library instance:
+EthernetClient client;
+
+char server[] = "www.arduino.cc";
+
+unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
+boolean lastConnected = false; // state of the connection last time through the main loop
+const unsigned long postingInterval = 60L*1000L; // delay between updates, in milliseconds
+ // the "L" is needed to use long type numbers
+
+void setup() {
+ // start serial port:
+ Serial.begin(9600);
+ // give the ethernet module time to boot up:
+ delay(1000);
+ // start the Ethernet connection using a fixed IP address and DNS server:
+ Ethernet.begin(mac, ip, myDns);
+ // print the Ethernet board/shield's IP address:
+ Serial.print("My IP address: ");
+ Serial.println(Ethernet.localIP());
+}
+
+void loop() {
+ // if there's incoming data from the net connection.
+ // send it out the serial port. This is for debugging
+ // purposes only:
+ if (client.available()) {
+ char c = client.read();
+ Serial.print(c);
+ }
+
+ // if there's no net connection, but there was one last time
+ // through the loop, then stop the client:
+ if (!client.connected() && lastConnected) {
+ Serial.println();
+ Serial.println("disconnecting.");
+ client.stop();
+ }
+
+ // if you're not connected, and ten seconds have passed since
+ // your last connection, then connect again and send data:
+ if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
+ httpRequest();
+ }
+ // store the state of the connection for next time through
+ // the loop:
+ lastConnected = client.connected();
+}
+
+// this method makes a HTTP connection to the server:
+void httpRequest() {
+ // if there's a successful connection:
+ if (client.connect(server, 80)) {
+ Serial.println("connecting...");
+ // send the HTTP PUT request:
+ client.println("GET /latest.txt HTTP/1.1");
+ client.println("Host: www.arduino.cc");
+ client.println("User-Agent: arduino-ethernet");
+ client.println("Connection: close");
+ client.println();
+
+ // note the time that the connection was made:
+ lastConnectionTime = millis();
+ }
+ else {
+ // if you couldn't make a connection:
+ Serial.println("connection failed");
+ Serial.println("disconnecting.");
+ client.stop();
+ }
+}
+
+
+
+
diff --git a/libraries/Ethernet/examples/WebServer/WebServer.ino b/libraries/Ethernet/examples/WebServer/WebServer.ino
new file mode 100644
index 0000000..5e5d67a
--- /dev/null
+++ b/libraries/Ethernet/examples/WebServer/WebServer.ino
@@ -0,0 +1,100 @@
+/*
+ Web Server
+
+ A simple web server that shows the value of the analog input pins.
+ using an Arduino Wiznet Ethernet shield.
+
+ Circuit:
+ * Ethernet shield attached to pins 10, 11, 12, 13
+ * Analog inputs attached to pins A0 through A5 (optional)
+
+ created 18 Dec 2009
+ by David A. Mellis
+ modified 9 Apr 2012
+ by Tom Igoe
+
+ */
+
+#include <SPI.h>
+#include <Ethernet.h>
+
+// Enter a MAC address and IP address for your controller below.
+// The IP address will be dependent on your local network:
+byte mac[] = {
+ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
+IPAddress ip(192,168,1,177);
+
+// Initialize the Ethernet server library
+// with the IP address and port you want to use
+// (port 80 is default for HTTP):
+EthernetServer server(80);
+
+void setup() {
+ // Open serial communications and wait for port to open:
+ Serial.begin(9600);
+ while (!Serial) {
+ ; // wait for serial port to connect. Needed for Leonardo only
+ }
+
+
+ // start the Ethernet connection and the server:
+ Ethernet.begin(mac, ip);
+ server.begin();
+ Serial.print("server is at ");
+ Serial.println(Ethernet.localIP());
+}
+
+
+void loop() {
+ // listen for incoming clients
+ EthernetClient client = server.available();
+ if (client) {
+ Serial.println("new client");
+ // an http request ends with a blank line
+ boolean currentLineIsBlank = true;
+ while (client.connected()) {
+ if (client.available()) {
+ char c = client.read();
+ Serial.write(c);
+ // if you've gotten to the end of the line (received a newline
+ // character) and the line is blank, the http request has ended,
+ // so you can send a reply
+ if (c == '\n' && currentLineIsBlank) {
+ // send a standard http response header
+ client.println("HTTP/1.1 200 OK");
+ client.println("Content-Type: text/html");
+ client.println("Connection: close"); // the connection will be closed after completion of the response
+ client.println("Refresh: 5"); // refresh the page automatically every 5 sec
+ client.println();
+ client.println("<!DOCTYPE HTML>");
+ client.println("<html>");
+ // output the value of each analog input pin
+ for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
+ int sensorReading = analogRead(analogChannel);
+ client.print("analog input ");
+ client.print(analogChannel);
+ client.print(" is ");
+ client.print(sensorReading);
+ client.println("<br />");
+ }
+ client.println("</html>");
+ break;
+ }
+ if (c == '\n') {
+ // you're starting a new line
+ currentLineIsBlank = true;
+ }
+ else if (c != '\r') {
+ // you've gotten a character on the current line
+ currentLineIsBlank = false;
+ }
+ }
+ }
+ // give the web browser time to receive the data
+ delay(1);
+ // close the connection:
+ client.stop();
+ Serial.println("client disonnected");
+ }
+}
+