int led = D0; // This is where your LED is plugged in. The other side goes to a resistor connected to GND.
int boardLed = D7;
int photoresistor = A0; // This is where your photoresistor is plugged in. The other side goes to the "power" pin (below).
int power = A5; // This is the other end of your photoresistor. The other side is plugged into the "photoresistor" pin (above).
// The reason we have plugged one side into an analog pin instead of to "power" is because we want a very steady voltage to be sent to the photoresistor.
// That way, when we read the value from the other side of the photoresistor, we can accurately calculate a voltage drop.
int analogvalue; // Here we are declaring the integer variable analogvalue, which we will use later to store the value of the photoresistor.
int present;
int critical_value = 30;
int dooropencutoff = 90;
int door;
bool dooropen = false;
bool whiskeypresent = true;
String data; //string global variable for publishing
// Next we go into the setup function.
void setup() {
// First, declare all of our pins. This lets our device know which ones will be used for outputting voltage, and which ones will read incoming voltage.
pinMode(led,OUTPUT); // Our LED pin is output (lighting up the LED)
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)
pinMode(boardLed,OUTPUT);
// Next, write the power of the photoresistor to be the maximum possible, so that we can use this for power.
digitalWrite(power,HIGH);
// We are going to declare a Paritcle.variable() here so that we can access the value of the photoresistor from the cloud.
Particle.variable("analogvalue", &analogvalue, INT);
// This is saying that when we ask the cloud for "analogvalue", this will reference the variable analogvalue in this app, which is an integer variable.
Particle.variable("door", &door, INT);
// We are also going to declare a Particle.function so that we can turn the LED on and off from the cloud.
Particle.subscribe("doorStatus",myHandler);
}
void myHandler(const char event,const char *data)
{ if (dooropen==true) {
if (strcmp(data,"Closed")==0) {
// if your buddy's beam is intact, then turn your board LED off
door=0;
dooropen=false;
delay(12000);
digitalWrite(boardLed,HIGH);
delay(1000);
digitalWrite(led,HIGH);
delay(3000);
analogvalue = analogRead(photoresistor);
if (whiskeypresent==true){
if (analogvalue<critical_value) {
Particle.publish("Whiskey Out");
whiskeypresent=false;
present=0;
updateThingspeak();
}}
else{
if (analogvalue>dooropencutoff){
}
else if(analogvalue>critical_value) {Particle.publish("whiskey");
whiskeypresent=true;
present=1;
updateThingspeak();
}
else{}
}
digitalWrite(boardLed,LOW);
digitalWrite(led,LOW);
}
else {
Particle.publish("Door","Open");
// if the data is something else, don't do anything.
// Really the data shouldn't be anything but those two listed above.
}}
else {
if (strcmp(data,"Open")==0){
door=1;
dooropen=true;
}
else{
}
}
}
bool updateThingspeak()
{
Particle.subscribe("hook-response/ThingSpeak", myHandler, MY_DEVICES);
bool success = Particle.publish("thingSpeakWrite_A0", "{ \"1\": \"" + String(abnalogvalue) + "\", \"k\": \"XXXXXXXXXXXXXXXX\" }", 60, PRIVATE);
lastPublish = now;
}
return success; //if sent, then turn of the send flag, otherwise let it try again.
}
Comments