diff --git a/bb.con b/bb.con new file mode 100755 index 0000000..f565c49 --- /dev/null +++ b/bb.con @@ -0,0 +1,8 @@ +drink number 0: Bier +price: 100 + +drink number 1: Wein +price: 100 + +drink number 2: AntiAlk +price: 050 diff --git a/beerbox.h b/beerbox.h index 87c8d4f..c83bc70 100644 --- a/beerbox.h +++ b/beerbox.h @@ -6,6 +6,7 @@ #define FILE_EXTENSION ".per" #define NAME_LEN 10 +#define led 4 //######### RFID CONSTANTS ########## const int startZeichen = 02; // Chip-Kennung beginnt mit 02 diff --git a/beerbox.ino b/beerbox.ino index a3a0ecf..592956d 100644 --- a/beerbox.ino +++ b/beerbox.ino @@ -34,6 +34,7 @@ String rfID; unsigned long oldTime = 0; +unsigned long ledTime = 0; void setup() { @@ -44,6 +45,10 @@ void setup() { lcd.init(); pinMode(2, INPUT_PULLUP); + pinMode(3, OUTPUT); + pinMode(led, OUTPUT); + //tone(3,440,5000); + //Fingerprint init finger.begin(57600); if (finger.verifyPassword()) { @@ -86,7 +91,9 @@ void setup() { currentDrink = 0; box->waiting = false; - + //turnOn led + digitalWrite(led,HIGH); + ledTime = millis(); } @@ -98,6 +105,7 @@ void loop() { checkButton(); boxTimer(); + ledTimer(); @@ -249,8 +257,8 @@ Person read_person(Beerbox *box, char *filename) { //giving the full filename here, since it makes stuff easier - Serial.print("The filename is: "); - Serial.println(filename); + //Serial.print("The filename is: "); + //Serial.println(filename); read = SD.open(filename, FILE_READ); if (read == false) { @@ -278,21 +286,21 @@ Person read_person(Beerbox *box, char *filename) { Serial.println(aperson.rfid_uuid); read_line_from_file(read, tmp, sizeof(tmp)); sscanf(tmp, "finger_uuid: %d", &aperson.finger_uuid); - Serial.println(aperson.finger_uuid); + //Serial.println(aperson.finger_uuid); //credits_left read_line_from_file(read, tmp, sizeof(tmp)); sscanf(tmp, "credits_left: %d", &aperson.credits_left); - Serial.println(aperson.credits_left); + //Serial.println(aperson.credits_left); while (read_line_from_file(read, tmp, sizeof(tmp)), strlen(tmp) > 0) { int tmpCount = 0; int tmpDrink = 0; sscanf(tmp, "drink_Count_%d: %d", &tmpDrink, &tmpCount); if (strlen(tmp)) { - Serial.print("drink: "); - Serial.print(tmpDrink); - Serial.print(" count: "); - Serial.println(tmpCount); + //Serial.print("drink: "); + //Serial.print(tmpDrink); + //Serial.print(" count: "); + //Serial.println(tmpCount); aperson.drinks_taken[tmpDrink] = tmpCount; } } @@ -436,7 +444,6 @@ Person* readAllPersons () { if (!entry.isDirectory()) { if (strcmp(strlwr(entry.name() + (strlen(entry.name()) - 4)), ".per")==0) { - Serial.println("Yea"); personCount++; //Yeah it is a person! } @@ -445,8 +452,8 @@ Person* readAllPersons () { entry.close(); } //now store them - Serial.print("count:"); - Serial.print(personCount); + //Serial.print("count:"); + //Serial.print(personCount); Person* readPersons = new Person[personCount]; int counter = 0; dir.rewindDirectory(); @@ -460,8 +467,8 @@ Person* readAllPersons () { if (strstr(strlwr(entry.name() + (len - 4)), ".per")) { //Yeah it is a person! - Serial.print("Person: "); - Serial.println(entry.name()); + //Serial.print("Person: "); + //Serial.println(entry.name()); Person thisPerson = read_person(box, entry.name()); readPersons[counter] = thisPerson; counter++; diff --git a/enroll.ino b/enroll.ino new file mode 100644 index 0000000..b41fe1e --- /dev/null +++ b/enroll.ino @@ -0,0 +1,276 @@ +uint8_t getFingerprintEnroll(int id) { + lcd.setCursor(0, 1); + lcd.print("Fin pls"); + + int p = -1; + while (p != FINGERPRINT_OK) { + p = finger.getImage(); + switch (p) { + case FINGERPRINT_OK: + Serial.println("Image taken"); + lcd.clear(); + lcd.print("Image 1"); + lcd.setCursor(0, 1); + lcd.print("Done"); + break; + case FINGERPRINT_NOFINGER: + Serial.println("."); + break; + case FINGERPRINT_PACKETRECIEVEERR: + Serial.println("Communication error"); + lcd.clear(); + lcd.print("Error"); + break; + case FINGERPRINT_IMAGEFAIL: + Serial.println("Imaging error"); + lcd.clear(); + lcd.print("Error"); + break; + default: + Serial.println("Unknown error"); + lcd.clear(); + lcd.print("Error"); + break; + } + } + + // OK success! + + p = finger.image2Tz(1); + switch (p) { + case FINGERPRINT_OK: + Serial.println("Image converted"); + break; + case FINGERPRINT_IMAGEMESS: + Serial.println("Image too messy"); + return p; + case FINGERPRINT_PACKETRECIEVEERR: + Serial.println("Communication error"); + return p; + case FINGERPRINT_FEATUREFAIL: + Serial.println("Could not find fingerprint features"); + return p; + case FINGERPRINT_INVALIDIMAGE: + Serial.println("Could not find fingerprint features"); + return p; + default: + Serial.println("Unknown error"); + return p; + } + + //Serial.println("Remove finger"); + lcd.clear(); + lcd.print("Remove"); + lcd.setCursor(0, 1); + lcd.print("Finger"); + delay(2000); + p = 0; + while (p != FINGERPRINT_NOFINGER) { + p = finger.getImage(); + } + + p = -1; + lcd.clear(); + lcd.print("Second"); + lcd.setCursor(0, 1); + lcd.print("time pls"); + Serial.println("Place same finger again"); + while (p != FINGERPRINT_OK) { + p = finger.getImage(); + switch (p) { + case FINGERPRINT_OK: + Serial.println("Image taken"); + break; + case FINGERPRINT_NOFINGER: + Serial.print("."); + break; + case FINGERPRINT_PACKETRECIEVEERR: + Serial.println("Communication error"); + lcd.clear(); + lcd.print("Error"); + break; + case FINGERPRINT_IMAGEFAIL: + Serial.println("Imaging error"); + lcd.clear(); + lcd.print("Error"); + break; + default: + Serial.println("Unknown error"); + lcd.clear(); + lcd.print("Error"); + break; + } + } + + // OK success! + + p = finger.image2Tz(2); + switch (p) { + case FINGERPRINT_OK: + Serial.println("Image converted"); + break; + case FINGERPRINT_IMAGEMESS: + Serial.println("Image too messy"); + return p; + case FINGERPRINT_PACKETRECIEVEERR: + Serial.println("Communication error"); + lcd.clear(); + lcd.print("Error"); + return p; + case FINGERPRINT_FEATUREFAIL: + Serial.println("Could not find fingerprint features"); + lcd.clear(); + lcd.print("Error"); + return p; + case FINGERPRINT_INVALIDIMAGE: + Serial.println("Could not find fingerprint features"); + lcd.clear(); + lcd.print("Error"); + return p; + default: + Serial.println("Unknown error"); + lcd.clear(); + lcd.print("Error"); + return p; + } + + + // OK converted! + p = finger.createModel(); + if (p == FINGERPRINT_OK) { + Serial.println("Prints matched!"); + lcd.clear(); + lcd.print("Match!"); + } else if (p == FINGERPRINT_PACKETRECIEVEERR) { + Serial.println("Communication error"); + return p; + } else if (p == FINGERPRINT_ENROLLMISMATCH) { + Serial.println("Fingerprints did not match"); + lcd.clear(); + lcd.print("Error"); + lcd.setCursor(0, 1); + lcd.print("mismatch"); + delay(1000); + return p; + } else { + Serial.println("Unknown error"); + return p; + } + + Serial.print("ID "); Serial.println(id); + p = finger.storeModel(id); + if (p == FINGERPRINT_OK) { + Serial.println("Stored!"); + } else if (p == FINGERPRINT_PACKETRECIEVEERR) { + Serial.println("Communication error"); + return p; + } else if (p == FINGERPRINT_BADLOCATION) { + Serial.println("Could not store in that location"); + return p; + } else if (p == FINGERPRINT_FLASHERR) { + Serial.println("Error writing to flash"); + return p; + } else { + Serial.println("Unknown error"); + return p; + } + return 0; +} + + +void getRFIDEnroll() { + Serial1.flush(); + if (Serial1.available() >= gesamtKennungLaenge) // wenn genug Zeichen eingegangen ... + { + if (Serial1.read() == startZeichen) // und das Startzeichen erkannt wurde ... + { + //Serial.print("Start-Byte erkannt ... "); + bytesGelesen = 0; // starten Lesen, also Zaehler auf 0 + while (bytesGelesen < kennungLaenge) // lese 12-stelligen Code + { + char zeichen = Serial1.read(); // lesen Zeichen + //Serial.print(zeichen); // zur Kontrolle + //Serial.print(" "); + if (zeichen == endeZeichen) // suche nach Ende-Befehl + { + //Serial.println("CR erkannt ..."); + break; + } + code[bytesGelesen] = zeichen; // speicher Zeichen im Puffer + bytesGelesen = bytesGelesen + 1; // ein Zeichen weiter + } + code[bytesGelesen] = 0; // beende Zeichenkette + Serial.print("RFID-Code lautet: "); + Serial.println(code); + zugang = code; //char[] to string + + //already existing + if (getRFIDIndex(code) != -1) { + Serial.print("Error: "); + Serial.print("This is "); + Serial.println(persons[getRFIDIndex(code)].name); + lcd.setCursor(0, 1); + lcd.print("Error! "); + + zugang = ""; //this time reset first! + delay(1000); // debounce time + serial1_flush_buffer(); + } + else { + // do not reset zugang + delay(1000); // debounce time + serial1_flush_buffer(); + } + + + + } + } + +} + + + +int write_pers_file(Person* thisPerson) { + Serial.println("Writing Person:"); + File write; + char tmp[25]; + + int i = 0; + //Serial.println(thisPerson.file_name); + write = SD.open(thisPerson->file_name, FILE_WRITE); + +// write.seek(0); + + + sprintf(tmp, "%s\n", thisPerson->name); + Serial.print(tmp); + write.write(tmp, strlen(tmp)); + sprintf(tmp, "rfid_uuid: %s\n", thisPerson->rfid_uuid); + Serial.print(tmp); + write.write(tmp, strlen(tmp)); + sprintf(tmp, "finger_uuid: %u\n", thisPerson->finger_uuid); + Serial.print(tmp); + write.write(tmp, strlen(tmp)); + sprintf(tmp, "credits_left: %u\n", thisPerson->credits_left); + write.write(tmp, strlen(tmp)); + + for (i = 0; i < drinksMax; i++) { + if (thisPerson->drinks_taken[i] != -1) { + sprintf(tmp, "\ndrink_Count_%d: %d\n", i, thisPerson->drinks_taken[i]); + write.write(tmp, strlen(tmp)); + Serial.println(tmp); + } + } + + + write.close(); + + //rescan it! + delete [] persons; // When done, free memory pointed to + persons = NULL; + persons = readAllPersons(); + Serial.println(sizeof(persons)); + return 0; + +} diff --git a/inputs.ino b/inputs.ino new file mode 100644 index 0000000..a718155 --- /dev/null +++ b/inputs.ino @@ -0,0 +1,202 @@ + + +void checkButton() { + //one change per click to make seleting drinks easier for the drunken + //if (digitalRead(2) != lastButtonState) { + //delayMicroseconds(50); //debounce delay + if (digitalRead(2) != lastButtonState) { + //ok we have a change! + if (digitalRead(2) == LOW) { + //pushed + digitalWrite(led,HIGH); + ledTime = millis(); + if (box->waiting == 0) { + if (currentDrink == drinksMax - 1) currentDrink = -1; + currentDrink++; + Serial.println(box->drinks[currentDrink].name); + lcd.clear(); + lcd.print("Drink:"); + lcd.setCursor(0, 1); + lcd.print(box->drinks[currentDrink].name); + } + else + { + //revert save + persons[lastPersonIndex].drinks_taken[currentDrink]--; + box->waiting = 0; + lcd.clear(); + lcd.print("Abort!"); + delay(500); + //reset to default + lcd.clear(); + lcd.print("Drink:"); + lcd.setCursor(0, 1); + lcd.print(box->drinks[currentDrink].name); + } + + } + else {//released + } + lastButtonState = digitalRead(2); + } + //} +} + + +bool checkFinger() { + + uint8_t p = finger.getImage(); + if (p != FINGERPRINT_OK) return -1; + + p = finger.image2Tz(); + if (p != FINGERPRINT_OK) return -1; + + digitalWrite(led,HIGH); + ledTime = millis(); + + p = finger.fingerFastSearch(); + if (p != FINGERPRINT_OK) return -1; + + // found a match! + // Serial.print("Found ID #"); Serial.print(finger.fingerID); + // Serial.print(" with confidence of "); Serial.println(finger.confidence); + + //ok now check which person that is! + + if (getFingerIndex(finger.fingerID) != -1) { + Serial.print("This is "); + Serial.println(persons[getFingerIndex(finger.fingerID)].name); + countUp(getFingerIndex(finger.fingerID), 0);//for finger context + } +} + +void checkRFID() { + Serial1.flush(); + if (Serial1.available() >= gesamtKennungLaenge) // wenn genug Zeichen eingegangen ... + { + if (Serial1.read() == startZeichen) // und das Startzeichen erkannt wurde ... + { + //Serial.print("Start-Byte erkannt ... "); + bytesGelesen = 0; // starten Lesen, also Zaehler auf 0 + while (bytesGelesen < kennungLaenge) // lese 12-stelligen Code + { + char zeichen = Serial1.read(); // lesen Zeichen + //Serial.print(zeichen); // zur Kontrolle + //Serial.print(" "); + if (zeichen == endeZeichen) // suche nach Ende-Befehl + { + //Serial.println("CR erkannt ..."); + break; + } + code[bytesGelesen] = zeichen; // speicher Zeichen im Puffer + bytesGelesen = bytesGelesen + 1; // ein Zeichen weiter + } + code[bytesGelesen] = 0; // beende Zeichenkette + Serial.print("RFID-Code lautet: "); + Serial.println(code); + digitalWrite(led,HIGH); + ledTime = millis(); + + //Serial.print("This is "); + //Serial.println(persons[getRFIDIndex(persons, code)].name); + if(getRFIDIndex( code)!=-1) + countUp(getRFIDIndex( code), 1); // 1 for rfid context + else + Serial.println("Error: Nonexisting Card"); + + zugang = ""; + delay(1000); // debounce time + serial1_flush_buffer(); + } + } +} + +void serial1_flush_buffer() +{ + while (Serial1.read() >= 0) + ; // do nothing +} + + +// +//LB +//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++) { + if (!strcmp(persons[i].rfid_uuid, rfid)) { + return i; + } + } + return -1; + +} + +int getFingerIndex( uint8_t fingerID) { + for (int i = 0; i < personCount; i++) { + if (persons[i].finger_uuid == fingerID) { + 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]); + lcd.print(" Ok? "); + lcd.setCursor(7, 1); + lcd.print("3"); + lastPersonIndex = personIndex; + box->waiting = 3; + oldTime = millis(); +} + + +void boxTimer() { + if (box->waiting != 0) { + if ((millis() - oldTime) > 1000) + { + if (box->waiting == 3) { + lcd.setCursor(7, 1); + lcd.print("2"); + box->waiting = 2; + } + else if (box->waiting == 2) { + lcd.setCursor(7, 1); + lcd.print("1"); + box->waiting = 1; + } else if (box->waiting == 1) { + update_pers_file(); + lcd.clear(); + lcd.print("Saved!"); + delay(500); + //reset to default + lcd.clear(); + lcd.print("Drink:"); + lcd.setCursor(0, 1); + lcd.print(box->drinks[currentDrink].name); + box->waiting = 0; + digitalWrite(led,false); + } + oldTime = millis(); + } + } +} + + + +void ledTimer() { + if (ledTime != 0) { + if ((millis() - ledTime) > 5000) + { + digitalWrite(led,LOW); + ledTime = 0; + } + } +} +