Skip to content

Instantly share code, notes, and snippets.

@tegila
Last active November 11, 2015 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tegila/e23c5a213938e7c3d445 to your computer and use it in GitHub Desktop.
Save tegila/e23c5a213938e7c3d445 to your computer and use it in GitHub Desktop.
/*
3-5-2015. Werkt niet. UDP Multicast packet niet ontvangen ( multicast.py / multicastlistener.c )
*/
#include <ESP8266WiFi.h>
//#include <WiFiUDP.h>
#include "Artnet.h"
// Artnet settings
Artnet artnet;
#include <APA102.h>
// Define which pins to use.
const uint8_t dataPin = 14;
const uint8_t clockPin = 12;
// Create an object for writing to the LED strip.
APA102<dataPin, clockPin> ledStrip;
// Set the number of LEDs to control.
const uint16_t ledCount = 60*5;
// Create a buffer for holding the colors (3 bytes per color).
rgb_color colors[ledCount];
// Set the brightness to use (the maximum is 31).
const uint8_t brightness = 10;
int status = WL_IDLE_STATUS;
const char* ssid = "home"; // your network SSID (name)
const char* pass = "thisisnotapassword"; // your network password
// Multicast declarations
IPAddress ipMulti(192, 168, 1, 254);
unsigned int portMulti = 6454; // local port to listen on (6454)
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
// setting up Station AP
WiFi.begin(ssid, pass);
// Wait for connect to AP
Serial.print("[Connecting]");
Serial.print(ssid);
int tries=0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
tries++;
if (tries > 30){
break;
}
}
// print your IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
artnet.begin(ip, ipMulti, portMulti);
artnet.setArtDmxCallback(onDmxFrame1);
}
void loop(){
int packetSize;
if (WiFi.status() != WL_CONNECTED) {
digitalWrite(16, LOW);
ESP.restart();
while(true);
} else {
artnet.read();
}
}
void onDmxFrame1(uint16_t universe, uint16_t length, uint8_t sequence, uint8_t* data)
{
int n;
// read universe and put into the right part of the display buffer
for (int i = 0; i < 10; i++) {
n = universe * 10 + i;
colors[n].red = data[i * 3];
colors[n].green = data[i * 3 + 1];
colors[n].blue = data[i * 3 + 2];
}
ledStrip.write(colors, ledCount, brightness);
}
/*The MIT License (MIT)
Copyright (c) 2014 Nathanaël Lécaudé
https://github.com/natcl/Artnet, http://forum.pjrc.com/threads/24688-Artnet-to-OctoWS2811
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.
*/
#include <Artnet.h>
Artnet::Artnet() {}
void Artnet::begin(IPAddress ip, IPAddress ipMulti, unsigned int portMulti)
{
//Ethernet.begin(mac,ip);
Udp.begin(portMulti);
//Udp.beginMulticast(ip, ipMulti, portMulti);
}
uint16_t Artnet::read()
{
packetSize = Udp.parsePacket();
if (packetSize <= MAX_BUFFER_ARTNET && packetSize > 0)
{
//Serial.print("\n packet size = ");
//Serial.print(packetSize);
Udp.read(artnetPacket, MAX_BUFFER_ARTNET);
// Check that packetID is "Art-Net" else ignore
for (byte i = 0 ; i < 9 ; i++)
{
if (artnetPacket[i] != ART_NET_ID[i])
return 0;
}
opcode = artnetPacket[8] | artnetPacket[9] << 8;
if (opcode == ART_DMX)
{
sequence = artnetPacket[12];
incomingUniverse = artnetPacket[14] | artnetPacket[15] << 8;
dmxDataLength = artnetPacket[17] | artnetPacket[16] << 8;
if (artDmxCallback) (*artDmxCallback)(incomingUniverse, dmxDataLength, sequence, artnetPacket + ART_DMX_START);
printPacketContent();
return ART_DMX;
}
if (opcode == ART_POLL)
{
return ART_POLL;
}
}
else
{
return 0;
}
}
void Artnet::printPacketHeader()
{
Serial.print("packet size = ");
Serial.print(packetSize);
Serial.print("\topcode = ");
Serial.print(opcode, HEX);
Serial.print("\tuniverse number = ");
Serial.print(incomingUniverse);
Serial.print("\tdata length = ");
Serial.print(dmxDataLength);
Serial.print("\tsequence n0. = ");
Serial.println(sequence);
}
void Artnet::printPacketContent()
{
for (uint16_t i = ART_DMX_START ; i < dmxDataLength ; i++){
Serial.print(artnetPacket[i], DEC);
Serial.print(" ");
}
Serial.println('\n');
}
/*The MIT License (MIT)
Copyright (c) 2014 Nathanaël Lécaudé
https://github.com/natcl/Artnet, http://forum.pjrc.com/threads/24688-Artnet-to-OctoWS2811
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.
*/
#ifndef ARTNET_H
#define ARTNET_H
#include <Arduino.h>
//#include <Ethernet.h>
//#include <EthernetUdp.h>
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>
// UDP specific
#define ART_NET_PORT 6454
// Opcodes
#define ART_POLL 0x2000
#define ART_DMX 0x5000
// Buffers
#define MAX_BUFFER_ARTNET 530
// Packet
#define ART_NET_ID "Art-Net\0"
#define ART_DMX_START 18
class Artnet {
public:
Artnet();
void begin(IPAddress ip, IPAddress ipMulti, unsigned int localPort);
uint16_t read();
void printPacketHeader();
void printPacketContent();
// Return a pointer to the start of the DMX data
inline uint8_t* getDmxFrame(void)
{
return artnetPacket + ART_DMX_START;
}
inline uint16_t getOpcode(void)
{
return opcode;
}
inline uint8_t getSequence(void)
{
return sequence;
}
inline uint16_t getUniverse(void)
{
return incomingUniverse;
}
inline uint16_t getLength(void)
{
return dmxDataLength;
}
inline void setArtDmxCallback(void (*fptr)(uint16_t universe, uint16_t length, uint8_t sequence, uint8_t* data))
{
artDmxCallback = fptr;
}
private:
//EthernetUDP Udp;
WiFiUDP Udp;
uint8_t artnetPacket[MAX_BUFFER_ARTNET];
uint16_t packetSize;
uint16_t opcode;
uint8_t sequence;
uint16_t incomingUniverse;
uint16_t dmxDataLength;
void (*artDmxCallback)(uint16_t universe, uint16_t length, uint8_t sequence, uint8_t* data);
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment