terrag42

ESP8266 Webserver with P9813

Apr 10th, 2017
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 33.13 KB | None | 0 0
  1.  /*-----------------------------------------------------------------------------------------------------
  2. ESP8266 FastLED Webserver code by Jeurgen Bruegl aka Gyro Gearloose
  3.    Access Point                        Web Server  e.g.
  4.    http://192.168.4.1                  http://192.168.1.252 (depends on your network)
  5.  
  6. Spaceship Menorah Project
  7. https://hackaday.io/project/10148-spaceship-menorah
  8. Using Adruino IDE with ESP8266 core (Boards manager), selected WeMos D1 Mini as the board.
  9. --------------------------------------------------------------------------------------*/
  10.  
  11. // FastLED setup ----------   FastLED has to be declared BEFORE the Webserver     ---------------------
  12. #define FASTLED_ESP8266_RAW_PIN_ORDER //maybe not needed, see https://github.com/FastLED/FastLED/wiki/ESP8266-notes
  13. #include "FastLED.h"
  14. FASTLED_USING_NAMESPACE
  15.  
  16. #if defined(FASTLED_VERSION) && (FASTLED_VERSION < 3001000)
  17. #warning "Requires FastLED 3.1 or later; check github for latest code."
  18. #endif
  19.  
  20. #define P9813_PIN   D6
  21. #define CLOCK_PIN   D7
  22. #define LED_TYPE    P9813
  23. #define msg7RESET   D8   //digital pin (no PWM)
  24. #define msg7Strobe  D5 //digital pin (no PWM)
  25. #define COLOR_ORDER   RGB
  26. #define NUM_LEDS    7
  27. CRGB leds[NUM_LEDS];
  28.  
  29. //#define BRIGHTNESS       128
  30. int BRIGHTNESS =           128;   // this is half brightness
  31. int new_BRIGHTNESS =       128;   // shall be initially the same as brightness
  32.  
  33. #define MILLI_AMPERE      15000    // IMPORTANT: set here the max milli-Amps of your power supply 5V 2A = 2000
  34. #define FRAMES_PER_SECOND  120    // here you can control the speed. With the Access Point / Web Server the
  35.                                   // animations run a bit slower.
  36.  
  37. int ledMode = 4;                  // this is the starting animation (Confetti)
  38.  
  39. /*/ variables for colorpal_beat
  40. CRGBPalette16 currentPalette;
  41. CRGBPalette16 targetPalette;
  42. uint8_t maxChanges = 24;
  43. TBlendType    currentBlending;
  44. *///end colorpal_beat variables
  45.  
  46. // Select EITHER ACCESS-Point  OR  WEB SERVER setup
  47. /*
  48. // ACCESS-Point setup ------------------------------------------------------------------------------------------------------
  49. #include <ESP8266WiFi.h>
  50. // comes with Huzzah installation. Enter in Arduino settings:
  51. // http://arduino.esp8266.com/package_esp8266com_index.json
  52.  
  53. const char* ssid = "ESP_FastLED_Access_Point";
  54. const char* password = "";  // set to "" for open access point w/o password; or any other pw (min length = 8 characters)
  55.  
  56. unsigned long ulReqcount;
  57.  
  58. // Create an instance of the server on Port 80
  59. WiFiServer server(80);
  60. //IPAddress apIP(192, 168, 1, 1);                           // if you want to configure another IP address
  61. void setup()
  62. {
  63.   // setup globals
  64.   ulReqcount=0;
  65.  
  66.   // prepare GPIO2       // not necessary for FastLED
  67.   pinMode(2, OUTPUT);
  68.   digitalWrite(2, 0);
  69.   // start serial
  70.   Serial.begin(9600);
  71.   delay(1);
  72.  
  73.   // Acess Point (AP) mode
  74.   WiFi.mode(WIFI_AP);
  75. //  WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));          // if you want to configure another IP address
  76.   WiFi.softAP(ssid, password);
  77.   server.begin();
  78. // end ACCESS-Point setup ---------------------------------------------------------------------------------------------------
  79. */
  80.  
  81. // WEB SERVER setup ---------------------------------------------------------------------------------------------------------
  82. #include <ESP8266WiFi.h>
  83. // comes with Huzzah installation. Enter in Arduino settings:
  84. // http://arduino.esp8266.com/package_esp8266com_index.json
  85.  
  86. const char* ssid = "...";  
  87. const char* password = "...";
  88. unsigned long ulReqcount;
  89. unsigned long ulReconncount;
  90.  
  91. WiFiServer server(80);  // Create an instance of the server on Port 80
  92.  
  93. ///////variables for matrix()
  94. // Palette definitions
  95. CRGBPalette16 currentPalette;
  96. CRGBPalette16 targetPalette;
  97. TBlendType    currentBlending;
  98. void WiFiStart()
  99. {
  100.  
  101.   // Connect to WiFi network
  102.   Serial.println();
  103.   Serial.println();
  104.   Serial.print("Connecting to ");
  105.   Serial.println(ssid);
  106.  
  107.   WiFi.begin(ssid, password);
  108.  
  109.   while (WiFi.status() != WL_CONNECTED) {
  110.     delay(500);
  111.     Serial.print(".");
  112.   }
  113.   Serial.println("");
  114.   Serial.println("WiFi connected");
  115.  
  116.   // Start the server
  117.   server.begin();
  118.   Serial.println("Server started");
  119.  
  120.   // Print the IP address
  121.   Serial.println(WiFi.localIP());
  122. }
  123.  
  124. void setup() {
  125.   ulReqcount=0;         // setup globals for Webserver
  126.   ulReconncount=0;
  127.  
  128.   // prepare GPIO2      // not necessary for FastLED
  129. // pinMode(2, OUTPUT);
  130. //  digitalWrite(2, 0);
  131.  pinMode(msg7RESET, OUTPUT);
  132.  pinMode(msg7Strobe, OUTPUT);
  133.  
  134.   // start serial
  135.   Serial.begin(115200);
  136.   delay(1);
  137.  
  138.   // inital connect
  139.   WiFi.mode(WIFI_STA);
  140.   WiFiStart();
  141. // end WEB SERVER setup -----------------------------------------------------------------------------------------------------
  142.  
  143.  
  144. // now the settings for FastLED  
  145.   delay(2000);          // sanity delay for LEDs
  146.  FastLED.addLeds<LED_TYPE,P9813_PIN,CLOCK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(DirectSunlight); // for APA102 (Dotstar)
  147.  //FastLED.addLeds<WS2812,WS2811_PIN,BGR>(dots, NUM_LEDS).setCorrection(DirectSunlight);
  148. ////////////setup variables for matrix()
  149.   currentPalette  = CRGBPalette16(CRGB::Black);
  150.   targetPalette   = RainbowColors_p;                            // Used for smooth transitioning.
  151.   currentBlending = LINEARBLEND;
  152. }
  153.  
  154. uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  155. uint32_t ticker; //used with millis() to time effects
  156.  
  157. void webserver() {   /// complete web server (same for access point) ////////////////////////////////////////////////////////
  158.   // Check if a client has connected
  159.   WiFiClient client = server.available();
  160.   if (!client)
  161.   {
  162.     return;
  163.   }
  164.  
  165.   // Wait until the client sends some data
  166.   Serial.println("new client");
  167.   unsigned long ultimeout = millis()+250;
  168.   while(!client.available() && (millis()<ultimeout) )
  169.   {
  170.     delay(1);
  171.   }
  172.   if(millis()>ultimeout)
  173.   {
  174.     Serial.println("client connection time-out!");
  175.     return;
  176.   }
  177.  
  178.   // Read the first line of the request
  179.  
  180.   String sRequest = client.readStringUntil('\r');
  181.   Serial.println(sRequest);
  182.   client.flush();
  183.  
  184.   // stop client, if request is empty
  185.   if(sRequest=="")
  186.   {
  187.     Serial.println("empty request! - stopping client");
  188.     client.stop();
  189.     return;
  190.   }
  191.  
  192.   // get path; end of path is either space or ?
  193.   // Syntax is e.g. GET /?pin=MOTOR1STOP HTTP/1.1
  194.   String sPath="",sParam="", sCmd="";
  195.   String sGetstart="GET ";
  196.   int iStart,iEndSpace,iEndQuest;
  197.   iStart = sRequest.indexOf(sGetstart);
  198. //  Serial.print("iStart ");
  199. //  Serial.println(iStart);
  200.  
  201.   if (iStart>=0)
  202.   {
  203.     iStart+=+sGetstart.length();
  204.     iEndSpace = sRequest.indexOf(" ",iStart);
  205.     iEndQuest = sRequest.indexOf("?",iStart);
  206.     if(iEndSpace>0)
  207.     {
  208.       if(iEndQuest>0)
  209.       {
  210.         // there are parameters
  211.         sPath  = sRequest.substring(iStart,iEndQuest);
  212.         sParam = sRequest.substring(iEndQuest,iEndSpace);
  213.       }
  214.       else
  215.       {
  216.         // NO parameters
  217.         sPath  = sRequest.substring(iStart,iEndSpace);
  218.       }
  219.     }
  220.   }
  221.  
  222.   //////////////////////////////////////////////////////////////////////////////////
  223.   // output parameters to serial, you may connect e.g. an Arduino and react on it //
  224.   //////////////////////////////////////////////////////////////////////////////////
  225.   if(sParam.length()>0)
  226.   {
  227.     int iEqu=sParam.indexOf("=");
  228.     if(iEqu>=0)
  229.     {
  230.       sCmd = sParam.substring(iEqu+1,sParam.length());
  231.       Serial.print("We are in output Parameters, value is: ");
  232.       Serial.println(sCmd);
  233.       char carray[4];                                // values 0..255 = 3 digits; array = digits + 1
  234.       sCmd.toCharArray(carray, sizeof(carray));      // convert char to the array
  235.       new_BRIGHTNESS = atoi(carray);                 // atoi() converts an ascii character array to an integer
  236.       if (new_BRIGHTNESS == 0) {new_BRIGHTNESS = BRIGHTNESS; }   // if something else is selected (no change in brightness)
  237.       BRIGHTNESS = new_BRIGHTNESS;                 // works not this way
  238.          FastLED.setBrightness(new_BRIGHTNESS);      // that's how the new value is assigned  
  239.       Serial.print("new Brightness: ");
  240.       Serial.println(new_BRIGHTNESS);
  241.     }
  242.   }
  243.  
  244.   //////////////////////////////
  245.   // format the html response //
  246.   //////////////////////////////
  247.   String sResponse,sHeader;
  248.  
  249.   ///////////////////////////////
  250.   // 404 for non-matching path //
  251.   ///////////////////////////////
  252.   if(sPath!="/")
  253.   {
  254.     sResponse="<html><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL was not found on this server.</p></body></html>";
  255.    
  256.     sHeader  = "HTTP/1.1 404 Not found\r\n";
  257.     sHeader += "Content-Length: ";
  258.     sHeader += sResponse.length();
  259.     sHeader += "\r\n";
  260.     sHeader += "Content-Type: text/html\r\n";
  261.     sHeader += "Connection: close\r\n";
  262.     sHeader += "\r\n";
  263.   }
  264.   //////////////////////////
  265.   // format the html page //
  266.   //////////////////////////
  267.   else
  268.   {
  269.     ulReqcount++;
  270.     sResponse  = "<html><head><title>ESP_FastLED_Access_Point</title></head><body>";
  271. //    sResponse += "<font color=\"#FFFFF0\"><body bgclror=\"#000000\">";  
  272.     sResponse += "<font color=\"#FFFFF0\"><body bgcolor=\"#151B54\">";  
  273.     sResponse += "<FONT SIZE=-1>";
  274.     sResponse += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=yes\">";
  275.     sResponse += "<h1>ESP FastLED DemoReel 100<br>";
  276.     sResponse += " Light Controller</h1>";
  277.  
  278. /*  this creates a list with ON / OFF buttons
  279.     // &nbsp is a non-breaking space; moves next character over
  280.     sResponse += "<p>Rainbow &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"?pin=FUNCTION1ON\"><button>--ON--</button></a>&nbsp;<a href=\"?pin=FUNCTION1OFF\"><button>--OFF--</button></a><br>";
  281.     sResponse += "<p>Rainbow Glitter<a href=\"?pin=FUNCTION2ON\"><button>--ON--</button></a>&nbsp;<a href=\"?pin=FUNCTION2OFF\"><button>--OFF--</button></a><br>";
  282.     sResponse += "<p>Confetti &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"?pin=FUNCTION3ON\"><button>--ON--</button></a>&nbsp;<a href=\"?pin=FUNCTION3OFF\"><button>--OFF--</button></a><br>";
  283.     sResponse += "<p>Sinelon &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"?pin=FUNCTION4ON\"><button>--ON--</button></a>&nbsp;<a href=\"?pin=FUNCTION4OFF\"><button>--OFF--</button></a><br>";
  284.     sResponse += "<p>Juggle&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"?pin=FUNCTION5ON\"><button>--ON--</button></a>&nbsp;<a href=\"?pin=FUNCTION5OFF\"><button>--OFF--</button></a></p>";
  285.     sResponse += "<p>BPM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"?pin=FUNCTION6ON\"><button>--ON--</button></a>&nbsp;<a href=\"?pin=FUNCTION6OFF\"><button>--OFF--</button></a></p>";
  286.     sResponse += "<p>Function 7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"?pin=FUNCTION7ON\"><button>--ON--</button></a>&nbsp;<a href=\"?pin=FUNCTION7OFF\"><button>--OFF--</button></a></p><br>";
  287. */
  288. //  This is a nice drop down menu
  289.     sResponse += "<FONT SIZE=+1>";
  290.     sResponse += "<form>";
  291. //    sResponse += "Select Animation<br>";
  292.     sResponse += "<p>Select:</p>";
  293.     sResponse += "<select name=\"sCmd\" size=\"7\" >";
  294.     sResponse += "<option value=\"FUNCTION1OFF\">All OFF</option>";
  295.     sResponse += "<option value=\"FUNCTION1ON\"selected>Fast Circle</option>";
  296.     sResponse += "<option value=\"FUNCTION2ON\">Rainbow Glitter</option>";
  297.     sResponse += "<option value=\"FUNCTION3ON\">Confetti</option>";
  298.     sResponse += "<option value=\"FUNCTION4ON\">Sinelon</option>";
  299.     sResponse += "<option value=\"FUNCTION5ON\">Juggle</option>";
  300.     sResponse += "<option value=\"FUNCTION6ON\">BPM</option>";
  301.     sResponse += "<option value=\"FUNCTION7ON\">Fire 2012</option>";
  302.     sResponse += "<option value=\"FUNCTION8ON\">Lightning</option>";
  303.     sResponse += "<option value=\"FUNCTION9ON\">The Matrix</option>";
  304.     sResponse += "<option value=\"FUNCTION10ON\">Stripes</option>";
  305.     sResponse += "<option value=\"FUNCTION11ON\">Equalize</option>";
  306.     sResponse += "<option value=\"FUNCTION12ON\">Sound 2 Light</option><br>";
  307.     sResponse += "</select>";
  308.     sResponse += "<br><br>";
  309.     sResponse += "<input type= submit>";
  310.     sResponse += "</form>";
  311. //    sResponse += "<FONT SIZE=-1>";
  312.  
  313. // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  314. // Slider          this works, however I got http://192.168.4.1/sCmd?FUNCTION_200=80  and the page was not found
  315. //                 I needed to take the FUNCTION_200=80 apart and call only FUNCTION_200 and assign
  316. //                 the value (=80) in "react on parameters" (line 512) to new_BRIGHTNESS
  317.  
  318. sResponse += "</p>";
  319. sResponse += "<form action=\"?sCmd\" >";    // ?sCmd forced the '?' at the right spot  
  320. sResponse += "<BR>Brightness &nbsp;&nbsp";  // perhaps we can show here the current value
  321. sResponse += round(new_BRIGHTNESS /2.5);    // this is just a scale depending on the max value; round for better readability
  322. sResponse += " %";
  323. sResponse += "<BR>";
  324. sResponse += "<input style=\"width:200px; height:50px\" type=\"range\" name=\"=FUNCTION_200\" id=\"cmd\" value=\"";   // '=' in front of FUNCTION_200 forced the = at the right spot
  325. sResponse += BRIGHTNESS;
  326. sResponse += "\" min=10 max=250 step=10 onchange=\"showValue(points)\" />";
  327. sResponse += "<BR><BR>";
  328. sResponse += "<input type=\"submit\">";
  329. sResponse += "</form>";
  330. sResponse += "<p>";
  331. // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  332.  
  333.  sResponse += "<FONT SIZE=-1>";
  334.  
  335.  
  336.     /////////////////////////
  337.     // react on parameters //
  338.     /////////////////////////
  339.     if (sCmd.length()>0)
  340.     {
  341.      
  342.       // write received command to html page
  343.       // switch the animiation (based on your choice in the case statement (main loop)
  344.       if(sCmd.indexOf("FUNCTION1ON")>=0)
  345.       {
  346.         Serial.println("1 ON");
  347.         ledMode = 2;
  348.       }
  349.       else if(sCmd.indexOf("FUNCTION1OFF")>=0)
  350.       {
  351.         Serial.println("1 OFF");
  352.         ledMode = 1;
  353.       }
  354.  
  355.       if(sCmd.indexOf("FUNCTION2ON")>=0)
  356.       {
  357.          Serial.println("2 ON");
  358.         ledMode = 3;
  359.       }
  360.       else if(sCmd.indexOf("FUNCTION2OFF")>=0)
  361.       {
  362.         Serial.println("2 OFF");
  363.         ledMode = 1;
  364.       }
  365.  
  366.       if(sCmd.indexOf("FUNCTION3ON")>=0)
  367.       {
  368.         Serial.println("3 ON");
  369.         ledMode = 4;
  370.  
  371.       }
  372.       else if(sCmd.indexOf("FUNCTION3OFF")>=0)
  373.       {
  374.         Serial.println("3 OFF");
  375.         ledMode = 1;
  376.  
  377.       }
  378.       if(sCmd.indexOf("FUNCTION4ON")>=0)
  379.       {
  380.         Serial.println("4 ON");
  381.         ledMode = 5;
  382.  
  383.       }
  384.       else if(sCmd.indexOf("FUNCTION4OFF")>=0)
  385.       {
  386.         Serial.println("4 OFF");
  387.         ledMode = 1;
  388.  
  389.       }
  390.       if(sCmd.indexOf("FUNCTION5ON")>=0)
  391.       {
  392.          Serial.println("5 ON");
  393.         ledMode = 6;
  394.  
  395.       }
  396.       else if(sCmd.indexOf("FUNCTION5OFF")>=0)
  397.       {
  398.         Serial.println("5 OFF");
  399.         ledMode = 1;
  400.  
  401.       }
  402.  
  403.       if(sCmd.indexOf("FUNCTION6ON")>=0)
  404.       {
  405.          Serial.println("6 ON");
  406.         ledMode = 7;
  407.  
  408.       }
  409.       else if(sCmd.indexOf("FUNCTION6OFF")>=0)
  410.       {
  411.         Serial.println("6 OFF");
  412.         ledMode = 1;
  413.  
  414.       }
  415.       if(sCmd.indexOf("FUNCTION7ON")>=0)
  416.       {
  417.         Serial.println("7 ON");
  418.         ledMode = 8;
  419.  
  420.       }
  421.       else if(sCmd.indexOf("FUNCTION7OFF")>=0)
  422.       {
  423.          Serial.println("7 OFF");
  424.         ledMode = 1;
  425.  
  426.       }
  427.            if(sCmd.indexOf("FUNCTION8ON")>=0)
  428.       {
  429.         Serial.println("8 ON");
  430.         ledMode = 9;
  431.  
  432.       }
  433.       else if(sCmd.indexOf("FUNCTION8OFF")>=0)
  434.       {
  435.          Serial.println("8 OFF");
  436.         ledMode = 1;
  437.  
  438.       }
  439.          if(sCmd.indexOf("FUNCTION9ON")>=0)
  440.       {
  441.         Serial.println("9 ON");
  442.         ledMode = 10;
  443.  
  444.       }
  445.       else if(sCmd.indexOf("FUNCTION9OFF")>=0)
  446.       {
  447.          Serial.println("9 OFF");
  448.         ledMode = 1;
  449.  
  450.       }
  451.          if(sCmd.indexOf("FUNCTION10ON")>=0)
  452.       {
  453.         Serial.println("10 ON");
  454.         ledMode = 11; //case 10: matrix()
  455.  
  456.       }
  457.       else if(sCmd.indexOf("FUNCTION10OFF")>=0)
  458.       {
  459.          Serial.println("10 OFF");
  460.         ledMode = 1;
  461.  
  462.       }
  463.       if(sCmd.indexOf("FUNCTION11ON")>=0)
  464.       {
  465.         Serial.println("11 ON");
  466.         ledMode = 12;
  467.  
  468.       }
  469.       else if(sCmd.indexOf("FUNCTION11OFF")>=0)
  470.       {
  471.          Serial.println("11 OFF");
  472.         ledMode = 1;
  473.  
  474.       }
  475.       if(sCmd.indexOf("FUNCTION12ON")>=0)
  476.       {
  477.         Serial.println("12 ON");
  478.         ledMode = 13;
  479.  
  480.       }
  481.       else if(sCmd.indexOf("FUNCTION12OFF")>=0)
  482.       {
  483.          Serial.println("12 OFF");
  484.         ledMode = 1;
  485.  
  486.       }
  487.  
  488. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  489. // oh well, I was a bit frustrated. Came up with the idea to make
  490. // 10 digits increments and let the URL (not) react on it.
  491. // However, I was able to assign a new_BRIGHTNESS value;
  492. // what after all serves the purpose. Maybe someone comes up with
  493. // a more ellegant way - HOPEFULLY
  494. // (more than 400 have downloaded my code but nobody felt the need  
  495. // to help. wtf - this is my very first attempt on HTML !
  496. // Guys, I'm a simple electrician, so PLEASE help  :(          )
  497.  
  498. // do not call a new page when the slider is moved, but assign the new value
  499. // to BRIGHTNESS (this is done in "output parameters to serial", line 314
  500.  
  501.       if(sCmd.indexOf("FUNCTION_200=20")>=0) { }
  502.       if(sCmd.indexOf("FUNCTION_200=30")>=0) { }
  503.       if(sCmd.indexOf("FUNCTION_200=40")>=0) { }
  504.       if(sCmd.indexOf("FUNCTION_200=50")>=0) { }
  505.       if(sCmd.indexOf("FUNCTION_200=60")>=0) { }
  506.       if(sCmd.indexOf("FUNCTION_200=70")>=0) { }
  507.       if(sCmd.indexOf("FUNCTION_200=80")>=0) { }
  508.       if(sCmd.indexOf("FUNCTION_200=90")>=0) { }
  509.       if(sCmd.indexOf("FUNCTION_200=100")>=0) { }
  510.       if(sCmd.indexOf("FUNCTION_200=110")>=0) { }
  511.       if(sCmd.indexOf("FUNCTION_200=120")>=0) { }
  512.       if(sCmd.indexOf("FUNCTION_200=130")>=0) { }
  513.       if(sCmd.indexOf("FUNCTION_200=140")>=0) { }
  514.       if(sCmd.indexOf("FUNCTION_200=150")>=0) { }
  515.       if(sCmd.indexOf("FUNCTION_200=160")>=0) { }
  516.       if(sCmd.indexOf("FUNCTION_200=170")>=0) { }
  517.       if(sCmd.indexOf("FUNCTION_200=180")>=0) { }
  518.       if(sCmd.indexOf("FUNCTION_200=190")>=0) { }
  519.       if(sCmd.indexOf("FUNCTION_200=200")>=0) { }
  520.       if(sCmd.indexOf("FUNCTION_200=210")>=0) { }
  521.       if(sCmd.indexOf("FUNCTION_200=220")>=0) { }
  522.       if(sCmd.indexOf("FUNCTION_200=230")>=0) { }
  523.       if(sCmd.indexOf("FUNCTION_200=240")>=0) { }
  524.       if(sCmd.indexOf("FUNCTION_200=250")>=0) { }
  525. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  526.     } // end sCmd.length()>0
  527.  
  528.     sResponse += "<BR>";
  529.     sResponse += "<BR>";
  530.     sResponse += "<BR>";
  531.     sResponse += "<FONT SIZE=-2>";
  532.     sResponse += "<font color=\"#FFDE00\">";
  533.     sResponse += "DemoReel 100 by Mark Kriegsman<BR>";
  534.     sResponse += "MSGEQ7 integration by Garrett Durland<BR>";
  535.     sResponse += "<font color=\"#FFFFF0\">";
  536.     sResponse += "Gyro Gearloose &nbsp;&nbsp;Feb 2016<BR>";
  537.     sResponse += "</body></html>";
  538.     sHeader  = "HTTP/1.1 200 OK\r\n";
  539.     sHeader += "Content-Length: ";
  540.     sHeader += sResponse.length();
  541.     sHeader += "\r\n";
  542.     sHeader += "Content-Type: text/html\r\n";
  543.     sHeader += "Connection: close\r\n";
  544.     sHeader += "\r\n";
  545.   }
  546.  
  547.   client.print(sHeader);  // Send the response to the client
  548.   client.print(sResponse);
  549.   client.stop();   // and stop the client
  550.   Serial.println("Client disonnected");  
  551.   }  // end of web server
  552.  
  553. /// END of complete web server //////////////////////////////////////////////////////////////////
  554.  
  555. // LED animations ###############################################################################
  556. void all_off() {
  557.   fill_solid(leds, NUM_LEDS, CRGB::Black);
  558. //  show_at_max_brightness_for_power();
  559. //  delay_at_max_brightness_for_power(1000/FRAMES_PER_SECOND);  
  560.   FastLED.show();
  561.   FastLED.delay(1000/FRAMES_PER_SECOND);
  562. }
  563.  
  564. void rainbow()
  565. {
  566.   // FastLED's built-in rainbow generator
  567.   EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
  568.   fill_rainbow( leds, NUM_LEDS, gHue, 7);
  569.   show_at_max_brightness_for_power();
  570.   delay_at_max_brightness_for_power(1000/FRAMES_PER_SECOND);
  571. }
  572.  
  573.  
  574. void addGlitter( fract8 chanceOfGlitter)
  575. {
  576.   if( random8() < chanceOfGlitter) {
  577.     leds[ random16(NUM_LEDS) ] += CRGB::White;
  578.   }
  579. }
  580. void rainbowWithGlitter()
  581. {
  582.   // built-in FastLED rainbow, plus some random sparkly glitter
  583.   rainbow();
  584.   addGlitter(80);
  585. }
  586. void confetti()
  587. {
  588.   // random colored speckles that blink in and fade smoothly
  589.   fadeToBlackBy( leds, NUM_LEDS, 10);
  590.   int pos = random16(NUM_LEDS);
  591.   leds[pos] += CHSV( gHue + random8(64), 200, 255);
  592.   show_at_max_brightness_for_power();
  593.   delay_at_max_brightness_for_power(1000/FRAMES_PER_SECOND);
  594. }
  595.  
  596. void sinelon()
  597. {
  598.   // a colored dot sweeping back and forth, with fading trails
  599.   fadeToBlackBy( leds, NUM_LEDS, 20);
  600.   int pos = beatsin16(13,0,NUM_LEDS);
  601.   leds[pos] += CHSV( gHue, 255, 192);
  602.   show_at_max_brightness_for_power();
  603.   delay_at_max_brightness_for_power(1000/FRAMES_PER_SECOND);
  604. }
  605.  
  606. void bpm()
  607. {
  608.   // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
  609.   uint8_t BeatsPerMinute = 62;
  610.   CRGBPalette16 palette = PartyColors_p;
  611.   uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
  612.   for( int i = 0; i < NUM_LEDS; i++) { //9948
  613.     leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
  614.   }
  615.   show_at_max_brightness_for_power();
  616.   delay_at_max_brightness_for_power(1000/FRAMES_PER_SECOND);
  617. }
  618.  
  619. void juggle() {
  620.   // eight colored dots, weaving in and out of sync with each other
  621.   fadeToBlackBy( leds, NUM_LEDS, 80);
  622.   byte dothue = 0;
  623.   for( int i = 0; i < 5; i++) {
  624.     leds[beatsin16(i+5,0,NUM_LEDS)] |= CHSV(dothue, 200, 255);
  625.     dothue += 51;
  626.   }
  627.   show_at_max_brightness_for_power();
  628.   delay_at_max_brightness_for_power(1000/FRAMES_PER_SECOND);
  629. }
  630. //########Constants for Fire2012#########
  631. // COOLING: How much does the air cool as it rises?
  632. // Less cooling = taller flames.  More cooling = shorter flames.
  633. // Default 50, suggested range 20-100
  634. #define COOLING  55
  635.  
  636. // SPARKING: What chance (out of 255) is there that a new spark will be lit?
  637. // Higher chance = more roaring fire.  Lower chance = more flickery fire.
  638. // Default 120, suggested range 50-200.
  639. #define SPARKING 120
  640. bool gReverseDirection = false;
  641. void Fire2012()
  642. {
  643. // Array of temperature readings at each simulation cell
  644.   static byte heat[NUM_LEDS];
  645.  
  646.   // Step 1.  Cool down every cell a little
  647.     for( int i = 0; i < NUM_LEDS; i++) {
  648.       heat[i] = qsub8( heat[i],  random8(0, ((COOLING * 10) / NUM_LEDS) + 2));
  649.     }
  650.  
  651.     // Step 2.  Heat from each cell drifts 'up' and diffuses a little
  652.     for( int k= NUM_LEDS - 1; k >= 2; k--) {
  653.       heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3;
  654.     }
  655.    
  656.     // Step 3.  Randomly ignite new 'sparks' of heat near the bottom
  657.     if( random8() < SPARKING ) {
  658.       int y = random8(7);
  659.       heat[y] = qadd8( heat[y], random8(160,255) );
  660.     }
  661.  
  662.     // Step 4.  Map from heat cells to LED colors
  663.     for( int j = 0; j < NUM_LEDS; j++) {
  664.       CRGB color = HeatColor( heat[j]);
  665.       int pixelnumber;
  666.       if( gReverseDirection ) {
  667.         pixelnumber = (NUM_LEDS-1) - j;
  668.       } else {
  669.         pixelnumber = j;
  670.       }
  671.       leds[pixelnumber] = color;
  672.     }
  673. }
  674. // Initialize global variables for fastcirc_beat_g
  675. int thiscount = 0;
  676. int thishue = 0;
  677. int thisdir = 1;
  678. void fast_circ () {
  679.   uint8_t beatval = beatsin8(10,5,50);
  680.   thiscount = (thiscount + 1)%NUM_LEDS;
  681.   leds[thiscount] = CHSV(thishue, 255, 255);
  682.   fadeToBlackBy(leds, NUM_LEDS, 224);
  683.   delay(beatval*2);                                           // I tried to use EVERY_N_MILLISECONDS, but it didn't work for some reason.
  684.   show_at_max_brightness_for_power();
  685.   if (thishue >= 1024) thishue=0;
  686.   thishue+=1;
  687.  
  688. }
  689. // begin global variables for lightning()
  690. uint8_t frequency = 50;                                       // controls the interval between strikes
  691. uint8_t flashes = 8;                                          //the upper limit of flashes per strike
  692. unsigned int dimmer = 1;
  693. uint8_t ledstart;                                             // Starting location of a flash
  694. uint8_t ledlen;  
  695. void lightning() {
  696.    for( int i = 0; i < NUM_LEDS; i++)
  697.    leds[i]=0;
  698.   ledstart = random16(NUM_LEDS);           // Determine starting location of flash
  699.   ledlen = random16(NUM_LEDS-ledstart);    // Determine length of flash (not to go beyond NUM_LEDS-1)
  700.   for (int flashCounter = 0; flashCounter < random8(3,flashes); flashCounter++) {
  701.     if(flashCounter == 0) dimmer = 5;     // the brightness of the leader is scaled down by a factor of 5
  702.     else dimmer = random16(1,3);           // return strokes are brighter than the leader
  703.     fill_solid(leds+ledstart,ledlen,CHSV(255, 0, 255/dimmer));
  704.     FastLED.show();                       // Show a section of LED's
  705.     delay(random8(4,10));                 // each flash only lasts 4-10 milliseconds
  706.     fill_solid(leds+ledstart,ledlen,CHSV(255,0,0));   // Clear the section of LED's
  707.     FastLED.show();    
  708.     if (flashCounter == 0) delay (150);   // longer delay until next flash after the leader
  709.     delay(50+random8(100));               // shorter delay between strokes  
  710.   } // for()
  711.   delay(random8(frequency)*100);          // delay between strikes
  712. } // loop()
  713.  
  714. ///////////////////////
  715. // Initialize global variables for matrix()
  716. int      thisdelay =  50;                                          // A delay value for the sequence(s)
  717. bool        huerot =   0;                                          // Does the hue rotate? 1 = yes
  718. uint8_t      bgclr =   0;
  719. uint8_t      bgbri =   0;
  720. //variable "thisdir" (for direction) defined earlier
  721.  
  722. void matrix () {
  723.   uint8_t secondHand = (millis() / 1000) % 25;                // Change '25' to a different value to change length of the loop.
  724.   static uint8_t lastSecond = 99;                             // Static variable, means it's only defined once. This is our 'debounce' variable.
  725.   if (lastSecond != secondHand) {                             // Debounce to make sure we're not repeating an assignment.
  726.     lastSecond = secondHand;
  727.     switch(secondHand) {
  728.       case  0: thisdelay=50; thishue=95; bgclr=140; bgbri=16; huerot=0; break;
  729.       case  5: targetPalette = OceanColors_p; thisdir=1; bgbri=0; huerot=1; break;
  730.       case 10: targetPalette = LavaColors_p; thisdir=0; thisdelay=30; thishue=0; bgclr=50; bgbri=15; huerot=0; break;
  731.       case 15: thisdelay=80; bgbri = 32; bgclr=96; thishue=random8(); break;
  732.       case 20: thishue=random8(); huerot=1; break;
  733.       case 25: break;
  734.     }
  735.   }
  736.   EVERY_N_MILLISECONDS(100) {
  737.     uint8_t maxChanges = 24;
  738.     nblendPaletteTowardPalette(currentPalette, targetPalette, maxChanges);   // AWESOME palette blending capability.
  739.   }
  740.  
  741.   EVERY_N_MILLISECONDS(thisdelay) {
  742. //    matrix();
  743.  
  744.   if (huerot) thishue++;
  745.  
  746.   if (random16(90) > 80) {
  747.     if (thisdir == 0) leds[0] = ColorFromPalette(currentPalette, thishue, 255, currentBlending); else leds[NUM_LEDS-1] = ColorFromPalette( currentPalette, thishue, 255, currentBlending);
  748.   }
  749.   else {
  750.     if (thisdir ==0) leds[0] = CHSV(bgclr, 255, bgbri); else leds[NUM_LEDS-1] = CHSV(bgclr, 255, bgbri);
  751.   }
  752.  
  753.   if (thisdir == 0) {
  754.     for (int i = NUM_LEDS-1; i >0 ; i-- ) leds[i] = leds[i-1];
  755.   } else {
  756.     for (int i = 0; i < NUM_LEDS ; i++ ) leds[i] = leds[i+1];
  757.   }
  758.   }
  759.   show_at_max_brightness_for_power();
  760. } // loop()
  761. void stripes()
  762. {
  763. static bool init = 1;
  764. if (init) {
  765. uint8_t   stripe1=random8();
  766. uint8_t   stripe2=stripe1+100;
  767. CRGB A = CHSV(stripe1,255,255);
  768. CRGB AB = CHSV(stripe1+30,230,220);
  769. CRGB B =  CHSV(stripe2,255,255);
  770. CRGB BA = CHSV(stripe2-30,230,220);
  771.  
  772. currentPalette = CRGBPalette16(
  773.         A, A, A, A, AB, AB, B, B, B, B, B, B, B, B, BA, BA
  774.   //    A, A, A, A, A, A, AB, AB, B, B, B, B, B, B, BA, BA
  775.   );
  776. init = 0;
  777. }
  778. EVERY_N_MILLISECONDS(100) {
  779. nblendPaletteTowardPalette(currentPalette, targetPalette, 24);   // AWESOME palette blending capability.
  780.   }
  781. EVERY_N_MILLISECONDS( 5000 )
  782. {
  783. uint8_t   stripe1=random8();
  784. uint8_t   stripe2=stripe1+100;
  785. CRGB A = CHSV(stripe1,255,255);
  786. CRGB AB = CHSV(stripe1+30,230,220);
  787. CRGB B =  CHSV(stripe2,255,255);
  788. CRGB BA = CHSV(stripe2-30,230,220);
  789.  
  790. targetPalette = CRGBPalette16(
  791.         A, A, A, A, AB, AB, B, B, B, B, B, B, B, B, BA, BA
  792.   //    A, A, A, A, A, A, AB, AB, B, B, B, B, B, B, BA, BA
  793.   );
  794.  
  795. //setupStripedPalette( CHSV(stripe1,255,255), CHSV(stripe1+25,255,255), CHSV(stripe2,255,255), CHSV(stripe2-25,255,255));
  796. }
  797.   static uint8_t startIndex = 0;
  798.   startIndex = startIndex + 2; /* higher = faster motion */
  799.  
  800.   fill_palette( leds, NUM_LEDS,
  801.                 startIndex, 6, /* higher = narrower stripes */
  802.                 currentPalette, 255, LINEARBLEND);
  803. delay_at_max_brightness_for_power(1000/FRAMES_PER_SECOND);
  804. }  
  805. /////global variables for MSGEQ7 sound reactive code///
  806. uint8_t MSGhue; //hue change variable that doesn't rely on every_n_milliseconds to change hue.
  807. int bands[7]; // there are 7 LEDs and 7 freq bands. This variable holds the smoothed data for use in the light dispalay
  808. int left[7]; //for raw (unsmoothed) audio data
  809. ////////////////
  810.  
  811. void equalize(){
  812.   digitalWrite(msg7RESET, HIGH);                  // reset the MSGEQ7's counter
  813.   delay(5);
  814.   digitalWrite(msg7RESET, LOW);
  815.  
  816.         for (int x = 0; x < 7; x++){
  817.                 digitalWrite(msg7Strobe, LOW);          // output each DC value for each freq band
  818.                 delayMicroseconds(35); // to allow the output to settle
  819.                 int spectrumRead = analogRead(A0);//returns a value from 0 to 1023
  820.                 int MSGval = map(spectrumRead, 0, 512, 0, 255);  // scale analogRead's value to Write's 255 max
  821.                 if (MSGval < 50) MSGval = MSGval / 2;                // bit of a noise filter, so the LEDs turn off at low levels
  822.  
  823.                 leds[x]=CHSV(MSGhue,255,MSGval);
  824.                if (millis() - ticker > 100){
  825.                   MSGhue++; //increases hue vale every 100 milliseconds
  826.                 ticker = millis();
  827.                }
  828.                 digitalWrite(msg7Strobe, HIGH);
  829.                 Serial.println(MSGval);
  830.         }
  831. }
  832.  
  833. void getBands(){
  834.    digitalWrite(msg7RESET, HIGH);                  // reset the MSGEQ7's counter
  835.         delay(5);
  836.         digitalWrite(msg7RESET, LOW);
  837.  
  838.         for (int x = 0; x < 7; x++){
  839.                 digitalWrite(msg7Strobe, LOW);          // output each DC value for each freq band
  840.                 delayMicroseconds(35); // to allow the output to settle
  841.                 int spectrumRead = analogRead(A0);
  842.  
  843.                 left[x] = map(spectrumRead, 0, 512, 0, 255);  // scale analogRead's value to Write's 255 max
  844.                 if (left[x] < 50)
  845.                         left[x] = left[x] / 2;                // bit of a noise filter, so the LEDs turn off at low levels
  846.  
  847.                 digitalWrite(msg7Strobe, HIGH);
  848.         }
  849. }
  850. void animation22(){
  851.   leds[3] = CRGB(bands[6], bands[5] / 3, bands[1] / 2);
  852.   leds[3].fadeToBlackBy(bands[3] / 12);
  853.   //move to the right
  854.   //Idea for long strips: Have the sound impulse accelerate as it moves outward in each direction.
  855.   for (int i = NUM_LEDS - 1; i > 3; i--) {
  856.  
  857.     leds[i] = leds[i - 1];
  858.  
  859.   }
  860.   // move to the left
  861.   for (int i = 0; i < 3; i++) {
  862.  
  863.     leds[i] = leds[i + 1];
  864.   }
  865.   FastLED.show();
  866.   fade_down(72);
  867. }
  868.  
  869. void sound_2_light(){
  870.   getBands();
  871.   soften_spectrum_data();
  872.   animation22();
  873. }
  874. void fade_down(uint8_t value){
  875.   for (int i = 0; i < NUM_LEDS; i++)    leds[i].fadeToBlackBy(value);
  876. }
  877. void soften_spectrum_data(){
  878.   for (byte i = 0; i < 7; i++)
  879.   {
  880.     uint8_t old = bands[i];
  881.     uint16_t data = left[i] + old; //this is for a mono MSGEQ7 setup. Petrick wrote his animation for a stereo setup
  882.     data = data / 2;
  883.     bands[i] = data;
  884.   }
  885. }
  886.  
  887. void loop() {
  888.   webserver();
  889.   if (ledMode != 999) {
  890.  
  891.      switch (ledMode) {
  892.       case  1: all_off(); break;
  893.       case  2: fast_circ(); break;
  894.       case  3: rainbowWithGlitter(); break;
  895.       case  4: confetti(); break;
  896.       case  5: sinelon(); break;
  897.       case  6: juggle(); break;
  898.       case  7: bpm(); break;
  899.       case  8: Fire2012(); break;
  900.       case  9: lightning(); break;
  901.       case 10: matrix(); break;
  902.       case 11: stripes(); break;
  903.       case 12: equalize(); break;
  904.       case 13: sound_2_light(); break;
  905.       }
  906.       }
  907. //  show_at_max_brightness_for_power();
  908. //  delay_at_max_brightness_for_power(1000/FRAMES_PER_SECOND);
  909.  
  910.   FastLED.show();  
  911.   if(ledMode != 12) FastLED.delay(1000/FRAMES_PER_SECOND);
  912.   EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
  913.  
  914. } // end of loop ************************************************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment