forked from Wien60Pioneers/beerbox
Added Support for Dataextration via Serial
This commit is contained in:
parent
0ff99e00f3
commit
531b4c847f
13
beerbox.ino
13
beerbox.ino
@ -102,8 +102,9 @@ void loop() {
|
||||
|
||||
boxTimer();
|
||||
ledTimer();
|
||||
|
||||
|
||||
bluetoothCommands();
|
||||
serialCom();
|
||||
|
||||
}//end loop
|
||||
|
||||
@ -382,12 +383,12 @@ Person* readAllPersons () {
|
||||
break;
|
||||
}
|
||||
if (!entry.isDirectory()) {
|
||||
if ((strcmp(strlwr(entry.name() + (strlen(entry.name()) - 4)), ".per")==0)&&(entry.name()[0]=='P'))
|
||||
if ((strcmp(strlwr(entry.name() + (strlen(entry.name()) - 4)), ".per")==0)&&(entry.name()[0]=='P'))
|
||||
{
|
||||
box->personCount++; //Yeah it is a person!
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
entry.close();
|
||||
}
|
||||
@ -420,7 +421,3 @@ Person* readAllPersons () {
|
||||
if(debug)Serial.println("Read all Persons!");
|
||||
return readPersons;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
248
bluetooth.ino
248
bluetooth.ino
@ -243,6 +243,250 @@ void bluetoothCommands() {
|
||||
|
||||
}//end function
|
||||
|
||||
void serialCom() {
|
||||
if (Serial.available() > 0) {
|
||||
char command = Serial.read();
|
||||
char subCommand = Serial.read();
|
||||
|
||||
char incomingByte = Serial.read();
|
||||
String data;
|
||||
while (incomingByte != ';') {
|
||||
data = String(data + incomingByte);
|
||||
incomingByte = Serial.read();
|
||||
}
|
||||
|
||||
if (command == '#') { //1 General Info Command
|
||||
if (subCommand == '1') { // Get All Drinks
|
||||
print_drinks_JSON(box, &Serial);
|
||||
} else if (subCommand == '2') { // Get All persons
|
||||
Serial.print("{\"persons\":[");
|
||||
for (int i = 0; i < box->personCount; i++) {
|
||||
if (i != 0)Serial.print(",");
|
||||
print_person_JSON(box, &persons[i], &Serial, true);
|
||||
print_person_JSON(box, &persons[i], &Serial, false);
|
||||
delay(100);
|
||||
}
|
||||
|
||||
Serial.println("]}");
|
||||
}
|
||||
else if (subCommand == '3') { //reset all counts
|
||||
|
||||
for (int i = 0; i < box->personCount; i++) {
|
||||
reset_person_drinks(box, &persons[i], i);
|
||||
print_person_JSON(box, &persons[i], &Serial, false);
|
||||
}
|
||||
Serial.println("{\"Success\"}");
|
||||
}
|
||||
} else if (command == '!') { //2 User Managment Command
|
||||
if (subCommand == '1') { // Create new User
|
||||
char incomingByte = Serial.read();
|
||||
String personName = data;
|
||||
|
||||
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);
|
||||
}//new User
|
||||
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, &Serial, false);
|
||||
} else {
|
||||
Serial.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);
|
||||
|
||||
}//user scan
|
||||
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);
|
||||
}
|
||||
Serial.println("{\"Success\"}");
|
||||
//No need to save finger since id is not changed in the process
|
||||
} else {
|
||||
Serial.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
|
||||
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();
|
||||
Serial.println("{\"Success\"}");
|
||||
//reset display
|
||||
lcd.clear();
|
||||
lcd.print("Drink:");
|
||||
lcd.setCursor(0, 1);
|
||||
lcd.print(box->drinks[currentDrink].name);
|
||||
|
||||
} else {
|
||||
Serial.println("{\"Error\":\"no such file\"}");
|
||||
}
|
||||
}//revoke rfid
|
||||
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)) {
|
||||
//Delete file
|
||||
SD.remove(filename);
|
||||
//rescan it!
|
||||
delete [] persons; // When done, free memory pointed to
|
||||
persons = NULL;
|
||||
persons = readAllPersons();
|
||||
if (debug)Serial.println(sizeof(persons));
|
||||
} else {
|
||||
Serial.println("{\"Error\":\"no such file\"}");
|
||||
}
|
||||
}//delete user
|
||||
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 = Serial.read();
|
||||
String data2;
|
||||
while (incomingByte != ';') {
|
||||
data2 = String(data2 + incomingByte);
|
||||
incomingByte = Serial.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, &Serial, false);
|
||||
|
||||
} else {
|
||||
Serial.println("{\"Error\":\"no such file\"}");
|
||||
}
|
||||
|
||||
}//set credits
|
||||
else if (subCommand == '8') { //set name by filename
|
||||
//TODO: Better namechecking
|
||||
char filename[data.length() + 1];
|
||||
Serial.println(data);
|
||||
data.toCharArray(filename, data.length() + 1);
|
||||
if (personExists(filename)) {
|
||||
//Scann second time for newName
|
||||
char incomingByte = Serial.read();
|
||||
String data2;
|
||||
while (incomingByte != ';') {
|
||||
data2 = String(data2 + incomingByte);
|
||||
incomingByte = Serial.read();
|
||||
}
|
||||
char name_string[11];
|
||||
data2.toCharArray(name_string, data2.length() + 1);
|
||||
lastPersonIndex = getFileIndex(filename);
|
||||
Person* setPerson = &persons[lastPersonIndex];
|
||||
strcpy(setPerson->name, name_string);
|
||||
update_pers_file();
|
||||
print_person_JSON(box, setPerson, &Serial, false);
|
||||
} else {
|
||||
Serial.println("{\"Error\":\"no such file\"}");
|
||||
}
|
||||
}//set name
|
||||
} // User Managment Command
|
||||
} //BLE available
|
||||
|
||||
}//end function
|
||||
|
||||
|
||||
void print_person_JSON(Beerbox *box, Person *aperson, HardwareSerial* com, bool less) {
|
||||
@ -251,7 +495,7 @@ void print_person_JSON(Beerbox *box, Person *aperson, HardwareSerial* com, bool
|
||||
sprintf(toprint, "{\"name\": \"%s\",\"filename\":\"%s\"}", aperson->name, aperson->file_name);
|
||||
com->write(toprint);
|
||||
} else {
|
||||
|
||||
|
||||
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++) {
|
||||
@ -287,5 +531,5 @@ void reset_person_drinks(Beerbox *box,Person *aperson,int personIndex){
|
||||
update_pers_file();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user