Maxwell KetchamKeith Kasler
Published

LightAlert

As the great Terry Crews once said "that's 49 cents of milk spilt over the table! Somebody's gonna drink that!" But instead... electricity.

IntermediateFull instructions provided4 hours426
LightAlert

Things used in this project

Hardware components

Photon
Particle Photon
×2
Relay (generic)
We used a SPDT Relay (Jzc-11F), but any generic relay should do.
×1
Resistor 221 ohm
Resistor 221 ohm
×2
Photo resistor
Photo resistor
×1
Jumper wires (generic)
Jumper wires (generic)
Three of these wires will be connected straight into your house's power circuit, so you may want to make them thicker to handle the higher voltage.
×5
Power Bank
×1

Software apps and online services

IFTTT Particle Service
Used for triggers and notifications.
Fritzing
Used to build the circuit schematics.
ThingSpeak API
ThingSpeak API
Used to record real-time data and plot graphs.
Particle - IDE
Used to generate code for photon devices.
Github IoT Debugger
Used as a WebHook to interconnect photon devices with ThingSpeak.

Story

Read more

Schematics

Particle Photon #1

Breadboard view
Megr 3171 particle photon 1 y9mt2fll0m

Particle Photon #2

Breadboard view.
Note: The relay is rated at a 5V minimum to activate the switch from NC to NO, though we used port D7 that is rated at 3.3V. This still worked for us, but if you are having issues getting the relay to switch try running an external battery pack across the relay coil.
Megr 3171 particle photon 2 b2bose8tkq

Photon #1 Schematic

Schematic of setup
Megr 3171 photon 1 schematic ryxrbshbgv

Photon #2 Schematic

Schematic of setup
Megr 3171 photon 2 schematic xluhhvbdqy

Light Check IFTTT

This runs the code to see if the lights are on. If the lights are on, the loop is allowed to run and send events that the lights are on to activate the relay.
Megr 3171 ifttt light check via text c75vjxuwme

Lights are off IFTTT

This sends a notification to your phone letting you know that the lights have been turned off or if they were already off.
Megr 3171 ifttt lights are off fg5huwuhbc

Code

Particle Photon #1

C/C++
int photoresistor = A0;
int power = A5;
int analogvalue;
int lowvalue;
int commandCheck=0;
#define publish_delay 8000
unsigned int lastPublish = 0;

void setup() {
pinMode(photoresistor,INPUT);
pinMode(power,OUTPUT);
Particle.function("checklights",check);
digitalWrite(power,HIGH);
Particle.variable("analogvalue", &analogvalue, INT);
lowvalue = 6; //This value was obtained from trial and error. It creates a buffer beween the values of when the lights are off and when the lights are on. Can be changed depending on the envirnoment that the sensor is place in.
analogvalue = analogRead(photoresistor);
}
void loop() {
    analogvalue = analogRead(photoresistor);
    unsigned long now = millis();
    if ((now - lastPublish) < publish_delay) {
        return;
    }

    int value = analogRead(A0);
    Particle.publish("thingSpeakWrite_A0", "{ \"1\": \"" + String(value) + "\", \"k\": \"R039SVKNF07FZEBQ\" }", 60, PRIVATE);
    lastPublish = now;
    while (commandCheck == 1)
    {
        if (analogvalue >= lowvalue){ //If the lights are on, event is published
        Particle.publish("maxkeithlighton","on");
        delay(1000);
    }
        else if (analogvalue < lowvalue){ //If the lights are off, it's published
        Particle.publish("maxkeithlightoff","off");
        delay(1000);
    }
    }
}
int check(String command){ //This allows us to command the relay on/off via IFTTT
    if (command=="on?") {
        commandCheck = 1;
        return 1;
    }
    else if (command=="off"){
        commandCheck =0;
        Particle.publish("maxkeithlightoff","off");
        return 0;
    }
    else {
        return -1;
    }
}

Particle Photon #2

C/C++
#define publish_delay 16000
unsigned int lastPublish = 0;
void setup()
{
  Particle.subscribe("maxkeithlighton", lights);
  Particle.subscribe("maxkeithlightoff", lights);
  pinMode(D7, OUTPUT);
  
}

void loop()
{
unsigned long now = millis();
    if ((now - lastPublish) < publish_delay) {
        return;
}
    int value = digitalRead(D7);
    Particle.publish("thingSpeakWrite_D7", "{ \"1\": \"" + String(value) + "\", \"k\": \"T8YQ6CN74QADE915\" }", 60, PRIVATE);
    lastPublish = now;
}

void lights(const char *event, const char *data)
{
    if (strcmp(data,"on")==0) 
  {
       digitalWrite(D7,HIGH);
  }
    else if (strcmp(data,"off")==0) 
  {
       digitalWrite(D7,LOW);
}
}

Credits

Maxwell Ketcham

Maxwell Ketcham

1 project • 1 follower
Contact
Keith Kasler

Keith Kasler

1 project • 0 followers
Contact
Thanks to John McAlpine.

Comments