From 31deb137f0c35012f2cf6396fb656a112216b804 Mon Sep 17 00:00:00 2001 From: Sam Hos Date: Sat, 30 Nov 2024 16:48:48 +0100 Subject: [PATCH] added mqtt and cleanup --- .gitignore | 2 ++ main/LEDMatrix.cpp | 26 +++++++++++++++++ main/LEDMatrix.h | 2 ++ main/conf.h | 0 main/main.ino | 71 +++++++++++++++++++++++++++++----------------- 5 files changed, 75 insertions(+), 26 deletions(-) create mode 100644 .gitignore create mode 100644 main/conf.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..52f34f8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode +secrets.h \ No newline at end of file diff --git a/main/LEDMatrix.cpp b/main/LEDMatrix.cpp index 35024ac..8df51f5 100644 --- a/main/LEDMatrix.cpp +++ b/main/LEDMatrix.cpp @@ -1,4 +1,5 @@ #include "LEDMatrix.h" +#include "font.h" LEDMatrix::LEDMatrix(uint8_t width, uint8_t height, uint16_t numLeds, uint16_t lastVisibleLed, CRGB* leds) : kMatrixWidth(width), kMatrixHeight(height), NUM_LEDS(numLeds), LAST_VISIBLE_LED(lastVisibleLed), leds(leds) { @@ -40,4 +41,29 @@ void LEDMatrix::drawPixel(uint8_t x, uint8_t y, CRGB color) { if (index <= LAST_VISIBLE_LED) { leds[index] = color; } +} + +void LEDMatrix::drawChar(char c, int x, int y, CRGB color) { + if (c < 32 || c > 127) return; // Ignore non-printable characters + + for (int i = 0; i < 5; i++) { + uint8_t line = font[(c - 32) * 5 + i]; // Access the font array as a one-dimensional array + for (int j = 0; j < 7; j++) { + if (line & 0x1) { + drawPixel(x + i, y + j, color); + } + line >>= 1; + } + } +} + +void LEDMatrix::drawText(const char* text, int x, int y, CRGB color) { + while (*text) { + drawChar(*text++, x, y, color); + x += 6; // Move to the next character position + if (x >= kMatrixWidth) { // Handle overflow to the next screen + x = 0; + y += 8; // Move to the next line + } + } } \ No newline at end of file diff --git a/main/LEDMatrix.h b/main/LEDMatrix.h index 0e3467c..6c154af 100644 --- a/main/LEDMatrix.h +++ b/main/LEDMatrix.h @@ -9,6 +9,8 @@ public: LEDMatrix(uint8_t width, uint8_t height, uint16_t numLeds, uint16_t lastVisibleLed, CRGB* leds); uint16_t XY(uint8_t x, uint8_t y); void drawPixel(uint8_t x, uint8_t y, CRGB color); + void drawChar(char c, int x, int y, CRGB color); + void drawText(const char* text, int x, int y, CRGB color); private: const uint8_t kMatrixWidth; diff --git a/main/conf.h b/main/conf.h new file mode 100644 index 0000000..e69de29 diff --git a/main/main.ino b/main/main.ino index 48fc809..2df3c0b 100644 --- a/main/main.ino +++ b/main/main.ino @@ -1,6 +1,9 @@ #include #include "LEDMatrix.h" -#include "font.h" + +#include +#include +#include "secrets.h" // Params for width and height const uint8_t kMatrixWidth = 32; @@ -13,42 +16,58 @@ const uint8_t kMatrixHeight = 16; 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(leds, NUM_LEDS); FastLED.setBrightness(40); FastLED.clear(); FastLED.show(); -} -void drawChar(char c, int x, int y, CRGB color) { - if (c < 32 || c > 127) return; // Ignore non-printable characters + WiFi.begin(ssid, password); + client.begin("192.168.178.155", 1883, net); + client.onMessage(messageReceived); - for (int i = 0; i < 5; i++) { - uint8_t line = font[(c - 32) * 5 + i]; // Access the font array as a one-dimensional array - for (int j = 0; j < 7; j++) { - if (line & 0x1) { - matrix.drawPixel(x + i, y + j, color); - } - line >>= 1; - } - } -} + connect(); -//TODO: Fix letter overflow -void drawText(const char* text, int x, int y, CRGB color) { - while (*text) { - drawChar(*text++, x, y, color); - x += 6; // Move to the next character position - if (x >= kMatrixWidth) { // Handle overflow to the next screen - x = 0; - y += 8; // Move to the next line - } - } } 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(); - drawText("abwawawawaa", 0, 0, CRGB::White); + matrix.drawText(payload.c_str(), 0, 0, CRGB::White); FastLED.show(); - delay(1000); + // 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()`. } \ No newline at end of file