· 5 years ago · Jul 24, 2020, 10:58 AM
1//TWI INTERFACE
2#include <Wire.h>
3#include <LiquidCrystal_I2C.h>
4LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4);
5//GLOBAL VARIABLE FOR ULTRASONIC RANGER
6const int Trig = 6; // Trig connected to pin 6
7const int Echo = 7; // Echo connected to pin 7
8long duration, cm;
9//RFID_SERIAL
10#include <SPI.h>
11#include <MFRC522.h>
12#define SS_PIN 10
13#define RST_PIN 9
14MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
15#include <deprecated.h>
16#include <require_cpp11.h>
17
18//SERVO
19#include <Servo.h>
20Servo myservo;
21int tries;
22bool scanned = false;
23bool loopdone;
24const int buzzer = 8;
25//THINGSPEAK
26#include "ThingSpeak.h"
27#include "WiFiEsp.h"
28#define SECRET_SSID "Linksys13858" // replace MySSID with your WiFi network name
29#define SECRET_PASS "ff8esktm3e" // replace MyPassword with your WiFi password
30
31#define SECRET_CH_ID 1098186 //Test channel for counting
32#define SECRET_WRITE_APIKEY "259RB2VBJ0TJXVPD" // replace XYZ with your channel write API Key
33
34char ssid[] = SECRET_SSID; // your network SSID (name)
35char pass[] = SECRET_PASS; // your network password
36int keyIndex = 0; // your network key Index number (needed only for WEP)
37WiFiEspClient client;
38
39// Emulate Serial1 on pins 6/7 if not present
40#ifndef HAVE_HWSERIAL1
41#include "SoftwareSerial.h"
42SoftwareSerial Serial1(2, 3); // RX, TX
43#define ESP_BAUDRATE 9600
44#else
45#define ESP_BAUDRATE 115200
46#endif
47
48unsigned long myChannelNumber = SECRET_CH_ID;
49const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
50
51int number;
52void setup()
53{
54 // initialize serial for ESP module
55 setEspBaudRate(ESP_BAUDRATE);
56
57 while (!Serial) {
58 ; // wait for serial port to connect. Needed for Leonardo native USB port only
59 }
60
61 Serial.print("Searching for ESP8266...");
62 // initialize ESP module
63 WiFi.init(&Serial1);
64
65 // check for the presence of the shield
66 if (WiFi.status() == WL_NO_SHIELD) {
67 Serial.println("WiFi shield not present");
68 // don't continue
69 while (true);
70 }
71 Serial.println("found it!");
72
73 ThingSpeak.begin(client); // Initialize ThingSpeak
74 lcd.init();
75 lcd.backlight();
76 Serial.begin(9600);
77 myservo.attach(9); // assume control signal connected to pin 9
78 myservo.write(0);
79 pinMode(Trig, OUTPUT); // UNO's output, ranger's input
80 pinMode(Echo, INPUT); // UNO's input, ranger's output
81 pinMode(buzzer, OUTPUT); // buzzer lmao
82 SPI.begin(); // Init SPI bus
83 mfrc522.PCD_Init(); // Init MFRC522 card
84
85}
86
87void loop()
88{
89 if(WiFi.status() != WL_CONNECTED){
90 Serial.print("Attempting to connect to SSID: ");
91 Serial.println(SECRET_SSID);
92 while(WiFi.status() != WL_CONNECTED){
93 WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
94 Serial.print(".");
95 delay(5000);
96 }
97 Serial.println("\nConnected.");
98 }
99 loopdone = false;
100 while (loopdone == false){
101 bool scanned = false;
102 tries = 0;
103 lcd.clear();
104 ultrasonicranger();
105 if (cm < 25)
106 {
107 TWI_LCD();
108
109 while (scanned == false){
110 RFID_SERIAL();
111
112 }
113 }
114 }
115 return;
116}
117
118void screenmsg(String message){
119 lcd.clear();
120 lcd.print(message);
121}
122
123void ultrasonicranger()
124{
125
126 digitalWrite(Trig, LOW);
127 delayMicroseconds(2);
128 digitalWrite(Trig, HIGH);
129 delayMicroseconds(5);
130 digitalWrite(Trig, LOW);
131
132 // 2. measure the duration of the HIGH pulse in Echo
133 // & every 58 us is an obstacle distance of 1 cm
134 duration = pulseIn(Echo, HIGH);
135 cm = duration / 58;
136
137 // 3. display the obstacle distance in serial monitor
138 delay(500);
139 Serial.println (cm);
140 return;
141}
142void TWI_LCD()
143{
144 lcd.setCursor(0, 0); // Set the cursor on the first column and first row.
145 lcd.print("Please tap your"); // Print the string "Hello World!"
146 lcd.setCursor(2, 1);
147 lcd.print(" card");
148 return;
149}
150
151
152void RFID_SERIAL() {
153 // Look for new cards
154 if ( ! mfrc522.PICC_IsNewCardPresent()) {
155 loopdone = true;
156 scanned = true;
157 return;
158 }
159
160 // Select one of the cards
161 if ( ! mfrc522.PICC_ReadCardSerial()) {
162 return ;
163 }
164
165 // Dump debug info about the card. PICC_HaltA() is automatically called.
166 //mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
167
168 Serial.print("UID tag :");
169 byte i;
170 String content = "";
171 for ( i = 0; i < mfrc522.uid.size; i++)
172 {
173 Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
174 Serial.print(mfrc522.uid.uidByte[i], HEX);
175 content.concat( String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
176 content.concat(String(mfrc522.uid.uidByte[i], HEX));
177 }
178 Serial.println();
179 Serial.print("Message : ");
180 content.toUpperCase();
181 // uid: 59 59 C9 56
182 if (content.substring(1) == "59 59 C9 56")
183 {
184 Serial.println("Authorized access");
185 Serial.println();
186 screenmsg("Access Granted");
187 delay(1500);
188 myservo.write(90);
189 bool person_there = true;
190 delay(2000);
191 ultrasonicranger();
192 while (person_there == true){
193 ultrasonicranger();
194 if (cm > 100){
195 delay(3000);
196 Serial.print ("Door Closing");
197 screenmsg("Doors closing");
198 delay(2000);
199 myservo.write(0);
200 person_there = false;
201 }
202 }
203 writetothingspeak1();
204 loop();
205
206 }
207
208 else {
209 Serial.println(" Access denied");
210 tries = tries + 1;
211 lcd.clear();
212 lcd.print("Access denied");
213 lcd.setCursor(2, 1);
214 lcd.print(3-tries);
215 lcd.print(" Tries Left");
216 tone(buzzer, 10000); // Send 1KHz sound signal...
217 delay(1000); // ...for 1 sec
218 noTone(buzzer); // Stop sound...
219 if (tries >= 3) {
220 failure();
221 } else {
222 delay(1000);
223 return;
224 }
225 writetothingspeak0();
226
227 return;
228
229 }
230}
231void failure(){
232 screenmsg("Please try again next time");
233 bool failure_present = true;
234 delay(5000);
235 while(failure_present == true){
236 ultrasonicranger();
237 if (cm > 100){
238 failure_present = false;
239 }
240 }
241 loop();
242}
243void writetothingspeak1(){
244 // Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
245 // pieces of information in a channel. Here, we write to field 2.
246 int x = ThingSpeak.writeField(myChannelNumber, 2, number, myWriteAPIKey);
247 if(x == 200){
248 Serial.println("Channel update successful.");
249 }
250 else{
251 Serial.println("Problem updating channel. HTTP error code " + String(x));
252 }
253
254 // change the value
255 number=1;
256
257 return;
258}
259
260void writetothingspeak0(){
261 // Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
262 // pieces of information in a channel. Here, we write to field 2.
263 int x = ThingSpeak.writeField(myChannelNumber, 2, number, myWriteAPIKey);
264 if(x == 200){
265 Serial.println("Channel update successful.");
266 }
267 else{
268 Serial.println("Problem updating channel. HTTP error code " + String(x));
269 }
270
271 // change the value
272 number=0;
273
274 return;
275}
276
277// This function attempts to set the ESP8266 baudrate. Boards with additional hardware serial ports
278// can use 115200, otherwise software serial is limited to 19200.
279void setEspBaudRate(unsigned long baudrate){
280 long rates[6] = {115200,74880,57600,38400,19200,9600};
281
282 Serial.print("Setting ESP8266 baudrate to ");
283 Serial.print(baudrate);
284 Serial.println("...");
285
286 for(int i = 0; i < 6; i++){
287 Serial1.begin(rates[i]);
288 delay(100);
289 Serial1.print("AT+UART_DEF=");
290 Serial1.print(baudrate);
291 Serial1.print(",8,1,0,0\r\n");
292 delay(100);
293 }
294
295 Serial1.begin(baudrate);
296}
297
298