Current Sensing Sketch

This sketch has electrical current sensors that detect when a motor (i.e. a sump pump) comes on and goes off.  It also detects when a furnace goes on and off by looking at the voltages on the furnace’s call-for-heat circuit.  It reports all of this to HCA.

// ard_72_150103.ino
// tried new scheme for furnace sensor timing
// added status check
// tried new scheme for spa and sump sensors
#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, 0x72 };
IPAddress ip(192, 168, 0, 72);
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
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
// sump vars
int sumpsensorpin = 0;  // analog port for sump
int sumpsa[] = {1000,1000,1000,1000,1000};  // sump sense array
int sumpst = 0;        //spa sense total
boolean sumprunning = false;
long sumpStartTime;
long sumpDuration;
String sumpString1 = “ardsumpdur=”;
String sumpString2 = “0000”;
int sumplooper = 0;  // looper var
// spa vars
int spasensorpin = 1;  //analog port for spa
int spasa[] = {1000,1000,1000,1000,1000};  // spa sense array
int spast = 0;        //spa sense total
long spaStartTime;
long spaDuration;
boolean sparunning = false;
String spaString1 = “ardspadur=”;
String spaString2 = “0000”;
int spalooper = 0;  // looper var
// furnace vars
int furnsensorpin = 2;  //analog port for furnace
int furnlooper = 0;  // looper var
int fsa[] = {500,500,500,500,500};  // furnace sense array
int fst = 0;  //furnace sense total
boolean furnrunning = false;
long furnStartTime;
long furnDuration;
String furnString1 = “ardfurndur=”;
String furnString2 = “0000”;

void setup()
{
//    Serial.begin(9600);
delay(5000);
// start the Ethernet and UDP:
Ethernet.begin(mac,ip);           // no need for gateway or mask
Udp.begin(localPort);
}

void loop()
{
delay(1000);
// handle sump pump sensor
for ( sumplooper = 0; sumplooper < 4; sumplooper++)
{
sumpsa[sumplooper] = analogRead(sumpsensorpin);
delay(4);
}
sumpst = sumpsa[0] + sumpsa[1] + sumpsa[2] + sumpsa[3];
//    Serial.println(sumpst);
if (sumpst > 3800 && sumprunning)  // sump has just stopped
{
sumprunning = false;        //reset flag
sumpDuration = (millis() – sumpStartTime)/1000;        //calc dur of run
sumpString2 = sumpString1 + sumpDuration;        // build string
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.print(sumpString2);        // send packet with dur
Udp.endPacket();
delay(1000);
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.write(“ardsumprun=no”);        // now send packet with trigger
Udp.endPacket();
}
if (sumpst < 3400 && !sumprunning)   // sump has just started
{
sumpStartTime = millis();        // start timer
sumprunning = true;        // reset flag
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.write(“ardsumprun=yes”);        // now send packet to reset trigger
Udp.endPacket();
}
// end of sump pump sensor
// handle spa heater sensor
for ( spalooper = 0; spalooper < 4; spalooper++)
{
spasa[spalooper] = analogRead(spasensorpin);
delay(4);
}
spast = spasa[0] + spasa[1] + spasa[2] + spasa[3];
//    Serial.println(spast);
if (spast > 3900 && sparunning)  // spa has just stopped
{
sparunning = false;
spaDuration = (millis() – spaStartTime)/1000;        //calc dur of run
spaString2 = spaString1 + spaDuration;        // build string
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.print(spaString2);        // send packet with dur
Udp.endPacket();
delay(1000);
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.write(“ardsparun=no”);        // now send packet with trigger
Udp.endPacket();
}
if (spast < 3400 && !sparunning)  // spa has just started
{
spaStartTime = millis();        // start timer
sparunning = true;        // reset flag
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.write(“ardsparun=yes”);        // now send packet to reset trigger
Udp.endPacket();

}
// end of spa heater sensor
// handle furnace sensor

for ( furnlooper = 0; furnlooper < 4; furnlooper++)
{
fsa[furnlooper] = analogRead(furnsensorpin);
delay(4);
}
fst = fsa[0] + fsa[1] + fsa[2] + fsa[3];
//    Serial.println(fst);
if (fst < 3900 && furnrunning)  // furnace has just stopped
{
furnrunning = false;
furnDuration = (millis() – furnStartTime)/1000;        //calc dur of run
furnString2 = furnString1 + furnDuration;        // build string
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.print(furnString2);        // send packet with dur
Udp.endPacket();
delay(1000);
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.write(“ardfurnrun=no”);        // now send packet with trigger
Udp.endPacket();
}
if (fst > 3900 && !furnrunning)  // furnace has just started
{
furnStartTime = millis();        // start timer
furnrunning = true;        // reset flag
Udp.beginPacket(IP_Remote, Port_Remote);
Udp.write(“ardfurnrun=yes”);        // now send packet to reset trigger
Udp.endPacket();

}
// end of furnace sensor
// 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(“ard72ok=yes”);        // send packet good status
Udp.endPacket();
}  // end of status check

}
// end of incoming packet handler

}

As In Various and Sundry