Files
LED-Matrix/main/main.ino
2024-11-30 16:48:48 +01:00

73 lines
1.7 KiB
C++

#include <FastLED.h>
#include "LEDMatrix.h"
#include <WiFi.h>
#include <MQTT.h>
#include "secrets.h"
// Params for width and height
const uint8_t kMatrixWidth = 32;
const uint8_t kMatrixHeight = 16;
#define NUM_LEDS (kMatrixWidth * kMatrixHeight)
#define DATA_PIN D2
#define LAST_VISIBLE_LED 511
CRGB leds[NUM_LEDS];
LEDMatrix matrix(kMatrixWidth, kMatrixHeight, NUM_LEDS, LAST_VISIBLE_LED, leds);
WiFiClient net;
MQTTClient client;
void setup() {
Serial.begin(9600);
FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(40);
FastLED.clear();
FastLED.show();
WiFi.begin(ssid, password);
client.begin("192.168.178.155", 1883, net);
client.onMessage(messageReceived);
connect();
}
void loop() {
client.loop();
//reconnect if it loses connection with mqtt broker
if (!client.connected()) {
connect();
}
}
//mqtt stuffs
void connect() {
Serial.print("checking wifi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.print("\nconnecting...");
while (!client.connect("espMatrix")) {
Serial.print(".");
delay(1000);
}
Serial.println("\nconnected!");
client.subscribe("Matrix/Text");
}
void messageReceived(String &topic, String &payload) {
Serial.println("incoming: " + topic + " - " + payload);
FastLED.clear();
matrix.drawText(payload.c_str(), 0, 0, CRGB::White);
FastLED.show();
// Note: Do not use the client in the callback to publish, subscribe or
// unsubscribe as it may cause deadlocks when other things arrive while
// sending and receiving acknowledgments. Instead, change a global variable,
// or push to a queue and handle it in the loop after calling `client.loop()`.
}