Hello people,
I am new here and to Arduino and have some questions about setting up Ethernet and UDP.
I want to send data from an analog pin down an Ethernet cable to my computer. I will then use this data in Max/MSP using the "UDP receive" object.
I am using the official Arduino Ethernet shield (W5100). Am I correct in assuming that I don't need to be connected to the internet to set up network in this way?
I have been looking through example code and have got so far but all of the projects I have found so far are about publishing data to web pages. Al of this extra code for the web page stuff is confusing me. I know it's missing a lot but I have this so far
#include <SPI.h>
#include <Ethernet.h>
#define LAZER 2 //LAZER connected to digital pin 2
int photocellPin = 0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the sensor divider
byte mac[] = { 0x90, 0xA2, 0XDA, 0X00, 0X4B, 0X9F}; //mac address of my ethernet shield
byte ip[] = { 188, 28, 134, 164}; //ip address of my computer
Server server(80);
void setup(void)
{
Ethernet.begin(mac, ip);
pinMode(LAZER, OUTPUT);
Serial.begin(9600);
}
void loop(void) {
digitalWrite(LAZER, HIGH);
photocellReading = analogRead(photocellPin);
Serial.print("Analog reading = ");
Serial.println(photocellReading); // the raw analog reading
delay(100);
}
I have tried the code below from the UDP tutorial but get this error when compiling:
sketch_may25a:23: error: 'UDP' does not name a type
Can anyone help me send the analog pin data via Ethernet into Max/MSP?
#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <Udp.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 };
byte ip[] = {
192,168,1,177 };
unsigned int localPort = 8888; // local port to listen on
// the next two variables are set when a packet is received
byte remoteIp[4]; // holds received packet's originating IP
unsigned int remotePort; // holds received packet's originating port
// 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
// A UDP instance to let us send and receive packets over UDP
UDP 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.available(); // note that this includes the UDP header
if(packetSize)
{
packetSize = packetSize - 8; // subtract the 8 byte header
Serial.print("Received packet of size ");
Serial.println(packetSize);
// read the packet into packetBufffer and get the senders IP addr and port number
Udp.readPacket(packetBuffer,UDP_TX_PACKET_MAX_SIZE, remoteIp, remotePort);
Serial.println("Contents:");
Serial.println(packetBuffer);
Udp.sendPacket( ReplyBuffer, remoteIp, remotePort);
}
delay(10);
}