#include <WiFi.h>
#include <PubSubClient.h>
// ----------------------------
// USER CONFIG - EDIT THESE !!!
// ----------------------------
const char* ssid = "YOUR_WIFI_SSID"; // <-- Replace
const char* password = "YOUR_WIFI_PASSWORD"; // <-- Replace
const char* student_id = "3091637K"; // <-- Replace (e.g., "A1234567X")
// MQTT broker (public). You may change to campus broker if provided.
const char* mqtt_server = "mqtt-dashboard.com"; // or "test.mosquitto.org"
const uint16_t mqtt_port = 8884;
// ----------------------------
// PIN ASSIGNMENTS
// ----------------------------
const int RED_LED_PIN = 23; // Blink
const int YELLOW_LED_PIN = 22; // MQTT controlled
const int POT_PIN = 36; // VP (ADC1_CH0)
// ----------------------------
// TIMING (ms)
// ----------------------------
const unsigned long BLINK_INTERVAL_MS = 1000; // toggle every 1s → full cycle 2s
const unsigned long PUBLISH_INTERVAL_MS = 2000; // publish pot every 2s
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastBlinkTime = 0;
unsigned long lastPublishTime = 0;
bool redLedState = false;
// Forward declarations
void setup_wifi();
void reconnect();
void mqttCallback(char* topic, byte* payload, unsigned int length);
void setup() {
Serial.begin(115200);
delay(100);
Serial.println();
Serial.println("Booting...");
pinMode(RED_LED_PIN, OUTPUT);
pinMode(YELLOW_LED_PIN, OUTPUT);
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(YELLOW_LED_PIN, LOW);
// Optional: configure ADC attenuation / width (defaults OK in Wokwi)
// analogReadResolution(12); // 0-4095
// analogSetAttenuation(ADC_11db); // full 0-3.3V range typical
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
client.setCallback(mqttCallback);
}
void loop() {
// Maintain MQTT connection
if (!client.connected()) {
reconnect();
}
client.loop();
unsigned long now = millis();
// --- Blink Red LED ---
if (now - lastBlinkTime >= BLINK_INTERVAL_MS) {
lastBlinkTime = now;
redLedState = !redLedState;
digitalWrite(RED_LED_PIN, redLedState ? HIGH : LOW);
}
// --- Publish Pot Value ---
if (now - lastPublishTime >= PUBLISH_INTERVAL_MS) {
lastPublishTime = now;
int potVal = analogRead(POT_PIN); // 0-4095
char topic[64];
snprintf(topic, sizeof(topic), "/%s/pot/raw", student_id);
char payload[16];
snprintf(payload, sizeof(payload), "%d", potVal);
bool ok = client.publish(topic, payload, false);
Serial.printf("Publish %s to %s %s\n", payload, topic, ok ? "OK" : "FAIL");
}
}
// --------------------------------------------------
// WiFi connect helper
// --------------------------------------------------
void setup_wifi() {
Serial.printf("Connecting to %s\n", ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
uint8_t dots = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
dots++;
if (dots % 10 == 0) {
Serial.printf(" (status=%d)\n", WiFi.status());
}
}
Serial.println();
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
// --------------------------------------------------
// MQTT message handler
// --------------------------------------------------
void mqttCallback(char* topic, byte* payload, unsigned int length) {
Serial.print("MQTT msg [");
Serial.print(topic);
Serial.print("] ");
// Copy payload to a small buffer (safe length check)
char msg[32];
unsigned int copyLen = (length < sizeof(msg) - 1) ? length : sizeof(msg) - 1;
memcpy(msg, payload, copyLen);
msg[copyLen] = '\0';
Serial.println(msg);
// Compare topic
char expected[64];
snprintf(expected, sizeof(expected), "/%s/y_led", student_id);
if (strcmp(topic, expected) == 0) {
if (strcasecmp(msg, "on") == 0) {
digitalWrite(YELLOW_LED_PIN, HIGH);
Serial.println("Yellow LED -> ON");
} else if (strcasecmp(msg, "off") == 0) {
digitalWrite(YELLOW_LED_PIN, LOW);
Serial.println("Yellow LED -> OFF");
} else {
Serial.println("Unknown payload (expected 'on' or 'off')");
}
}
}
// --------------------------------------------------
// MQTT reconnect helper
// --------------------------------------------------
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = String("ESP32-") + String((uint32_t)ESP.getEfuseMac(), HEX);
// Attempt to connect (no auth)
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Subscribe
char topic[64];
snprintf(topic, sizeof(topic), "/%s/y_led", student_id);
client.subscribe(topic);
Serial.print("Subscribed: ");
Serial.println(topic);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}