#include #include #include #include #include #include // Adapt to your setup const char* ssid = "UPC7110132"; const char* password = "j48yxwSjuktj"; const char* mqtt_server = "192.168.0.115"; 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); Wire.beginTransmission(0x54); Wire.write(0); Wire.write(r); Wire.write(g); Wire.write(b); Wire.endTransmission(); 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); Wire.begin(); } void loop() { // Reconnect if connection was lost if (!client.connected()) { reconnect(); } if(!client.loop()) client.connect(client_name); }