Created
December 8, 2023 01:16
-
-
Save vidplace7/4091d838813f2341d8bdf61456526a9b to your computer and use it in GitHub Desktop.
ESP32 ArtNet DMX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Based upon https://github.com/hideakitai/ArtNet | |
// and https://github.com/sparkfun/SparkFunDMX | |
// Examples: | |
// ArtNet Input: | |
// https://github.com/hideakitai/ArtNet/blob/main/examples/WiFi/receiver/receiver.ino | |
// https://github.com/hideakitai/ArtNet/blob/main/examples/Ethernet/receiver/receiver.ino | |
// DMX Output: | |
// https://github.com/sparkfun/SparkFunDMX/blob/master/examples/Example1-DMXOutput/Example1-DMXOutput.ino | |
// FastLED (for onboard RGB status light) | |
#include <FastLED.h> | |
#define LED_PIN 2 // Pin 2 on Thing Plus C is connected to onboard WS2812 LED | |
#define LED_COLOR_ORDER GRB | |
#define LED_CHIPSET WS2812 | |
#define NUM_LEDS 1 | |
#define LED_BRIGHTNESS 100 | |
CRGB leds[NUM_LEDS]; | |
// ArtNet - Currently using WiFi | |
#include <ArtnetWiFi.h> // Switch to `ArtnetETH.h` after adding Ethernet PHY | |
// Sparkfun DMX | |
#include <SparkFunDMX.h> // Include DMX library | |
SparkFunDMX dmx; // Create DMX object | |
HardwareSerial dmxSerial(2); // Create serial port to be used for DMX interface | |
uint8_t enPin = 21; // Enable pin for DMX shield | |
uint16_t numChannels = 18; // Number of DMX channels, can be up to 512 | |
// WiFi stuff | |
const char* wifi_ssid = "example_ssid"; | |
const char* wifi_pass = "example_pass"; | |
const IPAddress ip(10, 0, 2, 250); | |
const IPAddress gateway(10, 0, 2, 1); | |
const IPAddress subnet(255, 255, 255, 0); | |
// ArtNet - Receiver must be initialized after WiFi | |
ArtnetWiFiReceiver artnet; // Setup ArtNet Receiver | |
uint8_t universe1 = 1; // 0 - 15 | |
void setup() { | |
Serial.begin(115200); // Begin debug serial (not DMX serial) | |
// WiFi stuff | |
WiFi.begin(wifi_ssid, wifi_pass); | |
WiFi.config(ip, gateway, subnet); | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.print("."); // Will log '.' every half-second until connected | |
delay(500); | |
} | |
Serial.print("WiFi connected, IP = "); | |
Serial.println(WiFi.localIP()); | |
// FastLED stuff | |
FastLED.addLeds<LED_CHIPSET, LED_PIN, LED_COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip ); | |
FastLED.setBrightness( LED_BRIGHTNESS ); | |
// DMX stuff | |
dmxSerial.begin(DMX_BAUD, DMX_FORMAT); // Begin DMX serial port | |
dmx.begin(dmxSerial, enPin, numChannels); // Begin DMX driver | |
dmx.setComDir(DMX_WRITE_DIR); // Set DMX communication direction to WRITE mode | |
Serial.println("DMX initialized!"); | |
// ArtNet stuff | |
artnet.begin(); | |
// Events in universe 1 will call "callback(data, size)" | |
artnet.subscribe(universe1, callback); | |
} | |
void callback(const uint8_t* data, const uint16_t size) { | |
// Print serial debug output | |
Serial.print("lambda : artnet data (universe : "); | |
Serial.print(universe1); | |
Serial.print(", size = "); | |
Serial.print(size); | |
Serial.print(") :"); | |
// Set status LED to Purple while writing data | |
leds[0] = CRGB::Purple; | |
FastLED.show(); | |
// Write data (log output + DMX) | |
for (size_t i = 0; i < size; ++i) { | |
Serial.print(data[i]); | |
Serial.print(","); | |
// Write data to DMX channel (+1 because it starts at 1, not 0) | |
dmx.writeByte(data[i], i+1); | |
} | |
// Once all DMX data has been written, update() must be called to actually send it | |
dmx.update(); | |
// Turn off status LED after writing data | |
leds[0] = CRGB::Black; | |
FastLED.show(); | |
// Print newline after each DMX frame | |
Serial.println(); | |
} | |
void loop() { | |
artnet.parse(); // check if artnet packet has come and execute callback | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment