mbeduino + XBee + RFID

mbeduino に XBee と RFID リーダライタユニットを乗せてみました。



/*
  mbeduino, RFID, XBee Test Program
  Ver.0.1 : 2010/11/02

  CopyLeft Moo Soft (Akiyosi Yokoyama)  http://moosoft.jp

  mbeduino, HF-06SR, XBee
*/

#include <string>
using std::string;

#include "mbed.h"

// local filesystem
LocalFileSystem local("local");     // Create the local filesystem under the name "local"

// Beeper
#include "Beeper.h"
Beeper myBeeper(p21);

// LED
DigitalOut myled1(LED1);
DigitalOut myled2(LED2);
DigitalOut myled3(LED3);
DigitalOut myled4(LED4);

// Serial Port
Serial xbee(p13, p14);       // XBee tx, rx
Serial rfid(p28, p27);       // RFID tx, rx

// RFID
const int pub_data_len = 11; // Mifare   ex.DAAF536B4D + CR
//const int pub_data_len=23; // ISO15693 ex.01,0123456789ABCDEF + CR

char pub_rfid_scan[] = "XS"; // Scan Start Command
//char rfid_version[] = "V"; // Version
char pub_data[pub_data_len]; // Read Buffer
string pub_rfidData = "";    // Read RFID Data String

// function
void start_rfidScan(char *wdata); // RFID SCAN START
void read_card();                 // READ RFID Data
void led_flash();                 // LED ON & OFF
bool xbee_TxData();               // XBee TX Data
bool set_xbee();                  // Set XBee Serial Port
bool set_rfid();                  // Set RFID Serial Port
bool write_localUSB();            // local usb Write

// Main Loop
int main() {
    set_xbee();                        // Set Xbee Serial Port
    set_rfid();                        // Set RFID Serial Port

    while (true) {
        start_rfidScan(pub_rfid_scan); // RFID Scan Start
        read_card();                   // Read RFID Data
        xbee_TxData();                 // XBee TX Data
        write_localUSB();              // local usb Write
    }
}

// local usb Write
bool write_localUSB() {
    FILE *fp = fopen("/local/rfid.txt", "a"); // Open "out.txt" on the local file system for Append
    if (fp == NULL) {                         // a : Append, w : Write, r : Read
        error("Could not open file\n");
        return false;
    }

    fprintf(fp, "UID,%s\r\n", pub_rfidData.c_str());
    fclose(fp);
    return true;
}

// Set RFID Serial Port
bool set_rfid() {
    rfid.format(8, Serial::None, 1); // Default
    rfid.baud(38400);
    //device.printf("M0");           // Mifare -> Default
    //device.printf("M2");           // ISO15693
    return true;
}

// Set XBee Serial Port
bool set_xbee() {
    xbee.format(8, Serial::None, 1); // Default
    xbee.baud(9600);
    return true;
}

// XBee TX Data
bool xbee_TxData() {
    if (xbee.writeable()) {
        xbee.printf("UID,%s\r", pub_rfidData.c_str()); // XBee
        return true;
    } else {
        return false;
    }
}

// RFID SCAN START
void start_rfidScan(char *wdata) {
    if (rfid.writeable()) {
        rfid.printf("%s\r", wdata);
    }
}

// READ RFID Data
void read_card() {
    int ii;
    bool readOk = false;

    while (true) {
        // Initialize
        for (ii = 0; ii < pub_data_len; ii++) {
            pub_data[ii] = 0x20;
        }

        // Read Device
        for (ii = 0; ii < pub_data_len; ii++) {
            pub_data[ii] = rfid.getc();
            if (pub_data[ii] == 0x0d) {
                readOk = ii == pub_data_len - 1;
                break;
            } // if
        } // for

        if (readOk) {
            readOk = false;
            led_flash();    // Led ON & OFF
            break;
        }
    } // while
    pub_rfidData = "";
    for (ii = 0; ii < pub_data_len - 1; ii++) {
        pub_rfidData += pub_data[ii];
    }
}

// LED & Beep
void led_flash() {
    myled1 = 1;
    myled3 = 1;
    wait(0.1);
    myled2 = 1;
    myled4 = 1;
    wait(0.1);
    myled1 = 0;
    myled2 = 0;
    myled3 = 0;
    myled4 = 0;

    for (int i = 0; i < 3 ; i++) {
        myBeeper.note(95, 0.1);
    }
}

 


0 comments

You need to log in to post a comment