#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#define SET 0
#define READ 1
#define IO 2
#define MSG 3
#define FUNC 4
#define RED 5
#define GREEN 6
#define BLUE 3
intledSwitch=HIGH;intpwmPins[]={3,5,6,9,10,11};intdigInPins[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0};Stringreply="";intr=0;intg=0;intb=0;//network information
bytemac[]={0xAA,0xAA,0xAA,0xAA,0xAA,0xAA};IPAddressip(10,1,1,177);unsignedintlocalPort=6400;// local port to listen on
// buffers for receiving and sending data
charpacketBuffer[UDP_TX_PACKET_MAX_SIZE];//buffer to hold incoming packet,
charReplyBuffer[]="acknowledged";// a string to send back to sender
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDPUdp;voidsetup(){// start the Ethernet and UDP:
Ethernet.begin(mac,ip);Udp.begin(localPort);//all digital pins output
for(inti=0;i<14;i++){pinMode(i,OUTPUT);}Serial.begin(9600);}voidloop(){// if there's data available, read a packet
intpacketSize=Udp.parsePacket();if(packetSize){Serial.print(", port ");Serial.println(Udp.remotePort());// read the packet into packetBufffer
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);//parsing
Stringdatagram=String(packetBuffer);intprev_index=0;intcurr_index=0;StringtempCommands[10];inti=0;//splitting on ';'
while(prev_index<(datagram.length()-1)){curr_index=datagram.indexOf(';',prev_index+1)+1;tempCommands[i]=datagram.substring(prev_index,curr_index);prev_index=curr_index;i+=1;}Stringcommands[i-1];for(intn=0;n<i;n++){commands[n]=tempCommands[n];}switch((commands[0]).toInt()){caseSET://DIGITAL COMMAND
if(commands[1].charAt(0)=='d'){intdPort=(commands[1].substring(1)).toInt();intdSet=constrain((commands[2]).toInt(),0,1);if((dPort>=0)&&(dPort<14)){digitalWrite(dPort,dSet);reply="Digital Port "+String(dPort)+" set to "+String(dSet);Serial.println(reply);}else{reply="Digital Port "+String(dPort)+" is not a valid digital port";Serial.println(reply);}}//PWM COMMAND
elseif(commands[1].charAt(0)=='p'){intpPort=(commands[1].substring(1)).toInt();intpSet=constrain((commands[2]).toInt(),0,255);intisPWM=0;for(inti=0;i<sizeof(pwmPins);i++){if(pPort==pwmPins[i]){isPWM=1;break;}}if(isPWM){analogWrite(pPort,pSet);reply="Digital Port "+String(pPort)+" PWMed.";Serial.print(reply);}else{reply="Digital Port "+String(pPort)+" is not a valid PWM port.";Serial.print(reply);}if(pPort==RED){r=pSet;}if(pPort==GREEN){g=pSet;}if(pPort==BLUE){b=pSet;}}else{reply="Invalid SET command: "+String(commands[1]);Serial.println(reply);}break;caseREAD://DIGITAL READ
if(commands[1].charAt(0)=='d'){intdPort=(commands[1].substring(1)).toInt();if((dPort>=0)&&(dPort<14)){intdRead=digitalRead(dPort);Serial.print("Digital Port ");Serial.print(dPort);Serial.print(" reads: ");Serial.println(dRead);}else{Serial.print("Port ");Serial.print(dPort);Serial.println(" is not a valid digital port.");}}if(commands[1].charAt(0)=='a'){intaPort=(commands[1].substring(1)).toInt();if((aPort>=0)&&(aPort<6)){intaRead=analogRead(aPort);Serial.print("Analog Port ");Serial.print(aPort);Serial.print(" reads: ");Serial.println(aRead);}else{Serial.print("Port ");Serial.print(aPort);Serial.println(" is not a valid analog port.");}}break;caseIO:break;caseMSG:break;caseFUNC:break;default:Serial.print("Invalid initial command: ");Serial.println(commands[0]);}//send a reply, to the IP address and port that sent us the packet we received
charreplyArray[200];reply.toCharArray(replyArray,200);Udp.beginPacket(Udp.remoteIP(),Udp.remotePort());Udp.write(replyArray);Udp.endPacket();}}