Santiago Munoz
Created April 9, 2021

Automatic Light Switcher

Switch your lights on and off depending on the sunset and when people arrive.

21
Automatic Light Switcher

Things used in this project

Hardware components

Argon
Particle Argon
×1
Jumper wires (generic)
Jumper wires (generic)
×9
Ultra Sonic
×1

Story

Read more

Code

Code

C/C++
// This #include statement was automatically added by the Particle IDE.
#include <ArduinoJson.h>

StaticJsonDocument<512> doc;
// create servo object to control a servo
Servo myservo;  
// variable to store the servo position
int pos = 0;

//Setup of Ultra Sound Sensor
int trigPin = D2;
int echoPin = D3;

//Ultra Sound variables
long duration;
int distance;

void setup() {
    // Subscribe to the webhook response event
    Particle.subscribe("hook-response/sunset", myHandler, MY_DEVICES);
    myservo.attach(D4);  // attaches the servo on the D2 pin to the servo object
    Serial.begin(9600); //serial monitor
    myservo.write(0); //reset servo
    //Ultra Sonic
    pinMode(trigPin, OUTPUT); 
    pinMode(echoPin, INPUT); 

}

void loop() {
  // Get some data
  String data = String(180);
  // Trigger the integration
  Particle.publish("sunset", data, PRIVATE);
  // Wait 6 seconds
  delay(6000);

}

void myHandler(const char *event, const char *data) {
    
    //get hour
    String colin (":");
    String WordSearch (data);
    int colinPos = WordSearch.indexOf(colin, 0);
    String calcTime = WordSearch.substring(0, colinPos);
    int intCalcTime = calcTime.toInt();
    int finalTime = 0;
    
    //Convert Sunset hour UTC to CST
    if( intCalcTime > 6){
        finalTime = intCalcTime - 5;
    }
    else if(intCalcTime < 7){
        finalTime = intCalcTime + 5;
    }
    
    //Convert Current hour UTC to CST
    int timeNow = Time.hourFormat12(Time.now());
    
    if( timeNow > 6){
        timeNow = timeNow - 5;
    }
    else if(timeNow < 7){
        timeNow = timeNow + 5;
    }
    
    //Moving Servo
    int servoAngle = 0;
    
    //Testing Case
    timeNow = 7; //Replace with whatever time you want
    
    if(timeNow == finalTime){
        servoAngle = 180;
        myservo.write(servoAngle);
        delay(3);
    }
    
    
    /* //Real Deal of Sunset checker
    if (Time.isPM(Time.now())){ //Used to Verify 0-12 integer is Sunset by checking if its in the afternoon
        if (timeNow == finalTime){ //if current hour = sunset hour, then execute
            servoAngle = 180;
            myservo.write(servoAngle);       
            delay(3);        
        }
    }
    */
    
    //Sunset Time
    Serial.print("Webhook Sunset: ");
    Serial.println(data);
    
    //Hour Now
    Serial.println("Hour Now: ");
    Serial.println(timeNow);
    
    //Sunset Hour
    Serial.println("Sunset Hour: ");
    Serial.println(finalTime);
    
    
    //Ultra Sonic
    digitalWrite(trigPin, LOW);
    delay(200);
    
    digitalWrite(trigPin, HIGH);
    delay(400);
    digitalWrite(trigPin, LOW);
    
    duration = pulseIn(echoPin, HIGH);
    distance = duration*0.034/2;
    
    //Turn off Lights
    if (distance < 10 ){
        servoAngle = 0;
        myservo.write(servoAngle);
        delay(3);
    }
    
    Serial.print("Distance: ");
    Serial.println(distance);
    
}

Credits

Santiago Munoz
3 projects • 1 follower

Comments