ESP32 sends data at same place


attendace.aia (101.4 KB)
Hi, please help me. I want to send data from esp32 using bluetooth with command '1' will display the name and id in the string. and command '2' will display total number in the class.
i receive both data but it display in the same place in 'listabsent' and also the name&id is messy. help me to fix that.

Upload your esp32 code too.

ELE551_AttendanceSystem.ino (14.2 KB)

Yout .ino looks very comprehensive:

#include <Adafruit_Fingerprint.h>
#include <HardwareSerial.h>
#include <WiFi.h>
#include <LiquidCrystal_I2C.h>
#include <BluetoothSerial.h>

LiquidCrystal_I2C lcd(0x27, 24, 4);
BluetoothSerial SerialBT;

//----------------------------------------------------------------------

// Define the pins for the IN and OUT IR Sensor
const int increaseSensorPin = 33;
const int decreaseSensorPin = 26;

const int LEDgreen_PIN = 2;
const int BUZZER_PIN = 2;


int count1 = 0;
int totalCount = 0;


bool increaseFlag = false;
bool decreaseFlag = false;

//----------------------------------------------------------------------

String Event_Name = "fingerprint_data";

String Key = "cvi5wZXDq-lwutyDXF4ob_";

// Replace with your unique IFTTT URL resource
String resource = "/trigger/" + Event_Name + "/with/key/" + Key;

// Maker Webhooks IFTTT
const char* server = "maker.ifttt.com";

// Replace with your SSID and Password
const char* ssid     = "fardianaedrina👸🏻" ;
const char* password = "nana1234";

Adafruit_Fingerprint finger = Adafruit_Fingerprint(&Serial2);

//----------------------------------------------------------------------

// Define a structure to hold student information
struct Student {
  const char* name;
  const char* id;
};

//----------------------------------------------------------------------

// Declare an array to store information for each student from class M & T
Student students[] = {
  
  {"AHMAD HAIKAL BIN AHMAD FAKARUDDIN", "2022782807"},
  {"ARESSA SOFEA BINTI SHAHARUDIN", "2022908277"},
  {"CHE MUHAMMAD NAKHAIE BIN DRIS", "2022923409"},
  {"FARDIANA EDRINA BINTI MOHAMMAD SHAARI", "2022912843"},
  {"HANZALAH BIN MARUAN", "2020483308"},
  {"MOHAMMAD IQBAL RAFIQIN BIN MOHD ROZILAM", "2022907781"},
  {"MUHAMMAD LUQMAN AFIQ BIN MOHAMED LIAS", "2022923747"},
  {"NAWWARAH NABILAH BINTI MUHAMAD KHUSAINI", "2022912551"},
  {"NUR SYAHMINA BINTI SUHAIMI", "2022793767"},
  {"NURUL ATHIRAH BINTI MOHAMAD NAZRI", "2022765293"},
  {"SITI NURIAH BINTI YACOB", "2022745435"},

//----------------------------------------------------------------------
  
  {"ADAM AFFANDI BIN TAJUL ARIFFIN", "2022793627"},
  {"HARIS MURSHIDI BIN MAT ISA", "2022961967"},
  {"LUQMAN HAQEEM BIN ASYRAN RAOEF", "2022910375"},
  {"MUHAMMAD AMIRUL HAZIM BIN SHAHRIL", "2020847126"},
  {"MUHAMMAD HAKIMI AIMAN BIN HADRI", "2022793509"},
  {"MUHAMMAD IQMAL AFIQ BIN ALUWI", "2022787701"},
  {"MUHAMMAD SHAHRIL ELFY BIN SHAMSHUL HAZMI", "2022988137"},
  {"NIK MUHAMAD ADDLY BIN NIK ABDULLAH", "2022905445"},
  {"NORAZREEN BINTI MOHAMAD FAIZAL", "2020476838"},
  {"NUR ATHIRAH BINTI ROZAINI", "2022758441"},
  {"SITI NURHIDAYAH BINTI MOHD SUKRI", "2020615432"},
  {"ZAINUL HAZIMAN BIN HISAM", "2022978011"},

};

//----------------------------------------------------------------------

void setup()
{
  // Set up the IR sensor pins as input
  pinMode(increaseSensorPin, INPUT);
  pinMode(decreaseSensorPin, INPUT);

  pinMode(LEDgreen_PIN,OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);

  SerialBT.begin("ESP32-Bluetooth");

  // Initialize serial communication (for debugging)
  Serial.begin(115200);

//----------------------------------------------------------------------
  
  Serial.begin(115200);
  Serial2.begin(115200);
  while (!Serial);  // For Yun/Leo/Micro/Zero/...

  lcd.init();
  lcd.backlight();

  // Display welcome message on LCD
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Welcome to SMART");
  lcd.setCursor(0, 1);
  lcd.print("Attendance System");
  
  delay(3000);
  lcd.clear();
  Serial.println("\n\nAdafruit finger detect test");

  // set the data rate for the sensor serial port
  finger.begin(57600);
  delay(5);
  if (finger.verifyPassword()) {
    Serial.println("Found fingerprint sensor!");
  } else {
    Serial.println("Did not find fingerprint sensor :(");
    while (1) {
      delay(1);
    }
  }

  finger.getTemplateCount();
  Serial.print("Sensor contains "); Serial.print(finger.templateCount); Serial.println(" templates");

  // Display sensor information on LCD
  lcd.setCursor(0, 0);
  lcd.print("Templates: ");
  lcd.print(finger.templateCount);

  Serial.println("Waiting for valid fingerprint...");
  lcd.setCursor(0, 2);
  lcd.print("Waiting for valid fingerprint...");
  lcd.clear();

  Serial.print("Connecting to: ");
  Serial.print(ssid);
  lcd.setCursor(0, 0);
  lcd.print("Connecting to WiFi:");
  lcd.setCursor(0, 1);
  lcd.print(ssid);
  delay(3000);

  // Attempt to connect to WiFi
  WiFi.begin(ssid, password);

  int timeout = 10 * 4; // 10 seconds
  while (WiFi.status() != WL_CONNECTED  && (timeout-- > 0)) {
    delay(250);
    Serial.print(".");
  }
  Serial.println("");

  // Display WiFi connection status on LCD
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("WiFi connected!");
  lcd.setCursor(0, 1);
  lcd.print("IP Address: ");
  lcd.setCursor(0, 2);
  lcd.print(WiFi.localIP());

  Serial.print("WiFi connected in: ");
  Serial.print(millis());
  Serial.print(", IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {

  // Read the state of the increase sensor 
  int increaseState = digitalRead(increaseSensorPin);

  // Read the state of the decrease sensors 
  int decreaseState = digitalRead(decreaseSensorPin);

  // Check if the IR beam is interrupted for increasing count
  if (increaseState == LOW && !increaseFlag) {
    // Increment the count by 1
    count1++;
    totalCount++;

    // Display the count in the Serial Monitor
    Serial.print("Students Entered the Class, ");
    lcd.clear();
    lcd.setCursor(2,0);
    lcd.print("Students Entered");
    lcd.setCursor(6, 1);
    lcd.print("the Class");

    Serial.print("Enter Count: ");
    lcd.setCursor(0, 2);
    lcd.print("Enter Count: ");
    Serial.println(count1);
    lcd.print(count1);
    delay(2000);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Place your");
    lcd.setCursor(0, 1);
    lcd.print("fingerprint");
    
    // Set the flag to true to indicate that an increase has been registered
    increaseFlag = true;
  } else if (increaseState == HIGH) {
    // Reset the flag when the sensor beam is not interrupted
    increaseFlag = false;
  }

  // Check if the IR beams is interrupted for decreasing count
  if ((decreaseState == LOW) && !decreaseFlag && count1 > 0) {
    // Decrement the count by 1
    count1--;
    totalCount--;

    // Display the count in the Serial Monitor
    Serial.print("Students Exited the Class, ");
    lcd.clear();
    lcd.setCursor(2,0);
    lcd.print("Students Exited");
    lcd.setCursor(6, 1);
    lcd.print("the Class");

    Serial.print("Exit Count: ");
    lcd.setCursor(0, 2);
    lcd.print("Exit Count: ");
    Serial.println(count1);
    lcd.print(count1);
    delay(2000);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Place your");
    lcd.setCursor(0, 1);
    lcd.print("fingerprint");

    // Set the flag to true to indicate that a decrease has been registered
    decreaseFlag = true;
  } else if (decreaseState == HIGH) {
    // Reset the flag when the sensor beams are not interrupted
    decreaseFlag = false;
  }
if (SerialBT.available())  {
    byte command = SerialBT.read();
    if (command == 1) {
        viewAbsentStudents();
       
    }
    if (command == 2) {  // Example command 'R' for requesting total count
     

      SerialBT.print(totalCount);
    }
  }
    

  getFingerprintIDez();


  for (int i = 0; i < sizeof(students) / sizeof(students[0]); i++) {
    if (finger.fingerID == i + 1 && finger.confidence >= 60) {
      Serial.print("Found Fingerprint ID: ");
      Serial.println(finger.fingerID);

      digitalWrite(LEDgreen_PIN, HIGH);  // Turn on the LED
      tone(BUZZER_PIN, 1000);// Activate the buzzer for 2 seconds
      delay(500);  // Wait for 2 seconds
      digitalWrite(LEDgreen_PIN, LOW);  // Turn off the LED
      noTone(BUZZER_PIN);  // Turn off the buzzer
      
      Student currentStudent = students[i];

      Serial.print("Attendance Marked for ");
      Serial.println(currentStudent.name);

      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Name: ");
      lcd.print(currentStudent.name);

      lcd.setCursor(0, 3);
      lcd.print("ID: ");
      lcd.print(currentStudent.id);

      //makeIFTTTRequest();
      makeIFTTTRequest(currentStudent.name, currentStudent.id, "PRESENT");
      delay(3000);
      
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Total Students");
      lcd.setCursor(0, 1);
      lcd.print("in Class: ");
      lcd.setCursor(10, 1);
      lcd.print(totalCount);
      Serial.print("Total Students in Class: ");
      Serial.print(totalCount);

      // Delay for 5 seconds
      delay(2000);

      // Display "Put your fingerprint" message
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Put your fingerprint");
      lcd.setCursor(0, 1);
      lcd.print("for the next student");
     
    }
    
  }
  
  finger.fingerID = 0;
}

//----------------------------------------------------------------------------------------

uint8_t getFingerprintID() {
  
  uint8_t p = finger.getImage();
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image taken");
      break;
    case FINGERPRINT_NOFINGER:
      Serial.println("No finger detected");
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      return p;
    case FINGERPRINT_IMAGEFAIL:
      Serial.println("Imaging error");
      return p;
    default:
      Serial.println("Unknown error");
      return p;
  }

//---------------------------------------------------------------------------------------- OK success!

  p = finger.image2Tz();
  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;
  }

//---------------------------------------------------------------------------------------- OK converted!

  p = finger.fingerFastSearch();
  if (p == FINGERPRINT_OK) {
    Serial.println("Found a print match!");
  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
    Serial.println("Communication error");
    return p;
  } else if (p == FINGERPRINT_NOTFOUND) {
    Serial.println("Did not find a match");
    return p;
  } else {
    Serial.println("Unknown error");
    return p;
  }

//---------------------------------------------------------------------------------------- found a match!

  Serial.print("Found ID #"); Serial.print(finger.fingerID);
  Serial.print(" with confidence of "); Serial.println(finger.confidence);

  return finger.fingerID;
}

//----------------------------------------------------------------------------------------
// returns -1 if failed, otherwise returns ID #

int getFingerprintIDez() {
  uint8_t p = finger.getImage();
  if (p != FINGERPRINT_OK)  return -1;

  p = finger.image2Tz();
  if (p != FINGERPRINT_OK)  return -1;

  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);
  return finger.fingerID;
}

//----------------------------------------------------------------------------------------
// Make an HTTP request to the IFTTT web service

void makeIFTTTRequest(const char* studentName, const char* studentID, const char* attendanceStatus) {
  Serial.print("Connecting to ");
  Serial.print(server);

  WiFiClient client;
  int retries = 5;
  while (!client.connect(server, 80) && (retries-- > 0)) {
    Serial.print(".");
  }
  Serial.println();
  if (!client.connected()) {
    Serial.println("Failed to connect...");
  }

  Serial.print("Request resource: ");
  Serial.println(resource);

//----------------------------------------------------------------------------------------
// Create JSON object with student information

  String jsonObject = String("{\"value1\":\"") + studentName + "\",\"value2\":\"" + studentID + "\",\"value3\":\"" + attendanceStatus + "\"}";

  client.println(String("POST ") + resource + " HTTP/1.1");
  client.println(String("Host: ") + server);
  client.println("Connection: close\r\nContent-Type: application/json");
  client.print("Content-Length: ");
  client.println(jsonObject.length());
  client.println();
  client.println(jsonObject);

  int timeout = 5 * 10; // 5 seconds
  while (!client.available() && (timeout-- > 0)) {
    delay(100);
  }
  if (!client.available()) {
    Serial.println("No response...");
  }
  while (client.available()) {
    Serial.write(client.read());
  }

  Serial.println("\nclosing connection");
  client.stop();
}

void viewAbsentStudents() {
  String absentInfo = ""; // String to store absent student information


  // Iterate through students to check attendance
  for (int i = 0; i < sizeof(students) / sizeof(students[0]); i++) {
    bool isPresent = false;
    for (int j = 0; j < finger.templateCount; j++) {
      getFingerprintIDez();
      if (finger.fingerID == i + 1) {
        isPresent = true;
        break;
      }
    }
    if (!isPresent) {
     // lcd.setCursor(0, i + 1);
      Serial.println(students[i].name);
      
      Serial.println(students[i].id);
      Serial.println("Absent");

      // Collect absent student information
      absentInfo += students[i].name;
      absentInfo +=" ";
      absentInfo += students[i].id;
      
    }
  }

  // Send absent student information to MIT over Bluetooth
  SerialBT.print(absentInfo);

  // Add any additional formatting or delay as needed
  delay(5000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Put your fingerprint");
  lcd.setCursor(0, 1);
  lcd.print("for the next student");
}

Can you find a simple Bluetooth terminal program on the Play Store and use that to see your data traffic?

It looks like the person who wrote the sketch did almost all the work here, even sneaking in a web server?

I notice the code that builds up Bluetooth text to send listing absent students does nothing to delimit them with line break markers (\n) or commas.

If you are receiving names over BlueTooth, that would help

hi tq for that. how about to seperate the value i want. i want the total class appear in the textbox, it is appear at the viewlistabsent also.

Here are two changes that should deliver a Comma Separated Values (CSV) table.