Hello! I've built the following project:
https://learn.sparkfun.com/tutorials/sparkfun-esp32-dmx-to-led-shield/all
and connected it to WS2813 LEDs. I tested the output with FastLED cylon and it works great. I used the examples included and was able to get the ESP32 connected to wifi with an IP. I'm very familiar with Resolume and artnet, but as soon as I upload my sketch to the ESP32, the LEDs output random colors.
Original sketch:
#include <WiFi.h>
#include <WiFiUdp.h>
#include <ArtnetWifi.h> //https://github.com/rstephan/ArtnetWifi
#include <FastLED.h>
#include <ESP32Servo.h>
//Wifi settings
char ssid[] = "myDMX"; //Change these lines to an existing SSID and Password if you're trying to connect to an existing network
char password[] = "artnetnode";
// Artnet settings
ArtnetWifi artnet;
WiFiUdp UdpSend;
const int startUniverse = 0;
const int endUniverse = 0;//end Universe should be total channels/512
bool sendFrame = 1;
int previousDataLength = 0;
//Pin Definitions for ESP32 WROOM
#define CLOCK 5
#define DATA0 19
#define DATA1 18
#define DATA2 27
//Channel and Peripheral Definitions
#define NUM_LEDS 64
#define NUM_LED_CHANNELS NUM_LEDS * 3 //Ends up being 192 channels for our 8x8 LED matrix
#define PAN_CHANNEL 193
#define TILT_CHANNEL 194
CRGB matrix[NUM_LEDS];
Servo pan;
Servo tilt;
uint8_t hue = 0;
boolean connectWifi(void) //Sets our ESP32 device up as an access point
{
boolean state = true;
WiFi.mode(WIFI_AP_STA);
state = WiFi.softAP(ssid, password);
//Comment out the above two lines and uncomment the below line to connect to an existing network specified on lines 8 and 9
//state = WiFi.begin(ssid, password);
return state;
}
void onDmxFrame(uint16_t universe, uint16_t length, uint8_t sequence, uint8_t* data)
{
// read universe and put into the right part of the display buffer
//DMX data should be sent with the first LED in the string on channel 0 of Universe 0
for (int channel = 0; channel < length; channel++)
{
if (channel < NUM_LED_CHANNELS && channel % 3 == 0) //Only write on every 3rd piece of data so we correctly parse things into our RGB array
{
matrix[channel / 3] = CRGB(data[channel], data[channel + 1], data[channel + 2]);
}
else if (channel == PAN_CHANNEL - 1) //Subtract 1 due to the fact that we index at 0 and ignore the startcode
{
pan.write(map(data[channel], 0, 255, 0, 160));
}
else if (channel == TILT_CHANNEL - 1)
{
tilt.write(map(data[channel], 0, 255, 0, 160));
}
}
previousDataLength = length;
if (universe == endUniverse) //Display our data if we have received all of our universes, prevents incomplete frames when more universes are concerned.
{
FastLED.show();
UdpSend.flush();
}
}
void setup()
{
Serial.begin(115200);
//Fixture Hardware Declarations
FastLED.addLeds<APA102, DATA0, CLOCK, BGR>(matrix, NUM_LEDS);
pan.attach(DATA1);
tilt.attach(DATA2);
if (connectWifi())
{
Serial.println("Connected!");
}
artnet.begin();
artnet.setArtDmxCallback(onDmxFrame);
}
void loop()
{
artnet.read();
}
My modified sketch:
#include <WiFi.h>
#include <WiFiUDP.h>
#include <ArtnetWifi.h> //https://github.com/rstephan/ArtnetWifi
#include <FastLED.h>
// WiFi network name and password:
const char * ssid = "redacted";
const char * password = "redacted";
const int BUTTON_PIN = 0;
const int LED_PIN = LED_BUILTIN;
// Artnet settings
ArtnetWifi artnet;
WiFiUDP UDPSend;
const int startUniverse = 0;
const int endUniverse = 0;//end Universe should be total channels/512
bool sendFrame = 1;
int previousDataLength = 0;
//Pin Definitions for ESP32 WROOM
#define CLOCK 5
#define DATA0 19
#define DATA1 18
#define DATA2 27
//Channel and Peripheral Definitions
#define NUM_LEDS 120
#define NUM_LED_CHANNELS NUM_LEDS * 3 //Ends up being 360 channels
CRGB matrix[NUM_LEDS];
uint8_t hue = 0;
// connect to wifi – returns true if successful or false if not
boolean connectWifi(void)
{
int ledState = 0;
boolean state = true;
int i = 0;
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
Serial.println("Connecting to WiFi network: " + String(ssid));
while (WiFi.status() != WL_CONNECTED) {
// Blink LED while we're connecting:
digitalWrite(LED_PIN, ledState);
ledState = (ledState + 1) % 2; // Flip ledState
delay(500);
Serial.print(".");
if (i > 20){
state = false;
break;
}
i++;
}
if (state){
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
} else {
Serial.println("");
Serial.println("Connection failed.");
}
return state;
}
void onDmxFrame(uint16_t universe, uint16_t length, uint8_t sequence, uint8_t* data)
{
int ledState = 0;
digitalWrite(LED_PIN, ledState);
ledState = (ledState + 1) % 2; // Flip ledState
// read universe and put into the right part of the display buffer
//DMX data should be sent with the first LED in the string on channel 0 of Universe 0
for (int channel = 0; channel < length; channel++)
{
if (channel < NUM_LED_CHANNELS && channel % 3 == 0) //Only write on every 3rd piece of data so we correctly parse things into our RGB array
{
matrix[channel / 3] = CRGB(data[channel], data[channel + 1], data[channel + 2]);
}
}
previousDataLength = length;
if (universe == endUniverse) //Display our data if we have received all of our universes, prevents incomplete frames when more universes are concerned.
{
FastLED.show();
UDPSend.flush();
}
}
void setup()
{
Serial.begin(115200);
//Fixture Hardware Declarations
FastLED.addLeds<APA102, DATA0, CLOCK, BGR>(matrix, NUM_LEDS);
if (connectWifi())
{
Serial.println("Connected to WiFi network: " + String(ssid));
}
artnet.begin();
artnet.setArtDmxCallback(onDmxFrame);
}
void loop()
{
artnet.read();
}