Advertisement
djred2000

E1.31 Receive Arduino Mega 2560

Jun 27th, 2015
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. #include "FastLED.h"
  2.  
  3. // enter desired universe and subnet (sACN first universe is 1)
  4. #define DMX_SUBNET 0
  5. #define DMX_UNIVERSE 1
  6. #define E131_BUFFER_MAX 638
  7. #define E131_ADDRESS_OFFSET 126
  8.  
  9.  
  10. // buffers for receiving and sending data
  11. unsigned char E131Buffer[E131_BUFFER_MAX]; //buffer to hold incoming packet,
  12.  
  13. //Pixel Setup
  14. #define NUM_LEDS 50 //total number of pixels
  15. #define DATA_PIN 51 //pin pixels are connected to
  16. CRGB leds[NUM_LEDS]; //pixel array
  17.  
  18. void setup()
  19. {
  20. Serial.begin(115200); //initialize serial for USB serial monitor
  21. Serial1.begin(115200); //initialize serial input from ESP8266
  22. FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS); //initialize pixels
  23. Serial.println("Setup Complete");
  24. } //end setup
  25.  
  26. void E131Received(unsigned char* pbuff, int count)
  27. {
  28. if ( pbuff[113] == DMX_SUBNET )
  29. {
  30. if ( pbuff[114] == DMX_UNIVERSE )
  31. {
  32. if ( pbuff[E131_ADDRESS_OFFSET-1] == 0 ) //start code must be 0
  33. {
  34. leds[0].setRGB(pbuff[127],pbuff[128],pbuff[129]); //set pixel values
  35. Serial.println(pbuff[126]); //print value of byte 126 which is DMX channel 1
  36. }
  37. }
  38. }
  39. FastLED.show(); //output to pixels
  40. }
  41.  
  42.  
  43.  
  44. //checks to see if packet is E1.31 data
  45. int checkE131Headers(unsigned char* messagein, int messagelength)
  46. {
  47. Serial.print("Byte 1:");
  48. Serial.println(messagein[1],HEX);
  49. Serial.print("Byte 4:");
  50. Serial.println(messagein[4],HEX);
  51. Serial.print("Byte 12:");
  52. Serial.println(messagein[12],HEX);
  53. if ( messagein[1] == 0x10 && messagein[4] == 0x41 && messagein[12] == 0x37)
  54. {
  55. int ChannelCount = messagein[123] * 256 + messagein[124]; // number of values plus start code
  56. return ChannelCount -1; //Return how many values are in the packet.
  57. }
  58. return 0;
  59. }
  60.  
  61. void loop()
  62. {
  63. if (Serial1.available() > 0)
  64. {
  65. int packetSize = Serial1.readBytes(E131Buffer,E131_BUFFER_MAX); //read data into buffer and output size. Size should be 638 bytes.
  66. if(packetSize) //if data has been received
  67. {
  68. int TotalChannels = checkE131Headers(E131Buffer, packetSize); //pass data to see if it is E1.31
  69. if (TotalChannels) //if data comes back as E1.31
  70. {
  71. Serial.println("E1.31 Packet Received");
  72. E131Received(E131Buffer, TotalChannels); //process data function
  73. }
  74. }
  75. }
  76. } //end loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement