Software apps and online services

Story

ClapOn/OffLights

This project was designed to eliminate the struggle of forgetting to turn off lights before bed, and when the room is sufficiently lit from outdoor light. People who are lazy, forgetful, and clumsy would love this. All you need to start is just two photons and a spark fun sound detector.

SoundDetectorBasics

This is the first part of the project, it is fairly simple. All you need is a photon, some wires, a mini breadboard, and a SEN 12642 SparkFun Sound Detector. It is possible to not use the SEN 12642 sound detector, but you will have to use a microphone and an amplifier to boost the signal. With the SEN 12642 sound detector, you will want to use the envelope output pin. We have setup the Photon, so that the A0 pin is the reading the signal from the amplifier. The Vin and GND pins are used to supply power to the SEN 12642. Once its all set up, you will have to calibrate it to only activate when a certain peak signal is reached, and that will be done in the code.

LightSensorBasics

The second part included the photon, mini breadboard, the photo resistor, and a 100 kohms resistor. The Vin and GND from the photon are connected to the photo resistor and resistor in order to supply power. At first a bigger resistor was used with the photo resistor, but there was too much sensitivity for the amount of light it picked up. Lowering the resistor lowered the sensitivity a little bit. The photo resistor is connected to the A0 pin on the photon in order to get the light readings. The readings would go to ThinkSpeak to show what readings the photo resistor was picking up for when the light was on or off which was all done through the code.

Programming

The last and very important part of this project is the Smart Life "Mini Plug". This plug has an internal switch which can be controlled via the internet to turn on and off, perfect for the particle platform. The other major applications that are required are IFTTT, ThingSpeak, and Webhooks. The important part of this plug is that it is compatible with IFTTT which is basically a website which can run "if this then that" statements. It is extremely important that you make a webhook that receives the particle publishes from the photon and sends them directly to IFTTT, if you don't it can take either one minute to a hour for the smart plug to respond. No one has enough time for that.

We also included certain bits of information that is transferred to each photon. For example we used particle.subscribe to turn on the D7 led on the LightSensor when the light is on. When the light is turned off the D7 blinks rapidly then turns off showing the state of the light. In the SoundDetector, we have particle.subscribed to the event BrightRoom which turns off the light when the room exceeds a certain light level to hopefully help turn off any unnecessary lights.

Video

Pictures

Here is some of our data that was graphed from 3am to about 11am, and its cool that you can see right at the point where light started to enter my room at around 7am. I was curious as to why it was pegged at 4K readings. Since most of the time this sensor was only tested at night, actual sunlight has a far greater effect on the photo resistor than a simple lamp light. In the end, a different resistor was used to lower the voltage output value to compensate. It is also fun to see all the spikes and rough data when there is a person nearby.

Schematics

Light Sensor Circuit Diagram

Sound Detector Circuit Diagram

Code

Sound/Clap Sensor

C#

The Photon reads the output of a SEN-12642 sound detector, and when the output reaches a certain value the photon will publish a "clapOn" or "clapOff" event.

intaudioSensor=A0;//labeling pin A0 for future useintvalue=soundDetect();//making value a integerboollight=false;voidsetup(){pinMode(audioSensor,INPUT);//setting pinMode for input pinParticle.publish("clapOff",PRIVATE);// making sure program always knows which state it is in by starting with light offParticle.subscribe("BrightRoom",myHandler2,MY_DEVICES);// BrightRoom function turns light off when room is brighter than 4000}voidloop(){value=soundDetect();// runs sound detect programif(value>2500){//if loop constantly checks to see if sound value is higher than ####if(light==true){light=false;//sets light value to false which equals offParticle.publish("clapOff",PRIVATE);//sends publish event to ifttt applet looking for "event_name_here"delay(1000);//Delay to make sure loop doesn't run more than once per sound}else{light=true;// sets light value to true which equals onParticle.publish("clapOn",PRIVATE);//sends publish event to ifttt applet looking for "event_name_here"delay(1000);//Delay to make sure loop doesn't run more than once per sound}delay(1000);//delay added to make program function properly }}intsoundDetect(){//when soundDetect is call this is what is runreturnanalogRead(audioSensor);//this takes a reading from the audioSensor pin}voidmyHandler2(constchar*BrightRoom,constchar*data){//this function looks for BrightRoom EventParticle.publish("clapOff",PRIVATE);//sends publish event to ifttt applet looking for "event_name_here" }

LightSensor

C#

This code gathers the output of a photo resistor and sends the data to ThinkSpeak to be graphed. Also, when the room is too bright it will shut off light when its not needed.

intlightsensor=A0;// Labels A0 pin as lightsensor for further useintvalue=readlight();// sets value as an integervoidsetup(){pinMode(D7,OUTPUT);//sets D7 to be an outputdigitalWrite(D7,LOW);//turns D7 off when photon just starts to clear any previous requestspinMode(lightsensor,INPUT);Particle.subscribe("clapOn",myHandler,MY_DEVICES);// Subscribe to the integration response eventParticle.subscribe("clapOff",myHandler2,MY_DEVICES);// Subscribe to the integration response event}voidloop(){value=readlight();//Runs readlight() program written belowParticle.publish("LightRead","{ \"value\": "+String(value)+", \"key\": \"THINGSPEAK_API_KEY_HERE\" }",THINGSPEAKCHANNEL_ID_HERE,PRIVATE);//publishes to ThingSpeakdelay(1000);if(value>4000){Particle.publish("BrightRoom",PRIVATE);// This checks to see if the room is bright enough to turn light off }}intreadlight(){// readlight() program written here in global viewintanalogValue=analogRead(lightsensor);// sets lightsensor pin to a valuereturnanalogValue;// returns analogValue number to where readlight() is }intfunblink(){// code for the funblink() NOTE:Do not place below myHandler functions or Photon will only blink rapidly forever digitalWrite(D7,HIGH);delay(50);digitalWrite(D7,LOW);delay(50);digitalWrite(D7,HIGH);delay(50);digitalWrite(D7,LOW);delay(50);digitalWrite(D7,HIGH);delay(50);digitalWrite(D7,LOW);delay(50);digitalWrite(D7,HIGH);delay(50);digitalWrite(D7,LOW);delay(50);}voidmyHandler(constchar*clapOn,constchar*data){// Looks for the clapon eventdigitalWrite(D7,HIGH);// when clap on is published D7 Pin turned on }voidmyHandler2(constchar*clapOff,constchar*data){// looks for the clapoff eventfunblink();// runs the fun blink program}