Introduction: Particle Core: Send an Email When Motion Is Detected
This instructable is a part of series of instrucatbles, and if you are following the series in the last instrucatble I showed you how to get a motion sensor to the internet using a Particle core. In this instructable I'm going to show you how to take that simple circuit and make it send an email every time any motion is detected.
If you are getting started with the particle core you can check out my previous istructable as a guide on how to get started before actually trying out this instrucatble. I will also try to keep the instructable as simple as possible so that no one gets left behind.
So lets get started....
Step 1: Tools and Components
The list of components is quite simple, all the components you need is the same as the previous instructable -
- Spark Core
- LDR
- LED
- Resistor
No soldering skills are required to carry on this instructable, as we will be using a breadboard. You can also use a photon which is a cheaper version of the particle core.
Step 2: Getting Started
If you have followed my previous instructables you already have set up your core and have it connected to the internet.
If you are here first, then you can check the step two of any of the previous instructables in the series for steps on how to get started. The steps involve -
- Creating an account at Particle.io
- Getting it connected to the internet.
- Claiming a Core
- Trying out Tinker
- Trying out my previous instructables
If you have gone through all of these steps, then you are good to proceed to the next step.
Step 3: Circuit
The circuit is very simple, and everything is assembled on a breadboard, so no soldering skills are required. The circuit has an LDR and a LED and which is made to face each other. Once the beam of light form the led is interrupted by any motion in front of it a particle publish is triggered.
This is quite basic tutorial and the range of sensing is quite low in the next instructable I will show you how to actually connect a PIR motion sensor and link it to the network lights I explained in the last instructable.
All you do is have to follow the circuit and keep the LDR and the LED a fingers distance apart form each other.
Step 4: Code
After assembling the circuit all you have to do is get the core connected to the internet, and after that is done its time to upload the code to the core. Here is the code to be uploaded...
// -----------------------------------------
// Publish and Dashboard with Photoresistors // ----------------------------------------- // This app will publish an event when the beam of light between the LED and the photoresistor is broken. // It will publish a different event when the light is intact again. // Just like before, we're going to start by declaring which pins everything is plugged into. int led = D0; // This is where your LED is plugged in. The other side goes to a resistor connected to GND. int boardLed = D7; // This is the LED that is already on your device. // On the Core, it's the LED in the upper right hand corner. // On the Photon, it's next to the D7 pin. int photoresistor = A0; // This is where your photoresistor is plugged in. The other side goes to the "power" pin (below). int power = A5; // This is the other end of your photoresistor. The other side is plugged into the "photoresistor" pin (above). // The following values get set up when your device boots up and calibrates: int intactValue; // This is the average value that the photoresistor reads when the beam is intact. int brokenValue; // This is the average value that the photoresistor reads when the beam is broken. int beamThreshold; // This is a value halfway between ledOnValue and ledOffValue, above which we will assume the led is on and below which we will assume it is off. bool beamBroken = false; // This flag will be used to mark if we have a new status or now. We will use it in the loop. // We start with the setup function. void setup() { // This part is mostly the same: pinMode(led,OUTPUT); // Our LED pin is output (lighting up the LED) pinMode(boardLed,OUTPUT); // Our on-board LED is output as well pinMode(photoresistor,INPUT); // Our photoresistor pin is input (reading the photoresistor) pinMode(power,OUTPUT); // The pin powering the photoresistor is output (sending out consistent power) // Next, write the power of the photoresistor to be the maximum possible, which is 4095 in analog. digitalWrite(power,HIGH); // Since everyone sets up their leds differently, we are also going to start by calibrating our photoresistor. // This one is going to require some input from the user! // First, the D7 LED will go on to tell you to put your hand in front of the beam. digitalWrite(boardLed,HIGH); delay(2000); // Then, the D7 LED will go off and the LED will turn on. digitalWrite(boardLed,LOW); digitalWrite(led,HIGH); delay(500); // Now we'll take some readings... int on_1 = analogRead(photoresistor); // read photoresistor delay(200); // wait 200 milliseconds int on_2 = analogRead(photoresistor); // read photoresistor delay(300); // wait 300 milliseconds // Now flash to let us know that you've taken the readings... digitalWrite(boardLed,HIGH); delay(100); digitalWrite(boardLed,LOW); delay(100); digitalWrite(boardLed,HIGH); delay(100); digitalWrite(boardLed,LOW); delay(100); // Now the D7 LED will go on to tell you to remove your hand... digitalWrite(boardLed,HIGH); delay(2000); // The D7 LED will turn off... digitalWrite(boardLed,LOW); // ...And we will take two more readings. int off_1 = analogRead(photoresistor); // read photoresistor delay(200); // wait 200 milliseconds int off_2 = analogRead(photoresistor); // read photoresistor delay(1000); // wait 1 second // Now flash the D7 LED on and off three times to let us know that we're ready to go! digitalWrite(boardLed,HIGH); delay(100); digitalWrite(boardLed,LOW); delay(100); digitalWrite(boardLed,HIGH); delay(100); digitalWrite(boardLed,LOW); delay(100); digitalWrite(boardLed,HIGH); delay(100); digitalWrite(boardLed,LOW); // Now we average the "on" and "off" values to get an idea of what the resistance will be when the LED is on and off intactValue = (on_1+on_2)/2; brokenValue = (off_1+off_2)/2; // Let's also calculate the value between ledOn and ledOff, above which we will assume the led is on and below which we assume the led is off. beamThreshold = (intactValue+brokenValue)/2; } // Now for the loop. void loop() { /* In this loop function, we're going to check to see if the beam has been broken. When the status of the beam changes, we'll send a Spark.publish() to the cloud so that if we want to, we can check from other devices when the LED is on or off. We'll also turn the D7 LED on when the Photoresistor detects a beam breakagse. */ if (analogRead(photoresistor)>beamThreshold) { /* If you are above the threshold, we'll assume the beam is intact. If the beam was intact before, though, we don't need to change anything. We'll use the beamBroken flag to help us find this out. This flag monitors the current status of the beam. After the beam is broken, it is set TRUE and when the beam reconnects it is set to FALSE. */ if (beamBroken==true) { // If the beam was broken before, then this is a new status. // We will send a publish to the cloud and turn the LED on. // Send a publish to your devices... Spark.publish("beamStatus","intact",60,PRIVATE); // And flash the on-board LED on and off. digitalWrite(boardLed,HIGH); delay(500); digitalWrite(boardLed,LOW); // Finally, set the flag to reflect the current status of the beam. beamBroken=false; } else { // Otherwise, this isn't a new status, and we don't have to do anything. } } else { // If you are below the threshold, the beam is probably broken. if (beamBroken==false) { // Send a publish... Spark.publish("beamStatus","broken",60,PRIVATE); // And flash the on-board LED on and off. digitalWrite(boardLed,HIGH); delay(500); digitalWrite(boardLed,LOW); // Finally, set the flag to reflect the current status of the beam. beamBroken=true; } else { // Otherwise, this isn't a new status, and we don't have to do anything. } } }
Step 5: Finishing
The steps till now is similar to the previous instructables, this is the only step that is different. Once you got the circuit setup and got all the data displayed on the dashboard. Go the IFTTT home page and sign up for an account, after that enable particle channel and sign up in your particle account.
Now create a recipe with the details like in the pictures, and set the action to "Send an email", and enter your email id and create a message.
Now the next time you block the path of the beam you would get a mail notifying that.