-
Notifications
You must be signed in to change notification settings - Fork 0
/
PIR_Sensor_File.ino
132 lines (113 loc) · 4.04 KB
/
PIR_Sensor_File.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
/*
* //////////////////////////////////////////////////
* //making sense of the Parallax PIR sensor's output
* //////////////////////////////////////////////////
*
* Switches a LED according to the state of the sensors output pin.
* Determines the beginning and end of continuous motion sequences.
*
* @author: Kristian Gohlke / krigoo (_) gmail (_) com / http://krx.at
* @date: 3. September 2006
* @author: George Artem / georgeartem (_) gmail (_) com
* @date: 5. October 2015
*
* kr1 (cleft) 2006
* released under a creative commons "Attribution-NonCommercial-ShareAlike 2.0" license
* http://creativecommons.org/licenses/by-nc-sa/2.0/de/
*
* The sensor's output pin goes to HIGH if motion is present.
* The sensor will remain on HIGH for 4 seconds while motion is present.
* Code delays just over 4 seconds 4050 milliseconds before it takes a second reading
*/
#include <ESP8266WiFi.h>
/////////////////////////////
//VARS
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;
int pirPin = 5; //the digital pin connected to the PIR sensor's output
int ledPin = 0;
String courtstatus; //status of the tennis court to be sent via wifi
char server[] ="www.previousk.com"; // enter the name of the server
#define WLAN_SSID "strychnine" // enter the Wifi SSID
#define WLAN_PASS "TokyoPopSoda" // enter the Wifi Password
WiFiClient client;
/////////////////////////////
//SETUP
void setup(){
Serial.begin(9600);
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
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);
// //connect to WiFi network
Serial.print(F("Connecting to "));
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
}
Serial.println();
Serial.println(F("WiFi connected"));
Serial.println(F("IP address: "));
Serial.println(WiFi.localIP());
}
////////////////////////////
//LOOP
void loop(){
int state;
int i = 0;
while( i < 12 ){
if(digitalRead(pirPin) == HIGH){
digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
Serial.println("---");
Serial.print("motion detected at ");
Serial.print(millis()/1000);
Serial.println(" sec");
digitalWrite(pirPin, LOW);
i++;
state ++;
delay(5000);}
if(digitalRead(pirPin) == LOW){
courtstatus = "available";
Serial.println("inside while loop");
Serial.println(courtstatus);
i++;
delay(5000);}
}
if (state > 2){ // if the sensor has been tripped more than 4 times in a minute, send data
// Only publish to the website if the courtstatus is occupied!
courtstatus = "pirtest";
Serial.print(F("\nSending sensor value: "));
Serial.print(courtstatus);
Serial.print("... ");
// reset the 'state' variable to zero
//concatenates the string to be sent in the HTTP request
String stringOne = "GET /smartcourt/api/smartcourtdata.php?courtid=";
const int courtid =2;
String stringTwo = "&key=smartcourtdata";
String stringThree = "&locationid=";
const int locationid =1;
String stringFour = "&status=";
String stringFive = " HTTP/1.1";
String stringFinal = stringOne + courtid + stringTwo + stringThree + locationid + stringFour + courtstatus + stringFive;
//send http request and test if success
if (client.connect( server , 80)){
Serial.println("Connected");
client.println(stringFinal);
client.println("Host: www.previousk.com");
client.println("Connection: close");
client.println();}
else{
Serial.println("Connection Failed");
}
} // end if courtstatus condition
}