Mqtt blinker for topic notifications

Last Updated or created 2026-01-21

Last year I’ve made a led pole with digital fireworks.

Time to replace for something else ..

I’ve made a mqtt 1-D game in december.

I needed to change a lot to the javascript on the website to fix some stuff.

  • Fix IPhone control. (I hate iphone)
  • Fix screenlock timeout
  • Added meta refresh

The XMAS/Fireworks controller was often used, and I got notifications via my TV. (see other posts)

Now I want to see when MQTT movement when I’m in the livingroom.
So I programmed a Wemos controller to blink the internal when MQTT messages are received.

CODE:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* ssid = "WIFIAP";
const char* password = "WIFIPASS";

const char* mqtt_server = "MQTTBROKER";  // MQTT broker IP
const char* mqtt_topic  = "game/tilt";

WiFiClient espClient;
PubSubClient client(espClient);

String lastPayload = "";

void setup_wifi() {
  delay(10);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
}

void blinkLED() {
    digitalWrite(LED_BUILTIN, LOW);   // LED ON
    delay(200);
    digitalWrite(LED_BUILTIN, HIGH);  // LED OFF
    delay(200);
}

void callback(char* topic, byte* payload, unsigned int length) {
  String message;
  for (unsigned int i = 0; i < length; i++) {
    message += (char)payload[i];
  }

  // Blink only if topic value changed
  if (message != lastPayload) {
    blinkLED();
    lastPayload = message;
  }
}

void reconnect() {
  while (!client.connected()) {
    if (client.connect("WemosClientMqttBlink")) {
      client.subscribe(mqtt_topic);
    } else {
      delay(2000);
    }
  }
}

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH); 

  setup_wifi();

  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
}
Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *