Patrick AnnasPeter Cestrone
Published

Motion Activated Room Light

With this project, you'll never have to search for the light switch in the dark again.

EasyFull instructions provided5 hours447
Motion Activated Room Light

Things used in this project

Hardware components

Photon
Particle Photon
×2
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×7
Resistor 1k ohm
Resistor 1k ohm
×1
LED (generic)
LED (generic)
×1
Particle micro servo
×1

Story

Read more

Schematics

PIR Configuration

Pir setup dq12tnoxf1

Servo Configuration

Servo setup e4k4dgzuaz

Code

PIR code

C/C++
const String key = "RWEL6M0ZC87IA980"; //write key
int inputPin = D0;              // choose the input pin (for PIR sensor)
int ledPin = D1;                // LED Pin
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
int calibrateTime = 10000;      // wait for the thingy to calibrate
int rssival=0;
char publishString[40];
double motion = 70;
void setup()

{
  pinMode( ledPin, OUTPUT );
  pinMode(inputPin, INPUT);     // declare sensor as input
  Particle.variable ("motion" &motion);
}

void loop()
{

  // if the sensor is calibrated
  if ( calibrated() )
  {
  // get the data from the sensor
    readTheSensor();

    // report it out, if the state has changed
    reportTheData();
  }
}
void dopublish(){
    rssival = WiFi.RSSI();
    sprintf(publishString,"%d",rssival);
                
    Particle.publish("RSSI",publishString);
}
void readTheSensor() {
  val = digitalRead(inputPin);
}

bool calibrated() {
  return millis() - calibrateTime > 0;
}

void reportTheData() {

  // if the sensor reads high
  // or there is now motion
  if (val == HIGH) {

    // the current state is no motion
    // i.e. it's just changed
    // announce this change by publishing an eent
    if (pirState == LOW) {
      // we have just turned on
      Particle.publish("designingiot/s15/motion","Movement");
      // Update the current state
      pirState = HIGH;
      setLED( pirState );
    }
  } else {
    if (pirState == HIGH) {
      // we have just turned of
      // Update the current state
      pirState = LOW;
      setLED( pirState );
    }
  }
}
bool updateThingspeak() 
{
    //delay (2000);
    static int count = 0;
    Serial.println(count++);
    int rssival = WiFi.RSSI();
    //sprintf(publishString,"%d",rssival);
    //bool success = Particle.publish("RSSI",publishString);
    
    //sprintf(publishString, "%1.4f", checkbattery());

      bool success = Particle.publish("thingSpeakWrite_All", +
     "{ \"1\": \"" + String(rssival) + "\"," +
       "\"2\": \"" + String(motion) + "\"," +
       "\"k\": \"" + key + "\" }", 60, PRIVATE);
    return success; //if sent, then turn of the send flag, otherwise let it try again.
    
}
void setLED( int state )
{
  digitalWrite( ledPin, state );
}

Servo Code

C/C++
Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created


int pos = 0;    // variable to store the servo position
int led = D7;


void setup( ) {
    pinMode(led,OUTPUT);
    digitalWrite(led,LOW);
  // attaches the servo on the D0 pin to the servo object
  myservo.attach(D0);

  // register the Spark function

  Particle.subscribe("designingiot/s15/motion", anything, "270039001147353230333635");
}

void anything(const char *event, const char *data)
{
     digitalWrite(led,HIGH);
     if (strcmp(data, "Movement")==0)
     {
myservo.write(175);
delay(60000);
myservo.write(0);
     }
}

void loop()
{
  
}

Credits

Patrick Annas

Patrick Annas

1 project • 1 follower
Contact
Peter Cestrone

Peter Cestrone

1 project • 0 followers
Contact

Comments

Add projectSign up / Login