Here is a sketch that works for me.
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
byte arduinoMac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress arduinoIP(10, 0, 0, 19);
unsigned int arduinoPort = 8888;
IPAddress receiverIP(10, 0, 0, 13);
unsigned int receiverPort = 6000;
EthernetUDP Udp;
void setup() {
Ethernet.begin(arduinoMac,arduinoIP);
Udp.begin(arduinoPort);
}
void loop() {
Udp.beginPacket(receiverIP, receiverPort);
Udp.print(String(analogRead(A0), DEC));
Udp.write(",");
Udp.print(String(analogRead(A1), DEC));
Udp.endPacket();
delay(3000);
}
To log it on the other end, I use node.js . Here is a sample file that will read the incoming packets and log them to a file.
var dgram = require("dgram");
var server = dgram.createSocket("udp4");
var fs = require('fs');
var crlf = new Buffer(2);
crlf[0] = 0xD;
crlf[1] = 0xA;
function getDateTime() {
var date = new Date();
var hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
var min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;
var sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;
var year = date.getFullYear();
var month = date.getMonth() + 1;
month = (month < 10 ? "0" : "") + month;
var day = date.getDate();
day = (day < 10 ? "0" : "") + day;
return year + "/" + month + "/" + day + " " + hour + ":" + min + ", ";
}
server.on("error", function (err) {
console.log("server error:\n" + err.stack);
server.close();
});
server.on("message", function (msg, rinfo) {
console.log(getDateTime() + msg + " from " +
rinfo.address + ":" + rinfo.port);
fs.appendFile("mydata.txt",getDateTime() + msg + crlf, encoding='utf8',function(err){});
});
server.on("listening", function () {
var address = server.address();
console.log("server listening " +
address.address + ":" + address.port);
});
server.bind(6000);
You will have to adjust the IPs for your Arduino and host computer, but you should be able to figure it out.