NOTE: you need to set the CPU Frequency to 160 MHz to run something like this
#include <ESP8266WiFi.h>
#include <E131.h>
// set the pin numbers of the in/outputs
int output_1 = 12; // (D6) pin number of output 1 to triac
int output_2 = 13; // (D7) pin number of output 2 to triac
int output_3 = 14; // (D5) pin number of output 3 to triac
int output_4 = 16; // (D0) pin number of output 4 to triac
int output_D = 15; // (D8) debug waveform output
int input_zc = 4; // (D2) pin number of zero cross input
// variables for the DMX value & dummy untill E1.31 arrives
int channel_value_1 = 255; // update & dummy values for channel data
int channel_value_2 = 155; // update & dummy values for channel data
int channel_value_3 = 55; // update & dummy values for channel data
int channel_value_4 = 1; // update & dummy values for channel data
// general variables
long int num_channels; // number of DMX values in E1.31 packet
int loop_number = 255; // number of times too/looped
unsigned int step_ends; // single loop time value
//unsigned int step_delay = 30; // delay for 60Hz
unsigned int step_delay = 35; // delay for 50Hz
// E1.31 setup
const char ssid[] = "YOUR SSID"; // AP SSID
const char passphrase[] = "YOUR PASSPHRASE"; // AP password
const int universe = 101; // universe number
const int channel_1 = 1; // output 1 channel number
const int channel_2 = 2; // output 2 channel number
const int channel_3 = 3; // output 3 channel number
const int channel_4 = 4; // output 4 channel number
E131 e131;
void setup() {
// start serial
Serial.begin(115200); // outputs - IP etc
// set the function & state of the in / output pins
pinMode(input_zc, INPUT); // set pin 4 as input
pinMode(output_1, OUTPUT); // set pin 12 (D6) as output
pinMode(output_2, OUTPUT); // set pin 13 (D7) as output
pinMode(output_3, OUTPUT); // set pin 14 (D5) as output
pinMode(output_4, OUTPUT); // set pin 16 (D0) as output
pinMode(output_D, OUTPUT); // set pin 15 (D8) as output
READ_PERI_REG(PERIPHS_GPIO_BASEADDR + GPIO_ENABLE_ADDRESS) | 0xF000); // set gpio 12 -> 15 as outputs
pinMode(output_D, OUTPUT); // set pin 15 (D8) as output
digitalWrite(output_1, HIGH); // set output high / off
digitalWrite(output_2, HIGH); // set output high / off
digitalWrite(output_3, HIGH); // set output high / off
digitalWrite(output_4, HIGH); // set output high / off
// select unicast or multicast - one must be commented out
//e131.begin(ssid, passphrase); // unicast
e131.beginMulticast(ssid, passphrase, universe); // multicast
}
void loop() {
num_channels = e131.parsePacket(); //if a new packet has arrived parse it
// do this if a new packet arrives
if (num_channels) { // if a new packet has arrived - set new channel values
channel_value_1 = (e131.data[channel_1]); // update with value from E1.31 packet
channel_value_2 = (e131.data[channel_2]); // update with value from E1.31 packet
channel_value_3 = (e131.data[channel_3]); // update with value from E1.31 packet
channel_value_4 = (e131.data[channel_4]); // update with value from E1.31 packet
step_ends += 870; // as we didn't look for the zero cross & set 'stop_ends' (don't have time to when a packet comes in)
// use the value from the last loop and add some time to make the end time correct
}
else { // do this if NO new packet
while (!digitalRead(input_zc));
while (digitalRead(input_zc)) {
step_ends = micros(); // set time to now - saves a little time doing it in this loop
}
}
do {
// switch on output / triac if we have waited long enough
if (loop_number == channel_value_1)
WRITE_PERI_REG(PERIPHS_GPIO_BASEADDR + GPIO_OUT_W1TC_ADDRESS, 0x1000); // set output low / on
if (loop_number == channel_value_2)
WRITE_PERI_REG(PERIPHS_GPIO_BASEADDR + GPIO_OUT_W1TC_ADDRESS, 0x2000); // set output low / on
if (loop_number == channel_value_3)
WRITE_PERI_REG(PERIPHS_GPIO_BASEADDR + GPIO_OUT_W1TC_ADDRESS, 0x4000); // set output low / on
if (loop_number == channel_value_4)
WRITE_PERI_REG(RTC_GPIO_OUT, READ_PERI_REG(RTC_GPIO_OUT) & ~1); // set output low / on
// wait untill it's time to loop & set the next outputs if required
step_ends += step_delay; // add delay time to last end time to get new end time
while (micros() < step_ends) { // check time now against end time & loop
delay(0); // the magic delay - frees the processor to do some quick housekeeping, WiFi & stack stuff
}
loop_number--; // reduce loop counter by 1
} while (loop_number > 0); // 'do' it all again unless ==0
WRITE_PERI_REG(PERIPHS_GPIO_BASEADDR + GPIO_OUT_W1TS_ADDRESS, 0xF000); // re-sets all outputs high as we are close to zero cross
WRITE_PERI_REG(RTC_GPIO_OUT, READ_PERI_REG(RTC_GPIO_OUT) | 1); // set output high / on
loop_number = 255; // re-set the value for loop counter ready for the next run
}