-
Notifications
You must be signed in to change notification settings - Fork 0
/
final-project2.ino
165 lines (137 loc) · 4.21 KB
/
final-project2.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// This #include statement was automatically added by the Particle IDE.
#include "RCSwitch/RCSwitch.h"
//-----------These values are used for Lights--------------------------------
int Light = A0;
int analoglightvalue=0;
int lightThreshold=1000;
//-----------These values are used for Lights--------------------------------
//-------------------These values are used for temperature-------------------
int temp =A1;
int analogtempvalue=0;
int tempthreshold=1000;
//-------------------These values are used for temperature-------------------
//-------------------These values are used for Motion-------------------
int motion =D0;
int analogmotionvalue=0;
int motionthreshold=1; //in Minute
int motiontimer=0;
int lastmotion=0;
//-------------------These values are used for Motion------------------
//-------------------Function used to switch on and off things around house-------------
int Switches(String command);
int switchpin=D3;
RCSwitch switchObj = RCSwitch();
//-------------------Function used to switch on and off things around house-------------
int wait=500; // This is the wait time until particle checks the system again and before sending the email
void setup()
{
pinMode(Light,INPUT);
pinMode(temp,INPUT);
pinMode(motion,INPUT);
pinMode(switchpin, OUTPUT);
switchObj.enableTransmit(switchpin);
switchObj.setPulseLength(180);
//Expose the Switches function to IFTTT
Particle.function("Switch", Switches);
}
void loop() {
//-------------------Light Code Starts here--------------------------------------
analoglightvalue = analogRead(Light);
Particle.variable("LightVal", &analoglightvalue, INT);
if (analoglightvalue > lightThreshold)
{
Particle.publish( "Light","DO");
}
//-------------------Light Code Ends here--------------------------------------
//-------------------Temperature Code Starts here--------------------------------------
analogtempvalue = analogRead(temp);
analogtempvalue = ((((analogtempvalue * 3.2)/4095) - 0.5) * 100)*(9.0/5.0) + 32.0;
Particle.variable("TempVal", &analogtempvalue, INT);
if(analogtempvalue >tempthreshold)
{
Particle.publish( "Temperature","DO");
}
//-------------------Temperature Code Ends here--------------------------------------
//--------------------Motion Code Starts Here----------------------------------------
analogmotionvalue=digitalRead(motion);
Particle.variable("MotionVal", &analogmotionvalue, INT);
if (analogmotionvalue==1)
{
if (lastmotion==1 && motiontimer!=0){ motiontimer=2*60*motionthreshold;}
if (motiontimer==0)
{
Particle.publish( "Motion","1");
motiontimer=2*60*motionthreshold;
}
lastmotion=1;
}
else if (lastmotion!=analogmotionvalue)
{
if (motiontimer==0)
{
Particle.publish( "Motion","0");
lastmotion=0;
}
}
motiontimer--;
if(motiontimer<0){motiontimer=0;}
Particle.publish( "count",String(motiontimer)); //debug code
//--------------------Motion Code Ends Here----------------------------------------
delay(wait);
}
// this function automagically shows up in IFTTT
int Switches( String command)
{
// look for the matching argument "coffee" <-- max of 64 characters long
if(command == "11")
{
switchObj.send(4265267, 24);
return 1;
}
else if (command == "10")
{
switchObj.send(4265276, 24);
return 1;
}
else if (command == "21")
{
switchObj.send(4265411, 24);
return 1;
}
else if (command == "20")
{
switchObj.send(4265420, 24);
return 1;
}
else if (command == "31")
{
switchObj.send(4265731, 24);
return 1;
}
else if (command == "30")
{
switchObj.send(4265740, 24);
return 1;
}
else if (command == "41")
{
switchObj.send(4267267, 24);
return 1;
}
else if (command == "40")
{
switchObj.send(4267276, 24);
return 1;
}
else if (command == "51")
{
switchObj.send(4273411, 24);
return 1;
}
else if (command == "50")
{
switchObj.send(4273420, 24);
return 1;
}
else return -1;
}