#include <SPI.h>
#include <Ethernet.h>
#include <MemoryFree.h>
//Define Unit MAC Address
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0xD1, 0x61 };
//End Unit MAC Address
//Define Unit IP Address
IPAddress ip(192,168,1,177);
//IPAddress ip(10,1,10,177);
//End Unit IP Address
//Define Server IP Address
IPAddress server(192,168,1,30);
//IPAddress server(10,1,10,30);
//End Server IP Address
//Define Ethernet Client
EthernetClient client;
//End Ethernet Client
//Define Port
#define Port 50000
//Define IO Pins
#define Connection 8 //Client connected LED, active LOW
#define NoConnection 9 //Client not connected LED, active LOW
int OutputPins[] = {2,3,4,5,6,7,14,15,16,17,18,19};
//End Define IO Pins
void setup(){
//Set Pin Modes
pinMode(Connection, OUTPUT);
pinMode(NoConnection, OUTPUT);
SetPinModeOutput(OutputPins,12);
//End Pin Modes
for(int i = 0; i<20;i++){
digitalWrite(Connection, HIGH); //Flash the LED's a bunch to determine if the board was reset.
delay(50);
digitalWrite(Connection, LOW);
digitalWrite(NoConnection, HIGH);
delay(50);
digitalWrite(NoConnection, LOW);
delay(50); }
Ethernet.begin(mac,ip); //Start the Ethernet connection
//Serial.begin(9600); //Start the serial connection at 9600 baud
delay(1000); // Wait a bit to let the ethernet enable
ConnectionLedState(LOW); //Turn on disconnected LED
//Try to connect to the server
ClientConnect();
}
/******************************************************************************/
/******************************** MAIN LOOP ***********************************/
/******************************************************************************/
void loop(){
// Serial.print("freeMemory()=");
// Serial.println(freeMemory());
GetEthernetData();
//Serial.print("Buffer contains ");
//Serial.println(client.available());
//Serial.println(millis());
WriteEthernetData(freeMemory());
if(ClientDisconnect() == 1){
ClientConnect();}
delay(5);
}
/******************************************************************************/
/****************************** END MAIN LOOP *********************************/
/******************************************************************************/
//Controls the bicolor led, red = no connection, green = connection
void ConnectionLedState(boolean State){ //HIGH = connection
if(State == HIGH){
digitalWrite(Connection, LOW);
digitalWrite(NoConnection, HIGH);
}
if(State == LOW){
digitalWrite(Connection, HIGH);
digitalWrite(NoConnection, LOW);
}
}
//End Connection LED
/******************************************************************************/
/**************************** Get Ethernet Data *******************************/
/******************************************************************************/
char GetEthernetData(){
char temp = 0;
int count = 0;
int NumOfBytes = 0;
int TimeOut = millis();
if(client.available() > 0){ //If data is available
char Size[2]; //Create a char array for the size indicator
for(int x=0; x<2; x++){ //Get the chars for the size of the data packet
Size[x] = client.read();}
NumOfBytes = atoi(Size); //Change from char array to string.
// Serial.print("This many bytes: ");
// Serial.println(NumOfBytes);
while(client.available()<NumOfBytes && (millis() - TimeOut <350)){ //Wait for the packets to arrive
delay(25);
//Serial.println("Waiting");
}}
char Data[NumOfBytes]; //Create an array based on the size if the packet
for(int i = 0; i < NumOfBytes; i++){ //Get the packet
Data[i] = client.read();
}
if(NumOfBytes > 0){ //If data was read, pass it to the function to parse the data
ParseEthernet(Data, NumOfBytes);
}
}
/******************************************************************************/
/************************** End Get Ethernet Data *****************************/
/******************************************************************************/
/******************************************************************************/
/*************************** Parse Ethernet Data ******************************/
/******************************************************************************/
void ParseEthernet(char Data[], int Size){
char temp[2]; //Create temp char array
int CommandSize = Size/2;
int commands[CommandSize]; //Command array is half the size of char array.
for(int i = 0; i < Size; i++){
temp[0] = Data[i*2]; //Get the MSB of the value
temp[1] = Data[(i*2)+1]; //Get the LSB of the value
commands[i] = atoi(temp); //Convert that to an int
}
Control(commands, CommandSize);
}
/******************************************************************************/
/************************* End Parse Ethernet Data ****************************/
/******************************************************************************/
/******************************************************************************/
/*************************** Write Ethernet Data ******************************/
/******************************************************************************/
void WriteEthernetData(int Data){
//while (Serial.available() > 0) { //While serial data available print to Network
char inChar = Data;
if (client.connected()) {
client.print("OK");
client.print('r');
client.print('\n');
}
//}
}
/******************************************************************************/
/************************* End Write Ethernet Data ****************************/
/******************************************************************************/
/******************************************************************************/
/**************************** Client Disconnect *******************************/
/******************************************************************************/
int ClientDisconnect(){
if (!client.connected()) {
client.stop();
ConnectionLedState(LOW);
return 1;}
else{return 0;}
}
/******************************************************************************/
/************************** End Client Disconnect *****************************/
/******************************************************************************/
/******************************************************************************/
/****************************** Client Connect ********************************/
/******************************************************************************/
void ClientConnect(){
if(client.connect(server, Port)){ //If server connection is sucessful, turn on Green LED
ConnectionLedState(HIGH);
client.flush();}
else(ConnectionLedState(LOW)); //Else, turn on RED LED
}
/******************************************************************************/
/*************************** End Client Connect *******************************/
/******************************************************************************/
/******************************************************************************/
/********************************* Control ************************************/
/******************************************************************************/
void Control(int Commands[], int Size){
//Serial.print("Size is:");
//Serial.println(Size);
for(int i = 0; i < Size; i+=2){
ExecuteCommand(Commands[i], Commands[i+1]);
//Serial.println(i);
//Serial.println(i+1);
}
//Serial.println();
}
/******************************************************************************/
/******************************** End Control *********************************/
/******************************************************************************/
/******************************************************************************/
/****************************** Execute Command *******************************/
/******************************************************************************/
void ExecuteCommand(int Command, int Value){
boolean State = LOW;
if(Value == 1){
State = HIGH;}
digitalWrite(Command, State);
}
/******************************************************************************/
/**************************** End Execute Command *****************************/
/******************************************************************************/
/******************************************************************************/
/******************************* Set Pin Mode *********************************/
/******************************************************************************/
void SetPinModeOutput(int Pins[],int Size){
for(int i = 0; i < Size; i++){
pinMode(Pins[i], OUTPUT);}
}
/******************************************************************************/
/***************************** End Set Pin Mode *******************************/
/******************************************************************************/