Moheeb ZaraEMQ Technologies
Published

ESP32 LED Marquee using MQTT

Learn how to build a scrolling text display using MQTT, neopixels, and an ESP32

BeginnerFull instructions provided1 hour518
ESP32 LED Marquee using MQTT

Things used in this project

Hardware components

ESP32
Espressif ESP32
×1
RGB LED Pixel Matrix, NeoPixel NeoMatrix
RGB LED Pixel Matrix, NeoPixel NeoMatrix
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

EMQX
MQTTX

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Code

mqtt_marquee.ino

Arduino
#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>
#include <WiFi.h>
#include <ArduinoMqttClient.h>
#include <ArduinoJson.h>

char ssid[] = "";    // your network SSID (name)
char pass[] = "";    // your network password 

char mqtt_user[] = "test";
char mqtt_pass[] = "test";

WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);

const char broker[] = "";
int        port     = 1883;
const char subscribe_topic[]  = "led/matrix";

#define LED_BLACK		0
#define LED_RED_HIGH 		(31 << 11) 
#define LED_GREEN_HIGH 		(63 << 5)  
#define LED_BLUE_HIGH 		31

#define PIN 2
// Max is 255, 32 is a conservative value to not overload
// a USB power supply (500mA) for 12x12 pixels.
#define BRIGHTNESS 32

// Define matrix width and height.
#define mw 32
#define mh 8
Adafruit_NeoMatrix *matrix = new Adafruit_NeoMatrix(mw, mh, 
  PIN,
  NEO_MATRIX_TOP     + NEO_MATRIX_LEFT +
    NEO_MATRIX_ROWS + NEO_MATRIX_ZIGZAG,
  NEO_GRB            + NEO_KHZ800 );

const uint16_t colors[] = {

 LED_RED_HIGH, LED_GREEN_HIGH, LED_BLUE_HIGH 

};


void setup() {
  matrix->begin();
  matrix->setTextWrap(false);
  matrix->setBrightness(40);
  matrix->setTextColor(colors[0]);
  // Create serial connection and wait for it to become available.
  Serial.begin(9600);
  while (!Serial) {
    ; 
  }

  // Connect to WiFi
  Serial.print("Attempting to connect to WPA SSID: ");
  Serial.println(ssid);
  int tryDelay = 500;
    int numberOfTries = 20;
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    // failed, retry
    Serial.print(".");
    delay(5000);
  }

  Serial.println("You're connected to the network");
  Serial.println();

  // You can provide a username and password for authentication
  mqttClient.setUsernamePassword(mqtt_user, mqtt_pass);

  Serial.print("Attempting to connect to the MQTT broker.");

  if (!mqttClient.connect(broker, port)) {
    Serial.print("MQTT connection failed! Error code = ");
    Serial.println(mqttClient.connectError());

    while (1);
  }

  Serial.println("You're connected to the MQTT broker!");

  mqttClient.onMessage(onMqttMessage);

  Serial.print("Subscribing to topic: ");
  Serial.println(subscribe_topic);

  // subscribe to a topic
  mqttClient.subscribe(subscribe_topic);

  Serial.print("Waiting for messages on topic: ");
  Serial.println(subscribe_topic);
}

int x    = matrix->width();

int passes = 0;

String text = "";

void onMqttMessage(int messageSize) {
  Serial.print("Received a message with topic '");
  Serial.println(mqttClient.messageTopic());
  StaticJsonDocument<256> doc;
  deserializeJson(doc, mqttClient);
  const char* message = doc["message"];
  Serial.print("Message: '");
  Serial.println(message);
  text = message;
}

void loop() {

  mqttClient.poll();

  int totalShifts = (text.length() *6);   // Total shifts needed

  matrix->fillScreen(0);
  matrix->setCursor(x, 0);
  matrix->print(text);

  if(--x < -totalShifts) {  
    x = matrix->width();
    if(++passes >= 3) passes = 0;
    matrix->setTextColor(colors[passes]);

  }
   matrix->show();
   delay(50);
}

Credits

Moheeb Zara

Moheeb Zara

39 projects • 136 followers
Developer Advocate at EMQ Technologies - . Co-founder - South West Maker Festival. Hardware hacker, artist, robots, HeatSync Labs
EMQ Technologies

EMQ Technologies

5 projects • 6 followers
World's leading cloud-native MQTT-based IoT messaging platform and streaming database

Comments