Core named labrador
#include "blinkm.h"
// Debug mode
boolean DEBUG = false;
// variable setup
int calibrationTime = 30; // the time we give the sensor to calibrate
long unsigned int lowIn; // the time when the sensor outputs a low impulse
unsigned long pause = 60000UL;
boolean lockLow = true;
boolean takeLowTime;
int pirPin = A0; // the digital pin connected to the PIR sensor's output
boolean currentMotion = false; // did we detected motion, assume false at startup
//int ledPin = A1; // digital pin for LED
char myIpString[24]; // localIP
// home?
boolean home = false;
// blinkM
int blinkm_addr = 0x09;
int ledPin = 13;
int r = 0;
int g = 0;
int b = 0;
boolean isBlink = false;
int setrgb(String args){
Serial.print("Received: ");
Serial.println(args);
char szArgs[13];
args.toCharArray(szArgs, 12);
sscanf(szArgs, "%d,%d,%d", &r, &g, &b);
Serial.println("Color: ");
Serial.print(r);
Serial.print(",");
Serial.print(g);
Serial.print(",");
Serial.print(b);
Serial.println("");
if(!isBlink){
BlinkM_fadeToRGB(blinkm_addr, r, g, b);
}
return 0;
}
int setblink(String args){
int blink = args.toInt();
isBlink = (1 == blink) ? true : false;
if(!isBlink){
BlinkM_fadeToRGB(blinkm_addr, r, g, b);
}
if(DEBUG){Serial.print("Blinking set to: " + isBlink);}
}
void setup() {
// SETUP
Serial.begin(9600);
pinMode(pirPin, INPUT);
digitalWrite(pirPin, LOW);
// give the sensor some time to calibrate
if(DEBUG){Serial.print("calibrating sensor ");}
for(int i = 0; i < calibrationTime; i++) {
if(DEBUG){Serial.print(".");}
delay(1000);
}
if(DEBUG){
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
Serial.print("Registering the variable...");
}
// Register the variable
Spark.variable("motion", ¤tMotion , BOOLEAN);
Spark.publish("labrador rev13 is online...", NULL, 60, PRIVATE);
Spark.variable("home", &home, BOOLEAN);
// my network localIP
IPAddress localIp = WiFi.localIP();
sprintf(myIpString, "%d.%d.%d.%d", localIp[0], localIp[1], localIp[2], localIp[3]);
Spark.variable("ipAddress", myIpString, STRING);
if(DEBUG){Serial.println(".. done");}
// blinkM setup
BlinkM_begin();
delay(100); // wait for power to stabilize
BlinkM_stopScript(blinkm_addr);
BlinkM_setRGB(blinkm_addr, r, g, b);
Spark.function("setcolor", setrgb);
Spark.function("setblinking", setblink);
// LED
pinMode(ledPin, OUTPUT);
// take control of the LED
RGB.control(true);
// red, green, blue, 0-255
RGB.color(0, 0, 0);
}
void loop() {
//LOOP
if(digitalRead(pirPin) == HIGH){
if(lockLow){
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
BlinkM_fadeToRGB(blinkm_addr, 0, 0, 5);
if(DEBUG) {
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
}
delay(50);
currentMotion = true;
Spark.publish("labrador_motion", "true");
}
takeLowTime = true;
}
if(digitalRead(pirPin) == LOW){
if(takeLowTime){
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause,
//we assume that no more motion is going to happen
if(!lockLow && millis() - lowIn > pause){
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow = true;
BlinkM_fadeToRGB(blinkm_addr, 0, 0, 0);
if(DEBUG) {
Serial.print("motion ended at "); //output
Serial.print((millis() - pause)/1000);
Serial.println(" sec");
}
delay(50);
currentMotion = false;
Spark.publish("labrador_motion", "false");
}
}
if(isBlink){
BlinkM_fadeToRGB(blinkm_addr, r, g, b);
delay(1000);
BlinkM_fadeToRGB(blinkm_addr, 0, 0, 0);
delay(1000);
}
}
the IFTTT settings, not really sure how to copy it but here is the gist of it.
If motion on "labrador" is Equals true, then send me an SMS at 1234567890