{"id":23386947,"url":"https://github.com/nhathout/elect-a-leader","last_synced_at":"2026-04-29T00:34:24.028Z","repository":{"id":268928115,"uuid":"905606860","full_name":"nhathout/elect-A-leader","owner":"nhathout","description":"Secure E-Voting System.","archived":false,"fork":false,"pushed_at":"2024-12-23T06:20:19.000Z","size":3562,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-14T08:37:04.849Z","etag":null,"topics":["infrared-sensors","nfc","raspberry-pi","wireless"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nhathout.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-19T07:03:46.000Z","updated_at":"2024-12-23T06:20:23.000Z","dependencies_parsed_at":"2024-12-19T19:22:59.820Z","dependency_job_id":"42d40f43-701f-40ab-8b66-5d408e7da5bb","html_url":"https://github.com/nhathout/elect-A-leader","commit_stats":null,"previous_names":["nhathout/elect-a-leader"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nhathout%2Felect-A-leader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nhathout%2Felect-A-leader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nhathout%2Felect-A-leader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nhathout%2Felect-A-leader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nhathout","download_url":"https://codeload.github.com/nhathout/elect-A-leader/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247843209,"owners_count":21005422,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["infrared-sensors","nfc","raspberry-pi","wireless"],"created_at":"2024-12-22T01:13:54.404Z","updated_at":"2026-04-29T00:34:23.881Z","avatar_url":"https://github.com/nhathout.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Elect-A-Leader\n\n**Date:** November 7, 2024\n\n## Overview\nSecure E-Voting is a distributed, fail-safe system for casting and recording votes via a network of devices. Each fob device, equipped with an IR transmitter and receiver, simulates NFC-like communication to authenticate voters and send votes. A dynamically elected Coordinator handles vote aggregation, communication with a central server, and ensures continuous operation through failover mechanisms.\n\n## Key Features\n\n- **Voter Authentication \u0026 Vote Casting**  \n  - Each voter’s unique ID is sent alongside the vote using IR-based communication.  \n  - The Coordinator relays this data to a central database and confirms receipt.\n\n- **Coordinator Election \u0026 Failover**  \n  - Fobs use a leader election algorithm to select a Coordinator.  \n  - The system detects Coordinator failures, triggering an automatic failover to ensure uninterrupted voting.\n\n- **Secure Vote Transmission**  \n  - Voter IDs and votes flow through IR from the casting fob to the Coordinator.  \n  - The Coordinator securely forwards data to a central server running a Node.js and TingoDB backend.\n\n- **Data Logging \u0026 Web Interface**  \n  - Votes are logged in a central database, storing voter IDs and timestamps.  \n  - A web UI displays real-time results, and features a reset option for the database.\n\n- **Device Status Indication**  \n  - **Red LED:** Non-leader device.  \n  - **Blue LED:** Leader device (Coordinator).  \n  - **Green LED:** Timeouts or other status notifications.\n\n## Implementation Details\n\n### Hardware Integration\n- **Fobs with IR Transmitters/Receivers**  \n  - Simulate NFC, allowing each device to broadcast and receive messages.  \n  - Integrated buttons for initiating votes.  \n  - LED indicators for state feedback.\n\n- **ESP32 Microcontrollers**  \n  - Manage IR communication (transmit/receive).  \n  - Connect via Wi-Fi for Coordinator-to-server data flow.  \n  - Utilize FreeRTOS tasks and mutexes for concurrency and synchronization.\n\n### Network Communication\n- **Wi-Fi Connectivity**  \n  - Devices join a common network.  \n  - Votes and leader election messages are exchanged using UDP.\n\n- **Coordinator Role**  \n  - Receives votes (ID and chosen option).  \n  - Transmits data to the server on port 4000.  \n  - Confirms receipt to the voting fob.\n\n- **Backend (store_votes.js)**  \n  - Hosts a Node.js server with a TingoDB database (`mydb/votes`).  \n  - Exposes a web interface at `http://192.168.1.103:8080`, providing real-time results.  \n  - Allows database resets and debugging through a simple UI.\n\n### Leader Election\n- **Bully Algorithm**  \n  - Devices exchange messages (`MSG_ELECTION`, `MSG_ALIVE`, `MSG_VICTORY`, `MSG_KEEP_ALIVE`) to decide on a Coordinator.  \n  - If a Coordinator is unresponsive, other devices trigger a new election.  \n  - The newly elected Coordinator broadcasts a victory message and updates the system state.\n\n### FreeRTOS Tasks\n- **button_task**  \n  - Detects button presses and interprets them as votes.  \n  - Commits a vote transmission after a debounce or timeout.  \n\n- **ir_rx_task**  \n  - Continuously listens for IR messages.  \n  - Processes commands for joining the system, receiving Coordinator signals, and votes.\n\n- **bully_algorithm_task**  \n  - Manages leader election processes and monitors Coordinator health.  \n\n- **recv_task (UDP Receive)**  \n  - Handles incoming UDP messages (e.g., election calls, votes, keep-alive signals).\n\n- **id_task**  \n  - Blinks the onboard LED a set number of times to indicate the device’s ID for diagnostics.\n\n- **continuous_ir_transmit_task**  \n  - If the device is Coordinator, broadcasts `MSG_COORDINATOR_SIGNAL` via IR to maintain leadership status.\n\n### Vote Casting Workflow\n1. **Fob Device (Non-Coordinator):**  \n   - User presses a button to select a vote option (e.g., Red/Blue/Green).  \n   - Vote data (Voter ID + selected option) is sent to the Coordinator via UDP.\n\n2. **Coordinator:**  \n   - Logs vote data locally.  \n   - Confirms receipt back to the voter.  \n   - Transmits the vote to the central server via UDP on port 4000.\n\n3. **Server (store_votes.js):**  \n   - Inserts each vote record into TingoDB.  \n   - Updates the web interface for real-time voting results.\n\n## Results and Learnings\n\n- **Achievements:**  \n  - Achieved a robust distributed system where devices dynamically elect a Coordinator and handle failovers seamlessly.  \n  - IR-based “NFC” simulation successfully captured votes and authenticated users.  \n  - The centralized database and web UI provided an accessible snapshot of voting progress.\n\n- **Challenges:**  \n  - Fine-tuning IR signals to handle interference and ensure accurate detection.  \n  - Coordinating UDP messages in real-time to avoid missed or duplicated votes.  \n  - Preserving data integrity while managing concurrent device states demanded careful synchronization.\n\n- **Potential Improvements:**  \n  - Implement hardware interrupts for IR reception to improve responsiveness.  \n  - Enhance error detection and encryption for added security in vote transmission.  \n  - Include more elaborate logging and diagnostics for debugging complex failover events.\n\n## Demonstrations\n- [**Functionality Demo Video**](https://drive.google.com/file/d/14ioolii2PttAG8Jn80Jt-KU8r46mR65U/view?usp=sharing)\n- [**Design Walkthrough Video**](https://drive.google.com/file/d/1l6wGfmdgtvITa7rTuvHjnD1I_g9CTXzG/view?usp=sharing)\n\n## Appendix\ncircuit diagram:\n![circuit-diagram](circuit-diagram.jpeg)\n\n---\n\nBy combining IR-based “NFC” authentication, leader election, and a central database, Secure E-Voting showcases how ESP32 devices can form a resilient, distributed voting network. The fail-safe coordinator mechanism and real-time web interface underscore the system’s robustness and transparency for real-world voting scenarios.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnhathout%2Felect-a-leader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnhathout%2Felect-a-leader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnhathout%2Felect-a-leader/lists"}