#include <Arduino.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <FastLED.h>
#define DATA_PIN 14
#define NUM_LEDS 19
#define BRIGHTNESS 30
#define NUM_COLORS 8
CRGB leds[NUM_LEDS];
CRGB colors[NUM_COLORS] = {
CRGB::Red,
CRGB::Green,
CRGB::Blue,
CRGB::Yellow,
CRGB::Purple,
CRGB::Grey,
CRGB::Orange,
CRGB::DeepPink,
};
// WiFi credentials
const char *ssid = "Wokwi-GUEST";
const char *password = "";
// MQTT server credentials
const char *mqtt_server = "test"; // Replace with your MQTT broker address
const int mqtt_port = 1883; // Replace with your MQTT broker port if different
const char *mqtt_user = "test"; // Replace with your MQTT username, if any
const char *mqtt_password = "test"; // Replace with your MQTT password, if any
WiFiClient espClient;
PubSubClient client(espClient);
void connectToWiFi();
void runRainbow();
void connectToMQTT();
void callback(char *topic, byte *payload, unsigned int length);
void addTask(int index);
void setup()
{
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
Serial.begin(115200);
FastLED.setBrightness(BRIGHTNESS);
// Connect to WiFi
connectToWiFi();
// Setup MQTT client
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
// Connect to MQTT broker
connectToMQTT();
// set an initial color
for (int i = 0; i < NUM_LEDS; i++)
{
leds[i] = CRGB::Purple; // or any color you prefer
delay(50);
FastLED.show();
}
delay(1000);
FastLED.clear(true);
}
void loop()
{
// Ensure MQTT connection remains active
if (!client.connected())
{
connectToMQTT();
}
client.loop();
}
// Function to connect to WiFi
void connectToWiFi()
{
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected, IP: ");
Serial.println(WiFi.localIP());
}
// Function to connect to MQTT broker
void connectToMQTT()
{
while (!client.connected())
{
Serial.print("Connecting to MQTT...");
if (client.connect("ESP32Client", mqtt_user, mqtt_password))
{
Serial.println("connected");
// Subscribe to the topic
client.subscribe("task-update/add");
client.subscribe("task-update/delete");
client.subscribe("task-update/done");
}
else
{
Serial.print("failed, rc=");
Serial.print(client.state());
delay(5000);
}
}
}
// Callback function to handle incoming MQTT messages
void callback(char *topic, byte *payload, unsigned int length)
{
String message;
for (int i = 0; i < length; i++)
{
message += (char)payload[i];
}
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
Serial.println(message);
// when task is added to the list
if (String(topic) == "task-update/add")
{
int taskCount = message.toInt();
if (taskCount <= 19)
{
addTask(taskCount);
}
}
// when task is marked done ̰
if (String(topic) == "task-update/done")
{
int taskCount = message.toInt();
if (taskCount <= 19)
{
markAsDone(taskCount);
}
}
// when task is deleted from the list
if (String(topic) == "task-update/delete")
{
int taskCount = message.toInt();
if (taskCount <= 19)
{
removeTask(taskCount);
}
}
}
void addTask(int index)
{
leds[index] = CRGB::Orange;
Serial.print("Add: ");
Serial.println(index);
FastLED.show();
}
void markAsDone(int index)
{
leds[index] = CRGB::Green;
Serial.print("Done: ");
Serial.println(index);
FastLED.show();
}
void removeTask(int index)
{
leds[index] = CRGB::Black;
Serial.print("Remove: ");
Serial.println(index);
FastLED.show();
}
// Function to update the LEDs based on the task count
void updateLEDs(int taskCount)
{
for (int i = 0; i < NUM_LEDS; i++)
{
if (i < taskCount)
{
leds[i] = CRGB::Red; // or any color you prefer
}
}
FastLED.show();
}
void runRainbow()
{
for (int j = 0; j < NUM_COLORS; j++)
{
if (j % 2 == 0)
{
for (int i = 0; i < NUM_LEDS; i++)
{
leds[i] = colors[j];
FastLED.show();
delay(70);
// FastLED.clear(true);
}
}
else
{
for (int i = NUM_LEDS - 1; i >= 0; i--)
{
leds[i] = colors[j];
FastLED.show();
delay(70);
// FastLED.clear(true);
}
}
}
delay(2000);
FastLED.clear(true);
}