Advertisement
rainman002

esp8266 lamp

Nov 25th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.11 KB | None | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2. #ifdef __AVR__
  3.   #include <avr/power.h>
  4. #endif
  5.  
  6.  
  7. #include <ESP8266WiFi.h>
  8. #include <WiFiClient.h>
  9. #include <ESP8266WebServer.h>
  10.  
  11. //arduino
  12. #define LEDPIN 21
  13. #define RANDPIN 20
  14.  
  15. //nodemcu
  16. #define LEDPIN 2
  17. #define RANDPIN A0
  18.  
  19. #define LEDCOUNT 165
  20. #define ROWCOUNT 15
  21.  
  22. #define DECAY 12
  23.  
  24. const char *ssid = "lantern";
  25. const char *password = "lantern";
  26.  
  27. uint8_t rgbs_base[3*LEDCOUNT+6];
  28. uint8_t *rgbs = &rgbs_base[3];
  29. int16_t sway = 0;
  30. uint8_t scale = 0;
  31.  
  32. ESP8266WebServer server(80);
  33.  
  34. // Parameter 1 = number of pixels in strip
  35. // Parameter 2 = Arduino pin number (most are valid)
  36. // Parameter 3 = pixel type flags, add together as needed:
  37. //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  38. //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  39. //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
  40. //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  41. //   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
  42. Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDCOUNT, LEDPIN, NEO_GRB + NEO_KHZ800);
  43.  
  44.  
  45. // IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
  46. // pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
  47. // and minimize distance between Arduino and first pixel.  Avoid connecting
  48. // on a live circuit...if you must, connect GND first.
  49.  
  50. void setup()
  51. {
  52.  // if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
  53.   pinMode(LEDPIN, OUTPUT);
  54.   strip.begin();
  55.   strip.show();
  56.  
  57.   uint16_t i;
  58.   for(i=0;i<3*LEDCOUNT;i++)
  59.   {
  60.     rgbs[i] = 32;
  61.   }
  62.  
  63.     delay(1000);
  64.     Serial.begin(9600);
  65.     Serial.println();
  66.  
  67.     WiFi.softAP(ssid, password);
  68.  
  69.     IPAddress apip = WiFi.softAPIP();
  70.     Serial.print("visit: \n");
  71.     Serial.println(apip);
  72.     server.on("/", handleRoot);
  73.     server.on("/LED1", handleLed1);
  74.     server.on("/LED2", handleLed2);
  75.     server.on("/LED3", handleLed3);
  76.     server.on("/LED4", handleLed4);
  77.     server.begin();
  78.     Serial.println("HTTP server begunnered");
  79. }
  80.  
  81. void loop()
  82. {
  83.   uint8_t randint;
  84.   uint16_t i;
  85.  
  86.   for(i=0;i<3*(LEDCOUNT-ROWCOUNT);i++)
  87.   {
  88.     if(i % ROWCOUNT == 0)
  89.       sway = ((analogRead(RANDPIN) ^ micros()) & 15) - 8;
  90.  
  91.     if(sway == 0)
  92.       rgbs[i] = rgbs[i+3*ROWCOUNT] * DECAY / 16;
  93.     if(sway < 0)
  94.       rgbs[i] = ((rgbs[i+3*ROWCOUNT]*(16+sway)) + (rgbs[i+3*ROWCOUNT-3]*-sway))/16 * DECAY / 16;
  95.     if(sway > 0)
  96.       rgbs[i] = ((rgbs[i+3*ROWCOUNT]*(16-sway)) + (rgbs[i+3*ROWCOUNT+3]*sway))/16 * DECAY / 16;
  97.   }
  98.  
  99.   if(scale == 0)
  100.   {
  101.     for(i=3*(LEDCOUNT-ROWCOUNT);i<3*LEDCOUNT;i++)
  102.       rgbs[i] = 0;
  103.   }
  104.   else
  105.   {
  106.     for(i=3*(LEDCOUNT-ROWCOUNT);i<3*LEDCOUNT;i++)
  107.     {
  108.       randint = (analogRead(RANDPIN) ^ micros()) & 1;
  109.       if(randint && rgbs[i] > 0)
  110.         rgbs[i] = rgbs[i]*scale/16;
  111.       else if(~randint && rgbs[i] < 80)
  112.         rgbs[i] += scale;
  113.     }
  114.    
  115.     for(i=(LEDCOUNT-ROWCOUNT);i<LEDCOUNT;i++)
  116.     {
  117.       // turn green yellow
  118.       if(rgbs[i*3+1] > rgbs[i*3])
  119.         rgbs[i*3+1] = rgbs[i*3];
  120.     }
  121.   }
  122.  
  123.   rgbs[-3] = rgbs[0];
  124.   rgbs[-2] = rgbs[1];
  125.   rgbs[-1] = rgbs[2];
  126.   rgbs[3*LEDCOUNT] = rgbs[3*LEDCOUNT-3];
  127.   rgbs[3*LEDCOUNT+1] = rgbs[3*LEDCOUNT-2];
  128.   rgbs[3*LEDCOUNT+2] = rgbs[3*LEDCOUNT-1];
  129.    
  130. //  Serial.print("rand ");
  131. //  Serial.println(randint);
  132.  
  133.   for(i=0;i<LEDCOUNT;i++)
  134.   {
  135.     uint8_t r,g,b;
  136.     r = scalecolor(rgbs[i*3]);
  137.     g = scalecolor(rgbs[i*3+1]);
  138.     b = scalecolor(rgbs[i*3+2]>>3);
  139.     strip.setPixelColor(i, strip.Color(r,g,b));
  140.   }
  141.   strip.show();
  142.  
  143.   server.handleClient();
  144.  
  145.   delay(40);
  146. }
  147.  
  148. uint8_t scalecolor(uint8_t color)
  149. {
  150.   if(color > 79) color = 79;
  151.   if(color > 63) color = (color-48)*8;
  152.   else if(color > 47) color = (color-32)*4;
  153.   else if(color > 31) color = (color-16)*2;
  154.   return color;
  155. }
  156.  
  157. void handleRoot() {
  158.     response();
  159. }
  160.  
  161. void handleLed1() {
  162.   scale = 13;
  163.   //digitalWrite(LED_BUILTIN, stateLED);
  164.   response();
  165. }
  166.  
  167. void handleLed2() {
  168.   scale = 10;
  169.   //digitalWrite(LED_BUILTIN, stateLED);
  170.   response();
  171. }
  172. void handleLed3() {
  173.   scale = 4;
  174.   //digitalWrite(LED_BUILTIN, stateLED);
  175.   response();
  176. }
  177. void handleLed4() {
  178.   scale = 0;
  179.   //digitalWrite(LED_BUILTIN, stateLED);
  180.   response();
  181. }
  182.  
  183. const String HtmlHtml = "<html><head>"
  184.     "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" /></head>";
  185. const String HtmlHtmlClose = "</html>";
  186. const String HtmlTitle = "<h4>Magic Lantern</h4>";
  187. const String HtmlButtons =
  188.     "<a href=\"LED1\"><button style=\"display: block; width: 100%; height:90px;\">1</button></a><br/>"
  189.     "<a href=\"LED2\"><button style=\"display: block; width: 100%; height:90px;\">2</button></a><br/>"
  190.     "<a href=\"LED3\"><button style=\"display: block; width: 100%; height:90px;\">3</button></a><br/>"
  191.     "<a href=\"LED4\"><button style=\"display: block; width: 100%; height:90px;\">4</button></a><br/>";
  192.  
  193. void response()
  194. {
  195.   String htmlRes = HtmlHtml;
  196.   htmlRes += HtmlTitle;
  197.   htmlRes += HtmlButtons;
  198.   htmlRes += HtmlHtmlClose;
  199.   server.send(200, "text/html", htmlRes);
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement