Phillip BarkerAlexander Derby
Created November 4, 2016 © GPL3+

Photovoltaic Switch

Using a photo resistor to detect the change in light to change the output voltage on a certain pin to act as a switch when it gets dark.

EasyWork in progress10 hours96
Photovoltaic Switch

Things used in this project

Hardware components

Photon
Particle Photon
×1
Photo resistor
Photo resistor
×1
Resistor 22.1k ohm
Resistor 22.1k ohm
×1
Breadboard (generic)
Breadboard (generic)
×1
Resistor 47.5k ohm
Resistor 47.5k ohm
×1
Relay (generic)
×1
Resistor 1k ohm
Resistor 1k ohm
×2

Software apps and online services

ThingSpeak API
ThingSpeak API
This app is used to gather data to determine which Bit Voltage number should be used to turn the fan on or off.
Mobicle
ControlEverything.com Mobicle
This app is used to create buttons to turn our fan on, off, or set it to a auto mode.

Story

Read more

Schematics

Light Detecting Circuit

This circuit detects the amount of light in the area of the photon.

Trigger Circuit

This circuit acts like a trigger and turns the fan on or off.

Code

Trigger Code

C/C++
This code receives a command from the first photon and turns the fan on or off.
//naming the pins to be used
int Sourcepin = A4; 

void setup() {
    //declaring the action of used pins
    pinMode(Sourcepin, OUTPUT);
    }
void loop(){
    //subscribed to the loop of the condition of the switch 
    Particle.subscribe("wakawaka-switch",Power,"21003a000147353138383138");
    delay(1000);
}
  
void Power(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 "off" or "on"

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

  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,"on")==0) {
    // if your buddy's beam is intact, then turn your board LED off
    digitalWrite(Sourcepin,LOW);
  }
  else if (strcmp(data,"off")==0) {
    // if your buddy's beam is broken, turn your board LED on
    digitalWrite(Sourcepin,HIGH);
  }
  else {
    // if the data is something else, don't do anything.
    // Really the data shouldn't be anything but those two listed above.
  }
}

Analog Value Code

C/C++
This code detects the amount of voltage at the switch pins and then sends a command.
//needed three pins for three settings, off on auto, because IFTTT has no and statment option.
   //shorted to offread then resisted to gnd so that offread will read 0 or 4096
int onpin = A1;    //shorted to onread then resisted to gnd so that onread will read 0 or 4096
int autopin = A2;  //resited with 68Kohms to auto read then a photo resistor from auto read to gnd 

int onread = A6;
int autoread= A7;

int auto_value; //will be the anologe values at each of the read pins
int on_value;

String data;  //string global variable for publishing
const String key = "93ONX3LJ35N2BPCM"; //write key

// Next we go into the setup function.

void setup() {

    //read pins are the input of there output counterpart
    pinMode(onpin,OUTPUT);  
    pinMode(autopin,OUTPUT);
    
    
    pinMode(onread,INPUT);
    pinMode(autoread,INPUT);
  
    
    

    // declaring the analoge values as variables
    Particle.variable("auto_value", &auto_value, INT);
    Particle.variable("on_value", &on_value, INT);
    
 
    //naming the later function toggle
    
    Particle.function("toggle",Toggle);
 // Subscribe to the integration response event
  Particle.subscribe("hook-response/Photoresisance", myHandler, MY_DEVICES);
}

void myHandler(const char *event, const char *data) {
  // Handle the integration response
}
          


  

// Next is the loop function...

void loop() {


    // display the analoge voltage values 
    auto_value = analogRead(autoread);
    
    on_value = analogRead(onread);
    
    
    
    if (on_value>3000) {
        Particle.publish("wakawaka-switch","on");
    }
    else if (auto_value>2300) {
        Particle.publish("wakawaka-switch","on");
    }
    else if (auto_value<2000||on_value<1000){
    Particle.publish("wakawaka-switch","off");
    }
    else{
    }
     // Get some data
  //data = String(auto_value);
  // Trigger the integration
  //Particle.publish("Photoresisance",data, PRIVATE);
  // Wait 60 seconds
  updateThingspeak(); 
  delay(10000);

}

          
// Finally, we will write out our ledToggle function, which is referenced by the Particle.function() called "led"

int Toggle(String command) {
    if (command=="off") {
        digitalWrite(autopin,LOW);
        digitalWrite(onpin,LOW);
    return 1;
    }
    else if (command=="on") {
        digitalWrite(autopin,LOW);
        digitalWrite(onpin,HIGH);
    return 0;
    }
    else if (command=="auto"){
        digitalWrite(autopin,HIGH);
        digitalWrite(onpin,LOW);
    return -1;
    }
    else {
        return 2;
    }

}   
bool updateThingspeak() 
{
    //delay (2000);
    //static int count = 0;
    //Serial.println(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(auto_value) + "\"," +
       "\"2\": \"" + String(auto_value) + "\"," +
       "\"3\": \"" + String(auto_value) + "\"," +
       "\"4\": \"" + String(auto_value) + "\"," +
       "\"5\": \"" + String(auto_value) + "\"," +
       "\"6\": \"" + String(auto_value) + "\"," +
       "\"7\": \"" + String(auto_value) + "\"," +
       "\"8\": \"" + String(auto_value) + "\"," +
       "\"k\": \"" + key + "\" }", 60, PRIVATE);
    return success; //if sent, then turn of the send flag, otherwise let it try again.
    
}





      

Credits

Phillip Barker

Phillip Barker

1 project • 0 followers
Alexander Derby

Alexander Derby

1 project • 0 followers

Comments