This sketch controls an Arduino that acts like a fire alarm panel for a 4-wire monitored alarm system. When any of the detectors senses a fire or trouble it sends that status to HCA. It can reset the alarm system when HCA tells it to do so.
#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h> // UDP library
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x69 };
IPAddress ip(192, 168, 0, 69);
unsigned int localPort = 5001; // local port to listen on
IPAddress IP_Remote(192, 168, 0, 10); // IP address for smarthome
unsigned int Port_Remote = 5000; // Remote Arduino remote port
char cmdStatusCheck = ‘9’; // a “9” means smarthome is asking for status
char cmdResetFire = ‘1’; // a “1” means reset the fire alarm
EthernetUDP Udp; // An EthernetUDP instance
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet
char cmdIn = ‘0’; // used to obtain and compare buffer character
// pins 2, 4, 10, 11, 12, 13 are used by the ethernet shield
int val = 0;
int fireAlarmPin = 0; // analog pin 0
int fireAlarmRelayPin = 0; // digital pin 0
int fireTroubleLedPin = 5; // digital pin 5
int firePowerFaultPin = 6; // digital pin 6
int fireRelayPin = 7; // digital pin 7
int fireTripLedPin = 8; // digital pin 8
int fireResetPin = 9; // digital pin 9
// boolean flags to keep track of statii
boolean fireAlarm = false;
boolean fireTrouble = false;
boolean firePowerTrouble = false;
// finally do the setup
void setup()
{
// Open serial communications and wait for port to open:
// Serial.begin(9600);
delay(10000);
// start the Ethernet and UDP:
Ethernet.begin(mac,ip); // no need for gateway or mask
Udp.begin(localPort);
// first set the led outputs
digitalWrite(fireTripLedPin,LOW); // set alarm light off
digitalWrite(fireAlarmRelayPin,HIGH); // set alarm relay off
digitalWrite(fireTroubleLedPin,LOW); // set trouble light off
digitalWrite(fireRelayPin,HIGH); // turn power relay on, uses nc contacts
// then set all the output pin modes
pinMode(fireTripLedPin, OUTPUT);
pinMode(fireAlarmRelayPin, OUTPUT);
pinMode(fireTroubleLedPin, OUTPUT);
pinMode(fireRelayPin, OUTPUT);
// and set the input pin modes
pinMode(fireResetPin, INPUT_PULLUP);
pinMode(firePowerFaultPin, INPUT_PULLUP);
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.write(“ardfirearmed=yes”);
Udp.endPacket();
// Serial.print(“starting.\n”);
} // end of setup
// start of loop
void loop()
{
delay(1000); //cycle through every second
// check on alarm pin value
val = analogRead(fireAlarmPin);
// Serial.println(val);
if ( (val > 1000) && (!fireTrouble) )
{
// Serial.print(“Fire Trouble\n”);
fireTrouble = true;
digitalWrite(fireTroubleLedPin,HIGH);
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.write(“ardfiretrouble=yes”);
Udp.endPacket();
}
if ( (val < 300) && (!fireAlarm) )
{
// Serial.print(“Fire alarm!\n”);
fireAlarm = true;
digitalWrite(fireTripLedPin,HIGH);
digitalWrite(fireAlarmRelayPin,LOW);
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.write(“ardfirealarm=yes”);
Udp.endPacket();
}
if ( (digitalRead(firePowerFaultPin) == HIGH) && !firePowerTrouble )
{
// Serial.print(“Fire power fault!\n”);
firePowerTrouble = true;
digitalWrite(fireTroubleLedPin,HIGH);
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.write(“ardfirepowerfault=yes”);
Udp.endPacket();
}
if ( (digitalRead(firePowerFaultPin) == LOW) && firePowerTrouble )
{
// Serial.print(“Fire power fault!\n”);
firePowerTrouble = false;
digitalWrite(fireTroubleLedPin,LOW);
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.write(“ardfirepowerfault=no”);
Udp.endPacket();
}
if (digitalRead(fireResetPin) == LOW)
{
// Serial.print(“Fire reset\n”);
digitalWrite(fireTroubleLedPin,LOW);
digitalWrite(fireTripLedPin,LOW);
digitalWrite(fireAlarmRelayPin,HIGH);
digitalWrite(fireRelayPin,LOW);
fireAlarm = false;
fireTrouble = false;
firePowerTrouble = false;
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.write(“ardfirearmed=no”);
Udp.endPacket();
delay(5000);
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.write(“ardfirearmed=yes”);
Udp.endPacket();
digitalWrite(fireRelayPin,HIGH);
}
// see if there’s an incoming packet
int packetSize = Udp.parsePacket();
if(packetSize)
{
Udp.read(packetBuffer,1);
cmdIn = (packetBuffer[0]);
// respond to status check
if (cmdIn == cmdStatusCheck)
{
// Serial.println(“matched9!”);
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.write(“ard69ok=yes”); // send packet good status
Udp.endPacket();
}
// end of status check
// start of remote fire alarm reset
if (cmdIn == cmdResetFire)
{
// Serial.print(“Remote Fire reset\n”);
digitalWrite(fireTroubleLedPin,LOW);
digitalWrite(fireTripLedPin,LOW);
digitalWrite(fireAlarmRelayPin,HIGH);
digitalWrite(fireRelayPin,LOW);
fireAlarm = false;
fireTrouble = false;
firePowerTrouble = false;
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.write(“ardfirearmed=no”);
Udp.endPacket();
delay(5000);
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.write(“ardfirearmed=yes”);
Udp.endPacket();
digitalWrite(fireRelayPin,HIGH);
}
// end of remote fire alarm reset
}
// end of incoming packet handler
} // end of main loop