From 1bb6704d6ebacc37fbdd4930e4b5fe8d5d5b17f7 Mon Sep 17 00:00:00 2001 From: Lukas Bachschwell Date: Fri, 8 Jul 2016 12:51:21 +0200 Subject: [PATCH] Fixed Wrong files getting detected as persons Added HardwareSerial* as parameter to print person Fixed bug in check for file Added RFID and Finger revoke mechanisms Added user scan Added set user credits Enabled creditsystem --- beerbox.h | 5 +- beerbox.ino | 53 +++++---- bluetooth.ino | 312 ++++++++++++++++++++++++++++++++++---------------- inputs.ino | 32 ++++-- 4 files changed, 271 insertions(+), 131 deletions(-) diff --git a/beerbox.h b/beerbox.h index f9e32c3..923640e 100644 --- a/beerbox.h +++ b/beerbox.h @@ -1,4 +1,3 @@ -#define NUM_OF_PEOPLE 25 // This is (thank myself) not used #define NUM_OF_DRINKS 10 #define DRINK_NAME_MAX_LENGTH 13 #define FILE_NAME_LEN 8 @@ -31,8 +30,10 @@ typedef struct{ typedef struct{ Drink drinks[NUM_OF_DRINKS]; int waiting; + int scanning=0; int maxDrink=0; int maxID=0; + int personCount=0; }Beerbox; //this shall always just exist in a short term! @@ -42,7 +43,7 @@ typedef struct{ char name[NAME_LEN + 1]; char rfid_uuid[13]; uint8_t finger_uuid; - uint8_t credits_left; + int credits_left; }Person; diff --git a/beerbox.ino b/beerbox.ino index e5b285a..9d94524 100644 --- a/beerbox.ino +++ b/beerbox.ino @@ -12,7 +12,7 @@ Adafruit_Fingerprint finger = Adafruit_Fingerprint(&Serial2); LiquidCrystal_I2C lcd(0x38, 8, 2); // 0x38 for PCF***A on address 000 -#define debug 0 //General debug out on/off +#define debug 1 //General debug out on/off File myFile; const int chipSelect = 53; @@ -27,7 +27,6 @@ int currentDrink = 0; int lastPersonIndex = -1; Person* persons; -int personCount; String rfID; unsigned long oldTime = 0; @@ -115,7 +114,7 @@ void check_for_file(String filename, String extension) { char* buf = NULL; str.toCharArray(buf, str.length() + 1); - if (SD.exists("p001.per")) { + if (SD.exists(str)) { if(debug)Serial.println(str + " exists."); } else { @@ -182,6 +181,14 @@ Beerbox* read_beerbox(Beerbox *box) { } +bool personExists(char *filename){ + if((strcmp(strlwr(filename + (strlen(filename) - 4)), ".per")==0)&&(filename[0]=='P')){ + //valid person name + if(SD.exists(filename))return true; + } + return false; +} + Person read_person(Beerbox *box, char *filename) { Person aperson; @@ -217,7 +224,7 @@ Person read_person(Beerbox *box, char *filename) { memset(aperson.rfid_uuid, 0, 13); read_line_from_file(read, tmp, sizeof(tmp)); sscanf(tmp, "rfid_uuid: %12s", &aperson.rfid_uuid); - if(debug)Serial.println(aperson.rfid_uuid); + //if(debug)Serial.println(aperson.rfid_uuid); read_line_from_file(read, tmp, sizeof(tmp)); sscanf(tmp, "finger_uuid: %d", &aperson.finger_uuid); //if(debug)Serial.println(aperson.finger_uuid); @@ -240,35 +247,33 @@ Person read_person(Beerbox *box, char *filename) { } if (aperson.finger_uuid > box->maxID) box->maxID = aperson.finger_uuid; - print_person(box, &aperson); - //comment back in once it is fixed - //update_pers_file(box, &aperson); + //if(debug)print_person(box, &aperson,&Serial); + print_person_JSON(box,&aperson,&Serial); read.close(); return aperson; } -void print_person(Beerbox *box, Person *aperson) { +void print_person(Beerbox *box, Person *aperson, HardwareSerial* com) { - int i = 0; sprintf(toprint, "\nPerson is called \"%s\" and has the filename \"%s\".\n", aperson->name, aperson->file_name); - if(debug)Serial.write(toprint); + com->write(toprint); sprintf(toprint, "rfid_uuid: %s\n", aperson->rfid_uuid); - if(debug)Serial.write(toprint); + com->write(toprint); sprintf(toprint, "finger_uuid: %u\n", aperson->finger_uuid); - if(debug)Serial.print(toprint); + com->print(toprint); sprintf(toprint, "credits_left: %u\n", aperson->credits_left); - if(debug)Serial.print(toprint); + com->print(toprint); - for (i = 0; i < box->maxDrink; i++) { + for (int i = 0; i < box->maxDrink; i++) { if (aperson->drinks_taken[i] != -1) { sprintf(toprint, "\nDrink: %s\n", box->drinks[i].name); - if(debug)Serial.write(toprint); + com->write(toprint); sprintf(toprint, "Drink %d: %d\n", i, aperson->drinks_taken[i]); - if(debug)Serial.write(toprint); + com->write(toprint); sprintf(toprint, "Total: %d.%02d Euro\n", aperson->drinks_taken[i]*box->drinks[i].price / 100, (aperson->drinks_taken[i]*box->drinks[i].price) % 100); - if(debug)Serial.write(toprint); + com->write(toprint); } } @@ -368,7 +373,7 @@ Person* readAllPersons () { File dir = SD.open("/"); dir.rewindDirectory(); - personCount = 0; + box->personCount = 0; //count the valid persons: while (true) { File entry = dir.openNextFile(); @@ -376,9 +381,9 @@ Person* readAllPersons () { break; } if (!entry.isDirectory()) { - if (strcmp(strlwr(entry.name() + (strlen(entry.name()) - 4)), ".per")==0) + if ((strcmp(strlwr(entry.name() + (strlen(entry.name()) - 4)), ".per")==0)&&(entry.name()[0]=='P')) { - personCount++; //Yeah it is a person! + box->personCount++; //Yeah it is a person! } @@ -387,8 +392,8 @@ Person* readAllPersons () { } //now store them //if(debug)Serial.print("count:"); - //if(debug)Serial.print(personCount); - Person* readPersons = new Person[personCount]; + //if(debug)Serial.print(box->personCount); + Person* readPersons = new Person[box->personCount]; int counter = 0; dir.rewindDirectory(); while (true) { @@ -398,7 +403,7 @@ Person* readAllPersons () { } if (!entry.isDirectory()) { int8_t len = strlen(entry.name()); - if (strstr(strlwr(entry.name() + (len - 4)), ".per")) + if ((strstr(strlwr(entry.name() + (len - 4)), ".per"))&&(entry.name()[0]=='P')) { //Yeah it is a person! //if(debug)Serial.print("Person: "); @@ -411,7 +416,7 @@ Person* readAllPersons () { entry.close(); } dir.close(); - if(debug)Serial.println("Read all!"); + if(debug)Serial.println("Read all Persons!"); return readPersons; } diff --git a/bluetooth.ino b/bluetooth.ino index f2ef3c4..9b87592 100644 --- a/bluetooth.ino +++ b/bluetooth.ino @@ -1,123 +1,243 @@ -void bluetoothCommands(){ -if (Serial3.available() > 0) { +void bluetoothCommands() { + if (Serial3.available() > 0) { char command = Serial3.read(); char subCommand = Serial3.read(); - - if (command == '#') { //1 General Info Command - if(subCommand == '1'){ // Get All Drinks - char incomingByte = Serial3.read(); // ignore ; - - - } else if(subCommand == '2'){ // Get All filenames - char incomingByte = Serial3.read(); // ignore ; - //while - //filenames_JSON(box,person) - + char incomingByte = Serial3.read(); + String data; + while (incomingByte != ';') { + data = String(data + incomingByte); + incomingByte = Serial3.read(); + } + + if (command == '#') { //1 General Info Command + if (subCommand == '1') { // Get All Drinks + print_drinks_JSON(box, &Serial3); + } else if (subCommand == '2') { // Get All persons + Serial3.print("{\"persons\":["); + for (int i = 0; i < box->personCount; i++) { + if (i != 0)Serial3.print(","); + print_person_JSON(box, &persons[i], &Serial3); + } + Serial3.println("]}"); } } else if (command == '!') { //2 User Managment Command - if(subCommand == '1'){ // Create new User - - - char incomingByte = Serial3.read(); - String personName; - - while (incomingByte != ';'){ - personName = String(personName + incomingByte); - incomingByte = Serial3.read(); - } - if(debug)Serial.println(personName); - lcd.clear(); - lcd.print(personName); - if(debug)Serial.println(box->maxID); - //lcd.setCursor(0,1); - //lcd.print("Fin pls"); - int fingerReturn = -1; - while (fingerReturn != 0) - { - fingerReturn = getFingerprintEnroll(box->maxID + 1); - } - lcd.clear(); - lcd.print(personName); - lcd.setCursor(0, 1); - lcd.print("RFID pls"); + if (subCommand == '1') { // Create new User + char incomingByte = Serial3.read(); + String personName = data; - zugang = ""; - while (zugang == "") { - getRFIDEnroll(); - } - if(debug)Serial.println(zugang); - box->maxID++; - Person* newPerson = new Person; - personName.toCharArray(newPerson->name, personName.length()+1); - char tmp[FILE_NAME_LEN + 1]; - sprintf(tmp,"p%03d%s",box->maxID,FILE_EXTENSION); - strcpy(newPerson->file_name, tmp); - zugang.toCharArray(newPerson->rfid_uuid, zugang.length()+1); - newPerson->finger_uuid = box->maxID; - newPerson->credits_left = 1000; - for (int i = 0; i < box->maxDrink; i++) { - newPerson->drinks_taken[i]=0; - } - write_pers_file(newPerson); + if (debug)Serial.println(personName); + lcd.clear(); + lcd.print(personName); + if (debug)Serial.println(box->maxID); + //lcd.setCursor(0,1); + //lcd.print("Fin pls"); + int fingerReturn = -1; + while (fingerReturn != 0) + { + fingerReturn = getFingerprintEnroll(box->maxID + 1); + } + lcd.clear(); + lcd.print(personName); + lcd.setCursor(0, 1); + lcd.print("RFID pls"); + + zugang = ""; + while (zugang == "") { + getRFIDEnroll(); + } + if (debug)Serial.println(zugang); + box->maxID++; + Person* newPerson = new Person; + personName.toCharArray(newPerson->name, personName.length() + 1); + char tmp[FILE_NAME_LEN + 1]; + sprintf(tmp, "p%03d%s", box->maxID, FILE_EXTENSION); + strcpy(newPerson->file_name, tmp); + zugang.toCharArray(newPerson->rfid_uuid, zugang.length() + 1); + newPerson->finger_uuid = box->maxID; + newPerson->credits_left = 1000; + for (int i = 0; i < box->maxDrink; i++) { + newPerson->drinks_taken[i] = 0; + } + write_pers_file(newPerson); - zugang = ""; // reset here - lcd.clear(); - lcd.print(personName); - lcd.setCursor(0, 1); - lcd.print("Sucess!"); - delay(1000); - //and revert to normal: - lcd.clear(); - lcd.print("Drink:"); - lcd.setCursor(0, 1); - lcd.print(box->drinks[currentDrink].name); + zugang = ""; // reset here + lcd.clear(); + lcd.print(personName); + lcd.setCursor(0, 1); + lcd.print("Sucess!"); + delay(1000); + //and revert to normal: + lcd.clear(); + lcd.print("Drink:"); + lcd.setCursor(0, 1); + lcd.print(box->drinks[currentDrink].name); }//new User - else if(subCommand == '2'){ //get user by filename - - }//get user - else if(subCommand == '3'){ //trigger user scan for ID + else if (subCommand == '2') { //get user by filename + + char filename[data.length() + 1]; + Serial.println(data); + data.toCharArray(filename, data.length() + 1); + if (personExists(filename)) { + Person* aperson = &persons[getFileIndex(filename)]; + print_person_JSON(box, aperson, &Serial3); + } else { + Serial3.println("{\"Error\":\"no such file\"}"); + } + + }//get user + else if (subCommand == '3') { //trigger user scan + lcd.clear(); + lcd.print("Scanning"); + lcd.setCursor(0, 1); + lcd.print("..."); + box->scanning = true; + while (box->scanning) { + checkFinger(); + checkRFID(); + } + //After scan reset display + lcd.clear(); + lcd.print("Drink:"); + lcd.setCursor(0, 1); + lcd.print(box->drinks[currentDrink].name); - //return filename - }//user scan - else if(subCommand == '4'){ //revoke user finger by filename - + else if (subCommand == '4') { //revoke user finger by filename + char filename[data.length() + 1]; + Serial.println(data); + data.toCharArray(filename, data.length() + 1); + if (personExists(filename)) { + Person* changePerson = &persons[getFileIndex(filename)]; + + lcd.clear(); + lcd.print(changePerson->name); + int fingerReturn = -1; + while (fingerReturn != 0) + { + fingerReturn = getFingerprintEnroll(changePerson->finger_uuid); + } + Serial3.println("{\"Success\"}"); + //No need to save finger since id is not changed in the process + } else { + Serial3.println("{\"Error\":\"no such file\"}"); + } + //reset display + lcd.clear(); + lcd.print("Drink:"); + lcd.setCursor(0, 1); + lcd.print(box->drinks[currentDrink].name); + }//revoke finger - else if(subCommand == '5'){ //revoke user rfid by filename - + else if (subCommand == '5') { //revoke user rfid by filename + char filename[data.length() + 1]; + Serial.println(data); + data.toCharArray(filename, data.length() + 1); + if (personExists(filename)) { + lastPersonIndex = getFileIndex(filename); + Person* changePerson = &persons[lastPersonIndex]; + lcd.clear(); + lcd.print(changePerson->name); + lcd.setCursor(0, 1); + lcd.print("RFID pls"); + + zugang = ""; + while (zugang == "") { + getRFIDEnroll(); + } + if (debug)Serial.println(zugang); + //save zugang + zugang.toCharArray(changePerson->rfid_uuid, zugang.length() + 1); + //lastPersonIndex is set in the beginning + update_pers_file(); + Serial3.println("{\"Success\"}"); + //reset display + lcd.clear(); + lcd.print("Drink:"); + lcd.setCursor(0, 1); + lcd.print(box->drinks[currentDrink].name); + + } else { + Serial3.println("{\"Error\":\"no such file\"}"); + } }//revoke rfid - else if(subCommand == '6'){ //delete user by filename - + else if (subCommand == '6') { //delete user by filename + char filename[data.length() + 1]; + Serial.println(data); + data.toCharArray(filename, data.length() + 1); + if (personExists(filename)) { + Serial3.println("{\"Error\":\"not implemented yet\"}"); + } else { + Serial3.println("{\"Error\":\"no such file\"}"); + } }//delete user - else if(subCommand == '6'){ //set user credits - + else if (subCommand == '7') { //set user credits + char filename[data.length() + 1]; + Serial.println(data); + data.toCharArray(filename, data.length() + 1); + if (personExists(filename)) { + //Scann second time for credits + char incomingByte = Serial3.read(); + String data2; + while (incomingByte != ';') { + data2 = String(data2 + incomingByte); + incomingByte = Serial3.read(); + } + char credits_string[data2.length() + 1]; + data2.toCharArray(credits_string, data2.length() + 1); + lastPersonIndex = getFileIndex(filename); + Person* setPerson = &persons[lastPersonIndex]; + setPerson->credits_left = atoi(credits_string); + update_pers_file(); + print_person_JSON(box,setPerson,&Serial3); + + } else { + Serial3.println("{\"Error\":\"no such file\"}"); + } + }//set credits - else if(subCommand == '6'){ //set name by filename - + else if (subCommand == '8') { //set name by filename + char filename[data.length() + 1]; + Serial.println(data); + data.toCharArray(filename, data.length() + 1); + if (personExists(filename)) { + Serial3.println("{\"Error\":\"not implemented yet\"}"); + } else { + Serial3.println("{\"Error\":\"no such file\"}"); + } }//set name } // User Managment Command } //BLE available - }//end function +}//end function +void print_person_JSON(Beerbox *box, Person *aperson, HardwareSerial* com) { - - - void filenames_JSON(Beerbox *box, Person *aperson) { - - int i = 0; - sprintf(toprint, "{\"name\": \"%s\",\"filename\":\"%s\",\"creditsleft\":\"%s\",\"drinks\":[", aperson->name, aperson->file_name, aperson->credits_left); - Serial.write(toprint); - for (i = 0; i < box->maxDrink; i++) { + sprintf(toprint, "{\"name\": \"%s\",\"filename\":\"%s\",\"creditsleft\":\"%i\",\"drinks\":[", aperson->name, aperson->file_name, aperson->credits_left); + com->write(toprint); + for (int i = 0; i < box->maxDrink; i++) { if (aperson->drinks_taken[i] != -1) { - if(i=0) sprintf(toprint, "{\"%s\":\"%s\"}", box->drinks[i].name,aperson->drinks_taken[i]); - else sprintf(toprint, ",{\"%s\":\"%s\"}", box->drinks[i].name,aperson->drinks_taken[i]); - Serial.write(toprint); + if (i == 0) sprintf(toprint, "{\"%s\":\"%i\"}", box->drinks[i].name, aperson->drinks_taken[i]); + else sprintf(toprint, ",{\"%s\":\"%i\"}", box->drinks[i].name, aperson->drinks_taken[i]); + com->write(toprint); } } - Serial.write("]}"); + com->println("]}"); + box->scanning = false; } // end print JSON + +void print_drinks_JSON(Beerbox *box, HardwareSerial* com) { + com->print("{\"drinks\":["); + for (int i = 0; i < box->maxDrink; i++) { + if (box->drinks[i].price != -1) { + if (i == 0) sprintf(toprint, "{\"id\":\"%i\",\"name\":\"%s\",\"price\":\"%i\"}", i, box->drinks[i].name, box->drinks[i].price); + else sprintf(toprint, ",{\"id\":\"%i\",\"name\":\"%s\",\"price\":\"%i\"}", i, box->drinks[i].name, box->drinks[i].price); + com->write(toprint); + } + } + com->print("]}"); + +} diff --git a/inputs.ino b/inputs.ino index 3412432..0e76c81 100644 --- a/inputs.ino +++ b/inputs.ino @@ -1,5 +1,3 @@ - - void checkButton() { //one change per click to make seleting drinks easier for the drunken //if (digitalRead(2) != lastButtonState) { @@ -66,11 +64,13 @@ bool checkFinger() { if (getFingerIndex(finger.fingerID) != -1) { if(debug)Serial.print("This is "); if(debug)Serial.println(persons[getFingerIndex(finger.fingerID)].name); - countUp(getFingerIndex(finger.fingerID), 0);//for finger context + if(!box->scanning) countUp(getFingerIndex(finger.fingerID), 0);//for finger context + else print_person_JSON(box,&persons[getFingerIndex(finger.fingerID)],&Serial3); + } } -void checkRFID() { +bool checkRFID() { Serial1.flush(); if (Serial1.available() >= gesamtKennungLaenge) // wenn genug Zeichen eingegangen ... { @@ -99,10 +99,13 @@ void checkRFID() { //if(debug)Serial.print("This is "); //if(debug)Serial.println(persons[getRFIDIndex(persons, code)].name); - if(getRFIDIndex( code)!=-1) + if(getRFIDIndex( code)!=-1){ + if(!box->scanning) countUp(getRFIDIndex( code), 1); // 1 for rfid context - else + else print_person_JSON(box,&persons[getRFIDIndex( code)],&Serial3); + }else{ if(debug)Serial.println("Error: Nonexisting Card"); + } zugang = ""; delay(1000); // debounce time @@ -123,7 +126,7 @@ void serial1_flush_buffer() //Funktion schreiben, welche ein Array vom struct Person (Beliebige Groesse) sowie eine rfid-kennung uebernimmt und den Index der entsprechenden Person im array (beginnend bei 0)zurueckgiebt. int getRFIDIndex( char* rfid) { - for (int i = 0; i < personCount; i++) { + for (int i = 0; i < box->personCount; i++) { if (!strcmp(persons[i].rfid_uuid, rfid)) { return i; } @@ -133,7 +136,7 @@ int getRFIDIndex( char* rfid) { } int getFingerIndex( uint8_t fingerID) { - for (int i = 0; i < personCount; i++) { + for (int i = 0; i < box->personCount; i++) { if (persons[i].finger_uuid == fingerID) { return i; } @@ -141,13 +144,24 @@ int getFingerIndex( uint8_t fingerID) { return -1; } +int getFileIndex( char* filename) { + for (int i = 0; i < box->personCount; i++) { + if(!strcmp(persons[i].file_name,filename)) { + return i; + } + } + return -1; +} + void countUp(int personIndex, int context) { lcd.clear(); lcd.print(persons[personIndex].name); lcd.setCursor(0, 1); persons[personIndex].drinks_taken[currentDrink]++; - lcd.print(persons[personIndex].drinks_taken[currentDrink]); + //now care about credits + persons[personIndex].credits_left = persons[personIndex].credits_left - box->drinks[currentDrink].price ; + lcd.print(persons[personIndex].drinks_taken[currentDrink]); // Todo this can be changed with the credits left later... lcd.print(" Ok? "); lcd.setCursor(7, 1); lcd.print("3");