-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHonking Control (1).ino
289 lines (254 loc) · 7.62 KB
/
Honking Control (1).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
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
285
286
287
288
289
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 0 // GPIO0
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <EEPROM.h>
int sensorPin = D0; // select the input pin for the potentiometer
int sensorValue = 0; // variable to store the value coming from the sensor
int Buzzer = 14;
String IFTTT_KEY = "mJXR6NpdwgCGNDss5W46MxzxkhwYhzddiJdTeF9gfX-"; //replace this with your IFTTT applet key
int a=0;
const char* eventName = "CarWarning";
//const char* IFTTT_URL = "maker.ifttt.com";
const char* host = "maker.ifttt.com";
const char* ssid = "DT_LAB"; // WiFi User Name
const char* password = "fthu@050318"; //WiFi Passwords
Adafruit_SSD1306 OLED(OLED_RESET);
const int buttonPin = D3;
int i=0,m=0;
int buttonState;
int lastButtonState = HIGH;
unsigned long lastDebounceTime = 0,j=0,h=0,Currenttime,fun=0;
unsigned long debounceDelay = 50;
int firsttime = 1;
unsigned long startTime;
unsigned long pressTime;
int count = 0;
float Time;
int x = 0, y = 0, z = 0;
const char* door_state = "closed";
void setup() {
pinMode(Buzzer,OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
sensorValue = !(digitalRead(sensorPin)); //PUC
//Serial.println(sensorValue);
if (sensorValue ==HIGH && m==0){
OLED.begin();
OLED.clearDisplay();
OLED.setTextSize(1);
OLED.setTextColor(WHITE);
OLED.setCursor(0, 0);
OLED.println("Noise check");
OLED.display();
if (fun==fun){
Currenttime=millis();
fun=(!fun);
m=1;
}
}
if(sensorValue ==LOW){
m=0;
}
if ((millis()- Currenttime) >= 9000){
OLED.begin();
OLED.clearDisplay();
OLED.setTextSize(1);
OLED.setTextColor(WHITE);
OLED.setCursor(0, 0);
OLED.println("");
OLED.display();
fun=fun;
}
if(i==1 && (millis()-j) >5000){
OLED.begin();
OLED.clearDisplay();
OLED.setTextSize(1);
OLED.setTextColor(WHITE);
OLED.setCursor(0, 0);
OLED.println("");
OLED.display();
i=0;
}
int reading = digitalRead(buttonPin);
if (reading != lastButtonState)
{
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) {
digitalWrite(Buzzer,HIGH);// add
a += 1;
}
}
}
lastButtonState = reading;
if (digitalRead(buttonPin) == LOW) {
if (firsttime == 1) {
startTime = millis();
firsttime = 0;
z += 1;
}
pressTime = millis() - startTime;
if (pressTime >= 1) {
Time = pressTime / 1000.0;
Serial.println(Time);
}
}
else if (firsttime == 0) {
if (Time >= 2.0)
{
count += 1;
if (count > 5) {
Serial.println("Warning !!!!! Don't press Horn beyond 2 secs");
OLED.begin();
OLED.clearDisplay();
OLED.setTextSize(1);
OLED.setTextColor(WHITE);
OLED.setCursor(0, 0);
OLED.println("Decrease Honk Time");
OLED.display();
delay(500);
j=millis();
i=1;
x += 1;
count = 0;
}
}
if (x > 2) {
Serial.println("URL");
Serial.print("connecting to ");
Serial.println(host);
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
String url = "/trigger/CarWarning/with/key/";
url += IFTTT_KEY;
Serial.print("Requesting URL: ");
Serial.println(url);
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Content-Length: 13\r\n\r\n" +
"value1=" + door_state + "\r\n");
fine();
x = 0;
}
Serial.print("\t");
Serial.print("\t");
Serial.print(count);
Serial.print("\t");
Serial.print("\t");
Serial.println(a);
if (a > 10) {
Serial.println("Warning !!!!! You have excedded your honking limit");
delay(200);
OLED.begin();
OLED.clearDisplay();
OLED.setTextSize(1);
OLED.setTextColor(WHITE);
OLED.setCursor(0,5);
OLED.println("Decrease Honking");
delay(500);
OLED.display();
j=millis();
i=1;
y += 1;
a = 0;
}
if (y > 2) {
Serial.println("URL");
Serial.println("URL");
Serial.print("connecting to ");
Serial.println(host);
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
String url = "/trigger/CarWarning/with/key/";
url += IFTTT_KEY;
Serial.print("Requesting URL: ");
Serial.println(url);
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Content-Length: 13\r\n\r\n" +
"value1=" + door_state + "\r\n");
//wifi();
fine();
y = 0;
}
firsttime = 1;
}
}
void fine(){
OLED.begin();
OLED.clearDisplay();
OLED.setTextSize(1);
OLED.setTextColor(WHITE);
OLED.setCursor(0,5);
OLED.println("You are fined");
OLED.display();
delay(500);
j=millis();
i=1;
}
/*
void wifi(){
// Define the WiFi Client
WiFiClient client;
// Set the http Port
const int httpPort = 80;
// Make sure we can connect
if (!client.connect(IFTTT_URL, httpPort))
{
return;
}
// We now create a URI for the request
// String url = "/trigger/" + String(eventName) + "-"+String(eventCount)+ "/with/key/" + String(IFTTT_KEY);
String url = "/trigger/" + String(eventName) + "/with/key/" + String(IFTTT_KEY);
// Set some values for the JSON data depending on which event has been triggered
IPAddress ip = WiFi.localIP();
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
//String value_1 = "MH 37T 5770";
//String value_2 = String(h);
//String value_3 = "";
// Build JSON data string
String data = "";
//data = data + "\n" + "{\"value1\":\"" + value_1 + "\"}"; //,"value2\":\""+ value_2 +"\"}";
// Post the button press to IFTTT
if (client.connect(IFTTT_URL, httpPort))
{
String url = "/trigger/CarWarning/with/key/";
url += IFTTT_KEY;
Serial.print("Requesting URL: ");
Serial.println(url);
client.print(String("POST ") + url + " HTTP/1.1\r\n" +
"Host: " + IFTTT_URL + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Content-Length: 13\r\n\r\n" +
"value1=" + door_state + "\r\n");
}
}*/