#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer m_ServerCommands(8400); //server port
EthernetServer m_ServerSerial1(8401);
EthernetServer m_ServerSerial2(8402);
EthernetClient m_TcpClientCommands;
EthernetClient m_TcpClientSerial1;
EthernetClient m_TcpClientSerial2;
EthernetClient m_TcpClientSerial3;
//Command Parsing Variables
bool m_bMessageStartFound;
String m_sCurrentCommand;
//Struct for holding different command types
typedef void(*functionType)(String);
//LED Position Variables
int LED_1 = 26;
int LED_2 = 27;
int LED_3 = 28;
struct CommandFunction
{
String CommandString;
functionType Function;
};
CommandFunction Commands[6];
/*
CommandFunction Commands[] = {
{"ON", LED_On}
, {"OFF", LED_Off}
, {"Flash", LED_Flash}
, {"Status", Status}
, {"OPEN", SerialOpen}
, {"CLOSE", SerialClose}
};
*/
//Variables used for flashing LED
unsigned long previousMillis = 0;
const long interval = 1000;
int ledState1 = LOW;
int ledState2 = LOW;
int ledState3 = LOW;
bool baFlashArray[3];
void setup()
{
//init digital pins as an output
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
pinMode(LED_3, OUTPUT);
Commands[0] = {"ON", LED_On};
Commands[1] = {"OFF", LED_Off};
Commands[2] = {"Flash", LED_Flash};
Commands[3] = {"GetStatus", Status};
Commands[4] = {"OPEN", SerialOpen};
Commands[5] = {"CLOSE", SerialClose};
Commands[6] = {"Test", Test};
Ethernet.begin(mac, ip, gateway, subnet);
//m_ServerCommands.begin();
m_ServerSerial1.begin();
m_ServerSerial2.begin();
//enable serial data print
Serial.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600);
Serial.println("server test");
}
void loop()
{
FlashingFunction(baFlashArray[0], baFlashArray[1], baFlashArray[2]);
//TCPStatus("Start of Checks\t");
if(!m_TcpClientCommands.connected())
{
m_TcpClientCommands = m_ServerCommands.available();
}
if(!m_TcpClientSerial1.connected())
{
m_TcpClientSerial1 = m_ServerSerial1.available();
}
if(!m_TcpClientSerial2.connected())
{
m_TcpClientSerial2 = m_ServerSerial2.available();
}
//TCPStatus("End of Checks \t");
CheckTcpClientCommand();
CheckTcpClientSerials();
CheckSerials();
//Keeps IP address for DHCP
Ethernet.maintain();
}
void TCPStatus(String message)
{
/*
Serial.print("Function: ");
Serial.print(message);
Serial.print("TCP Status ");
Serial.print(m_TcpClientCommands.connected());
Serial.print(m_TcpClientSerial1.connected());
Serial.print(m_TcpClientSerial2.connected());
Serial.println();
*/
}
void CheckTcpClientCommand()
{
TCPStatus("TCPCommandClient \t");
if (m_TcpClientCommands.connected() && m_TcpClientCommands.available())
{
while (m_TcpClientCommands.available())
{
char temp = (char)m_TcpClientCommands.read();
//Check for Commands Here
CheckForCommand(temp);
}
}
}
void CheckTcpClientSerials()
{
TCPStatus("TCPClientSerials \t");
//Serial1
if (m_TcpClientSerial1.available())
{
Serial.println("TcpClient1 Data Available");
while (m_TcpClientSerial1.available())
{
Serial1.write((byte)m_TcpClientSerial1.read());
}
}
//Serial2
if (m_TcpClientSerial2.available())
{
Serial.println("TcpClient2 Data Available");
while (m_TcpClientSerial2.available())
{
Serial2.write((byte)m_TcpClientSerial2.read());
}
}
}
void CheckSerials()
{
TCPStatus("Serials \t\t");
while (Serial1.available())
{
byte Serial1Byte = Serial1.read();
Serial.println("Serial1 Data Available");
if(m_TcpClientSerial1.connected())
{
m_TcpClientSerial1.write(Serial1Byte);
}
}
while (Serial2.available())
{
byte Serial2Byte = Serial2.read();
Serial.println("Serial2 Data Available");
if(m_TcpClientSerial2.connected())
{
Serial.println("Serial2 TCP Send Byte");
m_TcpClientSerial2.write(Serial2Byte);
}
}
}
void CheckForCommand(char inputChar)
{
bool bFoundCommand = false;
switch (inputChar)
{
case '<':
m_bMessageStartFound = true;
m_sCurrentCommand = "";
break;
case '>':
if (m_bMessageStartFound == true)
{
ProcessCommand(m_sCurrentCommand);
}
m_bMessageStartFound = false;
m_sCurrentCommand = "";
break;
default:
if (m_bMessageStartFound == true)
{
// add inputChar to m_sCurrentCommand
m_sCurrentCommand += inputChar;
}
break;
}
}
void ProcessCommand(String userInput)
{
int firstComma = userInput.indexOf(',');
String sCommandString = userInput.substring(0, firstComma);
userInput.remove(0, firstComma + 1);
bool bCommandFound = false;
for (int i = 0; i < sizeof(Commands) / sizeof(CommandFunction); i++)
{
if (sCommandString == Commands[i].CommandString)
{
bCommandFound = true;
Serial.print("Function: Process Command(");
Serial.print(sCommandString);
Serial.println(")");
Commands[i].Function(userInput);
}
}
if (bCommandFound == false)
{
Serial.println("That is an invalid command");
}
}
void Status(String Variables)
{
Serial.println("Status Message Sent");
//TODO update this!
//m_TcpClientCommands.println("Status");
}
void LED_On(String LED)
{
switch (LED.toInt()) {
case 1:
{
baFlashArray[0] = false;
digitalWrite(LED_1, HIGH);
Serial.println("LED_1 ON");
}
break;
case 2:
{
baFlashArray[1] = false;
digitalWrite(LED_2, HIGH);
Serial.println("LED_2 ON");
}
break;
case 3:
{
baFlashArray[2] = false;
digitalWrite(LED_3, HIGH);
Serial.println("LED_3 ON");
}
break;
}
}
void LED_Off(String LED)
{
switch (LED.toInt()) {
case 1:
{
baFlashArray[0] = false;
digitalWrite(LED_1, LOW);
Serial.println("LED_1 OFF");
}
break;
case 2:
{
baFlashArray[1] = false;
digitalWrite(LED_2, LOW);
Serial.println("LED_2 OFF");
}
break;
case 3:
{
baFlashArray[2] = false;
digitalWrite(LED_3, LOW);
Serial.println("LED_3 OFF");
}
break;
}
}
void LED_Flash(String LED)
{
switch (LED.toInt()) {
case 1:
{
baFlashArray[0] = true;
Serial.println("LED_1 FLASH");
}
break;
case 2:
{
baFlashArray[1] = true;
Serial.println("LED_2 FLASH");
}
break;
case 3:
{
baFlashArray[2] = true;
Serial.println("LED_3 FLASH");
}
break;
}
}