-
Notifications
You must be signed in to change notification settings - Fork 14
/
sample_arduino_code.txt
288 lines (211 loc) · 7.8 KB
/
sample_arduino_code.txt
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 101 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
String readString;
#include <OneWire.h>
#include <DallasTemperature.h>
/*-----( Declare Constants )-----*/
#define ONE_WIRE_BUS 2 /*-(Connect to Pin 2 )-*/
/*-----( Declare objects )-----*/
/* Set up a oneWire instance to communicate with any OneWire device*/
OneWire ourWire(ONE_WIRE_BUS);
/* Tell Dallas Temperature Library to use oneWire Library */
DallasTemperature sensors(&ourWire);
/*-----( Declare Variables )-----*/
//////////////////////
int photocellPin = 1; // the cell and 10K pulldown are connected to a1
int photocellReading; // the analog reading from the analog resistor divider
boolean pirReading = false;
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int pirPin = 6; //the digital pin connected to the PIR sensor's output
void setup(){
pinMode(5, OUTPUT); //pin selected to control
//start Ethernet
Ethernet.begin(mac, ip, gateway, gateway, subnet);
server.begin();
//enable serial data print
Serial.begin(9600);
sensors.begin();
pinMode(pirPin, INPUT); ///PIR
digitalWrite(pirPin, LOW);
//give the sensor some time to calibrate
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);
}
void loop(){
readPIR();
readPhotocell();
float temperatureIndoor;
float temperatureOutdoor;
//boolean pirReading;
delay(100);
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}
//if HTTP request has ended
if (c == '\n') {
///////////////
/* Serial.println(readString); //print to serial monitor for debuging
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<TITLE>Arduino GET test page</TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<H1>Simple Arduino button</H1>");
client.println("<a href=\"/?on\">ON</a>");
client.println("<a href=\"/?off\">OFF</a>");
client.println("<a href=\"/?temp\">temp</a>");
client.println("</BODY>");
client.println("</HTML>");
delay(1);
*/
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: application/json;charset=utf-8");
client.println("Server: Arduino");
client.println("Connnection: close");
client.println();
sensors.requestTemperatures(); // Send the command to get temperatures
temperatureIndoor = (sensors.getTempCByIndex(0));
//temperatureOutdoor = (sensors.getTempFByIndex(0));
client.print("{");
client.print("\"temperature\":");
client.print(temperatureIndoor);
client.print(",");
//client.print("\"humidity\":");
//client.print(temperatureOutdoor);
//client.print("}");
//client.println();
//client.print(",");
//client.print("{");
client.print("\"lightval\":");
client.print(photocellReading);
client.print(",");
client.print("\"pirval\":");
client.print(pirReading);
//client.print("True");
client.print("}");
client.println();
//stopping client
client.stop();
///////////////////// control arduino pin
if(readString.indexOf("?on") >0)//checks for on
{
Serial.println(readString.indexOf("on"));
digitalWrite(5, HIGH); // set pin 5 high
Serial.println("Led On");
}
if(readString.indexOf("?off") >0)//checks for off
{
Serial.println(readString.indexOf("off"));
digitalWrite(5, LOW); // set pin 5 low
Serial.println("Led Off");
}
if(readString.indexOf("?temp") >0)//checks for off
{
Serial.println(readString.indexOf("temp"));
temperatureIndoor = (sensors.getTempCByIndex(0));
//digitalWrite(5, LOW); // set pin 5 low
Serial.println("temp sent");
}
if(readString.indexOf("?light") >0)//checks for off
{
Serial.println(readString.indexOf("light"));
//digitalWrite(5, LOW); // set pin 5 low
Serial.println("light sent");
}
//clearing string for next read
readString="";
//sensors.requestTemperatures(); // Send the command to get temperatures
//Serial.print("Device 1 (index 0) = ");
//Serial.print(sensors.getTempFByIndex(0));
//Serial.println(" Degrees F");
} ///c=n
} //client available
} //client connected
} //if client
} //loop
void readPhotocell(){
photocellReading = analogRead(photocellPin);
Serial.print("Analog reading = ");
Serial.print(photocellReading); // the raw analog reading
// We'll have a few threshholds, qualitatively determined
if (photocellReading < 10) {
Serial.println(" - Dark");
} else if (photocellReading < 400) {
Serial.println(" - Dim");
} else if (photocellReading < 600) {
Serial.println(" - Light");
} else if (photocellReading < 800) {
Serial.println(" - Bright");
} else {
Serial.println(" - Very bright");
}
delay(100);
}
void readPIR(){
Serial.println(pirReading); // the raw analog reading
if (digitalRead(pirPin) == HIGH) {
//digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
if (lockLow) {
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis() / 1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
pirReading = true;
//alarmTriggered();
}
if (digitalRead(pirPin) == LOW) {
//digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state
pirReading = false;
if (takeLowTime) {
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause,
//we assume that no more motion is going to happen
if (!lockLow && millis() - lowIn > pause) {
//makes sure this block of code is only executed again after
//a new motion sequence has been detected
lockLow = true;
Serial.print("motion ended at "); //output
Serial.print((millis() - pause) / 1000);
Serial.println(" sec");
delay(50);
}
}
}