William AllenTaylor Pearce
Published

Lazy Lights

An internet controlled light switch with logic.

IntermediateShowcase (no instructions)3 hours1,029
Lazy Lights

Things used in this project

Hardware components

Photon
Particle Photon
×2
Photo resistor
Photo resistor
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Servos (Tower Pro MG996R)
×1

Software apps and online services

Assistant SDK
Google Assistant SDK

Story

Read more

Schematics

Photoresistor Diagram

Servo Diagram

Code

Light Sensor

Arduino
This code reads the value of light intensity, and publishes the status of the lights (on/off) to the cloud.
#include <Ubidots.h>

#define TOKEN "A1E-o4CPzsR5WSWiyJHr1nR43BPOAZjW7i"

Ubidots ubidots(TOKEN);

int light = A0;
int power = A5;
int noLightThresh = 200;
int analogvalue;
int counter=0;

void setup() {
    
    Serial.begin(115200);
    pinMode(light,INPUT);
    pinMode(power,OUTPUT);
    digitalWrite(power,HIGH);
    
    
}

void loop() {
    analogvalue = analogRead(light);
    Particle.variable("analogvalue", &analogvalue, INT);
    
    if(analogRead(light) < noLightThresh){
        Particle.publish("wallen20_light_out","Off");
        
    }
    else {
        Particle.publish("wallen20_light_out","On");
    }
    delay(1000);
    
    counter=counter + 1;
    
    if (counter == 10) {
        ubidots.add("light_sensor", analogvalue); 
        ubidots.sendAll();
        counter = 0;
    }
    
}

Servo Control

Arduino
This code receives an on or off command, compares it to the condition of the lights, and moves the servo accordingly. It also counts and graphs the number of times an on / off command is sent.
#include <Ubidots.h>

#define TOKEN "A1E-o4CPzsR5WSWiyJHr1nR43BPOAZjW7i"

Servo servo;
Ubidots ubidots(TOKEN);
int counterOn = 0;
int counterOff = 0;

void setup() {
    Serial.begin(115200);
    servo.attach(D0);
    servo.write(10);
    Particle.function("setpos", setPos);
}

int setPos(String command) {
    
    if(command == "Lights_On") {
        counterOn = counterOn + 1;
        ubidots.add("counterOn", counterOn);
        ubidots.sendAll();
        Particle.subscribe("wallen20_light_out", LightCommandOn1);
        return 1;
    }
    else if (command == "Lights_Off") {
        counterOn = counterOff + 1;
        ubidots.add("counterOff", counterOff);
        ubidots.sendAll();
        Particle.subscribe("wallen20_light_out", LightCommandOff1);
        return 0;
    }
}



void LightCommandOn1(const char *event, const char *data) {
    if (strcmp(data,"Off")==0) {
        servo.write(180);
        delay(1500);
        Particle.unsubscribe();
        Particle.subscribe("wallen20_light_out", LightCommandOn2);
    }
    else if (strcmp(data,"On")==0) {
        delay(1500);
        Particle.unsubscribe();
    }
}

void LightCommandOn2(const char *event, const char *data) {
    if (strcmp(data,"Off")==0) {
        servo.write(10);
        delay(1500);
        Particle.unsubscribe();
        Particle.subscribe("wallen20_light_out", LightCommandOn3);
    }
    else if (strcmp(data,"On")==0) {
        delay(1500); 
        Particle.unsubscribe();
    }
}

void LightCommandOn3(const char *event, const char *data) {
    if (strcmp(data,"Off")==0) {
        servo.write(180);
        delay(1500);
        Particle.unsubscribe();
    }
    else if (strcmp(data,"On")==0) {
        delay(1500);
        Particle.unsubscribe();
    }
}

void LightCommandOff1(const char *event, const char *data) {
    if (strcmp(data,"On")==0) {
        servo.write(10);
        delay(1500);
        Particle.unsubscribe();
        Particle.subscribe("wallen20_light_out", LightCommandOff2);
    }
    else if (strcmp(data,"Off")==0) {
        delay(1500);
        Particle.unsubscribe();
    }
}

void LightCommandOff2(const char *event, const char *data) {
    if (strcmp(data,"On")==0) {
        servo.write(180);
        delay(1500);
        Particle.unsubscribe();
        Particle.subscribe("wallen20_light_out", LightCommandOff3);
    }
    else if (strcmp(data,"Off")==0) {
        delay(1500);
        Particle.unsubscribe();
    }
}

void LightCommandOff3(const char *event, const char *data) {
    if (strcmp(data,"On")==0) {
        servo.write(10);
        delay(1500);
        Particle.unsubscribe();
    }
    else if (strcmp(data,"Off")==0) {
        delay(1500);
        Particle.unsubscribe();
    }
}

Credits

William Allen

William Allen

1 project • 1 follower
Taylor Pearce

Taylor Pearce

0 projects • 1 follower
MEGR 3171 Instumentation & Measurement student

Comments