diff --git a/src/mqtt_esp32/mqtt_esp.ino b/src/mqtt_esp32/mqtt_esp.ino new file mode 100644 index 0000000000000000000000000000000000000000..1c7339cf021bc0d24163ffa37bc15de20be2555d --- /dev/null +++ b/src/mqtt_esp32/mqtt_esp.ino @@ -0,0 +1,149 @@ +#include <WiFi.h> +#include <PubSubClient.h> +#include <HardwareSerial.h> + +#define MATRIX_SIZE 10 // 10x10 +#define IMG_DATA_SIZE MATRIX_SIZE * MATRIX_SIZE * 3 //DATA SIZE FOR ONE MATRIX = RGB * MATRIX_SIZE + +// WiFi +const char *ssid = "uni-ete2"; // Enter your WiFi name +const char *password = "uni-ete2-esp32"; // Enter WiFi password + +// MQTT Broker +const char *mqtt_broker = "192.168.1.103"; +const char *topic_send = "fromesp"; +const char *topic_rcv = "toesp"; +const int mqtt_port = 1883; + +WiFiClient espClient; +PubSubClient client(espClient); + +// UART2 +HardwareSerial SerialPort(2); // use UART2 + +uint8_t cluster_size_x = 1; +uint8_t cluster_size_y = 1; + +void setup() +{ + // Set software serial baud to 115200; + Serial.begin(115200); + + // Set UART2 serial + SerialPort.begin(115200, SERIAL_8N1, 16, 17); + + // Connecting to a WiFi network + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.println("Connecting to WiFi.."); + } + Serial.println("Connected to the Wi-Fi network"); + + //connecting to a mqtt broker + client.setServer(mqtt_broker, mqtt_port); + client.setCallback(callback); + client.setBufferSize(2000); + + while (!client.connected()) + { + String client_id = "esp32-client-"; + client_id += String(WiFi.macAddress()); + Serial.printf("The client %s connects to the public MQTT broker\n", client_id.c_str()); + if (client.connect(client_id.c_str())) + { + Serial.println("Public EMQX MQTT broker connected"); + } else + { + Serial.print("failed with state "); + Serial.print(client.state()); + delay(2000); + } + } + + // Publish and subscribe + client.publish(topic_send, "Hi, I'm ESP32 ^^"); + client.subscribe(topic_rcv); +} + +uint8_t* mqtt_packet_to_raw(uint8_t* packet, unsigned int* length) +{ + for(int i = 0; i < *length; i++) + { + if(packet[i] == ':') + { + *length -= i - 2 - 2; + return packet + i + 2; + } + } + + return NULL; +} + +void send_data_to_matrix(uint8_t idx, uint8_t idy, uint8_t* data, uint32_t length) +{ + // ADD ID TO LEN + length += 2; + + // SEND UART PACKET HEADER + SerialPort.print((char)0x77); + SerialPort.print((char)length); + + // SEND MATRIX ID + SerialPort.print((char)idx); + SerialPort.print((char)idy); + + uint8_t crc = 0; + // SEND RGB DATA + for(int i = 0; i < length; i+=3) + { + // COMPUTE COLOR IN 16 BITS + uint8_t r = data[i] & 0x1F; + uint8_t g = data[i+1] & 0x3F; + uint8_t b = data[i+2] & 0x1F ; + uint16_t rgb = r << 11 | g << 5 | b; + + // SPLIT ON 8 BITS + uint8_t rgb_0 = rgb >> 8; + uint8_t rgb_1 = rgb & 0x0F; + + // COMPUTE CRC + crc = crc ^ rgb_0 ^ rgb_1; + + // SEND RGB + SerialPort.print((char) rgb_0); + SerialPort.print((char) rgb_1); + } + + // SEND UART PACKET FOOTER + SerialPort.print((char)(crc ^ length)); + SerialPort.print((char)0xAA); +} + +void callback(char *topic, byte *payload, unsigned int length) +{ + // TERMINAL SERIAL + Serial.print("Message arrived in topic: "); + Serial.println(topic); + + // GET RAW DATA + uint8_t* raw_data = mqtt_packet_to_raw(payload, &length); + + // SEND UART PACKET FOR EACH MATRIX + for(int x = 0; x < cluster_size_x; x++) + { + for(int y = cluster_size_y - 1; y >= 0; y--) + { + // SEND UART PACKET + send_data_to_matrix(x, y, raw_data, IMG_DATA_SIZE); + + // MOVE FORWARD IN DATA + raw_data += IMG_DATA_SIZE; + } + } +} + +void loop() { + // put your main code here, to run repeatedly: + client.loop(); +} diff --git a/src/mqtt_receive/BOOT.PY b/src/mqtt_hepialight/BOOT.PY similarity index 100% rename from src/mqtt_receive/BOOT.PY rename to src/mqtt_hepialight/BOOT.PY diff --git a/src/mqtt_receive/MAIN.PY b/src/mqtt_hepialight/MAIN.PY similarity index 100% rename from src/mqtt_receive/MAIN.PY rename to src/mqtt_hepialight/MAIN.PY diff --git a/src/mqtt_receive/USERLIB.PY b/src/mqtt_hepialight/USERLIB.PY similarity index 100% rename from src/mqtt_receive/USERLIB.PY rename to src/mqtt_hepialight/USERLIB.PY