Skip to content

Instantly share code, notes, and snippets.

@bstolte
Created March 26, 2014 05:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bstolte/bc7726b9afd01faf84ef to your computer and use it in GitHub Desktop.
Save bstolte/bc7726b9afd01faf84ef to your computer and use it in GitHub Desktop.
Motion Detector
// Uses a PIR sensor to detect movement, light an LED and eventually send a SMS
// based upon: https://raw.github.com/jedgarpark/Make_PIR_Sensor/master/MAKE_PIR_Sensor.pde
#include <string.h>
char server[] = "bstolte.com";
TCPClient client;
int ledPin = D5; // choose the pin for the LED
int inputPin = D7; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // set up the LED as an output, it will light up if motion is detected
pinMode(inputPin, INPUT); // read the input from the PIR sensor
Spark.variable("inputPin", &inputPin, INT); // Register the Spark variable for use with the API
Serial.begin(9600);
delay(1000);
Serial.println("connecting...");
}
void loop() {
val = digitalRead(inputPin); // read the value from the PIR sensor
if (val == HIGH) { // if the sensor detects motion
digitalWrite (ledPin, HIGH); // then turn on the LED
// Call bstolte.com/motion_sms/sendsms to send a text message
client.connect(server, 80);
client.println("GET /motion_sms/sendsms/4154256133/hello HTTP/1.0");
client.println("Connection: close");
client.print("Host: ");
client.println(server);
client.println("Accept: text/html, text/plain");
client.println();
client.flush();
client.stop();
delay(5000);
if (pirState == LOW) { // we have turned on so let's let the serial monitor know
Serial.println("Motion detected!");
pirState = HIGH; // set it to high
}
}
else {
digitalWrite (ledPin, LOW); // turn the LED off
delay(300); // wait again for 300ms
if (pirState == HIGH) { // we have just turned off
Serial.println("Motion ended!");
pirState = LOW;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment