Advertisement
GyroGearloose

Huzzah & FastLED 01

Jan 31st, 2016
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.07 KB | None | 0 0
  1. /*--------------------------------------------------
  2. First trial: sending change requests via HTML works,
  3. however, the animation stops right away.
  4.  
  5. ToDo
  6. Include case and break, etc. to have the animation
  7. running until a new request is being sent.
  8.  
  9. HTTP 1.1 Webserver for ESP8266
  10. for ESP8266 adapted Arduino IDE
  11.  
  12. works w. Huzzah
  13.  
  14. BIG Thanks to
  15. Stefan Thesen 04/2015
  16. https://blog.thesen.eu/stabiler-http-1-1-wlan-webserver-mit-dem-esp8266-microcontroller/
  17.  
  18. Running stable for days
  19. (in difference to all samples I tried)
  20.  
  21. Does HTTP 1.1 with defined connection closing.
  22. Reconnects in case of lost WiFi.
  23. Handles empty requests in a defined manner.
  24. Handle requests for non-exisiting pages correctly.
  25.  
  26. This demo allows to switch two functions:
  27. Function 1 creates serial output and toggels GPIO2
  28. Function 2 just creates serial output.
  29.  
  30. Serial output can e.g. be used to steer an attached
  31. Arduino, Raspberry etc.
  32. --------------------------------------------------*/
  33.  
  34. #include "FastLED.h"
  35.  
  36. #include <ESP8266WiFi.h>
  37.  
  38. const char* ssid = "your Network";
  39. const char* password = "your Password";
  40.  
  41.  
  42. unsigned long ulReqcount;
  43. unsigned long ulReconncount;
  44.  
  45.  
  46. // Create an instance of the server on Port 80
  47. WiFiServer server(80);
  48.  
  49. FASTLED_USING_NAMESPACE
  50. #if FASTLED_VERSION < 3001000
  51. #error "Requires FastLED 3.1 or later; check github for latest code."
  52. #endif
  53.  
  54. #define DATA_PIN    13
  55. //#define CLK_PIN   4
  56. #define LED_TYPE    WS2811
  57. #define COLOR_ORDER GRB
  58. #define NUM_LEDS    16
  59. CRGB leds[NUM_LEDS];
  60.  
  61. #define BRIGHTNESS          64
  62. #define FRAMES_PER_SECOND  120
  63.  
  64.  
  65.  
  66.  
  67. void setup()
  68. {
  69.   // setup globals
  70.   ulReqcount=0;
  71.   ulReconncount=0;
  72.  
  73.   // prepare GPIO2
  74.   pinMode(2, OUTPUT);
  75.   digitalWrite(2, 0);
  76.  
  77.   // start serial
  78.   Serial.begin(9600);
  79.   delay(1);
  80.  
  81.   // inital connect
  82.   WiFi.mode(WIFI_STA);
  83.   WiFiStart();
  84.   FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  85.     FastLED.setBrightness(BRIGHTNESS);
  86.  
  87. }
  88.  
  89. void WiFiStart()
  90. {
  91.   ulReconncount++;
  92.  
  93.   // Connect to WiFi network
  94.   Serial.println();
  95.   Serial.println();
  96.   Serial.print("Connecting to ");
  97.   Serial.println(ssid);
  98.  
  99.   WiFi.begin(ssid, password);
  100.  
  101.   while (WiFi.status() != WL_CONNECTED) {
  102.     delay(500);
  103.     Serial.print(".");
  104.   }
  105.   Serial.println("");
  106.   Serial.println("WiFi connected");
  107.  
  108.   // Start the server
  109.   server.begin();
  110.   Serial.println("Server started");
  111.  
  112.   // Print the IP address
  113.   Serial.println(WiFi.localIP());
  114. }
  115.  
  116.  
  117.  
  118. uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
  119. uint8_t gHue = 0; // rotating "base color" used by many of the patterns
  120.  
  121.  
  122. void loop()
  123. {
  124.   // check if WLAN is connected
  125.   if (WiFi.status() != WL_CONNECTED)
  126.   {
  127.     WiFiStart();
  128.   }
  129.  
  130.   // Check if a client has connected
  131.   WiFiClient client = server.available();
  132.   if (!client)
  133.   {
  134.     return;
  135.   }
  136.  
  137.   // Wait until the client sends some data
  138.   Serial.println("new client");
  139.   unsigned long ultimeout = millis()+250;
  140.   while(!client.available() && (millis()<ultimeout) )
  141.   {
  142.     delay(1);
  143.   }
  144.   if(millis()>ultimeout)
  145.   {
  146.     Serial.println("client connection time-out!");
  147.     return;
  148.   }
  149.  
  150.   // Read the first line of the request
  151.   String sRequest = client.readStringUntil('\r');
  152.   //Serial.println(sRequest);
  153.   client.flush();
  154.  
  155.   // stop client, if request is empty
  156.   if(sRequest=="")
  157.   {
  158.     Serial.println("empty request! - stopping client");
  159.     client.stop();
  160.     return;
  161.   }
  162.  
  163.   // get path; end of path is either space or ?
  164.   // Syntax is e.g. GET /?pin=MOTOR1STOP HTTP/1.1
  165.   String sPath="",sParam="", sCmd="";
  166.   String sGetstart="GET ";
  167.   int iStart,iEndSpace,iEndQuest;
  168.   iStart = sRequest.indexOf(sGetstart);
  169.   if (iStart>=0)
  170.   {
  171.     iStart+=+sGetstart.length();
  172.     iEndSpace = sRequest.indexOf(" ",iStart);
  173.     iEndQuest = sRequest.indexOf("?",iStart);
  174.    
  175.     // are there parameters?
  176.     if(iEndSpace>0)
  177.     {
  178.       if(iEndQuest>0)
  179.       {
  180.         // there are parameters
  181.         sPath  = sRequest.substring(iStart,iEndQuest);
  182.         sParam = sRequest.substring(iEndQuest,iEndSpace);
  183.       }
  184.       else
  185.       {
  186.         // NO parameters
  187.         sPath  = sRequest.substring(iStart,iEndSpace);
  188.       }
  189.     }
  190.   }
  191.  
  192.   ///////////////////////////////////////////////////////////////////////////////
  193.   // output parameters to serial, you may connect e.g. an Arduino and react on it
  194.   ///////////////////////////////////////////////////////////////////////////////
  195.   if(sParam.length()>0)
  196.   {
  197.     int iEqu=sParam.indexOf("=");
  198.     if(iEqu>=0)
  199.     {
  200.       sCmd = sParam.substring(iEqu+1,sParam.length());
  201.       Serial.println(sCmd);
  202.     }
  203.   }
  204.  
  205.  
  206.   ///////////////////////////
  207.   // format the html response
  208.   ///////////////////////////
  209.   String sResponse,sHeader;
  210.  
  211.   ////////////////////////////
  212.   // 404 for non-matching path
  213.   ////////////////////////////
  214.   if(sPath!="/")
  215.   {
  216.     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>";
  217.    
  218.     sHeader  = "HTTP/1.1 404 Not found\r\n";
  219.     sHeader += "Content-Length: ";
  220.     sHeader += sResponse.length();
  221.     sHeader += "\r\n";
  222.     sHeader += "Content-Type: text/html\r\n";
  223.     sHeader += "Connection: close\r\n";
  224.     sHeader += "\r\n";
  225.   }
  226.   ///////////////////////
  227.   // format the html page
  228.   ///////////////////////
  229.   else
  230.   {
  231.     ulReqcount++;
  232.     sResponse  = "<html><head><title>BroogleLabs ESP8266 Steuerung</title></head><body>";
  233.     sResponse += "<font color=\"#000000\"><body bgcolor=\"#d0d0f0\">";
  234.     sResponse += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=yes\">";
  235.     sResponse += "<h1>Demo f&uumlr ESP8266 Steuerung</h1>";
  236.     sResponse += "Funktion 1 schaltet GPIO2 und erzeugt eine serielle Ausgabe.<BR>";
  237.     sResponse += "Funktion 2 erzeugt nur eine serielle Ausgabe.<BR>";
  238.     sResponse += "<FONT SIZE=+1>";
  239.     sResponse += "<p>Funktion 1 <a href=\"?pin=FUNCTION1ON\"><button>einschalten</button></a>&nbsp;<a href=\"?pin=FUNCTION1OFF\"><button>ausschalten</button></a></p>";
  240.     sResponse += "<p>Funktion 2 <a href=\"?pin=FUNCTION2ON\"><button>einschalten</button></a>&nbsp;<a href=\"?pin=FUNCTION2OFF\"><button>ausschalten</button></a></p>";
  241.    
  242.     //////////////////////
  243.     // react on parameters
  244.     //////////////////////
  245.     if (sCmd.length()>0)
  246.     {
  247.       // write received command to html page
  248.       sResponse += "Kommando:" + sCmd + "<BR>";
  249.      
  250.       // switch GPIO
  251.       if(sCmd.indexOf("FUNCTION1ON")>=0)
  252.       {
  253. //        digitalWrite(2, 1);
  254.         sinelon();
  255.       }
  256.       else if(sCmd.indexOf("FUNCTION1OFF")>=0)
  257.       {
  258. //        digitalWrite(2, 0);
  259.         rainbow() ;
  260.       }
  261.     }
  262.    
  263.     sResponse += "<FONT SIZE=-2>";
  264.     sResponse += "<BR>Aufrufz&auml;hler=";
  265.     sResponse += ulReqcount;
  266.     sResponse += " - Verbindungsz&auml;hler=";
  267.     sResponse += ulReconncount;
  268.     sResponse += "<BR>";
  269. //    sResponse += "Stefan Thesen 04/2015<BR>";
  270.     sResponse += "</body></html>";
  271.    
  272.     sHeader  = "HTTP/1.1 200 OK\r\n";
  273.     sHeader += "Content-Length: ";
  274.     sHeader += sResponse.length();
  275.     sHeader += "\r\n";
  276.     sHeader += "Content-Type: text/html\r\n";
  277.     sHeader += "Connection: close\r\n";
  278.     sHeader += "\r\n";
  279.  
  280.  
  281.            
  282.  
  283.   }
  284.  
  285.   // Send the response to the client
  286.   client.print(sHeader);
  287.   client.print(sResponse);
  288.  
  289.   // and stop the client
  290.   client.stop();
  291.   Serial.println("Client disonnected");
  292.  
  293.   FastLED.show();  
  294.   // insert a delay to keep the framerate modest
  295.   FastLED.delay(1000/FRAMES_PER_SECOND);
  296.  
  297.   // do some periodic updates
  298.   EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
  299. }
  300.  
  301. void rainbow()
  302. {
  303.   // FastLED's built-in rainbow generator
  304.   fill_rainbow( leds, NUM_LEDS, gHue, 7);
  305. }
  306.  
  307. void sinelon()
  308. {
  309.   // a colored dot sweeping back and forth, with fading trails
  310.   fadeToBlackBy( leds, NUM_LEDS, 20);
  311.   int pos = beatsin16(13,0,NUM_LEDS);
  312.   leds[pos] += CHSV( gHue, 255, 192);
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement