This sketch receives a command from HCA and triggers one of it relays accordingly.
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
// Enter a MAC address and IP address for your controller below.
// change these to appropriate values
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x68 };
IPAddress ip(192, 168, 0, 68);
unsigned int Port_Remote = 5000; // remote port for status check
IPAddress IP_Remote(192, 168, 0, 10); // IP address for smarthome
unsigned int localPort = 5001; // local port to listen on
// we don’t check on the sender’s port or addr for now
char cmdGDoper = ‘1’; // a “1” means flip the garage door opener
char cmdResetPC = ‘2’; // a “2” means turn the computer off for 60 sec to reboot it
char cmdBathBlowerOn = ‘3’; // a “3” means turn the bath room blower on
char cmdBathBlowerOff = ‘4’; // a “4” means turn the bath room blower off
char cmdStatusCheck = ‘9’; // a “9” means reply with good status
char cmdIn = ‘0’; // used to obtain and compare buffer character
int garageDoorOperPin = 9; // digital pin to control garage door opener
int resetPCPin = 8; // digital pin to control pc relay
int bathBlowerPin = 7; // digital pin to control blower
// can not use dig pins 2, 4, 10, 11, 12, 13
// An EthernetUDP instance to let us receive packets over UDP
EthernetUDP Udp;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet
void setup() {
digitalWrite(garageDoorOperPin, HIGH); // high=off for this relay
digitalWrite(resetPCPin, HIGH);
digitalWrite(bathBlowerPin, LOW); // low=off for this relay
pinMode(garageDoorOperPin, OUTPUT);
pinMode(resetPCPin, OUTPUT);
pinMode(bathBlowerPin, OUTPUT);
// start the Ethernet and UDP:
Ethernet.begin(mac, ip);
Udp.begin(localPort);
delay(10000);
// Serial.begin(9600);
// Serial.println(“Starting”);
}
void loop() {
int packetSize = Udp.parsePacket();
if(packetSize)
{
// Serial.print(“Received packet of size “);
// Serial.println(packetSize);
// Serial.print(“From “);
// IPAddress remote = Udp.remoteIP();
// for (int i =0; i < 4; i++)
// {
// Serial.print(remote[i], DEC);
// if (i < 3)
// {
// Serial.print(“.”);
// }
// }
//
// Serial.print(“, port “);
// Serial.println(Udp.remotePort());
// read the packet into packetBufffer
Udp.read(packetBuffer,1);
// Serial.print(“Contents:”);
// Serial.println(packetBuffer);
cmdIn = (packetBuffer[0]);
// press garage door opener
if (cmdIn == cmdGDoper)
{
// Serial.println(“matched1!”);
digitalWrite(garageDoorOperPin, LOW); // relay uses NO connections
delay (100); // oper relay for 0.1 sec
digitalWrite(garageDoorOperPin, HIGH);
}
// trip smarthome pc relay for a minute
if (cmdIn == cmdResetPC)
{
// Serial.println(“matched2!”);
digitalWrite(resetPCPin, LOW); // relay uses NC connections
delay (60000); // delay for 60 seconds
digitalWrite(resetPCPin, HIGH);
}
// turn bath blower on
if (cmdIn == cmdBathBlowerOn)
{
// Serial.println(“matched3!”);
digitalWrite(bathBlowerPin, HIGH); // relay uses NO connections
}
// turn bath blower off
if (cmdIn == cmdBathBlowerOff)
{
// Serial.println(“matched4!”);
digitalWrite(bathBlowerPin, LOW); // relay uses NO connections
}
// reply to status check
// respond to status check
if (cmdIn == cmdStatusCheck)
// Serial.println(“matched9!”);
{
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.write(“ard68ok=yes”); // send packet good status
Udp.endPacket();
} // end of status check
} // end of incoming packet handler
} // end of main loop