Kevin PattersonGarrett Spencer
Published

Lazy Lamp

A lamp that you can turn on and off at your convenience. Left it on? No problem, a simple graph can show you whether or not you left it on!

BeginnerProtip763
Lazy Lamp

Things used in this project

Hardware components

Resistor 221 ohms
×1
Photo resistor
Photo resistor
×1
Solid State Relay
Solid State Relay
×1
Photon
Particle Photon
×1
AA Batteries
AA Batteries
×1

Software apps and online services

Particle IDE
ThingSpeak API
ThingSpeak API

Story

Read more

Schematics

Graph from ThingSpeak

This is the graph we got from when we ran ThingSpeak to get a graph of Analog Value vs. time when the Light bulb blinked on and off.

Circuit Diagram for Lazy Lamp

This is the circuit diagram for our project. Everything that is in the photo is what was used in order for the lightbulb to turn on and off and to get a graph from ThingSpeak.

Code

Blink an LED

C/C++
This is the code for when we are testing the two particles to see if the LED flashes.
// ------------
// Blink an LED
// ------------

/*-------------

We've heavily commented this code for you. If you're a pro, feel free to ignore it.

Comments start with two slashes or are blocked off by a slash and a star.
You can read them, but your device can't.
It's like a secret message just for you.

Every program based on Wiring (programming language used by Arduino, and Particle devices) has two essential parts:
setup - runs once at the beginning of your program
loop - runs continuously over and over

You'll see how we use these in a second. 

This program will blink an led on and off every second.
It blinks the D7 LED on your Particle device. If you have an LED wired to D0, it will blink that LED as well.

-------------*/


// First, we're going to make some variables.
// This is our "shorthand" that we'll use throughout the program:

int led1 = D0; // Instead of writing D0 over and over again, we'll write led1
// You'll need to wire an LED to this one to see it blink.

int led2 = D7; // Instead of writing D7 over and over again, we'll write led2
// This one is the little blue LED on your board. On the Photon it is next to D7, and on the Core it is next to the USB jack.

// Having declared these variables, let's move on to the setup function.
// The setup function is a standard part of any microcontroller program.
// It runs only once when the device boots up or is reset.

void setup() {

  // We are going to tell our device that D0 and D7 (which we named led1 and led2 respectively) are going to be output
  // (That means that we will be sending voltage to them, rather than monitoring voltage that comes from them)

  // It's important you do this here, inside the setup() function rather than outside it or in the loop function.

  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);

}

// Next we have the loop function, the other essential part of a microcontroller program.
// This routine gets repeated over and over, as quickly as possible and as many times as possible, after the setup function is called.
// Note: Code that blocks for too long (like more than 5 seconds), can make weird things happen (like dropping the network connection).  The built-in delay function shown below safely interleaves required background activity, so arbitrarily long delays can safely be done if you need them.

void loop() {
  // To blink the LED, first we'll turn it on...
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);

PublishSubscribeLamp

C/C++
This is the code that allows when the lamp turns on and off to send data to ThingSpeak and produce a graph.
// -----------------------------------------
// Publish and Subscribe with Photoresistors
/* -----------------------------------------

Go find a buddy who also has a Particle device.
Each of you will pick a unique event name
   (make it weird so that no one else will have it)
   (no more that 63 ASCII characters, and no spaces)

In the following code, replace "your_unique_event_name" with your chosen name.
Replace "buddy_unique_event_name" with your buddy's chosen name.

Have your buddy do the same on his or her IDE.

Then, each of you should flash the code to your device.

Breaking the beam on one device will turn on the D7 LED on the second device.

But how does this magic work? Through the miracle of publish and subscribe.

We are going to Particle.publish a public event to the cloud.
That means that everyone can see you event and anyone can subscribe to it.
You and your buddy will both publish an event, and listen for each others events.

------------------------------------------*/
// Timer end.
Timer timer3(15000, updateThingspeak);

int led = D6;
int boardLed = D0;
int photoresistor = A0;
int power = A5;

int intactValue;
int brokenValue;
int beamThreshold;
int lamp_on;

bool beamBroken = false;

const String key = "2TMNJ5OYTECLWTZO"; //thingspeak write key


// We start with the setup function.

void setup() {
  Serial.begin(115200);
  //timer.start(); //initialize timer
  
  Particle.variable("lamp_on", &lamp_on, INT);
  
    // Subscribe to the integration response event
  Particle.subscribe("hook-response/lamp_on", myHandler, MY_DEVICES);
  // This part is mostly the same:
  pinMode(led,OUTPUT); // Our LED pin is output (lighting up the LED)
  pinMode(boardLed,OUTPUT); // Our on-board LED is output as well
  pinMode(photoresistor,INPUT);  // Our photoresistor pin is input (reading the photoresistor)
  pinMode(power,OUTPUT); // The pin powering the photoresistor is output (sending out consistent power)

  // Here we are going to subscribe to your buddy's event using Particle.subscribe
  Particle.subscribe("lamp_on", myHandler);
  // Subscribe will listen for the event buddy_unique_event_name and, when it finds it, will run the function myHandler()
  // (Remember to replace buddy_unique_event_name with your buddy's actual unique event name that they have in their firmware.)
  // myHandler() is declared later in this app.
// Now for the myHandler function, which is called when the cloud tells us that our buddy's event is published.

  // Next, write the power of the photoresistor to be the maximum possible, which is 4095 in analog.
  analogWrite(power,4095);

  // Since everyone sets up their leds differently, we are also going to start by calibrating our photoresistor.
  // This one is going to require some input from the user!

  // Calibrate:
  // First, the D7 LED will go on to tell you to put your hand in front of the beam.
  digitalWrite(boardLed,HIGH);
  delay(2000);

  // Then, the D7 LED will go off and the LED will turn on.
  digitalWrite(boardLed,LOW);
  digitalWrite(led,HIGH);
  delay(500);

  // Now we'll take some readings...
  int off_1 = analogRead(photoresistor); // read photoresistor
  delay(200); // wait 200 milliseconds
  int off_2 = analogRead(photoresistor); // read photoresistor
  delay(1000); // wait 1 second

  // Now flash to let us know that you've taken the readings...
  digitalWrite(boardLed,HIGH);
  delay(100);
  digitalWrite(boardLed,LOW);
  delay(100);
  digitalWrite(boardLed,HIGH);
  delay(100);
  digitalWrite(boardLed,LOW);
  delay(100);

  // Now the D7 LED will go on to tell you to remove your hand...
  digitalWrite(boardLed,HIGH);
  delay(2000);

  // The D7 LED will turn off...
  digitalWrite(boardLed,LOW);

  // ...And we will take two more readings.
  int on_1 = analogRead(photoresistor); // read photoresistor
  delay(200); // wait 200 milliseconds
  int on_2 = analogRead(photoresistor); // read photoresistor
  delay(300); // wait 300 milliseconds

  // Now flash the D7 LED on and off three times to let us know that we're ready to go!
  digitalWrite(boardLed,HIGH);
  delay(100);
  digitalWrite(boardLed,LOW);
  delay(100);
  digitalWrite(boardLed,HIGH);
  delay(100);
  digitalWrite(boardLed,LOW);
  delay(100);
  digitalWrite(boardLed,HIGH);
  delay(100);
  digitalWrite(boardLed,LOW);

  intactValue = (on_1+on_2)/2;
  brokenValue = (off_1+off_2)/2;
  beamThreshold = (intactValue+brokenValue)/2;

}


void loop() {
//The piece of code below is what is being read off of the photoresistor in the circuit.
    int analog_value = analogRead(A0);
    
  // This loop sends a publish when the beam is broken.
  if (analogRead(photoresistor)>beamThreshold) {
    if (beamBroken==true) {
        Particle.publish("lamp_on","the lamp is off");
        // publish this public event
        // rename your_unique_event_name with your actual unique event name. No spaces, 63 ASCII characters.
        // give your event name to your buddy and have them put it in their app.

        // Set the flag to reflect the current status of the beam.
        beamBroken=false;
    }
  }

  else {
      if (beamBroken==false) {
        // Same deal as before...
        Particle.publish("lamp_on","the lamp is on");
        delay(10000);
        beamBroken=true;
      }
  }
  stuff();
}


// Now for the myHandler function, which is called when the cloud tells us that our buddy's event is published.
void myHandler(const char *event, const char *data)
{
  /* Particle.subscribe handlers are void functions, which means they don't return anything.
  They take two variables-- the name of your event, and any data that goes along with your event.
  In this case, the event will be "buddy_unique_event_name" and the data will be "intact" or "broken"

  Since the input here is a char, we can't do
     data=="intact"
    or
     data=="broken"

  chars just don't play that way. Instead we're going to strcmp(), which compares two chars.
  If they are the same, strcmp will return 0.
  */

  if (strcmp(data,"the lamp is off")==0) {
    // if your buddy's beam is intact, then turn your board LED off
    digitalWrite(boardLed,LOW);
    lamp_on = 1;
  }
  else if (strcmp(data,"the lamp is on")==0) {
    // if your buddy's beam is broken, turn your board LED on
    digitalWrite(boardLed,HIGH);
    lamp_on = 0;
  }
  else {
    // if the data is something else, don't do anything.
    // Really the data shouldn't be anything but those two listed above.
  }
}
      

void stuff() {
  // Get some data
  String data = String(10);
  // Trigger the integration
  Particle.publish("lamp_on", data, PRIVATE);
  // Wait 60 seconds
  delay(10000);
}
          


void myHandlera(const char *event, const char *data) {
  // Handle the integration response
}
   
   
   bool updateThingspeak() 
{
    //delay (2000);
    int count;
    count++;
    Serial.print("Updating Thingspeak:");
    Serial.print(count);
    
    int rssival = WiFi.RSSI();
    //sprintf(publishString,"%d",rssival);
    //bool success = Particle.publish("RSSI",publishString);
    
    //sprintf(publishString, "%1.4f", checkbattery());

      bool success = Particle.publish("thingSpeakWrite_All", +
     "{ \"1\": \"" + String(lamp_on) + "\"," +
     
       "\"k\": \"" + key + "\" }", 60, PRIVATE);
    return success; //if sent, then turn of the send flag, otherwise let it try again.
    
}      

//2TMNJ5OYTECLWTZO

Credits

Kevin Patterson

Kevin Patterson

1 project • 0 followers
Garrett Spencer

Garrett Spencer

1 project • 0 followers

Comments