daily pastebin goal
84%
SHARE
TWEET

FastLED_Huzzah_stable_ Webserver

GyroGearloose Feb 13th, 2016 3,100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*-------------------------------------------------------------------------------------
  2.    Works with Huzzah; other modules not tested yet.
  3.  
  4.    v01: webserver extended to 7 on and off switches
  5.    v02: added demo reel. Had to declare FastLED before
  6.         the webserver. Then put the complete webserver
  7.         into a void webserver() and leave in the loop
  8.         just the case statements and the FastLEDshow.
  9.  
  10.  Usage: after upload open the Serial Monitor in Arduino and see what
  11.         IP address is returned. In my case it is 192.168.1.252
  12.         Open this IP address in a browser (PC or phone)
  13.  
  14.    Gyro Gearloose, 02/2016
  15. /*-------------------------------------------------------------------------------------
  16. HTTP 1.1 Webserver for ESP8266 adapted to Arduino IDE
  17.  
  18. From Stefan Thesen 04/2015
  19. https://blog.thesen.eu/stabiler-http-1-1-wlan-webserver-mit-dem-esp8266-microcontroller/
  20.  
  21. Running stable for days
  22. (in difference to all samples I tried)
  23.  
  24. Does HTTP 1.1 with defined connection closing.
  25. Reconnects in case of lost WiFi.
  26. Handles empty requests in a defined manner.
  27. Handle requests for non-exisiting pages correctly.
  28.  
  29. This demo allows to switch two functions:
  30. Function 1 creates serial output and toggels GPIO2
  31. Function 2 just creates serial output.
  32.  
  33. Serial output can e.g. be used to steer an attached
  34. Arduino, Raspberry etc.
  35. ---------------------------------------------------------------------------------------*/
  36.  
  37. // FastLED setup -----FastLED has to be declared BEFORE the Webserver-----
  38. #include "FastLED.h"
  39. FASTLED_USING_NAMESPACE
  40.  
  41. #if defined(FASTLED_VERSION) && (FASTLED_VERSION < 3001000)
  42. #warning "Requires FastLED 3.1 or later; check github for latest code."
  43. #endif
  44.  
  45. #define DATA_PIN      13     // for Huzzah: Pins w/o special function:  #4, #5, #12, #13, #14 //6
  46. //#define CLK_PIN     12
  47. //#define LED_TYPE    APA102
  48. //#define COLOR_ORDER BGR
  49. #define LED_TYPE      WS2811
  50. #define COLOR_ORDER   GRB
  51. #define NUM_LEDS      16     // 24
  52. CRGB leds[NUM_LEDS];
  53.  
  54. #define BRIGHTNESS          96
  55. #define FRAMES_PER_SECOND  120
  56.  
  57. int ledMode = 2;            // this is the starting animation
  58.  
  59. // Websever setup -----------------------------------------------------
  60. #include <ESP8266WiFi.h>
  61. // comes with Huzzah installation. Enter in Arduino settings:
  62. // http://arduino.esp8266.com/package_esp8266com_index.json
  63.  
  64. const char* ssid = "Your Access Point";   // 192.168.1.252
  65. const char* password = "Your Password";
  66.  
  67. unsigned long ulReqcount;
  68. unsigned long ulReconncount;
  69.  
  70. WiFiServer server(80);  // Create an instance of the server on Port 80
  71.  
  72. void setup()
  73. {
  74.  
  75.   ulReqcount=0;         // setup globals for Webserver
  76.   ulReconncount=0;
  77.  
  78.   // prepare GPIO2      // not necessary for FastLED
  79.   pinMode(2, OUTPUT);
  80.   digitalWrite(2, 0);
  81.  
  82.   // start serial
  83.   Serial.begin(9600);
  84.   delay(1);
  85.  
  86.   // inital connect
  87.   WiFi.mode(WIFI_STA);
  88.   WiFiStart();
  89.  
  90.   FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  91.   //FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  92.   FastLED.setBrightness(BRIGHTNESS);
  93. }
  94.  
  95. uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  96.  
  97. void WiFiStart()
  98. {
  99.   ulReconncount++;
  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 loop() {
  125.   webserver();
  126.   if (ledMode != 999) {
  127.  
  128.      switch (ledMode) {
  129.       case  1: all_off(); break;
  130.       case  2: rainbow(); break;
  131.       case  3: rainbowWithGlitter(); break;
  132.       case  4: confetti(); break;
  133.       case  5: sinelon(); break;
  134.       case  6: juggle(); break;
  135.       case  7: bpm(); break;
  136.       }
  137.       }
  138.      
  139. //  for (int i = 0; i < 1000; i++) {  does not work
  140.   FastLED.show();  
  141.   FastLED.delay(1000/FRAMES_PER_SECOND);
  142.   EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
  143. //}
  144.  
  145.  
  146. } // end of loop *************************************************************************************
  147.  
  148. void webserver() {   /// complete web server /////////////////////////////////////////////////////////
  149.   // check if WLAN is connected
  150.   if (WiFi.status() != WL_CONNECTED)
  151.   {
  152.     WiFiStart();
  153.   }
  154.  
  155.   // Check if a client has connected
  156.   WiFiClient client = server.available();
  157.   if (!client)
  158.   {
  159.     return;
  160.   }
  161.  
  162.   // Wait until the client sends some data
  163.   Serial.println("new client");
  164.   unsigned long ultimeout = millis()+250;
  165.   while(!client.available() && (millis()<ultimeout) )
  166.   {
  167.     delay(1);
  168.   }
  169.   if(millis()>ultimeout)
  170.   {
  171.     Serial.println("client connection time-out!");
  172.     return;
  173.   }
  174.  
  175.   // Read the first line of the request
  176.   String sRequest = client.readStringUntil('\r');
  177.   //Serial.println(sRequest);
  178.   client.flush();
  179.  
  180.   // stop client, if request is empty
  181.   if(sRequest=="")
  182.   {
  183.     Serial.println("empty request! - stopping client");
  184.     client.stop();
  185.     return;
  186.   }
  187.  
  188.   // get path; end of path is either space or ?
  189.   // Syntax is e.g. GET /?pin=MOTOR1STOP HTTP/1.1
  190.   String sPath="",sParam="", sCmd="";
  191.   String sGetstart="GET ";
  192.   int iStart,iEndSpace,iEndQuest;
  193.   iStart = sRequest.indexOf(sGetstart);
  194.   if (iStart>=0)
  195.   {
  196.     iStart+=+sGetstart.length();
  197.     iEndSpace = sRequest.indexOf(" ",iStart);
  198.     iEndQuest = sRequest.indexOf("?",iStart);
  199.    
  200.     // are there parameters?
  201.     if(iEndSpace>0)
  202.     {
  203.       if(iEndQuest>0)
  204.       {
  205.         // there are parameters
  206.         sPath  = sRequest.substring(iStart,iEndQuest);
  207.         sParam = sRequest.substring(iEndQuest,iEndSpace);
  208.       }
  209.       else
  210.       {
  211.         // NO parameters
  212.         sPath  = sRequest.substring(iStart,iEndSpace);
  213.       }
  214.     }
  215.   }
  216.  
  217.   ///////////////////////////////////////////////////////////////////////////////
  218.   // output parameters to serial, you may connect e.g. an Arduino and react on it
  219.   ///////////////////////////////////////////////////////////////////////////////
  220.   if(sParam.length()>0)
  221.   {
  222.     int iEqu=sParam.indexOf("=");
  223.     if(iEqu>=0)
  224.     {
  225.       sCmd = sParam.substring(iEqu+1,sParam.length());
  226.       Serial.println(sCmd);
  227.     }
  228.   }
  229.    
  230.   ///////////////////////////
  231.   // format the html response
  232.   ///////////////////////////
  233.   String sResponse,sHeader;
  234.  
  235.   ////////////////////////////
  236.   // 404 for non-matching path
  237.   ////////////////////////////
  238.   if(sPath!="/")
  239.   {
  240.     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>";
  241.    
  242.     sHeader  = "HTTP/1.1 404 Not found\r\n";
  243.     sHeader += "Content-Length: ";
  244.     sHeader += sResponse.length();
  245.     sHeader += "\r\n";
  246.     sHeader += "Content-Type: text/html\r\n";
  247.     sHeader += "Connection: close\r\n";
  248.     sHeader += "\r\n";
  249.   }
  250.   ///////////////////////
  251.   // format the html page
  252.   ///////////////////////
  253.   else
  254.   {
  255.     ulReqcount++;
  256.     sResponse  = "<html><head><title>ESP8266 control for DemoReel 100</title></head><body>";
  257. //    sResponse  = "<html><head><title>Demo f&uumlr ESP8266 Steuerung</title></head><body>";
  258.     sResponse += "<font color=\"#000000\"><body bgcolor=\"#d0d0f0\">";
  259.     sResponse += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=yes\">";
  260.     sResponse += "<h1>ESP8266 control for DemoReel 100</h1>";
  261. //    sResponse += "<h1>Demo f&uumlr ESP8266 Steuerung</h1>";
  262.     sResponse += "DemoReel 100 by Mark Kriegsman<BR>";
  263.     sResponse += "Webserver by Stefan Thesen<BR>";
  264. //    sResponse += "Funktion 1 schaltet GPIO2 und erzeugt eine serielle Ausgabe.<BR>";
  265. //    sResponse += "Funktion 2 erzeugt nur eine serielle Ausgabe.<BR>";
  266.  
  267.     sResponse += "<FONT SIZE=+1>";
  268.     sResponse += "<p>Rainbow         . <a href=\"?pin=FUNCTION1ON\"><button>--ON--</button></a>&nbsp;<a href=\"?pin=FUNCTION1OFF\"><button>--OFF--</button></a></p>";
  269.     sResponse += "<p>Rainbow Glitter . <a href=\"?pin=FUNCTION2ON\"><button>--ON--</button></a>&nbsp;<a href=\"?pin=FUNCTION2OFF\"><button>--OFF--</button></a></p>";
  270.     sResponse += "<p>Confetti        . <a href=\"?pin=FUNCTION3ON\"><button>--ON--</button></a>&nbsp;<a href=\"?pin=FUNCTION3OFF\"><button>--OFF--</button></a></p>";
  271.     sResponse += "<p>Sinelon         . <a href=\"?pin=FUNCTION4ON\"><button>--ON--</button></a>&nbsp;<a href=\"?pin=FUNCTION4OFF\"><button>--OFF--</button></a></p>";
  272.     sResponse += "<p>Juggle          . <a href=\"?pin=FUNCTION5ON\"><button>--ON--</button></a>&nbsp;<a href=\"?pin=FUNCTION5OFF\"><button>--OFF--</button></a></p>";
  273.     sResponse += "<p>BMP             . <a href=\"?pin=FUNCTION6ON\"><button>--ON--</button></a>&nbsp;<a href=\"?pin=FUNCTION6OFF\"><button>--OFF--</button></a></p>";
  274.     sResponse += "<p>Function 7      . <a href=\"?pin=FUNCTION7ON\"><button>--ON--</button></a>&nbsp;<a href=\"?pin=FUNCTION7OFF\"><button>--OFF--</button></a></p>";
  275.                                         // how to set the buttons in one row?
  276.     //////////////////////
  277.     // react on parameters
  278.     //////////////////////
  279.     if (sCmd.length()>0)
  280.     {
  281.       // write received command to html page
  282.       sResponse += "Command: " + sCmd + "<BR>";
  283.      
  284.       // switch GPIO
  285.       if(sCmd.indexOf("FUNCTION1ON")>=0)
  286.       {
  287.         Serial.println("1 ON");
  288.         ledMode = 2;
  289.       }
  290.       else if(sCmd.indexOf("FUNCTION1OFF")>=0)
  291.       {
  292.         Serial.println("1 OFF");
  293.         ledMode = 1;
  294.       }
  295.  
  296.       if(sCmd.indexOf("FUNCTION2ON")>=0)
  297.       {
  298.          Serial.println("2 ON");
  299.         ledMode = 3;
  300.       }
  301.       else if(sCmd.indexOf("FUNCTION2OFF")>=0)
  302.       {
  303.         Serial.println("2 OFF");
  304.         ledMode = 1;
  305.       }
  306.  
  307.       if(sCmd.indexOf("FUNCTION3ON")>=0)
  308.       {
  309.          Serial.println("3 ON");
  310.         ledMode = 4;
  311.  
  312.       }
  313.       else if(sCmd.indexOf("FUNCTION3OFF")>=0)
  314.       {
  315.         Serial.println("3 OFF");
  316.         ledMode = 1;
  317.  
  318.       }
  319.       if(sCmd.indexOf("FUNCTION4ON")>=0)
  320.       {
  321.         Serial.println("4 ON");
  322.         ledMode = 5;
  323.  
  324.       }
  325.       else if(sCmd.indexOf("FUNCTION4OFF")>=0)
  326.       {
  327.         Serial.println("4 OFF");
  328.         ledMode = 1;
  329.  
  330.       }
  331.       if(sCmd.indexOf("FUNCTION5ON")>=0)
  332.       {
  333.          Serial.println("5 ON");
  334.         ledMode = 6;
  335.  
  336.       }
  337.       else if(sCmd.indexOf("FUNCTION5OFF")>=0)
  338.       {
  339.         Serial.println("5 OFF");
  340.         ledMode = 1;
  341.  
  342.       }
  343.  
  344.       if(sCmd.indexOf("FUNCTION6ON")>=0)
  345.       {
  346.          Serial.println("6 ON");
  347.         ledMode = 7;
  348.  
  349.       }
  350.       else if(sCmd.indexOf("FUNCTION6OFF")>=0)
  351.       {
  352.         Serial.println("6 OFF");
  353.         ledMode = 1;
  354.  
  355.       }
  356.       if(sCmd.indexOf("FUNCTION7ON")>=0)
  357.       {
  358.         Serial.println("7 ON");
  359.         ledMode = 8;
  360.  
  361.       }
  362.       else if(sCmd.indexOf("FUNCTION7OFF")>=0)
  363.       {
  364.          Serial.println("7 OFF");
  365.         ledMode = 1;
  366.  
  367.       }
  368.  
  369.     } // end sCmd.length()>0
  370.    
  371.     sResponse += "<FONT SIZE=-2>";
  372. //    sResponse += "<BR>Aufrufz&auml;hler=";
  373.     sResponse += "<BR>clicks on page =";
  374.     sResponse += ulReqcount;
  375. //    sResponse += " - Verbindungsz&auml;hler=";
  376.     sResponse += " - connections to page =";
  377.     sResponse += ulReconncount;
  378.     sResponse += "<BR>";
  379.     sResponse += "Credits to Stefan Thesen and Mark KriegsmanBR>";
  380.     sResponse += "</body></html>";
  381.    
  382.     sHeader  = "HTTP/1.1 200 OK\r\n";
  383.     sHeader += "Content-Length: ";
  384.     sHeader += sResponse.length();
  385.     sHeader += "\r\n";
  386.     sHeader += "Content-Type: text/html\r\n";
  387.     sHeader += "Connection: close\r\n";
  388.     sHeader += "\r\n";
  389.   }
  390.  
  391.   // Send the response to the client
  392.   client.print(sHeader);
  393.   client.print(sResponse);
  394.  
  395.  
  396.   // and stop the client
  397.   client.stop();
  398.   Serial.println("Client disonnected");  
  399.   }
  400. /// END of complete web server //////////////////////////////////////////////////////////////////
  401.  
  402. // LED animations ###############################################################################
  403. void all_off() {
  404.   fill_solid(leds, NUM_LEDS, CRGB::Black);
  405.   FastLED.show();
  406.   FastLED.delay(1000/FRAMES_PER_SECOND);
  407. }
  408.  
  409. void rainbow()
  410. {
  411.   // FastLED's built-in rainbow generator
  412.   EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
  413.   fill_rainbow( leds, NUM_LEDS, gHue, 7);
  414.   FastLED.show();  
  415.   FastLED.delay(1000/FRAMES_PER_SECOND);
  416. }
  417.  
  418. void rainbowWithGlitter()
  419. {
  420.   // built-in FastLED rainbow, plus some random sparkly glitter
  421.   rainbow();
  422.   addGlitter(80);
  423. }
  424.  
  425. void addGlitter( fract8 chanceOfGlitter)
  426. {
  427.   if( random8() < chanceOfGlitter) {
  428.     leds[ random16(NUM_LEDS) ] += CRGB::White;
  429.   }
  430. }
  431.  
  432. void confetti()
  433. {
  434.   // random colored speckles that blink in and fade smoothly
  435.   fadeToBlackBy( leds, NUM_LEDS, 10);
  436.   int pos = random16(NUM_LEDS);
  437.   leds[pos] += CHSV( gHue + random8(64), 200, 255);
  438.   FastLED.show();  
  439.   FastLED.delay(1000/FRAMES_PER_SECOND);
  440. }
  441.  
  442. void sinelon()
  443. {
  444.   // a colored dot sweeping back and forth, with fading trails
  445.   fadeToBlackBy( leds, NUM_LEDS, 20);
  446.   int pos = beatsin16(13,0,NUM_LEDS);
  447.   leds[pos] += CHSV( gHue, 255, 192);
  448.   FastLED.show();  
  449.   FastLED.delay(1000/FRAMES_PER_SECOND);
  450. }
  451.  
  452. void bpm()
  453. {
  454.   // colored stripes pulsing at a defined Beats-Per-Minute (BPM)
  455.   uint8_t BeatsPerMinute = 62;
  456.   CRGBPalette16 palette = PartyColors_p;
  457.   uint8_t beat = beatsin8( BeatsPerMinute, 64, 255);
  458.   for( int i = 0; i < NUM_LEDS; i++) { //9948
  459.     leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10));
  460.   }
  461.   FastLED.show();  
  462.   FastLED.delay(1000/FRAMES_PER_SECOND);
  463. }
  464.  
  465. void juggle() {
  466.   // eight colored dots, weaving in and out of sync with each other
  467.   fadeToBlackBy( leds, NUM_LEDS, 20);
  468.   byte dothue = 0;
  469.   for( int i = 0; i < 8; i++) {
  470.     leds[beatsin16(i+7,0,NUM_LEDS)] |= CHSV(dothue, 200, 255);
  471.     dothue += 32;
  472.   }
  473.   FastLED.show();  
  474.   FastLED.delay(1000/FRAMES_PER_SECOND);
  475. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top