Things used in this project

Hardware components:
Core
Particle Spark Core
×1
atmega168
×1
pn532 breakout board/ adafruit
×1

Schematics

fritzing breadboard
Pn532 sparkcore bb

Code

particle firmwareC/C++
flash this with web ide
update 7-24// took out setTimeout(0) and flush() in loop.
//  firmware to receive bytes from atmega186 over USART and send over Wifi
//  The atmega168 is programmed to talk to the pn532 nfc card reader and then send the card info
//    over USART.

#define debug 0
#define MAXLEN 256

TCPServer server = TCPServer(2342);
TCPClient client;
bool unlocked = false;

void setup() {
    
    Serial1.begin(9600);
    //Serial1.setTimeout(0);
    if (debug) {
        Serial.begin(9600);
        while(!Serial.available()) SPARK_WLAN_Loop();
    
        Serial.println("Listening for connection...");
        if (WiFi.ready()) {
            Serial.println(WiFi.localIP());
        } else {
            Serial.println("wifi not ready");
        }
    }
    
    server.begin();
    Serial1.flush();
    
}


int pollSerial() {
    bool on = false;
    int i, success;
    unsigned long scantime;
    char cmd, valL, valH;
    uint8_t dataframe[32];
    int intvalL, intvalH;
    
    //client.setTimeout(0);
    //client.flush();
    
    if (debug) {
        Serial.println("connected");
    }
    
    while (true) {
        
        char s_buff[MAXLEN] = {0};
        Serial1.flush();
        
        if (on) {
            while (true) {
                if (Serial1.available()) {
                    Serial1.readBytes(s_buff, 64);
                    if (debug) {
                        for (i=0;i<32;i++) {
                        Serial.print(s_buff[i]);
                        }
                    }
                    
                    if (s_buff[0] == 2) {
                        break;
                    }
                }
            }
            
            dataframe[0] = 0x81;
            for (i = 1;i < 15;i++) {
                dataframe[i] = s_buff[i];
            }
            dataframe[i] = '\0';
            
            success = server.write((uint8_t *)dataframe, 16);
            
            if (!success) {
                if (debug) {
                    Serial.println("dataframe name failed");
                }
            }
            
            cmd = 0xA1;
            // atoi with single bytes
            valL = ((s_buff[19] - 0x30) * 10) + (s_buff[20] - 0x30);
            valH = ((s_buff[21] - 0x30) * 10) + (s_buff[22] - 0x30);
            if (debug) {
                Serial.println(valL);
                Serial.println(valH);
            }
            
            dataframe[0] = 0x82;
            dataframe[1] = cmd;
            dataframe[2] = valL;
            dataframe[3] = valH;
            
            success = server.write(dataframe, 4);
            if (debug) {
                for (i=0;i<5;i++) {
                    Serial.print(dataframe[i]);
                }
                if (!success)
                    Serial.println("dataframe value failed");
            }
            on = false;
        }
        /* check for incomming */
        char rec_buf[MAXLEN] = {0};
        if (client.available()) {
            if (debug) {
                Serial.println("data to recieve");
            }
            
            char count[1];
            int  bytes;
    
            bytes = client.readBytes(count, 1);
            if (bytes < 0) {
                rec_buf[0] = '\0';
            }                           
            bytes = client.readBytes((char *)rec_buf, count[0]);  
   
            rec_buf[count[0]] = '\0';
            
            if (unlocked) {
                /*first byte: opcode
                 *seconde byte: cmd
                 *3-paylen bytes: data
                 */
                 if (debug) {
                     for (i = 0;i < 16;i++) {
                         Serial.print(rec_buf[i]);
                     }
                 }
                 if (rec_buf[2] == 0x80) {
                     if (debug) {
                         Serial.println("Bye!");
                     }
                     
                     cmd = 0xF1;
                     valL = 0xFF;
                     valH = 0xFF;
                     dataframe[0] = 0x82;
                     dataframe[1] = cmd;
                     dataframe[2] = valL;
                     dataframe[3] = valH;
                     success = server.write(dataframe, 4);
                     if (!success) {
                         if (debug) {
                             Serial.println("couldn't send no pass");
                         }
                        client.stop();
                        break;
                     }
                 }
                 if (rec_buf[2] == 0x20) {
                     on = true;
                     if (debug) {
                         Serial.println("scanning..");
                     }
                     
                 }
        
            } 
        }
    }
    
    return 0;
}

int verifyPass() {
    char rec_buf[MAXLEN] = {0};
    char count[1];
    int  bytes;
    
    bytes = client.readBytes(count, 1);
    if (bytes < 0) {
        rec_buf[0] = '\0';
    }                           
    bytes = client.readBytes((char *)rec_buf, count[0]);  
   
    rec_buf[count[0]] = '\0';
    
    if (strcmp((char *)rec_buf, "yousuck") != 0) {
        if (debug) {
            Serial.println("incorrect password");
        Serial.println(rec_buf);
        }
        return 0;
    }
    unlocked = true;
    return 1;
}

void loop() {
    
    if (client.connected()) {
        client.flush();
        if (!verifyPass()) {
            if (debug) 
                Serial.println("exiting");
            
            client.stop();
            while (true) {};
        }
        pollSerial();
    } else {
        client = server.available();
    }
}
AppDelegate.m
for the mac app.
avr code to write Mifare classic with pn532
compile with gavrasm. flash with avrdude
avr code to read and dec Mifare classic with pn532
compile with gavrasm. flash with avrdude.

Credits

Sprofile qedu5qn6qn
Bob Burns
1 project • 4 followers
Started programming again after 25? years.
Contact

Comments