EthernetUDP.parsePacket hang

Hi guys, I’m working on a project where data for a rgb light is sent:

art-net server > art-net receiver > light

I am currently using my smartphone to serve up the art-net data over wifi, it then goes to an Arduino Duemilanove + ethernet shield + nordic nRF24L01+ wireless transceiver. The arduino should then take the Art-net data and send it using the nRF24L01+ to the lamp.

I have got both halves of the data transfer process working, ie art-net data part, then the data part for the nRF24L01+, but I seem to be having problems getting them to work together!

The code I used for the art-net receiver was from Karistouf, I modified it to work with Arduino 1.0, and I’ve managed to test it. Controlling an LED on the Arduinos pin 3 from my phone worked great.

Unfortunately when I put the code for the art-net side and the nRF24L01+ together the sketch compiles fine but seems to hang on Udp.parsePacket(). I checked the RAM usage in both the art-net code on its own and my cobbled together code, both just before parsePacket(), and my code has more RAM available (I have halved the size of buffer_dmx in my code).

I would be very grateful if someone could have a look over my code the show me where I’ve gone wrong, I’ve been trying to work it out of hours but I think it’s a bit above me at the moment.

Here is the modified code for just the art-net side that works fine:

/*
ARTNET RECEIVER V2

This SCRIPT allows you to use arduino with ethernet shield or wifi shield and recieve artnet data. Up to you to use channels as you want.

It works with Arduino 023 software

If you have implemented ameliorations to this sketch, please, contribute by sending back modifications, ameliorations, derivative sketch. It will be a pleasure
to let them accessible to community



For VVVV patchers, please think to put a mainloop node in your patch: minimum frame rate is 40fps with bulbs. But do not go too much higher ( max 100 fps).

This sketch is part of white cat lighting board suite: /http://www.le-chat-noir-numerique.fr  
wich is sending data to many different types of devices, and includes a direct communication in serial also with arduino likes devices
you may find whitecat interresting because its theatre based logic ( cuelist and automations) AND live oriented ( masters, midi, etc)

(c)Christoph Guillermet
http://www.le-chat-noir-numerique.fr
karistouf@yahoo.fr
*/


#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008
#define short_get_high_byte(x) ((HIGH_BYTE & x) >> 8)
#define short_get_low_byte(x)  (LOW_BYTE & x)
#define bytes_to_short(h,l) ( ((h << 8) & 0xff00) | (l & 0x00FF) );

EthernetUDP Udp;

//MAC and IP of the ethernet shield
//MAC adress of the ethershield is stamped down the shield
//to translate it from hexa decimal to decimal, use: http://www.frankdevelopper.com/outils/convertisseur.php
//HARDWARE
byte mac[] = {  0x90, 0xA2, 0xDA, 0x00, 0x8A, 0x35  };//the mac adress of ethernet shield or uno shield board
byte ip[] = {   192,168,1,177 };// the IP adress of your device, that should be in same universe of the network you are using, here: 192.168.1.x

// the next two variables are set when a packet is received
byte remoteIp[4];        // holds received packet's originating IP
unsigned int remotePort; // holds received packet's originating port

//customisation: edit this if you want for example read and copy only 4 or 6 channels from channel 12 or 48 or whatever.
const int number_of_channels=512; //512 for 512 channels
const int channel_position=1; // 1 if you want to read from channel 1

// buffers
const int MAX_BUFFER_UDP=1024;//definition to do to make work UDP lib
char packetBuffer[MAX_BUFFER_UDP]; //buffer to store incoming data
byte buffer_dmx[number_of_channels+channel_position]; //buffer to store filetered DMX data

// art net parameters
unsigned int localPort = 6454;      // artnet UDP port is by default 6454
const int art_net_header_size=17;
const int max_packet_size=576;
char ArtNetHead[8]="Art-Net";
char OpHbyteReceive=0;
char OpLbyteReceive=0;
short is_artnet_version_1=0;
short is_artnet_version_2=0;
short seq_artnet=0;
short artnet_physical=0;
short incoming_universe=0;
boolean is_opcode_is_dmx=0;
boolean is_opcode_is_artpoll=0;
boolean match_artnet=1;
short Opcode=0;


void setup() {
  //déclaration des ports PWM 
  //ici une diode pour test
  pinMode(3, OUTPUT);  //check with leds + resistance in pwm, this will not work with pins 10 and 11, used by RJ45 shield
  pinMode(5, OUTPUT);  //check with leds + resistance in pwm, this will not work with pins 10 and 11, used by RJ45 shield
  pinMode(6, OUTPUT);  //check with leds + resistance in pwm, this will not work with pins 10 and 11, used by RJ45 shield 
  // démarrage ethernet et serveur UDP
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
}

void loop() {
  
  int packetSize = Udp.parsePacket();
  if( packetSize>art_net_header_size && packetSize<=max_packet_size)//check size to avoid unneeded checks
  {
    Udp.read(packetBuffer,MAX_BUFFER_UDP);  
    //read header
     match_artnet=1;//valeur de stockage
     for (int i=0;i<7;i++)
     {
      if(char(packetBuffer[i])!=ArtNetHead[i])
      {match_artnet=0;break;}//if not corresponding, this is not an artnet packet, so we stop reading
     } 
     
     if (match_artnet==1)//if its an artnet header
     { 
          /*artnet protocole revision, not really needed
          is_artnet_version_1=packetBuffer[10]; 
          is_artnet_version_2=packetBuffer[11];*/
      
          /*sequence of data, to avoid lost packets on routeurs
          seq_artnet=packetBuffer[12];*/
          
          /*physical port of  dmx N°
          //artnet_physical=packetBuffer[13];*/
      
     //operator code enables to know wich type of message Art-Net it is
     Opcode=bytes_to_short(packetBuffer[9],packetBuffer[8]);
     
     if(Opcode==0x5000)//if opcode is DMX type
      {
       is_opcode_is_dmx=1;is_opcode_is_artpoll=0;
       }   
       
     else if(Opcode==0x2000)//if opcode is artpoll 
     {
     is_opcode_is_artpoll=1;is_opcode_is_dmx=0;
     //( we should normally reply to it, giving ip adress of the device)
     } 
     

     if(  is_opcode_is_dmx=1)//if its DMX data we will read it now
     {
     //if you need to filter DMX universes, uncomment next line to have the universe rceived
     //incoming_universe= bytes_to_short(packetBuffer[15],packetBuffer[14])
     
     //getting data from a channel position, on a precise amount of channels, this to avoid to much operation if you need only 4 channels for example
     for(int i=channel_position-1;i< number_of_channels;i++)//channel position
      {
     buffer_dmx[i]= byte(packetBuffer[i+17]);
       }
      }
 
     }//end of sniffing 
    
  }

//stuff to do on PWM or whatever
 analogWrite(3,buffer_dmx[3]);  
 analogWrite(5,buffer_dmx[5]); 
 analogWrite(6,buffer_dmx[6]);   
}

Thank-you in advance!

Deeksie

Here is my cobbled together code that doesn’t work:

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#define short_get_high_byte(x) ((HIGH_BYTE & x) >> 8)
#define short_get_low_byte(x)  (LOW_BYTE & x)
#define bytes_to_short(h,l) ( ((h << 8) & 0xff00) | (l & 0x00FF) );
#define CE 8
#define CSN 7
#define NumLights 2 //Number of lights used

byte lights[NumLights][8]={{1,4,0,0,0,0,0,0},
                           {10,4,0,0,0,0,0,0}};
                           
byte mac[] = {  0x90, 0xA2, 0xDA, 0x00, 0x8A, 0x35  };
byte ip[] = {   192,168,1,178 };
byte remoteIp[4];        // holds received packet's originating IP
unsigned int remotePort; // holds received packet's originating port
const int number_of_channels=256; //512 for 512 channels
const int channel_position=1; // 1 if you want to read from channel 1

// buffers
const int MAX_BUFFER_UDP=1024;//definition to do to make work UDP lib
char packetBuffer[MAX_BUFFER_UDP]; //buffer to store incoming data
byte buffer_dmx[number_of_channels+channel_position]; //buffer to store filetered DMX data

// art net parameters
unsigned int localPort = 6454;      // artnet UDP port is by default 6454
const int art_net_header_size=17;
const int max_packet_size=576;
char ArtNetHead[8]="Art-Net";
char OpHbyteReceive=0;
char OpLbyteReceive=0;
short is_artnet_version_1=0;
short is_artnet_version_2=0;
short seq_artnet=0;
short artnet_physical=0;
short incoming_universe=0;
boolean is_opcode_is_dmx=0;
boolean is_opcode_is_artpoll=0;
boolean match_artnet=1;
short Opcode=0;

EthernetUDP Udp;

void setup()
{ 
  Serial.begin(19200);
  
  pinMode(CE, OUTPUT);
  pinMode(CSN, OUTPUT);
  
  digitalWrite(CE, LOW); // Set high for Rx
  digitalWrite(CSN, HIGH);
  
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
  
  SetupRegisters();
  Serial.println("Setup complete");
}

void loop()
{
  Serial.println("Loop Start");
  int packetSize = Udp.parsePacket();
  Serial.println("Parsed");
  if( packetSize>art_net_header_size && packetSize<=max_packet_size)//check size to avoid unneeded checks
  {
    Serial.println("reading buffer");
    Udp.read(packetBuffer,MAX_BUFFER_UDP);  
    Serial.println("read to buffer");
    //read header
     match_artnet=1;//valeur de stockage
     for (int i=0;i<7;i++)
     {
      if(char(packetBuffer[i])!=ArtNetHead[i])
      {match_artnet=0;break;}//if not corresponding, this is not an artnet packet, so we stop reading
     } 
     
     if (match_artnet==1)//if its an artnet header
     { 
          /*artnet protocole revision, not really needed
          is_artnet_version_1=packetBuffer[10]; 
          is_artnet_version_2=packetBuffer[11];*/
      
          /*sequence of data, to avoid lost packets on routeurs
          seq_artnet=packetBuffer[12];*/
          
          /*physical port of  dmx N°
          //artnet_physical=packetBuffer[13];*/
      
     //operator code enables to know wich type of message Art-Net it is
     Opcode=bytes_to_short(packetBuffer[9],packetBuffer[8]);
     
     if(Opcode==0x5000)//if opcode is DMX type
      {
       is_opcode_is_dmx=1;is_opcode_is_artpoll=0;
       }   
       
     else if(Opcode==0x2000)//if opcode is artpoll 
     {
     is_opcode_is_artpoll=1;is_opcode_is_dmx=0;
     //( we should normally reply to it, giving ip adress of the device)
     } 
     

     if(  is_opcode_is_dmx=1)//if its DMX data we will read it now
     {
     //if you need to filter DMX universes, uncomment next line to have the universe rceived
     //incoming_universe= bytes_to_short(packetBuffer[15],packetBuffer[14])
     
     //getting data from a channel position, on a precise amount of channels, this to avoid to much operation if you need only 4 channels for example
     for(int i=channel_position-1;i< number_of_channels;i++)//channel position
      {
     buffer_dmx[i]= byte(packetBuffer[i+17]);
       }
      }
       Serial.println("end of sniffing");
     }//end of sniffing 
    
  }
  
Serial.println(buffer_dmx[3]);
  
checkChange();
}

void checkChange()
{
  for(int i=0; i<NumLights; i++)
  {
    int count=0;
    for(int y=0; y<lights[i][1]; y++)
    {
      if(buffer_dmx[lights[i][0]+y] != lights[i][2+y])
      {
        lights[i][2+y] = buffer_dmx[lights[i][0]+y];
        count++;
      }
    }
    
    if(count != 0)
    {
      sendColour(lights[i][0], lights[i][1]);
    }
  }
}

void sendColour(byte Channel, byte numChan)
{
  SPI.begin();
  
  //Clear interupts
  digitalWrite(CSN, LOW);
  SPI.transfer(0x20 + 0x07);
  SPI.transfer(B01111110);
  digitalWrite(CSN, HIGH);
  
  //Call setup sub function
  setSend(Channel, numChan);
  
  //Load payload to FIFO
  digitalWrite(CSN, LOW);
  SPI.transfer(0xA0);
  for(int i=0; i<numChan; i++)
  {
    SPI.transfer(buffer_dmx[Channel+i]);
  }
  digitalWrite(CSN, HIGH);
  SPI.end();
  
  //Transmit payload
  digitalWrite(CE, HIGH);
  digitalWrite(CE, LOW);
}

void setSend(byte Channel, byte numChan)
{
  SPI.begin();
  
  byte value = Channel;
  //This section for Rx address
  digitalWrite(CSN, LOW);
  SPI.transfer(0x20 + 0x0A);
  for(int i=0; i<5; i++)
  {
    SPI.transfer(Channel);
    Channel += 32;
  }
  digitalWrite(CSN, HIGH);
  
  //This section for Tx address
  digitalWrite(CSN, LOW);
  SPI.transfer(0x20 + 0x10);
  for(int i=0; i<5; i++)
  {
    SPI.transfer(value);
    value += 32;
  }
  digitalWrite(CSN, HIGH);
  
  digitalWrite(CSN, LOW);
  SPI.transfer(0x20 + 0x11);
  SPI.transfer(numChan);
  digitalWrite(CSN, HIGH);
  
  SPI.end();
}

void SetupRegisters()
{
  byte reg[5][2] = {{0x00, B00001110}, //Set B0 for Tx, set B1 for Rx
                    {0x02, B00000001}, //Enable RX pipe 1
                    {0x05, B01010100}, //Set RF CHANNEL
                    {0x06, B00000111}, //Set RF power and speed
                    {0x11, B00000011}}; //RF pipe payload: 3 bytes
  SPI.begin();
  
  for(byte B=0; B<5; B++)
  {
    digitalWrite(CSN, LOW);
    SPI.transfer(0x20 + reg[B][0]);
    SPI.transfer(reg[B][1]);
    digitalWrite(CSN, HIGH);
    delay(1);
  }
  SPI.end();
}