//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.
}
Comments