Austin Alexander SouthTaylor Chandler
Published © GPL3+

MEGR 3171 Automatic Room Lighting

Remove the need for light switches and wasted time unplugging and plugging in various lights in the room

IntermediateFull instructions provided15 hours250
MEGR 3171 Automatic Room Lighting

Things used in this project

Hardware components

Argon
Particle Argon
×2
Etekcity Remote Control Outlet Wireless Remote Light Switch
×1
ELEGOO 8 Channel DC 5V Relay Module with Optocoupler
ELEGOO 8 Channel DC 5V Relay Module with Optocoupler
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×2

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
ThingSpeak API
ThingSpeak API

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Wiring Diagram

Full layout of the wiring of the project

Code

"Alex" device code

C/C++
This code is designed to take data from an ultrasonic sensor, the other particle and inputs from the particle app and then use this data to control the relay device connected to the outlet remote.
// This #include statement was automatically added by the Particle IDE.
#include <elapsedMillis.h>

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

elapsedMillis timeElapsed;

unsigned int interval = 15000;

int cm = 0;

int trigPin = D10;
int echoPin = D11;

int var1;
int varold1;

int onB = 1;
int offB = 0;

int num = 0;
String lights = String(0);

HC_SR04 rangefinder = HC_SR04(trigPin, echoPin);

void setup()
{
    Particle.subscribe("Outlet1", Outlet1val);

	            
}
void loop()
{
    
       //this section of code is designed to publish the current number of lights to thinkspeak
     if(num > 3){
        num = 3;
    }
    else if(num < 0){
        num = 0;
    }
    
    String lights = String(num); 
    
  if (timeElapsed > interval) 
  {				
      Particle.publish("lights", lights, PRIVATE);
      timeElapsed = 0;             
  }
        //this part of the code compares the information directly from the Alex ultrasonic sensor
        cm = rangefinder.getDistanceCM();
        
        
        if (cm>0 && cm<50){
            var1 = 1;
            }
            else if(cm == -1){
                 var1 = var1; 
                 //this part of the code is to prevent the "error" return value of -1 from affecting the code
            }
            else{
                var1 = 2;
            }
        
        
                if(var1 != varold1){
                    
                        if(var1 == 1){
                                num = num + 2;
                                pinMode(D8, OUTPUT);
	        	                digitalWrite(D8, 0);
	                        	delay(300);
	        	                digitalWrite(D8, 1);
	        	                
	        	                String gaurd = String(onB); 
	        	                Particle.publish( "Alex", gaurd, PRIVATE);
                               delay(500);
                        }
                         
                        else if(var1 == 2){
                                num = num - 1;
                                pinMode(D7, OUTPUT);
		                        digitalWrite(D7, 0);
		                        delay(300);
		                        digitalWrite(D7, 1);
		                        
		                        String gaurd = String(offB); 
		                        Particle.publish( "Alex", gaurd, PRIVATE);
                        }
            
                }
           varold1 = var1;
             delay(1100);
}
//this last part of the code takes data from the Taylor device and changes the lights in the Taylor area
void Outlet1val(const char *event, String data){
    
    int data1val;
		data1val = String(data).toInt(); 
		
	if (data1val>0 && data1val<50){
	    
	           num = num + 1;
                pinMode(D5, OUTPUT);
		        digitalWrite(D5, 0);
		        delay(500);
	        	digitalWrite(D5, 1);

	            
    }
    
    else if(data1val == -1)
    {
      
        
    }
        else{
                num = num - 1;
                pinMode(D6, OUTPUT);
	        	digitalWrite(D6, 0);
	        	delay(500);
	        	digitalWrite(D6, 1);
        }
        
        if (data1val>=100 && data1val<200){
	    
                    num = num + 1;
	                pinMode(D3, OUTPUT);
	                digitalWrite(D3, 0);
	                delay(300);
	                digitalWrite(D3, 1);
        }
        else if(data1val == -1)
        {
      
        
        }
        else{
                
                num = num - 1;
            	pinMode(D4, OUTPUT);
	        	digitalWrite(D4, 0);
	            delay(300);
	        	digitalWrite(D4, 1);
        }
       
}


       
		
		

"Taylor" device code

C/C++
This code is setup to take data measurements, do a quick comparison and then publish the distance value.
// This #include statement was automatically added by the Particle IDE.
#include <HC_SR04.h>

int cm = 0;
int cm2 = 0;
int trigPin = D4;
int echoPin = D5;

int var1;
int varold1;
int var2;
int varold2;
int var3;
int varold3;

int on = 1;
int off = 0;

int data1val = 0;

HC_SR04 rangeFinder = HC_SR04(trigPin, echoPin);


void setup() {
    Particle.subscribe("Alex", dis2); // this is the security feature
    pinMode(D7, OUTPUT);
}

void loop() {
  //this part of the code compares values from the sensor and publishes the output if the sensor value changes range
    cm = rangeFinder.getDistanceCM();
    
    
    if (cm>0 && cm<50){
        var1 = 1;
    }
    else if(cm == -1){
        var1 = var1; 
    }
        else{
                var1 = 2;
        }
        
        
    if (cm>=100 && cm<150){
        var2 = 1;
    }
    else if(cm == -1){
        var2 = var2; 
    }
    else{
                var2 = 2;
    }
        
        
        if(var1 != varold1 || var2 != varold2){
                Particle.publish( "Outlet1", (String (cm)));
        }
        
        delay(1500);
        
       
        varold1 = var1;
        varold2 = var2;
        varold3 = var3;

}

void dis2(const char *event, String data){
    //this part of the code turns on the LED when a person is in front of the Alex device 
    int data1val;
	data1val = String(data).toInt(); 
		if(data1val == 1){
		    digitalWrite(D7, HIGH);
		}
		else
		{
		    digitalWrite(D7, LOW);
		}

}

Remote code

C/C++
This code is used to manually control which IoT outlets turn on and off
// This #include statement was automatically added by the Particle IDE.
#include <HC_SR04.h>

/* Tinker
 * This is a simple application to read and toggle pins on a Particle device.
 * For the extended version of the Tinker app supporting more pins, see
 * https://github.com/particle-iot/device-os/blob/develop/user/applications/tinker/application.cpp
 */
double cm = 0.0;

int trigPin = D10;
int echoPin = D11;

int onB = 1;
int offB = 0;
/* Function prototypes -------------------------------------------------------*/
int outlet1on(String command);
int outlet1off(String command);
int outlet2on(String command);
int outlet2off(String command);
int outlet3on(String command);
int outlet3off(String command);
int num = 0;
String lights = String(0);

 HC_SR04 rangefinder = HC_SR04(trigPin, echoPin);

/* This function is called once at start up ----------------------------------*/
void setup()
{

	Particle.function("2Sring Lights OFF", outlet1off);
	Particle.function("1Sring Lights ON", outlet1on);
	Particle.function("4Lamp OFF", outlet2off);
	Particle.function("3Lamp ON", outlet2on);
	Particle.function("6Bedlights OFF", outlet3off);
	Particle.function("5Bedlights ON", outlet3on);
  

}
/* This function loops forever --------------------------------------------*/



void loop()
{
    if(num > 3){
        num = 3;
    }
    else if(num < 0){
        num = 0;
    }
        String lights = String(num);    
        Particle.publish("lights", lights, PRIVATE);
        //delay(15001);
        
        
        cm = rangefinder.getDistanceCM();
    String dis = String(cm); 
//    Particle.publish( "Alex", dis, PRIVATE);
    delay(1500);
}



int outlet1off(String command)
{
	    num = num - 1;
	    
        pinMode(D7, OUTPUT);
		digitalWrite(D7, 0);
		delay(200);
		digitalWrite(D7, 1);
		String gaurd = String(offB); 
	      //  	                Particle.publish( "Alex", gaurd, PRIVATE);
	        	           
		return 0;
		
		  
}


int outlet1on(String command)
{
    	num = num + 1;

        pinMode(D8, OUTPUT);
		digitalWrite(D8, 0);
		delay(200);
		digitalWrite(D8, 1);
		
		
		String gaurd = String(onB); 
	     //   	                Particle.publish( "Alex", gaurd, PRIVATE);
	      
		return 1;
	

}

//outlet 2
int outlet2on(String command)
{
    	num = num + 1;

        pinMode(D5, OUTPUT);
		digitalWrite(D5, 0);
		delay(300);
		digitalWrite(D5, 1);
		
		return 2;
}
int outlet2off(String command)
{
	    num = num - 1;

        pinMode(D6, OUTPUT);
		digitalWrite(D6, 0);
		delay(300);
		digitalWrite(D6, 1);
		return 3;
}


//outlet 3
int outlet3off(String command)
{
    	num = num - 1;

        pinMode(D4, OUTPUT);
		digitalWrite(D4, 0);
		delay(200);
		digitalWrite(D4, 1);
		return 4;
}
int outlet3on(String command)
{
        num = num + 1;
	
        pinMode(D3, OUTPUT);
		digitalWrite(D3, 0);
		delay(200);
		digitalWrite(D3, 1);
		return 5;
}

Credits

Austin Alexander South

Austin Alexander South

1 project • 2 followers
Taylor Chandler

Taylor Chandler

1 project • 2 followers

Comments

Add projectSign up / Login