Nealson LawrenceIan Peksa
Published © LGPL

Smart Light - MEGR 3171 - Fall 2016

This project uses 2 Particle Photons to control whether the lights are on or off in your room automatically by sensing ambient light.

BeginnerWork in progress4 hours936
Smart Light - MEGR 3171 - Fall 2016

Things used in this project

Hardware components

Photon
Particle Photon
×2
Photo resistor
Photo resistor
×1
Resistor 221 ohm
Resistor 221 ohm
×2
LED (generic)
LED (generic)
×1
Servos (Tower Pro MG996R)
Just your generic servo motor that came with the builders kit purchased on Particle's website.
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Mobicle
ControlEverything.com Mobicle

Story

Read more

Schematics

Light Sensing Photon

This is the Particle Photon with the photo resistor attached to it to read the ambient light values, then publish the event either "Bright" or "Dark" to the Console so the other Particle can subscribe to this event to control the servo.

Particle Photon Controlling Servo Motor

This Particle Photon responds to the messages sent from the light sensing photon and controls the servo to turn the light switch on or off.

Code

Light Sensing Photon

Arduino
This is the Particle Photon with the photo resistor attached to it to read the ambient light values, then publish the event either "Bright" or "Dark" to the Console so the other Particle can subscribe to this event to control the servo.
int led = D0;
int photoresistor = A0;
int power = A5;
int analogvalue;     

//This section of code simply assigns the pins on the Particle Photon, and names 
//variables that will be used later in the code.

void setup() {
    pinMode(led,OUTPUT);
    pinMode(power,OUTPUT);
    pinMode(photoresistor, INPUT);
    
    digitalWrite(power,HIGH);
    
    Particle.variable("analogvalue", &analogvalue, INT);
    
}

//This section of the code is the setup for the rest of the code.  It applies power
//to the pins requiring power, or names the pin to recieve an input; in this case
//from our photo resistor.

void loop() {
    
    analogvalue = analogRead(photoresistor);
    delay(100);
    
    if (analogvalue < 60){
        Particle.publish("LightStatus_NTL95UNCC","Bright", PUBLIC);
        digitalWrite(led,HIGH);
        delay(4000);
        //delay(900000);
    }
    else {
        Particle.publish("LightStatus_NTL95UNCC","Dark", PUBLIC);
        digitalWrite(led,LOW);
        delay(4000);
        //delay(900000);
    }
    
}
    
//This loop checks the analogue value of the photoresistor and compares it to the
//average light value constant selected.  Then, depending on whether the value is
//higher or lower than the selected constant, the Particle publishes an event to the
//console, with the event reading either "Bright" or "Dark."
//The delay is in there simply to slow the iteration process of the loop so the 
//Particle does not overload the console with too many publishes per second.    

Servo Control Code

Arduino
This part of the code is subscribing to the light sensing particle in order to get a value to base whether it should turn the light on or off. The servo functions from 0-180 degrees, so calibration for your specific light switch will probably be required.
//ServoLightSwitch
// Connect RED wire to VIN (~5V)
// Connect ORANGE, YELLOW, or WHITE wire to A0 (servo signal)
// Connect BLACK or BROWN wire to GND (0V)
// Adjust these connections for your particular servo
// if you have a wiring diagram for it
//----------------------------------------------------------------------------

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

void setup()
{ Particle.subscribe("LightStatus_NTL95UNCC", servo);
  // attaches the servo on the A4 pin to the servo object
  myservo.attach(A4);

  // register the Spark function
  Spark.function("servo", updateServo);
}
int updateServo(String command)
{
  // convert string to integer
  uint8_t pos = command.toInt();
  
  
}
void servo(const char *event, const char *data)
{
    if (strcmp(data,"Dark")==0) 
  {
    // if your buddy's beam is intact, then turn your board LED off
    myservo.write(67);
  }
  else if (strcmp(data,"Bright")==0) {
    // if your buddy's beam is broken, turn your board LED on
    myservo.write(30);
  }
  else {
    // if the data is something else, don't do anything.
    // Really the data shouldn't be anything but those two listed above.
  }
}

Credits

Nealson Lawrence

Nealson Lawrence

1 project • 1 follower
Ian Peksa

Ian Peksa

1 project • 0 followers

Comments