#include #include #include #include #include // Adapt to your setup const char* ssid = "YourNetworkNameHere"; const char* password = "YourPasswordHere"; const char* mqtt_server = "192.168.0.123"; const char* client_name = "ESP32Matrix"; WiFiClient espClient; PubSubClient client(espClient); // connector pinout //G1 R1 | //GND B1 | //G2 R2 | //GND B2 | //B A | //D C | //LAT CLK| //GND OE | //Default connections //uint8 OE = 23; //uint8 CLK = 22; //uint8 LAT = 03; //uint8 CH_A = 21; //uint8 CH_B = 19; //uint8 CH_C = 18; //uint8 CH_D = 5; //uint8 R1 = 17; //uint8 G1 = 16; //uint8 BL1 = 4; //uint8 R2 = 0; //uint8 G2 = 2; //uint8 BL2 = 15; // Changed R2 to pin 25 and LAT to pin 26 ESP32RGBmatrixPanel matrix(23, 22, 26, 17, 16, 04, 25, 02, 15, 21, 19, 18, 5); void setup_wifi() { delay(10); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); // Wait for connection WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("WiFi connected - ESP IP address: "); Serial.println(WiFi.localIP()); } // Gets called whenever a mqtt message arrives on a topic we subscribed for void callback(String topic, byte* message, unsigned int length) { Serial.print("Message arrived on topic: "); Serial.print(topic); static char buf[128]; snprintf(buf, 128, ". Message (%u bytes): ", length); Serial.print(buf); String msg; for(unsigned int i = 0; i < length; i++) { Serial.print((char)message[i]); msg += (char)message[i]; } Serial.println(); // Convert string to int and set color of matrix static uint8_t r = 0, g = 0, b = 0; if(topic == "matrix/red") r = msg.toInt(); if(topic == "matrix/green") g = msg.toInt(); if(topic == "matrix/blue") b = msg.toInt(); snprintf(buf, 128, "color: (%u, %u, %u)", r, g, b); Serial.println(buf); matrix.fillScreen(ESP32RGBmatrixPanel::AdafruitColor(r, g, b)); Serial.println(); } // In case we loose connection void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect if (client.connect(client_name)) { Serial.println("connected"); // Just subscribe to everything concerning the matrix client.subscribe("matrix/+"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } } // declare timer and callback which updates the matrix hw_timer_t* displayUpdateTimer = nullptr; void IRAM_ATTR onDisplayUpdate() { matrix.update(); } void setup() { // Setup serial, wifi, and mqtt Serial.begin(115200); setup_wifi(); client.setServer(mqtt_server, 1883); client.setCallback(callback); matrix.setBrightness(2); matrix.fillScreen(ESP32RGBmatrixPanel::AdafruitColor(0, 55, 55)); // Setup timer 0 counting upwards with prescaler dividing by 160, // resulting in 0.5MHz update frequency displayUpdateTimer = timerBegin(0, 160, true); if(displayUpdateTimer){ // Attach callback to timer timerAttachInterrupt(displayUpdateTimer, &onDisplayUpdate, true); // Raise interrupt when counter reaches value 2 (i.e. every 2µs) timerAlarmWrite(displayUpdateTimer, 2, true); // Enable timer timerAlarmEnable(displayUpdateTimer); } else { Serial.println("Failed to setup timer"); } matrix.update(); } void loop() { // Reconnect if connection was lost if (!client.connected()) { reconnect(); } if(!client.loop()) client.connect(client_name); }