Components and supplies
I2C LCD
Pushbutton Switch, Pushbutton
Relay (generic)
Buzzer
ESP32
Arduino Nano R3
NeoPixel Ring: WS2812 5050 RGB LED
RFID Module (Generic)
RGB LCD Shield Kit, 16x2 Character Display
Apps and platforms
Fritzing
Linux (Mint)
Arduino IDE
circuito.io
Tinkercad
Project description
Code
Code
c_cpp
1#include <Adafruit_NeoPixel.h> 2#include <SPI.h> 3#include <MFRC522.h> 4#include <Arduino.h> 5#include <WiFi.h> 6#include <HTTPClient.h> 7#include <Tone32.h> //https://github.com/lbernstone/Tone 8#include <LiquidCrystal_I2C.h> 9#include <Wire.h> 10#include <string> 11 12#define ON 1 13#define OFF 0 14 15#define BUZZER_CHANNEL 0 16#define RELAY_PIN 2 17#define NEOPIXEL_PIN 4 18#define RFID_SS_PIN 14 19#define BUZZER_PIN 15 20#define RFID_RST_PIN 27 21#define STRIPSIZE 16 22#define LANGUAGE_PIN 34 23 24#define OPEN_DOOR 0 25#define DOOR_CLOSES_IN 1 26#define DONT_FORGET_CLOSE_DOOR 2 27#define THE_DOOR 3 28#define ERROR_CARD 4 29#define NOT_FOUND 5 30#define ACCESS 6 31#define GRANTED 7 32#define ACTIVATING 8 33#define PORTS 9 34#define CONNECTED_TO 10 35#define OFFICEYA_NETWORK 11 36#define CONNECTING 12 37#define ACTIVATING_RFID 13 38#define RFID_ACTIVATED 14 39#define NO_NETWORK 15 40#define LANGUAGE_CHANGED 16 41#define ACUTAL_LANGUAGE 17 42#define INNOVATING 18 43#define SOLUTIONS 19 44#define MAX_ELEMENTS 20 45 46const String english[MAX_ELEMENTS] = {"Open the door", 47 "It closes in:", 48 "Dont forget to", 49 "CLOSE the door", 50 "Error, card", 51 "not found", 52 "ACCESS", 53 "GRANTED", 54 "Activating", 55 "Ports...", 56 "Connecting to", 57 "OfficeYA Network", 58 "Connecting...", 59 "Activating RFID", 60 "RFID Activaded", 61 "Network not AVBL", 62 "Language changed", 63 "Using English", 64 "** Innovating **", 65 "*** Solutions **"}; 66const String spanish[MAX_ELEMENTS] = {"Abra la puerta", 67 "Se cierra en: ", 68 "No olvide CERRAR", 69 "la puerta", 70 "ERROR, tarjeta", 71 "no LOCALIZADA", 72 "ACCESO", 73 "AUTORIZADO", 74 "Activando", 75 "Puertos...", 76 "Conectando a la", 77 "red de OfficeYA", 78 "Conectando...", 79 "Activando RFID", 80 "Activado RFID", 81 "Red no disponible", 82 "Cabmio de idioma", 83 "Usando ESPANOL", 84 "** Innovando ***", 85 "** Soluciones **"}; 86const int lcdColumns = 18; 87const int lcdRows = 2; 88int language = 1; // 1 = Spanish, 0 = English 89int prev_language = 9; 90int loop_counter = 1; 91boolean incrementing = true; 92String card = ""; 93String messages[MAX_ELEMENTS]; 94 95MFRC522 rfid(RFID_SS_PIN, RFID_RST_PIN); 96LiquidCrystal_I2C lcd(0x3F, lcdColumns, lcdRows); 97Adafruit_NeoPixel strip = Adafruit_NeoPixel(STRIPSIZE, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800); 98 99void lcdMessage(String text1, String text2, int delayms = 500) { 100 lcd.clear(); 101 lcd.setCursor(0, 0); 102 lcd.print(text1); 103 lcd.setCursor(0, 1); 104 lcd.print(text2); 105 delay(delayms); 106} 107 108void changeLanguage() { 109 if (language != prev_language) { 110 if (language == 1) { 111 for (int i = 0; i <= MAX_ELEMENTS; i++) { 112 messages[i] = spanish[i]; 113 } 114 } else { 115 for (int i = 0; i <= MAX_ELEMENTS; i++) { 116 messages[i] = english[i]; 117 } 118 } 119 prev_language = language; 120 if (language == 1) { 121 language = 0; 122 } else { 123 language = 1; 124 } 125 } 126 lcdMessage(messages[LANGUAGE_CHANGED], messages[ACUTAL_LANGUAGE], 1500); 127} 128 129void lightIt(int led, uint32_t color, int wait) { 130 strip.setPixelColor(led, color); 131 strip.show(); 132 delay(wait); 133} 134 135void colorWipe(uint32_t c, uint8_t wait, int mode = 1) { 136 if (mode == 1) { 137 for (uint16_t i = 0; i < strip.numPixels(); i++) { 138 lightIt(i, c, wait); 139 } 140 } else { 141 for (uint16_t i = (strip.numPixels() - 1); i > 0; i--) { 142 lightIt(i, c, wait); 143 } 144 lightIt(0, c, wait); 145 } 146} 147 148void relayMode(int mode, int delayms = 500) { 149 if (mode == 1) { 150 lcdMessage(messages[OPEN_DOOR], "", 100); 151 digitalWrite(RELAY_PIN, HIGH); 152 lcdMessage(messages[OPEN_DOOR], messages[DOOR_CLOSES_IN], 0); 153 for (int i = 0; i < 10; i++) { 154 lcd.setCursor(14, 1); 155 lcd.print(10 - i); 156 if (i != 0) { 157 lcd.setCursor(15, 1); 158 lcd.print(" "); 159 } 160 colorWipe(strip.Color(0, 200, 0), 28); 161 delay(104); 162 colorWipe(strip.Color(0, 0, 0), 28); 163 } 164 } else { 165 lcdMessage(messages[DONT_FORGET_CLOSE_DOOR], messages[THE_DOOR], 100); 166 digitalWrite(RELAY_PIN, LOW); 167 delay(delayms); 168 } 169} 170 171void errorSound() { 172 colorWipe(strip.Color(200, 0, 0), 25); 173 lcdMessage(messages[ERROR_CARD], messages[NOT_FOUND]); 174 for (int i = 0; i < 4; i++) { 175 tone(BUZZER_PIN, 150, 200, BUZZER_CHANNEL); 176 tone(BUZZER_PIN, 125, 225, BUZZER_CHANNEL); 177 } 178 noTone(BUZZER_PIN, BUZZER_CHANNEL); 179 delay(3000); 180 colorWipe(strip.Color(0, 0, 0), 25, 0); 181} 182 183void okSound() { 184 lcdMessage(messages[ACCESS], messages[GRANTED], 500); 185 tone(BUZZER_PIN, 350, 150, BUZZER_CHANNEL); 186 tone(BUZZER_PIN, 275, 250, BUZZER_CHANNEL); 187 tone(BUZZER_PIN, 350, 150, BUZZER_CHANNEL); 188 noTone(BUZZER_PIN, BUZZER_CHANNEL); 189} 190 191void checkLanguage() { 192 if (digitalRead(LANGUAGE_PIN) == 1) { 193 Serial.println("Push button pushed, changing language"); 194 changeLanguage(); 195 } 196} 197 198uint32_t Wheel(byte WheelPos) { 199 if (WheelPos < 85) { 200 return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); 201 } else if (WheelPos < 170) { 202 WheelPos -= 85; 203 return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); 204 } else { 205 WheelPos -= 170; 206 return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); 207 } 208} 209 210void rainbow(uint8_t wait) { 211 uint16_t i, j; 212 for (j = 0; j < 256; j++) { 213 for (i = 0; i < strip.numPixels(); i++) { 214 strip.setPixelColor(i, Wheel((i + j) & 255)); 215 } 216 checkLanguage(); 217 strip.show(); 218 delay(wait); 219 } 220} 221 222void detectCardRFID(){ 223 if ( ! rfid.PICC_IsNewCardPresent() || ! rfid.PICC_ReadCardSerial() ) { 224 if (incrementing){ 225 if (loop_counter <= 15){ 226 checkLanguage(); 227 loop_counter++; 228 } else { 229 lcdMessage(messages[INNOVATING], messages[SOLUTIONS], 0); 230 loop_counter = 15; 231 incrementing = false; 232 } 233 } else { 234 if (loop_counter >= 1){ 235 checkLanguage(); 236 loop_counter--; 237 } else { 238 lcdMessage("*** OfficeYA ***", "Business Centers", 0); 239 loop_counter = 0; 240 incrementing = true; 241 } 242 } 243 return; 244 } else { 245 Serial.print(F("Card UID:")); 246 for (byte i = 0; i < rfid.uid.size; i++) { 247 card += rfid.uid.uidByte[i]; 248 Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "); 249 Serial.print(rfid.uid.uidByte[i], HEX); 250 } 251 Serial.println(); 252 Serial.print("Decimal: "); 253 Serial.println(card); 254 Serial.println(); 255 } 256} 257 258void verifyAccessRights(){ 259 if (card != "") { 260 String url = ""; 261 if (WiFi.status() == WL_CONNECTED) { 262 HTTPClient http; 263 url = "http://youripaddress/status/" + card + ".json"; 264 http.begin(url); 265 http.addHeader("Content-Type", "text/plain"); 266 int httpResponseCode = http.POST(""); 267 if (httpResponseCode > 0) { 268 if ( strcmp("ok", http.getString().c_str()) == 0 ) { 269 okSound(); 270 relayMode(ON); 271 relayMode(OFF, 1000); 272 } else { 273 errorSound(); 274 } 275 } else { 276 errorSound(); 277 } 278 http.end(); 279 } else { 280 lcdMessage("ERROR:", messages[NO_NETWORK], 2000); 281 } 282 } 283} 284 285void setup() { 286 char* ssid = ""; 287 char* password = ""; 288 ssid = "yourssid"; 289 password = "yourpassword"; 290 Serial.begin(115200); 291 changeLanguage(); 292 delay(100); 293 294 strip.begin(); 295 strip.setBrightness(25); 296 strip.show(); 297 colorWipe(strip.Color(0, 0, 0), 1); 298 299 lcd.init(); 300 lcd.backlight(); 301 302 lcdMessage(messages[ACTIVATING], messages[PORTS]); 303 pinMode(RELAY_PIN, OUTPUT); 304 pinMode(BUZZER_PIN, OUTPUT); 305 pinMode(LANGUAGE_PIN, INPUT); 306 307 lcdMessage(messages[CONNECTED_TO], messages[OFFICEYA_NETWORK]); 308 Serial.print("*** Connecting to network: "); 309 Serial.print(ssid); 310 Serial.println(" ***"); 311 WiFi.begin(ssid, password); 312 while (WiFi.status() != WL_CONNECTED) { 313 lcdMessage(messages[CONNECTING], "", 500); 314 } 315 Serial.println(""); 316 Serial.println("Connected to WiFi"); 317 Serial.print("IP Address: "); 318 Serial.println(WiFi.localIP()); 319 320 lcdMessage(messages[ACTIVATING_RFID], messages[RFID_ACTIVATED]); 321 SPI.begin(); 322 rfid.PCD_Init(); 323 delay(10); 324 325 digitalWrite(RELAY_PIN, LOW); 326 lcdMessage("*** OfficeYA ***", "Business Centers", 1); 327} 328 329void loop() { 330 card = ""; 331 rainbow(15); 332 detectCardRFID(); 333 verifyAccessRights(); 334}
Code
c_cpp
1#include <Adafruit_NeoPixel.h> 2#include <SPI.h> 3#include <MFRC522.h> 4#include 5 <Arduino.h> 6#include <WiFi.h> 7#include <HTTPClient.h> 8#include <Tone32.h> 9 //https://github.com/lbernstone/Tone 10#include <LiquidCrystal_I2C.h> 11#include 12 <Wire.h> 13#include <string> 14 15#define ON 1 16#define OFF 0 17 18#define 19 BUZZER_CHANNEL 0 20#define RELAY_PIN 2 21#define NEOPIXEL_PIN 4 22#define 23 RFID_SS_PIN 14 24#define BUZZER_PIN 15 25#define RFID_RST_PIN 27 26#define 27 STRIPSIZE 16 28#define LANGUAGE_PIN 34 29 30#define OPEN_DOOR 0 31#define 32 DOOR_CLOSES_IN 1 33#define DONT_FORGET_CLOSE_DOOR 2 34#define THE_DOOR 35 3 36#define ERROR_CARD 4 37#define NOT_FOUND 5 38#define 39 ACCESS 6 40#define GRANTED 7 41#define ACTIVATING 42 8 43#define PORTS 9 44#define CONNECTED_TO 10 45#define 46 OFFICEYA_NETWORK 11 47#define CONNECTING 12 48#define ACTIVATING_RFID 49 13 50#define RFID_ACTIVATED 14 51#define NO_NETWORK 15 52#define 53 LANGUAGE_CHANGED 16 54#define ACUTAL_LANGUAGE 17 55#define INNOVATING 56 18 57#define SOLUTIONS 19 58#define MAX_ELEMENTS 20 59 60const 61 String english[MAX_ELEMENTS] = {"Open the door", 62 "It 63 closes in:", 64 "Dont forget to", 65 66 "CLOSE the door", 67 "Error, 68 card", 69 "not found", 70 "ACCESS", 71 72 "GRANTED", 73 "Activating", 74 75 "Ports...", 76 "Connecting 77 to", 78 "OfficeYA Network", 79 "Connecting...", 80 81 "Activating RFID", 82 "RFID 83 Activaded", 84 "Network not AVBL", 85 86 "Language changed", 87 "Using 88 English", 89 "** Innovating **", 90 "*** 91 Solutions **"}; 92const String spanish[MAX_ELEMENTS] = {"Abra la puerta", 93 94 "Se cierra en: ", 95 "No 96 olvide CERRAR", 97 "la puerta", 98 "ERROR, 99 tarjeta", 100 "no LOCALIZADA", 101 "ACCESO", 102 103 "AUTORIZADO", 104 "Activando", 105 106 "Puertos...", 107 "Conectando 108 a la", 109 "red de OfficeYA", 110 "Conectando...", 111 112 "Activando RFID", 113 "Activado 114 RFID", 115 "Red no disponible", 116 "Cabmio 117 de idioma", 118 "Usando ESPANOL", 119 "** 120 Innovando ***", 121 "** Soluciones **"}; 122const 123 int lcdColumns = 18; 124const int lcdRows = 2; 125int 126 language = 1; // 1 = Spanish, 0 = English 127int prev_language 128 = 9; 129int loop_counter = 1; 130boolean incrementing 131 = true; 132String card = ""; 133String 134 messages[MAX_ELEMENTS]; 135 136MFRC522 rfid(RFID_SS_PIN, RFID_RST_PIN); 137LiquidCrystal_I2C 138 lcd(0x3F, lcdColumns, lcdRows); 139Adafruit_NeoPixel strip = Adafruit_NeoPixel(STRIPSIZE, 140 NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800); 141 142void lcdMessage(String text1, String 143 text2, int delayms = 500) { 144 lcd.clear(); 145 lcd.setCursor(0, 0); 146 lcd.print(text1); 147 148 lcd.setCursor(0, 1); 149 lcd.print(text2); 150 delay(delayms); 151} 152 153void 154 changeLanguage() { 155 if (language != prev_language) { 156 if (language == 157 1) { 158 for (int i = 0; i <= MAX_ELEMENTS; i++) { 159 messages[i] = 160 spanish[i]; 161 } 162 } else { 163 for (int i = 0; i <= MAX_ELEMENTS; 164 i++) { 165 messages[i] = english[i]; 166 } 167 } 168 prev_language 169 = language; 170 if (language == 1) { 171 language = 0; 172 } else { 173 174 language = 1; 175 } 176 } 177 lcdMessage(messages[LANGUAGE_CHANGED], 178 messages[ACUTAL_LANGUAGE], 1500); 179} 180 181void lightIt(int led, uint32_t color, 182 int wait) { 183 strip.setPixelColor(led, color); 184 strip.show(); 185 delay(wait); 186} 187 188void 189 colorWipe(uint32_t c, uint8_t wait, int mode = 1) { 190 if (mode == 1) { 191 for 192 (uint16_t i = 0; i < strip.numPixels(); i++) { 193 lightIt(i, c, wait); 194 195 } 196 } else { 197 for (uint16_t i = (strip.numPixels() - 1); i > 0; i--) 198 { 199 lightIt(i, c, wait); 200 } 201 lightIt(0, c, wait); 202 } 203} 204 205void 206 relayMode(int mode, int delayms = 500) { 207 if (mode == 1) { 208 lcdMessage(messages[OPEN_DOOR], 209 "", 100); 210 digitalWrite(RELAY_PIN, HIGH); 211 lcdMessage(messages[OPEN_DOOR], 212 messages[DOOR_CLOSES_IN], 0); 213 for (int i = 0; i < 10; i++) { 214 lcd.setCursor(14, 215 1); 216 lcd.print(10 - i); 217 if (i != 0) { 218 lcd.setCursor(15, 219 1); 220 lcd.print(" "); 221 } 222 colorWipe(strip.Color(0, 200, 223 0), 28); 224 delay(104); 225 colorWipe(strip.Color(0, 0, 0), 28); 226 227 } 228 } else { 229 lcdMessage(messages[DONT_FORGET_CLOSE_DOOR], messages[THE_DOOR], 230 100); 231 digitalWrite(RELAY_PIN, LOW); 232 delay(delayms); 233 } 234} 235 236void 237 errorSound() { 238 colorWipe(strip.Color(200, 0, 0), 25); 239 lcdMessage(messages[ERROR_CARD], 240 messages[NOT_FOUND]); 241 for (int i = 0; i < 4; i++) { 242 tone(BUZZER_PIN, 243 150, 200, BUZZER_CHANNEL); 244 tone(BUZZER_PIN, 125, 225, BUZZER_CHANNEL); 245 246 } 247 noTone(BUZZER_PIN, BUZZER_CHANNEL); 248 delay(3000); 249 colorWipe(strip.Color(0, 250 0, 0), 25, 0); 251} 252 253void okSound() { 254 lcdMessage(messages[ACCESS], messages[GRANTED], 255 500); 256 tone(BUZZER_PIN, 350, 150, BUZZER_CHANNEL); 257 tone(BUZZER_PIN, 275, 258 250, BUZZER_CHANNEL); 259 tone(BUZZER_PIN, 350, 150, BUZZER_CHANNEL); 260 noTone(BUZZER_PIN, 261 BUZZER_CHANNEL); 262} 263 264void checkLanguage() { 265 if (digitalRead(LANGUAGE_PIN) 266 == 1) { 267 Serial.println("Push button pushed, changing language"); 268 changeLanguage(); 269 270 } 271} 272 273uint32_t Wheel(byte WheelPos) { 274 if (WheelPos < 85) { 275 return 276 strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); 277 } else if (WheelPos < 170) 278 { 279 WheelPos -= 85; 280 return strip.Color(255 - WheelPos * 3, 0, WheelPos 281 * 3); 282 } else { 283 WheelPos -= 170; 284 return strip.Color(0, WheelPos 285 * 3, 255 - WheelPos * 3); 286 } 287} 288 289void rainbow(uint8_t wait) { 290 uint16_t 291 i, j; 292 for (j = 0; j < 256; j++) { 293 for (i = 0; i < strip.numPixels(); 294 i++) { 295 strip.setPixelColor(i, Wheel((i + j) & 255)); 296 } 297 checkLanguage(); 298 299 strip.show(); 300 delay(wait); 301 } 302} 303 304void detectCardRFID(){ 305 306 if ( ! rfid.PICC_IsNewCardPresent() || ! rfid.PICC_ReadCardSerial() ) { 307 if 308 (incrementing){ 309 if (loop_counter <= 15){ 310 checkLanguage(); 311 312 loop_counter++; 313 } else { 314 lcdMessage(messages[INNOVATING], 315 messages[SOLUTIONS], 0); 316 loop_counter = 15; 317 incrementing = 318 false; 319 } 320 } else { 321 if (loop_counter >= 1){ 322 checkLanguage(); 323 324 loop_counter--; 325 } else { 326 lcdMessage("*** OfficeYA ***", 327 "Business Centers", 0); 328 loop_counter = 0; 329 incrementing = 330 true; 331 } 332 } 333 return; 334 } else { 335 Serial.print(F("Card 336 UID:")); 337 for (byte i = 0; i < rfid.uid.size; i++) { 338 card += rfid.uid.uidByte[i]; 339 340 Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " "); 341 Serial.print(rfid.uid.uidByte[i], 342 HEX); 343 } 344 Serial.println(); 345 Serial.print("Decimal: "); 346 347 Serial.println(card); 348 Serial.println(); 349 } 350} 351 352void verifyAccessRights(){ 353 354 if (card != "") { 355 String url = ""; 356 if (WiFi.status() == WL_CONNECTED) 357 { 358 HTTPClient http; 359 url = "http://youripaddress/status/" + card 360 + ".json"; 361 http.begin(url); 362 http.addHeader("Content-Type", 363 "text/plain"); 364 int httpResponseCode = http.POST(""); 365 if (httpResponseCode 366 > 0) { 367 if ( strcmp("ok", http.getString().c_str()) == 0 ) { 368 okSound(); 369 370 relayMode(ON); 371 relayMode(OFF, 1000); 372 } else { 373 374 errorSound(); 375 } 376 } else { 377 errorSound(); 378 379 } 380 http.end(); 381 } else { 382 lcdMessage("ERROR:", messages[NO_NETWORK], 383 2000); 384 } 385 } 386} 387 388void setup() { 389 char* ssid = ""; 390 char* 391 password = ""; 392 ssid = "yourssid"; 393 password = "yourpassword"; 394 395 Serial.begin(115200); 396 changeLanguage(); 397 delay(100); 398 399 strip.begin(); 400 401 strip.setBrightness(25); 402 strip.show(); 403 colorWipe(strip.Color(0, 0, 0), 404 1); 405 406 lcd.init(); 407 lcd.backlight(); 408 409 lcdMessage(messages[ACTIVATING], 410 messages[PORTS]); 411 pinMode(RELAY_PIN, OUTPUT); 412 pinMode(BUZZER_PIN, OUTPUT); 413 414 pinMode(LANGUAGE_PIN, INPUT); 415 416 lcdMessage(messages[CONNECTED_TO], messages[OFFICEYA_NETWORK]); 417 418 Serial.print("*** Connecting to network: "); 419 Serial.print(ssid); 420 Serial.println(" 421 ***"); 422 WiFi.begin(ssid, password); 423 while (WiFi.status() != WL_CONNECTED) 424 { 425 lcdMessage(messages[CONNECTING], "", 500); 426 } 427 Serial.println(""); 428 429 Serial.println("Connected to WiFi"); 430 Serial.print("IP Address: "); 431 432 Serial.println(WiFi.localIP()); 433 434 lcdMessage(messages[ACTIVATING_RFID], 435 messages[RFID_ACTIVATED]); 436 SPI.begin(); 437 rfid.PCD_Init(); 438 delay(10); 439 440 441 digitalWrite(RELAY_PIN, LOW); 442 lcdMessage("*** OfficeYA ***", "Business 443 Centers", 1); 444} 445 446void loop() { 447 card = ""; 448 rainbow(15); 449 450 detectCardRFID(); 451 verifyAccessRights(); 452}
Downloadable files
Connections Diagram
Connections Diagram
Connections Diagram
Connections Diagram
Documentation
Case
Case
Comments
Only logged in users can leave comments
addicttux
0 Followers
•0 Projects
Table of contents
Intro
1
0