Commits

bjoern  committed ad1dce4

moved library examples to proper subfolders

  • Participants
  • Parent commits 17128cd

Comments (0)

Files changed (4)

File libraries/Ethernet/examples/UdpReceive.pde

-#include <Ethernet.h>
-#include <Udp.h>
-/* UdpReceive.pde: Example how to receive packets over UDP 
- * prints received packet to serial port
- *
- * bjoern@cs.stanford.edu 12/29/2008
- */
-
-/* ETHERNET CONFIGURATION *************************************/
-/* ARDUINO: set MAC, IP address of Ethernet shield, its gateway,
- and local port to listen on for incoming packets */
-byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //MAC address to use
-byte ip[] = { 192, 168, 11, 200 }; // Arduino's IP address
-byte gw[] = { 192, 168, 11, 1 };   // Gateway IP address
-int localPort = 8888; //local port to listen on
-/***************************************************************/
-
-#define MAX_SIZE 32 //maximum packet size
-
-byte packetBuffer[MAX_SIZE]; //buffer to hold incoming packet
-int packetSize; //holds received packet size
-byte remoteIp[4]; //holds recvieved packet's originating IP
-int remotePort; //holds received packet's originating port
-
-int i;
-
-
-void setup() {
-  Ethernet.begin(mac,ip,gw);
-  Udp.begin(localPort);
-  Serial.begin(9600); 
-}
-
-void loop() {  
-  //if there's data available, read a packet
-  if(Udp.available()) {
-    packetSize = Udp.readPacket(packetBuffer,MAX_SIZE,remoteIp,(uint16_t *)&remotePort);
-    if(packetSize <= MAX_SIZE) {
-      
-      Serial.print("Received packet of size ");
-      Serial.println(packetSize);
-      
-      Serial.print("From IP ");
-      for(i=0; i<4; i++) {
-        Serial.print(remoteIp[i],DEC);
-        Serial.print(".");
-      }
-      
-      Serial.print(" Port ");
-      Serial.print((remotePort >> 8),DEC); //this doesn't work yet - should be unsigned
-      Serial.println((remotePort & 0xFF),DEC); 
-      
-      Serial.println("Contents:");
-      for(i=0; i<packetSize; i++) {
-        Serial.print(packetBuffer[i],BYTE);
-      }
-      Serial.println("");
-      
-    } else {
-      //PANIC - packet too long!
-      // we've already clobbered mem past our buffer boundary
-    }
-  }
-  //wait a bit
-  delay(10);  
-}

File libraries/Ethernet/examples/UdpReceive/UdpReceive.pde

+#include <Ethernet.h>
+#include <Udp.h>
+
+/* UdpReceive.pde: Example how to receive packets over UDP 
+ * prints received packet to serial port
+ * bjoern@cs.stanford.edu 12/29/2008
+ */
+
+/* ETHERNET SHIELD CONFIGURATION  
+ * set MAC, IP address of Ethernet shield, its gateway,
+ * and local port to listen on for incoming packets 
+ */
+byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC address to use
+byte ip[] = { 192, 168, 11, 200 }; // Arduino's IP address
+byte gw[] = { 192, 168, 11, 1 };   // Gateway IP address
+int localPort = 8888; // local port to listen on
+
+#define MAX_SIZE 32 // maximum packet size
+byte packetBuffer[MAX_SIZE]; //buffer to hold incoming packet
+int packetSize; // holds received packet size
+byte remoteIp[4]; // holds recvieved packet's originating IP
+unsigned int remotePort; // holds received packet's originating port
+
+int i;
+
+/* SETUP: init Ethernet shield, start UDP listening, open serial port */
+void setup() {
+  Ethernet.begin(mac,ip,gw);
+  Udp.begin(localPort);
+  Serial.begin(9600); 
+}
+/* LOOP: wait for incoming packets and print each packet to the serial port */
+void loop() {  
+  
+  // if there's data available, read a packet
+  if(Udp.available()) {
+    packetSize = Udp.readPacket(packetBuffer,MAX_SIZE,remoteIp,(uint16_t *)&remotePort);
+    if(packetSize <= MAX_SIZE) {
+      
+      Serial.print("Received packet of size ");
+      Serial.println(packetSize);
+      
+      Serial.print("From IP ");
+      for(i=0; i<3; i++) {
+        Serial.print(remoteIp[i],DEC);
+        Serial.print(".");
+      }
+      Serial.print(remoteIp[3],DEC);
+      
+      Serial.print(" Port ");
+      Serial.println(remotePort); 
+      
+      Serial.println("Contents:");
+      for(i=0; i<packetSize; i++) {
+        Serial.print(packetBuffer[i],BYTE);
+      }
+      Serial.println("");
+      
+    } else {
+      // PANIC - packet too long!
+      // we've already clobbered mem past our buffer boundary
+    }
+  }
+  //wait a bit
+  delay(10);  
+}

File libraries/Ethernet/examples/UdpSend.pde

-#include <Ethernet.h>
-#include <Udp.h>
-/* UdpSend.pde: Example how to send packets over UDP 
- * to check for received packets on Unix-ish setup, execute:
- * sudo tcpdump -ien0 "udp port 8000"
- *
- * bjoern@cs.stanford.edu 12/29/2008
- */
-
-/* ETHERNET CONFIGURATION *************************************/
-/* ARDUINO: set MAC, IP address of Ethernet shield, its gateway,
- and local port to listen on for incoming packets */
-byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //MAC address to use
-byte ip[] = { 192, 168, 11, 200 }; // Arduino's IP address
-byte gw[] = { 192, 168, 11, 1 };   // Gateway IP address
-int localPort = 8888; //local port to listen on
-
-/* TARGET: set this to IP/Port of computer that will receive
- * UDP messages from Arduino */
-byte targetIp[] = { 192, 168, 11, 15};
-int targetPort = 8000;
-/***************************************************************/
-
-byte packet[] = { 'h','e','l','l','o' };
-int packetLen = 5;
-
-void setup() {
-  Ethernet.begin(mac,ip,gw);
-  Udp.begin(localPort);
-}
-
-void loop() {
-  //send one packet a second
-  delay(1000);
-  Udp.sendPacket(packet,packetLen,targetIp,targetPort);
-}

File libraries/Ethernet/examples/UdpSend/UdpSend.pde

+#include <Ethernet.h>
+#include <Udp.h>
+/* UdpSend.pde: Example how to send packets over UDP 
+ * to check for received packets on Unix-ish setup, execute:
+ * sudo tcpdump -ien0 "udp port 8000"
+ * bjoern@cs.stanford.edu 12/29/2008 */
+
+/* ETHERNET CONFIGURATION 
+ * ARDUINO: set MAC, IP address of Ethernet shield, its gateway,
+ * and local port to listen on for incoming packets */
+byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //MAC address to use
+byte ip[] = { 192, 168, 11, 200 }; // Arduino's IP address
+byte gw[] = { 192, 168, 11, 1 };   // Gateway IP address
+int localPort = 8888; //local port to listen on
+
+/* TARGET: set this to IP/Port of computer that will receive
+ * UDP messages from Arduino */
+byte targetIp[] = { 192, 168, 11, 15};
+int targetPort = 8000;
+
+/* A binary packet we'll send to our target - can contain 0x00 */
+byte packet[] = { 'h','e','l','l','o','\0','w','o','r','l','d','\0' };
+int packetLen = 12;
+
+
+void setup() {
+  Ethernet.begin(mac,ip,gw);
+  Udp.begin(localPort);
+}
+
+void loop() {
+  // this version of sendPacket sends a zero-terminated string.
+  Udp.sendPacket("hello, world.",targetIp,targetPort);
+  delay(1000);
+  
+  // this version sends an arbitrary buffer with specified length;
+  // buffer can contain '\0'
+  Udp.sendPacket(packet,packetLen,targetIp,targetPort);
+  delay(1000);
+}