Advertisement
MrRockchip

lantern_part8266

Nov 9th, 2021
1,100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.03 KB | None | 0 0
  1. //// [000.E662FE][001.E662FE][002.E662FE][003.E662FE][004.E662FE][005.E662FE][006.E662FE][007.E662FE]!
  2.  
  3. /* Neopixel web server with ESP8266-01
  4.  * with switch to rainbow animation
  5.  *
  6.  * Tom Tobback Sep 2016
  7.  * BuffaloLabs www.cassiopeia.hk/kids
  8. LICENSE:
  9. Copyright (c) 2016 Tom Tobback
  10.  
  11. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  12.  
  13. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  14.  
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16.  
  17.  * web page esp/rgb returns POST request with 3 RGB parameters
  18.  * web page inspired by https://github.com/dimsumlabs/nodemcu-httpd
  19.  *
  20.  * voltage regulator from 5V to 3.3V
  21.  * neopixel strip 8 LEDs on GPIO2, and Vcc on 3.3V otherwise the data goes wrong sometimes
  22.  *
  23.  * ESP8266 ESP-01 connections
  24.  * Vcc 3.3V
  25.  * GND GND
  26.  * GPIO2 neopixel data pin with 430ohm resistor
  27.  * GPIO3 switch between animation/webserver (only at startup!) with 4K3 pull-down resistor and IN5819 diode to Vcc
  28.  * CH_PD 3.3V (actually Vcc)
  29.  * RST 3.3V (actually Vcc)
  30.  *
  31.  * 3 way switch on 3.3V to choose between webserver and animation
  32.  * GPIO3=RX is connected to switch: HIGH= animation LOW= webserver
  33.  * in HIGH state the power goes from GPIO3 to Vcc via a diode IN5819
  34.  * in LOW state the Vcc power cannot reach GPIO3
  35.  */
  36.  
  37. #define STRIP_NUMPIXELS 8
  38.  
  39. #include <ESP8266WiFi.h>
  40. #include <DNSServer.h>
  41. #include <ESP8266WebServer.h>
  42.  
  43. /* Set up your SSID and Password */
  44. const char*     ssid =  "NodeMCU"; // SSID
  45. const char* password = "19216811"; // Password
  46.  
  47. /* Set up your IP addresses */
  48. IPAddress local_ip(192,168,1,1);
  49. IPAddress  gateway(192,168,1,1);
  50. IPAddress subnet(255,255,255,0);
  51.  
  52. const byte DNS_PORT = 53;
  53. DNSServer dnsServer;
  54. ESP8266WebServer webServer(80);
  55.  
  56. boolean animation;
  57.  
  58. String webpageRGB = ""
  59.  "<!DOCTYPE html><html><head><title>Neopixel control</title><meta name='mobile-web-app-capable' content='yes' />"
  60.  "<meta name='viewport' content='width=device-width' /></head><body style='margin: 0px; padding: 0px;'>"
  61.  "<canvas id='colorspace'></canvas></body>"
  62.  "<script type='text/javascript'>"
  63.  "(function () {"
  64.  " var canvas = document.getElementById('colorspace');"
  65.  " var ctx = canvas.getContext('2d');"
  66.  " function drawCanvas() {"
  67.  " var colours = ctx.createLinearGradient(0, 0, window.innerWidth, 0);"
  68.  " for(var i=0; i <= 360; i+=10) {"
  69.  " colours.addColorStop(i/360, 'hsl(' + i + ', 100%, 50%)');"
  70.  " }"
  71.  " ctx.fillStyle = colours;"
  72.  " ctx.fillRect(0, 0, window.innerWidth, window.innerHeight);"
  73.  " var luminance = ctx.createLinearGradient(0, 0, 0, ctx.canvas.height);"
  74.  " luminance.addColorStop(0, '#ffffff');"
  75.  " luminance.addColorStop(0.05, '#ffffff');"
  76.  " luminance.addColorStop(0.5, 'rgba(0,0,0,0)');"
  77.  " luminance.addColorStop(0.95, '#000000');"
  78.  " luminance.addColorStop(1, '#000000');"
  79.  " ctx.fillStyle = luminance;"
  80.  " ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);"
  81.  " }"
  82.  " var eventLocked = false;"
  83.  " function handleEvent(clientX, clientY) {"
  84.  " if(eventLocked) {"
  85.  " return;"
  86.  " }"
  87.  " function colourCorrect(v) {"
  88.  " return Math.round(1023-(v*v)/64);"
  89.  " }"
  90.  " var data = ctx.getImageData(clientX, clientY, 1, 1).data;"
  91.  " var params = ["
  92.  " 'r=' + colourCorrect(data[0]),"
  93.  " 'g=' + colourCorrect(data[1]),"
  94.  " 'b=' + colourCorrect(data[2])"
  95.  " ].join('&');"
  96.  " var req = new XMLHttpRequest();"
  97.  " req.open('POST', '?' + params, true);"
  98.  " req.send();"
  99.  " eventLocked = true;"
  100.  " req.onreadystatechange = function() {"
  101.  " if(req.readyState == 4) {"
  102.  " eventLocked = false;"
  103.  " }"
  104.  " }"
  105.  " }"
  106.  " canvas.addEventListener('click', function(event) {"
  107.  " handleEvent(event.clientX, event.clientY, true);"
  108.  " }, false);"
  109.  " canvas.addEventListener('touchmove', function(event){"
  110.  " handleEvent(event.touches[0].clientX, event.touches[0].clientY);"
  111.  "}, false);"
  112.  " function resizeCanvas() {"
  113.  " canvas.width = window.innerWidth;"
  114.  " canvas.height = window.innerHeight;"
  115.  " drawCanvas();"
  116.  " }"
  117.  " window.addEventListener('resize', resizeCanvas, false);"
  118.  " resizeCanvas();"
  119.  " drawCanvas();"
  120.  " document.ontouchmove = function(e) {e.preventDefault()};"
  121.  " })();"
  122.  "</script></html>";
  123.  //////////////////////////////////////////////////////////////////////////////////////////////////
  124.  
  125. static uint32_t strip_color(uint8_t r, uint8_t g, uint8_t b)
  126. {
  127.   return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
  128. }
  129.  
  130. void serial_printhex(uint32_t num, uint16_t digits)
  131. {
  132.   if ((digits >= 3) && (num < 0x100)) {
  133.     Serial.print("0");
  134.   }
  135.   if ((digits >= 2) && (num < 0x10)) {
  136.     Serial.print("0");
  137.   }
  138.   Serial.print(num, HEX);
  139. }
  140.  
  141. void strip_setPixelColor(uint32_t n, uint8_t r, uint8_t g, uint8_t b)
  142. {
  143.   Serial.print("[");
  144.   serial_printhex(n, 3);
  145.   Serial.print(".");
  146.   serial_printhex(r, 2);
  147.   serial_printhex(g, 2);
  148.   serial_printhex(b, 2);
  149.   Serial.print("]");
  150.   return;
  151. }
  152.  
  153. void strip_show()
  154. {
  155.   Serial.print("!");
  156.   return;
  157. }
  158.  
  159. void strip_setPixelColor(uint32_t n, uint32_t c)
  160. {
  161.   uint8_t r = (uint8_t)(c >> 16);
  162.   uint8_t g = (uint8_t)(c >> 8);
  163.   uint8_t b = (uint8_t)c;
  164.   strip_setPixelColor(n, r, g, b);
  165. }
  166.  
  167. void handleRGB()
  168. {
  169.   // Serial.println("handle RGB..");
  170.  
  171.   webServer.send(200, "text/html", webpageRGB);
  172.  
  173.   String red   = webServer.arg(0); // read RGB arguments
  174.   String green = webServer.arg(1);
  175.   String blue  = webServer.arg(2);
  176.  
  177.   allRGB(255 - red.toInt() / 4, 255 - green.toInt() / 4, 255 - blue.toInt() / 4);
  178. }
  179.  
  180. //////////////////////////////////////////////////////////////////////////////////////////////////
  181.  //////////////////////////////////////////////////////////////////////////////////////////////////
  182.  //////////////////////////////////////////////////////////////////////////////////////////////////
  183.  
  184. void setup()
  185. {
  186.  /*
  187.   * It is recommended to use the lower baud rate. The higher the baud rate,
  188.   * the higher the bit error rate will be, and the control action might fail.
  189.   */
  190.   Serial.begin(4800); // Initialize the ESP8266 <=> MEGA2560 serial port
  191.  
  192.   /* STRIP */// strip.begin();
  193.   strip_show(); // Initialize all pixels to 'off'
  194.   /* STRIP */// strip.setBrightness(250); // 0-255 range
  195.  
  196.   animation = false;
  197.  
  198.   if (!animation) { // start webserver
  199.     allRGB(0, 0, 0);
  200.     delay(1000);
  201.     // Serial.println("Starting webserver...");
  202.  
  203.     WiFi.mode(WIFI_AP);
  204.     WiFi.softAPConfig(local_ip, gateway, subnet);
  205.     WiFi.softAP(ssid, password);
  206.  
  207.     // if DNSServer is started with "*" for domain name, it will reply with provided IP to all DNS request
  208.     dnsServer.start(DNS_PORT, "esp", local_ip);
  209.     webServer.on("/", handleRGB);
  210.  
  211.     webServer.begin();
  212.  
  213.     introRed(); // knightrider
  214.     // colorWipe(0x859d12, 5);
  215.     // rainbow(5);
  216.     // rainbowCycle(5);
  217.     // theaterChaseRainbow(5);
  218.     // theaterChase(0x7e1ae1, 5);
  219.     // colorWipe(0x123456, 5);
  220.    
  221.     //// strip_setPixelColor(0, 255, 0, 0);
  222.     //// strip_setPixelColor(3, 255, 0, 0);
  223.     //// strip_setPixelColor(4, 255, 0, 0);
  224.     //// strip_setPixelColor(7, 255, 0, 0);
  225.     strip_show();
  226. /*
  227.  * CURRENT LOG:
  228.  * CLEAR_ALL, CLEAR_ALL + 0 LED RED, CLEAR_ALL + 1 LED RED, ..., CLEAR_ALL + 7 LED RED,
  229.  * CLEAR_ALL + 6 LED RED, ..., CLEAR_ALL + 0 LED RED, ..., CLEAR_ALL + 7 LED RED, ...,
  230.  * CLEAR_ALL + 1 LED RED, CLEAR_ALL, 8 LED RED (???), 0 + 3 + 4 + 7 LED RED
  231.  */
  232.   }
  233.   else {
  234.     WiFi.disconnect();
  235.     WiFi.softAPdisconnect(true);
  236.     WiFi.mode(WIFI_STA);
  237.     // Serial.println("Starting animation...");
  238.   }
  239. }
  240.  
  241. //////////////////////////////////////////////////////////////////////////////////////////////////
  242.  
  243. void loop()
  244. {
  245.   if (animation) {
  246.     rainbow(20);
  247.     rainbowCycle(20);
  248.     theaterChaseRainbow(50);
  249.   }
  250.   else {
  251.     dnsServer.processNextRequest();
  252.     webServer.handleClient();
  253.   }
  254. }
  255.  //////////////////////////////////////////////////////////////////////////////////////////////////
  256.  /////////////////////// NEOPIXEL FUNCTIONS //////////////////////////////////////////////////
  257.  //////////////////////////////////////////////////////////////////////////////////////////////////
  258.  
  259. // Fill the dots one after the other with a color
  260. void colorWipe(uint32_t c, uint8_t wait)
  261. {
  262.   uint32_t i;
  263.  
  264.   for (i = 0; i < STRIP_NUMPIXELS; i++) {
  265.     strip_setPixelColor(i, c);
  266.     strip_show();
  267.     delay(wait);
  268.   }
  269. }
  270.  
  271. void rainbow(uint8_t wait)
  272. {
  273.   uint32_t i;
  274.   uint16_t j;
  275.  
  276.   for (j = 0; j < 256; j++) {
  277.     for (i = 0; i < STRIP_NUMPIXELS; i++) {
  278.       strip_setPixelColor(i, Wheel((i + j) & 255));
  279.     }
  280.     strip_show();
  281.     delay(wait);
  282.   }
  283. }
  284.  
  285. // Slightly different, this makes the rainbow equally distributed throughout
  286. void rainbowCycle(uint8_t wait)
  287. {
  288.   uint32_t i;
  289.   uint16_t j;
  290.  
  291.   for (j = 0; j < 256 * 5; j++) { // 5 cycles of all colors on wheel
  292.     for (i = 0; i < STRIP_NUMPIXELS; i++) {
  293.       strip_setPixelColor(i, Wheel(((i * 256 / STRIP_NUMPIXELS) + j) & 255));
  294.     }
  295.     strip_show();
  296.     delay(wait);
  297.   }
  298. }
  299.  
  300. //Theatre-style crawling lights.
  301. void theaterChase(uint32_t c, uint8_t wait)
  302. {
  303.   uint32_t i;
  304.   uint16_t j, q;
  305.  
  306.   for (j = 0; j < 10; j++) { //do 10 cycles of chasing
  307.     for (q = 0; q < 3; q++) {
  308.       for (i = 0; i < STRIP_NUMPIXELS; i = i + 3) {
  309.         strip_setPixelColor(i + q, c); //turn every third pixel on
  310.       }
  311.       strip_show();
  312.  
  313.       delay(wait);
  314.  
  315.       for (i = 0; i < STRIP_NUMPIXELS; i = i + 3) {
  316.         strip_setPixelColor(i + q, 0); //turn every third pixel off
  317.       }
  318.     }
  319.   }
  320. }
  321.  
  322. //Theatre-style crawling lights with rainbow effect
  323. void theaterChaseRainbow(uint8_t wait)
  324. {
  325.   uint32_t i;
  326.   uint16_t j, q;
  327.  
  328.   for (j = 0; j < 256; j++) { // cycle all 256 colors in the wheel
  329.     for (q = 0; q < 3; q++) {
  330.       for (i = 0; i < STRIP_NUMPIXELS; i = i + 3) {
  331.         strip_setPixelColor(i + q, Wheel( (i + j) % 255)); //turn every third pixel on
  332.       }
  333.       strip_show();
  334.  
  335.       delay(wait);
  336.  
  337.       for (i = 0; i < STRIP_NUMPIXELS; i = i + 3) {
  338.         strip_setPixelColor(i + q, 0); //turn every third pixel off
  339.       }
  340.     }
  341.   }
  342. }
  343.  
  344. // Input a value 0 to 255 to get a color value.
  345. // The colours are a transition r - g - b - back to r.
  346. uint32_t Wheel(byte WheelPos)
  347. {
  348.   WheelPos = 255 - WheelPos;
  349.   if (WheelPos < 85) {
  350.     return strip_color(255 - WheelPos * 3, 0, WheelPos * 3);
  351.   }
  352.   if (WheelPos < 170) {
  353.     WheelPos -= 85;
  354.     return strip_color(0, WheelPos * 3, 255 - WheelPos * 3);
  355.   }
  356.   WheelPos -= 170;
  357.   return strip_color(WheelPos * 3, 255 - WheelPos * 3, 0);
  358. }
  359.  
  360. //////////////////////////////// TOM NEOPIXEL //////////////////////////////////////////////////
  361.  
  362. void introRed()
  363. {
  364.   uint32_t i;
  365.   uint16_t u;
  366.  
  367.   for (u = 0; u < 3; u++) {
  368.     for (i = 0; i < STRIP_NUMPIXELS; i++) {
  369.       oneRed(i);
  370.       delay(100);
  371.     }
  372.     for (i = STRIP_NUMPIXELS - 2; i > 0 ; i--) {
  373.       oneRed(i);
  374.       delay(100);
  375.     }
  376.   }
  377.   /* STRIP */// oneRed(STRIP_NUMPIXELS); // switch all off (switch on only 8, but beyond range)
  378.   for (i = 0; i < STRIP_NUMPIXELS; i++) {
  379.     strip_setPixelColor(i, 0, 0, 0);
  380.   }
  381. }
  382.  
  383. void oneRed(uint32_t u)
  384. {
  385.   uint32_t i;
  386.  
  387.   for (i = 0; i < STRIP_NUMPIXELS; i++) {
  388.     strip_setPixelColor(i, 0, 0, 0);
  389.   }
  390.   strip_setPixelColor(u, 255, 0, 0);
  391.   strip_show();
  392. }
  393.  
  394. void allRGB(uint8_t r, uint8_t g, uint8_t b)
  395. {
  396.   uint32_t i;
  397.  
  398.   for (i = 0; i < STRIP_NUMPIXELS; i++) {
  399.     strip_setPixelColor(i, r, g, b);
  400.   }
  401.   strip_show();
  402. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement