lostbucket
New member
Code:
/* A simple sketch to switch relays/SSRs from an E1.31 source.
* Copyright 2017 Barnabybear <diychristmas.org> Full details at end of code.
* First issue 25-Oct-2017
*
* Ethercard section based on a sketch 'udpListner' by Brian Lee <cybexsoft@hotmail.com> 2013-4-7
*/
#include <EtherCard.h>
#include <IPAddress.h>
// sets static IP
#define STATIC 1 // set to 1 to disable DHCP (adjust myip/gwip values below)
#if STATIC
// ethernet interface ip address
static byte myip[] = { 192, 168, 1, 62 };
// gateway ip address
static byte gwip[] = { 192, 168, 1, 1 };
#endif
// ethernet mac address - must be unique on your network.
static byte mymac[] = { 0x70, 0x69, 0x69, 0x2D, 0x30, 0x31 };
// the universe number to listen for.
unsigned int universe = 1;
// the number of outputs used (1 to 16).
const byte num_outs = 14;
// the channel numbers to apply to the outputs (1 to 512).
int channel[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
// the output numbers to use - channel order from above is displayed on these outputs.
// 10, 11, 12 & 13 are used by the ethernet module.
byte output_num[16] = {2, 3, 4, 5, 6, 7, 8, 9, 14, 15, 16, 17, 18, 19};
// to use all 16 outputs - change 'num_outs' to 16.
// comment out the line above and uncomment the line below.
// NOTE: you will need to remove the nano from your circuit to flash it
// as we are now using Tx & Rx to drive relays and comment out the
// serial output for testing or it will switch your relays/SSRs.
//byte output_num[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 14, 15, 16, 17, 18, 19};
// set outputs as standard or inverted - one pair must be commented out.
// standard outputs: on = high, off = low.
bool out_on = 1;
bool out_off = 0;
// inverted outputs: on = low, high = high.
//bool out_on = 0;
//bool out_off = 1;
// enables the serial output for setup testing - one set must be commented out.
//bool setup_output = 0; // serial output disabled
bool setup_output = 1; // serial output enabled
int n; // a variable for counters
byte data_1; // a variable for data
byte Ethernet::buffer[681]; // tcp/ip send and receive buffer
//callback that prints received packets to the serial port
void sACN_data(uint16_t dest_port, uint8_t src_ip[IP_LEN], uint16_t src_port, const char *data, uint16_t len) {
// check to see if the packet is an ASC-E1.17 type
if (!((data[4] ^ 0x41) && (data[5] ^ 0x53) && (data[6] ^ 0x43) && (data[7] ^ 0x2D) &&
(data[8] ^ 0x45) && (data[9] ^ 0x31) && (data[10] ^ 0x2E) && (data[11] ^ 0x31) &&
(data[12] ^ 0x37))) {
// check to see if the universe number is correct
if (!((universe ^ data[114]) + (universe >> 8 ^ data[113]))) {
if (setup_output) {
Serial.print("Universe = ");
Serial.println((data[113] <<8)+(data[114]));
}
// Serial.print("Channel count = ");
// unsigned int x = (((data[123] << 8) + data[124]) - 1);
// Serial.println(x);
for ( n = 0; n < num_outs; n++) {
data_1 = data[channel[n] + 125];
if (data_1 > 127) {
digitalWrite(output_num[n], out_on);
if (setup_output) print_high(); // setup print
}
else {
digitalWrite(output_num[n], out_off);
if (setup_output) print_low(); // debug print - comment out during normal use.
}
}
}
}
}
void setup() {
if (setup_output) Serial.begin(115200); // debug print - comment out during normal use.
for (n = 0; n < num_outs; n++) {
pinMode(output_num[n], OUTPUT);
digitalWrite(output_num[n], out_off);
}
if (setup_output) Serial.println(F("\n[backSoon]")); // debug print - comment out during normal use.
if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0)
if (setup_output) Serial.println(F("Failed to access Ethernet controller")); // debug print - comment out during normal use.
#if STATIC
ether.staticSetup(myip, gwip);
#else
if (!ether.dhcpSetup())
if (setup_output) Serial.println(F("DHCP failed")); // debug print - comment out during normal use.
#endif
if (setup_output) {
ether.printIp("IP: ", ether.myip);
ether.printIp("GW: ", ether.gwip);
ether.printIp("DNS: ", ether.dnsip);
}
//register udpSerialPrint() to port 5568 (e1.31 port).
ether.udpServerListenOnPort(&sACN_data, 5568);
//register udpSerialPrint() to port 42.
ether.udpServerListenOnPort(&sACN_data, 42);
// Serial.end();
for (n = 0; n < num_outs; n++) {
pinMode(output_num[n], OUTPUT);
digitalWrite(output_num[n], LOW);
}
}
void loop() {
//this must be called for ethercard functions to work.
ether.packetLoop(ether.packetReceive());
delay(1);
}
// a function to print data for setup.
void print_high() {
Serial.print("Output ");
Serial.print(output_num[n]);
Serial.print(" : Channel ");
Serial.print(channel[n]);
Serial.print(" : Channel offset ");
Serial.print(channel[n] + 125);
Serial.print(" : Channel data ");
Serial.print(data_1);
Serial.print(" : Output ON ");
Serial.println("");
}
// a function to print data for debuging.
void print_low() {
Serial.print("Output ");
Serial.print(output_num[n]);
Serial.print(" : Channel ");
Serial.print(channel[n]);
Serial.print(" : Channel offset ");
Serial.print(channel[n] + 125);
Serial.print(" : Channel data ");
Serial.print(data_1);
Serial.print(" : Output OFF ");
Serial.println("");
}
/*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
Here is the code to have the nano become a Pixel controller controlling around 325 pixels safely. With a nano and ethernet adapter. Pretneding to be a SansDevice doing E1.31 So you would just pull in the controller you make to xLights / Vixen as a sansdevice using the E1.31 proto.