rgbfill.ino 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. RGB Fill Lights
  3. Author: tobychui
  4. This is the firmware for tobychui's
  5. Programmable RGB Fill Light
  6. Recommend compiling with following board profiles
  7. - Wemos D1 Mini
  8. - CPU Speed: 160Mhz
  9. CopyRight tobychui, All Right Reserved
  10. */
  11. /* WiFi and Discovery */
  12. #include <ESP8266WiFi.h>
  13. #include <WiFiManager.h>
  14. #include <ESP8266mDNS.h>
  15. #include <ESP8266WebServer.h>
  16. #include <ArduinoJson.h>
  17. #include "LittleFS.h"
  18. /* Hardware & Controls */
  19. #include <Adafruit_NeoPixel.h>
  20. // Hardware Configs
  21. #define PIN D2
  22. #define NUM_LEDS 50
  23. #define BUTTON_MODE D7
  24. #define BUTTON_ADD D0
  25. #define BUTTON_COLOR D5
  26. #define BUTTON_MINUS D6
  27. #define BUTTON_AUTOINC_DELAY 500 //Delay before auto increments
  28. #define BUTTON_HOLD_DELAY 70 //Auto-increment delays
  29. #define BUTTON_DEBOUNCE 70 //Naive debounce delay in ms
  30. #define MAX_BRIGHTNESS 256 //Make sure the battery you using can output the current required by LEDs
  31. #define MAX_CTRLBRIGHTNESS 32 //Max brightness for signaling LED (the one above the button)
  32. #define WHITE_MIN_TEMP 2500 //Min color temperature in white mode
  33. #define WHITE_MAX_TEMP 10000 //Max color temperature in white mode
  34. //Runtimes
  35. /* Network Settings */
  36. #define DEVICE_NAME "FL1010-RGB"
  37. #define MDNS_NAME "fl1010-rgb"
  38. #define LISTENING_PORT 80
  39. WiFiManager wifiManager;
  40. ESP8266WebServer server(LISTENING_PORT);
  41. String deviceUUID = "";
  42. /*
  43. Current Mode
  44. 0 = White Mode (+/- Change the K value of the light)
  45. 1 = RGB Mode (+/C/- Change the RGB values of the light)
  46. 2 = Pure Color Mode (C Change the default color palletes, +- change brightness offset)
  47. */
  48. int currentMode = 0;
  49. /*
  50. Adjusting Catergory
  51. [White Mode]
  52. 0 = Color Temperature (K)
  53. 1 = Brightness
  54. [RGB Mode]
  55. 0 = Red
  56. 1 = Green
  57. 2 = Blue
  58. [Pallete Mode]
  59. 0 = Color Pallete
  60. 1 = Brightness
  61. */
  62. int adjustingCatergory = 0;
  63. /*
  64. Color Values
  65. [White Mode]
  66. TEMP, Brightness, (un-used)
  67. [RGB Mode]
  68. R, G, B
  69. [Pallete Mode]
  70. PalleteID, Brightness, (unused)
  71. */
  72. int values[] = {0, 0, 0};
  73. int currentCtrlLedRgb[] = {0, 0, 0};
  74. bool wifiControlMode = false;
  75. Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
  76. //Set the current control LED color
  77. void setControlLEDColor(int r, int g, int b) {
  78. currentCtrlLedRgb[0] = r;
  79. currentCtrlLedRgb[1] = g;
  80. currentCtrlLedRgb[2] = b;
  81. strip.setPixelColor(0, strip.Color(r, g, b));
  82. strip.show();
  83. }
  84. //Set the current LED light color by given RGB values
  85. void setLightColor(int r, int g, int b) {
  86. for (int i = 1; i < NUM_LEDS; i++) {
  87. strip.setPixelColor(i, strip.Color(r, g, b));
  88. }
  89. strip.show();
  90. }
  91. void setup() {
  92. //Start Serial
  93. Serial.begin(115200);
  94. //Set the buttons to input pin
  95. pinMode(BUTTON_MODE, INPUT);
  96. pinMode(BUTTON_ADD, INPUT);
  97. pinMode(BUTTON_COLOR, INPUT);
  98. pinMode(BUTTON_MINUS, INPUT);
  99. strip.begin();
  100. delay(300);
  101. setLightColor(0, 0, 0);
  102. //Check if mode button is hold. If yes, enter WiFi mode
  103. int tmp = digitalRead(BUTTON_MODE);
  104. if (tmp == LOW) {
  105. //Load device UUID
  106. deviceUUID = WiFi.macAddress();
  107. deviceUUID.replace(":", "-");
  108. //Setup WiFi
  109. setControlLEDColor(255, 255, 0); //Yellow
  110. Serial.println("Entering WiFi Mode");
  111. wifiManager.autoConnect(DEVICE_NAME);
  112. if (WiFi.status() == WL_CONNECTED) {
  113. Serial.println("WiFi Connected");
  114. setControlLEDColor(0, 32, 0); //Green
  115. wifiControlMode = true;
  116. //Start API service over WiFi
  117. registerAPI();
  118. } else {
  119. Serial.println("Network error. Running in offline mode");
  120. setControlLEDColor(255, 0, 0); //Red
  121. }
  122. } else {
  123. // Initialize all pixels to natural warm white light
  124. loadWhiteModeDefault();
  125. }
  126. }
  127. void loop() {
  128. if (wifiControlMode){
  129. server.handleClient();
  130. MDNS.update();
  131. }else{
  132. handleButtonLogic();
  133. }
  134. }