I am looking to control a large number of LEDs, purely for entertainment purposes and not work related.
The user interface (UI) is coded in javascript and will be sending data (a color array) approximately 60 times per second.
The LED driver is a microcontroller (ESP8266) programmed in C++ using the websocket library with the Arduino IDE.
The LEDs are addressable and controlled over a single pin. More information on the data transmission between the LEDs and the microcontroller can be found in this datasheet.But I am utilizing a simplified library that controls them using i2s and DMA to offload CPU usage.
Currently, I am sending one color to the first LED. The color is converted into a hex string to save space.
Client
//javascript
websocket.send('#ff0000');
Server
//c++
if(payload[0]=='#'){
uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16);
pixels[1].R=((rgb >> 16) & 0xFF);
pixels[1].G=((rgb >> 8) & 0xFF);
pixels[1].B=((rgb >> 0) & 0xFF);
}
Initially, I thought about sending a "hex" like string and then splitting it...
"#ff0000ff0000ff0000"
//or
"#ff0000#ff0000#ff0000"
However, after discussing with others, they suggested using binary or ascii. This seems difficult for me so I would require some examples, especially for the C++ part. I'm unsure if the websocket library can handle ascii or binary properly and then convert it back to numbers.
The problem:
The microcontroller has several limitations including memory constraints, transmission speed, and the time it takes to convert data. Therefore, the final color array should be small in size and easy to convert back to RGB values.
What is the best way to send and receive a color array?
The selection of various libraries involves thorough research:
- Arduino IDE: User-friendly with the chip and has a large community.
- Arduino ESP8266 Websocket lib: Reportedly the fastest based on the Arduino IDE.
- WS2812B i2s: Utilizes DMA to control LEDs without involving the CPU.
- Using JavaScript for UI: Compatible with all mobile devices and free to use.
- Websockets: Allows bidirectional real-time data transfer and works on all mobile devices, also free.