#include <WiFi.h> // WiFi header file
#include <PubSubClient.h> // MQTT publish and subscribe header file
#include <Wire.h> // I2C header file
#include <LiquidCrystal_I2C.h> // I2C lcd header file
#include <Arduino.h>
// === Robert Gardner ====
// Final Project Component, Option 1
// WiFi and MQTT Settings
const char* ssid = "Wokwi-GUEST"; // Replace with your WiFi SSID
const char* password = ""; // Replace with your WiFi password
const char* mqttServer = "test.mosquitto.org"; // MQTT broker
int port = 1883;
// MQTT Client Variables
String macAddressString;
char macAddress[50];
char clientId[50];
WiFiClient espClient;
PubSubClient client(espClient);
// LCD Setup
LiquidCrystal_I2C lcd(0x27, 16, 2);
// LED and Button Pins
const int redLightNorthSouth = 14;
const int yellowLightNorthSouth = 12;
const int greenLightNorthSouth = 13;
const int redLightEastWest = 25;
const int yellowLightEastWest = 26;
const int greenLightEastWest = 27;
const int crossWalkButton = 19;
const int emergencyBlueLED = 16;
const int buzzerPin = 32;
// Timing Constants
const int walkTime = 10000; // 10 seconds
const int yellowTime = 2000; // 2 seconds
const int redTime = 3000; // 3 seconds
// State Variables
int crossWalkButtonState = 1; // 0: button pressed, 1: button not pressed
int loopCount = 0;
int secondsLeft = 0;
int iotControl = 0; // 0: normal mode, 1: emergency mode
// Function Prototypes
void wifiConnect();
void mqttReconnect();
void callback(char* topic, byte* message, unsigned int length);
void handleNormalTraffic();
void handlePedestrianSignal();
void setup() {
Serial.begin(115200);
randomSeed(analogRead(0));
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
wifiConnect();
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println(WiFi.macAddress());
macAddressString = WiFi.macAddress();
macAddressString.replace(":", "_");
strcpy(macAddress, macAddressString.c_str()); // Copy to char array
Serial.println(macAddressString);
// MQTT Setup
client.setServer(mqttServer, port);
client.setCallback(callback);
// Initialize Pins
pinMode(crossWalkButton, INPUT_PULLUP);
attachInterrupt(crossWalkButton, buttonPressed, FALLING);
pinMode(redLightNorthSouth, OUTPUT);
pinMode(yellowLightNorthSouth, OUTPUT);
pinMode(greenLightNorthSouth, OUTPUT);
pinMode(redLightEastWest, OUTPUT);
pinMode(yellowLightEastWest, OUTPUT);
pinMode(greenLightEastWest, OUTPUT);
pinMode(emergencyBlueLED, OUTPUT);
pinMode(buzzerPin, OUTPUT);
// LCD Setup
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("=== CEIS-114 ===");
}
void wifiConnect() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
}
void mqttReconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
long r = random(1000);
sprintf(clientId, "clientId-%ld", r);
if (client.connect(clientId)) {
Serial.print(clientId);
Serial.println(" connected");
client.subscribe("LED");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void callback(char* topic, byte* message, unsigned int length) {
String stMessage;
for (int i = 0; i < length; i++) {
stMessage += (char)message[i];
}
if (String(topic) == "LED") {
if (stMessage == "ON") {
iotControl = 1;
} else if (stMessage == "OFF") {
iotControl = 0;
}
}
}
void handleNormalTraffic() {
// Red - Green - Yellow cycle for North-South and East-West
// (Red for NS, Green for EW)
digitalWrite(redLightNorthSouth, HIGH);
digitalWrite(yellowLightNorthSouth, LOW);
digitalWrite(greenLightNorthSouth, LOW);
digitalWrite(redLightEastWest, LOW);
digitalWrite(yellowLightEastWest, LOW);
digitalWrite(greenLightEastWest, HIGH);
delay(redTime);
// (Yellow for EW)
digitalWrite(redLightNorthSouth, HIGH);
digitalWrite(yellowLightNorthSouth, LOW);
digitalWrite(greenLightNorthSouth, LOW);
digitalWrite(redLightEastWest, LOW);
digitalWrite(yellowLightEastWest, HIGH);
digitalWrite(greenLightEastWest, LOW);
delay(yellowTime);
// (Red for EW, Green for NS)
digitalWrite(redLightNorthSouth, LOW);
digitalWrite(yellowLightNorthSouth, LOW);
digitalWrite(greenLightNorthSouth, HIGH);
digitalWrite(redLightEastWest, HIGH);
digitalWrite(yellowLightEastWest, LOW);
digitalWrite(greenLightEastWest, LOW);
delay(redTime);
// (Yellow for NS)
digitalWrite(redLightNorthSouth, LOW);
digitalWrite(yellowLightNorthSouth, HIGH);
digitalWrite(greenLightNorthSouth, LOW);
digitalWrite(redLightEastWest, HIGH);
digitalWrite(yellowLightEastWest, LOW);
digitalWrite(greenLightEastWest, LOW);
delay(yellowTime);
// (Red for NS, Green for EW)
digitalWrite(redLightNorthSouth, HIGH);
digitalWrite(yellowLightNorthSouth, LOW);
digitalWrite(greenLightNorthSouth, LOW);
digitalWrite(redLightEastWest, LOW);
digitalWrite(yellowLightEastWest, LOW);
digitalWrite(greenLightEastWest, HIGH);
delay(redTime);
}
void handlePedestrianSignal() {
// Pedestrian Signal Logic
// Turn off all LEDs
digitalWrite(yellowLightNorthSouth, LOW);
digitalWrite(greenLightNorthSouth, LOW);
digitalWrite(yellowLightEastWest, LOW);
digitalWrite(greenLightEastWest, LOW);
// Turn on red LEDs for both sides
digitalWrite(redLightNorthSouth, HIGH);
digitalWrite(redLightEastWest, HIGH);
// "Walk" countdown
for (int i = 10; i >= 0; i--) {
Serial.print(" == Walk == ");
Serial.println(i);
lcd.setCursor(0, 1);
lcd.print(" == Walk == ");
lcd.setCursor(14, 1);
lcd.print(i);
digitalWrite(buzzerPin, HIGH);
delay(500);
digitalWrite(buzzerPin, LOW);
delay(500);
}
// Turn off Red LEDs
digitalWrite(redLightNorthSouth, LOW);
digitalWrite(redLightEastWest, LOW);
// Yellow LED for both sides
digitalWrite(yellowLightNorthSouth, HIGH);
digitalWrite(yellowLightEastWest, HIGH);
delay(yellowTime);
// Turn off Yellow LEDs
digitalWrite(yellowLightNorthSouth, LOW);
digitalWrite(yellowLightEastWest, LOW);
}
void loop() {
if (!client.connected()) {
mqttReconnect();
}
client.loop();
if (iotControl == 0) {
// Normal Traffic Light Mode
lcd.setCursor(0, 1);
lcd.print(" = Do Not Walk! =");
Serial.println(" == Do Not Walk == ");
digitalWrite(emergencyBlueLED, LOW);
if (crossWalkButtonState == 0) {
handlePedestrianSignal();
crossWalkButtonState = 1; // Reset button state
} else {
handleNormalTraffic();
}
} else {
// Emergency Mode
lcd.setCursor(0, 1);
lcd.println("= Emergency! =");
Serial.println("= Emergency! =");
digitalWrite(yellowLightNorthSouth, LOW);
digitalWrite(greenLightNorthSouth, LOW);
digitalWrite(yellowLightEastWest, LOW);
digitalWrite(greenLightEastWest, LOW);
digitalWrite(redLightNorthSouth, HIGH);
digitalWrite(redLightEastWest, HIGH);
digitalWrite(emergencyBlueLED, HIGH);
digitalWrite(buzzerPin, HIGH);
delay(500);
digitalWrite(emergencyBlueLED, LOW);
digitalWrite(buzzerPin, LOW);
delay(500);
}
}
void buttonPressed() {
static unsigned long lastInterruptTime = 0;
unsigned long interruptTime = millis();
if (interruptTime - lastInterruptTime > 20) {
crossWalkButtonState = 0;
}
lastInterruptTime = interruptTime;
}