Yarana Iot Guru
Published © MIT

Smart Attendance System Using 4G LTE Module | HTTP Request

A portable, reliable Smart Attendance System that logs RFID/Code-based attendance to a cloud server via a 4G LTE modem — no Wi-Fi required

BeginnerFull instructions provided8 hours18
Smart Attendance System Using 4G LTE Module | HTTP Request

Things used in this project

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

💻 Server side — MySQL & PHP (example)

C/C++
SQL schema (MySQL)
CREATE DATABASE attendance_db;
USE attendance_db;

CREATE TABLE devices (
  id INT AUTO_INCREMENT PRIMARY KEY,
  device_label VARCHAR(100) UNIQUE,
  api_token VARCHAR(128),
  location VARCHAR(255),
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE attendance (
  id INT AUTO_INCREMENT PRIMARY KEY,
  device_id INT,
  uid VARCHAR(128),
  event_time DATETIME,
  status VARCHAR(50),
  raw_payload TEXT,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (device_id) REFERENCES devices(id)
);

PHP endpoint (attendance.php)

C/C++
Save this file in your web server (e.g., /var/www/html/api/attendance.php):
<?php
// attendance.php
header('Content-Type: application/json');

// DB config
$db_host = 'localhost';
$db_user = 'db_user';
$db_pass = 'db_pass';
$db_name = 'attendance_db';

// Read incoming
$raw = file_get_contents("php://input");
$data = json_decode($raw, true);

// Simple validation
if (!$data || !isset($data['device_label']) || !isset($data['token']) || !isset($data['uid'])) {
    http_response_code(400);
    echo json_encode(['status'=>'error','message'=>'Invalid payload']);
    exit;
}

$device_label = $data['device_label'];
$token = $data['token'];
$uid = $data['uid'];
$event_time = isset($data['event_time']) ? $data['event_time'] : date('Y-m-d H:i:s');

$mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name);
if ($mysqli->connect_errno) {
    http_response_code(500);
    echo json_encode(['status'=>'error','message'=>'DB connection failed']);
    exit;
}

// Verify device token
$stmt = $mysqli->prepare("SELECT id FROM devices WHERE device_label=? AND api_token=? LIMIT 1");
$stmt->bind_param('ss', $device_label, $token);
$stmt->execute();
$stmt->bind_result($device_id);
if (!$stmt->fetch()) {
    http_response_code(403);
    echo json_encode(['status'=>'error','message'=>'Invalid device or token']);
    exit;
}
$stmt->close();

// Insert attendance
$stmt = $mysqli->prepare("INSERT INTO attendance (device_id, uid, event_time, status, raw_payload) VALUES (?, ?, ?, ?, ?)");
$status = 'present';
$raw_payload = $mysqli->real_escape_string($raw);
$stmt->bind_param('issss', $device_id, $uid, $event_time, $status, $raw_payload);
$ok = $stmt->execute();

if ($ok) {
    echo json_encode(['status'=>'success','message'=>'Logged','id'=>$stmt->insert_id]);
} else {
    http_response_code(500);
    echo json_encode(['status'=>'error','message'=>'Insert failed']);
}

$stmt->close();
$mysqli->close();
?>

🔧 ESP32 Arduino code (example using TinyGSM for SIM7600)

C/C++
This example uses TinyGsmClient and HTTPClient. Install libraries: TinyGSM, WiFiClientSecure (for HTTPS if needed), and MFRC522 for RFID.
// Smart Attendance using ESP32 + 4G LTE (SIM7600 style) + RC522
// By Yarana IoT Guru

#include <TinyGsmClient.h>
#include <HTTPClient.h>
#include <HardwareSerial.h>
#include <SPI.h>
#include <MFRC522.h>

// --- CONFIG ---
#define MODEM_RX 16  // RX pin to modem TX
#define MODEM_TX 17  // TX pin to modem RX
#define MODEM_PWRKEY 4 // if needed to power on module (optional)
#define APN "your_apn_here"
#define API_URL "http://yourserver.com/api/attendance.php" // use https if available
#define DEVICE_LABEL "site_office_01"
#define API_TOKEN "super_secret_token"

// RFID pins
#define RST_PIN 4
#define SS_PIN 5

// Serial for modem
HardwareSerial SerialAT(1);

// TinyGSM modem type (change if using different modem)
#define TINY_GSM_MODEM_SIM7600

#include <TinyGsmClient.h>
TinyGsm modem(SerialAT);
TinyGsmClient client(modem);

MFRC522 mfrc522(SS_PIN, RST_PIN);

void setup() {
  Serial.begin(115200);
  delay(10);
  Serial.println("Yarana IoT Guru - Smart Attendance with 4G");

  // Modem serial
  SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);

  SPI.begin();           // Init SPI bus
  mfrc522.PCD_Init();    // Init MFRC522

  // Start modem
  Serial.println("Initializing modem...");
  if (!modem.init()) {
    Serial.println("Failed to initialize modem");
    while(true){ delay(1000); }
  }
  Serial.println("Modem init OK");

  // Set APN
  Serial.print("Connecting to network... ");
  if (!modem.gprsConnect(APN, "", "")) {
    Serial.println("GPRS connect failed");
  } else {
    Serial.println("GPRS connected");
  }
}

String buildPayload(String uid) {
  // build JSON
  String payload = "{\"device_label\":\"" + String(DEVICE_LABEL) + "\",";
  payload += "\"token\":\"" + String(API_TOKEN) + "\",";
  payload += "\"uid\":\"" + uid + "\",";
  payload += "\"event_time\":\"" + String(timestampNow()) + "\"}";
  return payload;
}

String timestampNow() {
  // If modem supports NTP or network time, fetch it. For simplicity use millis-based time or placeholder
  // You should implement RTC or NTP for accurate timestamps.
  return String("2025-10-29 12:00:00");
}

void loop() {
  // Look for RFID
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    delay(50);
    return;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    delay(50);
    return;
  }

  // Build UID string
  String uid = "";
  for (byte i = 0; i < mfrc522.uid.size; i++) {
    if (mfrc522.uid.uidByte[i] < 0x10) uid += "0";
    uid += String(mfrc522.uid.uidByte[i], HEX);
  }
  uid.toUpperCase();

  Serial.print("Card UID: "); Serial.println(uid);

  String payload = buildPayload(uid);
  sendAttendance(payload);

  // Feedback (beep / LED)
  delay(500);
}

void sendAttendance(String jsonPayload) {
  Serial.println("Sending attendance: " + jsonPayload);
  if (!modem.isNetworkConnected()) {
    Serial.println("No network, cannot send. Implement queue and retry.");
    return;
  }

  // Use HTTPClient via TinyGsmClient
  TinyGsmClient httpClient = client;

  HTTPClient http;
  http.begin(httpClient, API_URL); // if https, ensure secure client is used
  http.addHeader("Content-Type", "application/json");

  int httpCode = http.POST(jsonPayload);
  if (httpCode > 0) {
    String response = http.getString();
    Serial.printf("HTTP %d : %s\n", httpCode, response.c_str());
  } else {
    Serial.println("HTTP POST failed");
  }
  http.end();
}

Credits

Yarana Iot Guru
40 projects • 11 followers
Yarana Iot Guru Yarana IoT Guru: Arduino, ESP32, GSM, NodeMCU & more. Projects, Tutorials & App Development. Innovate with us!
Thanks to Yarana IoT Guru.

Comments