Advertisement
Guest User

WIFI WOLF

a guest
Jan 24th, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. /*
  2. This example will receive multiple universes via Art-Net and control a strip of
  3. WS2812 LEDs via the FastLED library: https://github.com/FastLED/FastLED
  4. This example may be copied under the terms of the MIT license, see the LICENSE file for details
  5. */
  6.  
  7. #if defined(ARDUINO_ARCH_ESP32)
  8. #include <WiFi.h>
  9. #else
  10. #include <ESP8266WiFi.h>
  11. #endif
  12. #include <WiFiUdp.h>
  13. #include <ArtnetWifi.h>
  14. #include <FastLED.h>
  15.  
  16. // Wifi settings
  17. const char* ssid = "ZyXEL NBG-418N v2";
  18. const char* password = "JNSQU39336";
  19.  
  20. // LED settings
  21. const int numLeds = 120; // CHANGE FOR YOUR SETUP
  22. const int numberOfChannels = numLeds * 3; // Total number of channels you want to receive (1 led = 3 channels)
  23. const byte dataPin = 7;
  24. CRGB leds[numLeds];
  25.  
  26. // Art-Net settings
  27. ArtnetWifi artnet;
  28. const int startUniverse = 0; // CHANGE FOR YOUR SETUP most software this is 1, some software send out artnet first universe as 0.
  29.  
  30. // Check if we got all universes
  31. const int maxUniverses = numberOfChannels / 512 + ((numberOfChannels % 512) ? 1 : 0);
  32. bool universesReceived[maxUniverses];
  33. bool sendFrame = 1;
  34. int previousDataLength = 0;
  35.  
  36.  
  37. // connect to wifi – returns true if successful or false if not
  38. boolean ConnectWifi(void)
  39. {
  40. boolean state = true;
  41. int i = 0;
  42.  
  43. WiFi.begin(ssid, password);
  44. Serial.println("");
  45. Serial.println("Connecting to WiFi");
  46.  
  47. // Wait for connection
  48. Serial.print("Connecting");
  49. while (WiFi.status() != WL_CONNECTED) {
  50. delay(500);
  51. Serial.print(".");
  52. if (i > 20){
  53. state = false;
  54. break;
  55. }
  56. i++;
  57. }
  58. if (state){
  59. Serial.println("");
  60. Serial.print("Connected to ");
  61. Serial.println(ssid);
  62. Serial.print("IP address: ");
  63. Serial.println(WiFi.localIP());
  64. } else {
  65. Serial.println("");
  66. Serial.println("Connection failed.");
  67. }
  68.  
  69. return state;
  70. }
  71.  
  72. void initTest()
  73. {
  74. for (int i = 0 ; i < numLeds ; i++) {
  75. leds[i] = CRGB(127, 0, 0);
  76. }
  77. FastLED.show();
  78. delay(500);
  79. for (int i = 0 ; i < numLeds ; i++) {
  80. leds[i] = CRGB(0, 127, 0);
  81. }
  82. FastLED.show();
  83. delay(500);
  84. for (int i = 0 ; i < numLeds ; i++) {
  85. leds[i] = CRGB(0, 0, 127);
  86. }
  87. FastLED.show();
  88. delay(500);
  89. for (int i = 0 ; i < numLeds ; i++) {
  90. leds[i] = CRGB(0, 0, 0);
  91. }
  92. FastLED.show();
  93. }
  94.  
  95. void onDmxFrame(uint16_t universe, uint16_t length, uint8_t sequence, uint8_t* data)
  96. {
  97. sendFrame = 1;
  98. // set brightness of the whole strip
  99. if (universe == 15)
  100. {
  101. FastLED.setBrightness(data[0]);
  102. FastLED.show();
  103. }
  104.  
  105. // Store which universe has got in
  106. if ((universe - startUniverse) < maxUniverses) {
  107. universesReceived[universe - startUniverse] = 1;
  108. }
  109.  
  110. for (int i = 0 ; i < maxUniverses ; i++)
  111. {
  112. if (universesReceived[i] == 0)
  113. {
  114. //Serial.println("Broke");
  115. sendFrame = 0;
  116. break;
  117. }
  118. }
  119.  
  120. // read universe and put into the right part of the display buffer
  121. for (int i = 0; i < length / 3; i++)
  122. {
  123. int led = i + (universe - startUniverse) * (previousDataLength / 3);
  124. if (led < numLeds)
  125. leds[led] = CRGB(data[i * 3], data[i * 3 + 1], data[i * 3 + 2]);
  126. }
  127. previousDataLength = length;
  128.  
  129. if (sendFrame)
  130. {
  131. FastLED.show();
  132. // Reset universeReceived to 0
  133. memset(universesReceived, 0, maxUniverses);
  134. }
  135. }
  136.  
  137. void setup()
  138. {
  139. Serial.begin(115200);
  140. ConnectWifi();
  141. artnet.begin();
  142. FastLED.addLeds<WS2812, dataPin, GRB>(leds, numLeds);
  143. initTest();
  144.  
  145. // this will be called for each packet received
  146. artnet.setArtDmxCallback(onDmxFrame);
  147. }
  148.  
  149. void loop()
  150. {
  151. // we call the read function inside the loop
  152. artnet.read();
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement