#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <HTTPClient.h>
// Set up LCD: I2C Address 0x27, 16 columns, 2 rows
LiquidCrystal_I2C lcd(0x27, 16, 2);
// WiFi credentials
const char* ssid = "Grey pj 5G";
const char* password = "SANDHU87";
// Voting API (Flask backend)
const char* vote_url = "http://127.0.0.1:5000/vote";
int vote = 0; // 0 = No vote, 1 = Candidate A, 2 = Candidate B
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
// Wi-Fi Connection
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected!");
// Display Voting Options
lcd.setCursor(0, 0);
lcd.print("1. Alice 2. Bob");
lcd.setCursor(0, 1);
lcd.print("Press 1 or 2");
pinMode(18, INPUT_PULLUP); // Candidate A button
pinMode(19, INPUT_PULLUP); // Candidate B button
pinMode(23, INPUT_PULLUP); // Confirm Vote
}
void loop() {
if (digitalRead(18) == LOW) {
vote = 1;
lcd.clear();
lcd.print("You selected: Alice");
delay(1000);
}
if (digitalRead(19) == LOW) {
vote = 2;
lcd.clear();
lcd.print("You selected: Bob");
delay(1000);
}
if (digitalRead(23) == LOW && vote != 0) {
lcd.clear();
lcd.print("Submitting Vote...");
submitVote(vote);
delay(2000);
}
}
void submitVote(int candidate) {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = String(vote_url) + "?candidate=" + String(candidate);
http.begin(url);
int httpResponseCode = http.GET();
if (httpResponseCode == 200) {
lcd.clear();
lcd.print("Vote Confirmed!");
} else {
lcd.clear();
lcd.print("Error Submitting");
}
http.end();
} else {
lcd.clear();
lcd.print("No WiFi Conn.");
}
}