Cesar Pantoja / Mbed 2 deprecated RFID_ACCESS

Dependencies:   ControlAcceso MFRC522 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "MFRC522.h"
00003 #include "ESP8266.h"
00004 
00005 #define IP "104.196.113.161" // Domoti Server IP Address
00006 #define PORT "6666" // Domoti Server IP Address
00007 char snd[255],rcv[100];
00008 char *spr;
00009 char UIDstr[10];
00010 uint8_t dir;
00011 
00012 Serial pc(SERIAL_TX, SERIAL_RX);
00013 
00014 MFRC522 *rf_in;
00015 MFRC522 *rf_out;
00016 
00017 ESP8266 wifi(PA_11, PA_12, 9600); // baud rate for wifi
00018 
00019 DigitalOut ESP_Enable(PB_12);
00020 DigitalOut ESP_Reset(PA_7);
00021 DigitalOut green(PA_5);
00022 
00023 void connect() {
00024     green = 1;
00025     while (1) {
00026         wifi.Join("DOMOTI_EXT", "domotizamos");     // Your wifi username & Password
00027         //wifi.Join("AulasAMIGAS", "900292680");    // Your wifi username & Password
00028         wifi.RcvReply(rcv, 3000);                 // receive a response from ESP
00029         if (!strstr(rcv,"A\"")) continue;
00030         wait(10);                                 // waits for response from ESP
00031         while (1) {
00032             wifi.GetIP(rcv);                      // receive an IP address from the AP
00033             if (!strstr(rcv,"busy")) break;
00034         }
00035         if (!strstr(rcv,"AR")) break;
00036     }
00037     pc.printf("Conectado\r\n");
00038 }
00039 
00040 void buildSocket() {
00041     green = 1;
00042     wifi.SetSingle();
00043     wifi.RcvReply(rcv, 3000);
00044     while (1) {
00045         //strcpy(snd, "AT+CIPSTART=\"TCP\",\"104.196.113.161\",6666");
00046         strcpy(snd, "AT+CIPSTART=\"TCP\",\"10.69.17.20\",6666");
00047         wifi.SendCMD(snd);
00048         wifi.RcvReply(rcv, 3000);
00049         pc.printf("Conectando al servidor.... \r\n");
00050         if (strstr(rcv,"Linked")||strstr(rcv,"ALREAY CONNECT")) break;
00051     }
00052     pc.printf("Link creado\r\n");
00053 }
00054 
00055 void readCard(MFRC522 *rf, bool in) {
00056     green = 1;
00057     if (!rf->PICC_ReadCardSerial()) return;
00058     sprintf(UIDstr, "%02X%02X%02X%02X",
00059                     rf->uid.uidByte[0],
00060                     rf->uid.uidByte[1],
00061                     rf->uid.uidByte[2],
00062                     rf->uid.uidByte[3]);
00063     if (in) dir = 1;
00064     else dir = 2;
00065 }
00066 
00067 int main()
00068 {
00069     green = 1;
00070     // Init ESP
00071     ESP_Enable = 0;
00072     ESP_Reset = 0;
00073     ESP_Reset = 1;
00074     ESP_Enable = 1;
00075     wifi.Reset();
00076     wait(5);
00077     wifi.SetMode(1);             // set ESP mode to 1
00078     wifi.RcvReply(rcv, 1000);    // receive a response from ESP
00079     connect();                   // Connect to wifi
00080     buildSocket();               // Build socket
00081     
00082     // Init RF sensors
00083     rf_in = new MFRC522(PB_15, PB_14, PB_13, PC_4, PA_13);
00084     rf_in->PCD_Init();
00085     rf_out = new MFRC522(PC_12, PC_11, PC_10, PD_2, PA_14);
00086     rf_out->PCD_Init();
00087     while(1) {
00088         green = 0;
00089         dir = 0;
00090         if (rf_in->PICC_IsNewCardPresent()) {readCard(rf_in,1);}
00091         else if (rf_out->PICC_IsNewCardPresent()) readCard(rf_out,0);
00092         if (!dir) {wait_ms(500);continue;}
00093 
00094         strcpy(snd, "AT+CIPSEND=12");
00095         wifi.SendCMD(snd);
00096         wifi.RcvReply(rcv, 500);
00097         // Socket verification
00098         if (strstr(rcv,"ERROR")||strstr(rcv,"link is not")) {
00099             buildSocket();
00100             continue;
00101         }
00102         if (dir==1) sprintf(snd,"%s,I",UIDstr);
00103         else sprintf(snd,"%s,O",UIDstr);
00104         pc.printf("REQ: %s\r\n", snd);
00105         wifi.SendCMD(snd);
00106         wifi.RcvReply(rcv, 10000);
00107         pc.printf("%s\r\n", rcv);
00108         
00109         spr = strchr(rcv,'+');
00110         if (spr!=NULL) {
00111             if (spr[1]=='I'&&spr[3]=='D') {
00112                 spr = strchr(rcv,':');
00113                 if (spr[1]=='1') {
00114                     green = 1;
00115                     pc.printf("Acceso Concedido\r\n");
00116                 } else {
00117                     pc.printf("Acceso Denegado\r\n");
00118                 }
00119             }
00120         } else pc.printf("No respuesta\r\n");
00121         wait_ms(1000);
00122     }
00123 }