Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
647
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.77 KB | None | 0 0
  1. #include <Adafruit_MQTT.h>
  2. #include <Adafruit_MQTT_Client.h>
  3. #define FASTLED_ESP8266_RAW_PIN_ORDER
  4. #include <FastLED.h>
  5. #include <EEPROM.h>
  6. #define LED_PIN 2
  7. #define NUM_LEDS 300 // Number of lights
  8. #define BRIGHTNESS 120 // Brightness
  9. #define LED_TYPE WS2812B
  10. #define COLOR_ORDER GRB
  11. CRGB leds[NUM_LEDS];
  12. uint8_t color;
  13. uint8_t colour; // Ripple colour is randomized.
  14. int center = 0; // Center of the current ripple.
  15. int step = -1; // -1 is the initializing step.
  16. uint8_t myfade = 255; // Starting brightness.
  17. #define maxsteps 16
  18. int num,nums = 1;
  19. int w = 0;
  20. int forth = 0;
  21. int b=50;
  22. int Red = 50;
  23. int Green = 50;
  24. int Blue = 50;
  25. /****************************** Speed ******************************************/
  26.  
  27. int UPDATES_PER_SECOND = 250;// High the number the faster the patterns will move
  28.  
  29. /********************************************************************************/
  30. #define CHANCE_OF_TWINKLE 2
  31. #define DELTA_COLOR_DOWN CRGB(1,0,1)
  32. #define DELTA_COLOR_UP CRGB(4,0,4)
  33. #define PEAK_COLOR CRGB(0,128,128)
  34. #define BASE_COLOR CRGB(0,32,32)
  35. #define MASTER_BRIGHTNESS 255
  36.  
  37. #include <ESP8266WiFi.h>
  38.  
  39. String next;
  40. #define led D7
  41.  
  42. #define WLAN_SSID "xxxx" // Nazwa sieci WiFi
  43. #define WLAN_PASS "xxx" // Hasło do Wifi
  44.  
  45. /************************* Adafruit.io Setup *********************************/
  46.  
  47. #define AIO_SERVER "io.adafruit.com"
  48. #define AIO_SERVERPORT 1883 // use 8883 for SSL
  49. #define AIO_USERNAME "xxxxx" // Nazwa konta na stronie io.adafruit.com
  50. #define AIO_KEY "xxxxx" // Klucz autoryzacji projektu
  51.  
  52. /************ Global State (you don't need to change this!) ******************/
  53.  
  54. // Create an ESP8266 WiFiClient class to connect to the MQTT server.
  55. WiFiClient client;
  56. // or... use WiFiFlientSecure for SSL
  57. //WiFiClientSecure client;
  58.  
  59. // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
  60. Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
  61.  
  62. /****************************** Feeds ***************************************/
  63.  
  64.  
  65. // Setup a feed called 'onoff' for subscribing to changes.
  66.  
  67. /****************************** FeedName ************************************/
  68. Adafruit_MQTT_Subscribe Mode = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/mode");
  69.  
  70.  
  71.  
  72. CRGBPalette16 currentPalette;
  73. TBlendType currentBlending;
  74.  
  75. extern CRGBPalette16 myRedWhiteBluePalette;
  76. extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;
  77.  
  78.  
  79. void MQTT_connect();
  80.  
  81.  
  82. void setup() {
  83. Serial.begin(115200);
  84. EEPROM.begin(512);
  85.  
  86. pinMode(5, OUTPUT);
  87. pinMode(0, OUTPUT);
  88.  
  89. pinMode(led,OUTPUT);
  90. digitalWrite(led,HIGH);
  91.  
  92. delay( 100 ); // power-up safety delay
  93. FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  94. FastLED.setBrightness( BRIGHTNESS );
  95. color = random16(0,255);
  96. currentPalette = RainbowColors_p;
  97. currentBlending = LINEARBLEND;
  98.  
  99.  
  100. // Connect to WiFi access point.
  101. Serial.println(); Serial.println();
  102. Serial.print("Connecting to ");
  103. Serial.println(WLAN_SSID);
  104.  
  105. WiFi.begin(WLAN_SSID, WLAN_PASS);
  106. while (WiFi.status() != WL_CONNECTED) {
  107. delay(500);
  108. Serial.print(".");
  109. }
  110. Serial.println();
  111.  
  112. digitalWrite(led,LOW);
  113.  
  114. Serial.println("WiFi connected");
  115. Serial.println("IP address: ");
  116. Serial.println(WiFi.localIP());
  117.  
  118.  
  119. // Setup MQTT subscription for onoff feed.
  120. mqtt.subscribe(&Mode);
  121.  
  122. }
  123.  
  124. void loop() {
  125.  
  126. MQTT_connect();
  127.  
  128. Adafruit_MQTT_Subscribe *subscription;
  129.  
  130. while ((subscription = mqtt.readSubscription(2))) {
  131.  
  132. if (subscription == &Mode) {
  133. Serial.print("Mode > ");
  134. Serial.print(F("Got: "));
  135. Serial.println((char *)Mode.lastread);
  136. byte Mode1_state = atoi((char *)Mode.lastread);
  137. //analogWrite(0, Light2_State);
  138. //EEPROM.write(1, Mode_state);
  139. EEPROM.commit();
  140. next = ((char *)Mode.lastread);
  141. ModeChange();
  142. //Serial.println(EEPROM.read(1));
  143. //Serial.println(Light2_State);
  144. }
  145. }
  146.  
  147. if(w==1){
  148. poweron();
  149. static uint8_t startIndex = 0;
  150. startIndex = startIndex + 1; /* motion speed */
  151. FillLEDsFromPaletteColors( startIndex);
  152. }
  153. else if(w==3){
  154. ripple();
  155. }
  156. else if(w==2){
  157. poweroff();
  158. Red = 0;
  159. Green = 0;
  160. Blue = 0;
  161. FillSolid();
  162. w=0;
  163. }
  164. else if(w==4){
  165. TwinkleMapPixels();
  166. }
  167. else if(w==5){
  168. back();
  169. }
  170. else if(w==6){
  171. rave();
  172. //FillSolid();
  173. }
  174.  
  175.  
  176. FastLED.show();
  177. FastLED.delay(1000 / UPDATES_PER_SECOND);
  178. }
  179.  
  180. void MQTT_connect() {
  181. int8_t ret;
  182.  
  183. // Stop if already connected.
  184. if (mqtt.connected()) {
  185. //Serial.println("Connected");
  186. return;
  187. }
  188.  
  189. digitalWrite(led,HIGH);
  190. Serial.print("Connecting to MQTT... ");
  191.  
  192. uint8_t retries = 3;
  193.  
  194. while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
  195. Serial.println(mqtt.connectErrorString(ret));
  196. Serial.println("Retrying MQTT connection in 5 seconds...");
  197. mqtt.disconnect();
  198. delay(5000); // wait 5 seconds
  199. retries--;
  200. if (retries == 0) {
  201. // basically die and wait for WDT to reset me
  202. //while (1);
  203. Serial.println("Wait 10 min to reconnect");
  204. delay(600000);
  205. }
  206. }
  207. Serial.println("MQTT Connected!");
  208. digitalWrite(led,LOW);
  209. }
  210.  
  211. void FillLEDsFromPaletteColors( uint8_t colorIndex)
  212. {
  213. uint8_t brightness = 255;
  214.  
  215. for( int i = 0; i < NUM_LEDS; i++) {
  216. leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
  217. colorIndex += 3;
  218. }
  219. }
  220.  
  221. void ModeChange(){
  222. Serial.println(next);
  223. if (next == "Off" or next == "off"or next == "one" or next == "One"){
  224. Serial.println("Item 1 selected");
  225. w=2;
  226. }
  227. else if (next == "Rainbow" or next == "rainbow" or next == "on" or next == "On" or next == "two"or next == "Two"){
  228. w=1;
  229. currentPalette = RainbowColors_p;
  230. }
  231.  
  232. else if (next == "rainbow colors" or next == "three"or next == "Three" or next == "tree" or next == "Tree"){
  233. currentPalette = RainbowStripeColors_p;
  234. w=1;
  235. }
  236. else if (next == "Heat" or next == "heat"or next == "four"or next == "Four" or next == "for" or next == "For"){
  237. currentPalette = HeatColors_p;
  238. w=1;
  239. }
  240. else if (next == "Lava" or next == "lava" or next == "five"or next == "Five"){
  241. currentPalette = LavaColors_p;
  242. w=1;
  243. }
  244. else if (next == "Forest" or next == "forest" or next == "six"or next == "6"){
  245. currentPalette =ForestColors_p;
  246. w=1;
  247. }
  248. else if (next == "Clouds" or next == "seven"or next == "7"){
  249. currentPalette = CloudColors_p;
  250. w=1;
  251. }
  252. else if (next == "Ocean" or next == "eight"or next == "8"){
  253. currentPalette = OceanColors_p;
  254. w=1;
  255. }
  256. else if (next == "party colors" or next == "nine"or next == "9"){
  257. currentPalette = PartyColors_p;
  258. w=1;
  259. }
  260. else if (next == "changing colors" or next == "ten"or next == "10"){
  261. SetupPurpleAndGreenPalette();
  262. w=1;
  263. }
  264. else if (next == "random colors" or next == "11"){
  265. SetupTotallyRandomPalette();
  266. w=1;
  267. }
  268. else if (next == "one color" or next == "12"){
  269. OneColour();
  270. w=1;
  271. }
  272. else if (next == "two colors" or next == "13"){
  273. OneColourlots();
  274. w=1;
  275. }
  276. else if (next == "four colors" or next == "14"){
  277. OneColourlotss();
  278. w=1;
  279. }
  280. else if (next == "Ripple" or next == "15"){
  281. w=3;
  282. }
  283. else if (next == "twinkle" or next == "16"){
  284. w=4;
  285. InitPixelStates();
  286. }
  287. else if (next == "back and fouth" or next == "17"){
  288. w=5;
  289. InitPixelStates();
  290. }
  291. else if (next == "Christmas" or next == "18"){
  292. w=1;
  293. red();
  294. }
  295. else if (next == "Rave" or next == "19"){
  296. w=6;
  297. }
  298. }
  299.  
  300. void back(){
  301. delay(2000 /UPDATES_PER_SECOND);
  302. for (int i = 0; i < NUM_LEDS; i++) leds[i] = CRGB(0,0,0);
  303.  
  304. if (forth ==0){
  305. colour = random16(0,255);
  306. }
  307. if (forth == 0){
  308. num = 1;
  309. }
  310. else if(forth == (NUM_LEDS-1)){
  311. num = -1;
  312. }
  313. if (b ==0){
  314. color = random16(0,255);
  315. }
  316. if (b == 0){
  317. nums = 1;
  318. }
  319. else if(b == (NUM_LEDS-1)){
  320. nums = -1;
  321. }
  322. leds[forth] = CHSV(colour, 255, 255);
  323. leds[b] = CHSV(color, 255, 255);
  324. forth = forth + num;
  325. b=b+nums;
  326. //Serial.println(forth);
  327. }
  328.  
  329. void poweroff()
  330. { pinMode(A0, INPUT);}
  331. //-----------------------------------------------
  332. void poweron()
  333. { pinMode(A0, OUTPUT);
  334. digitalWrite(A0, LOW); }
  335. //-----------------------------------------------
  336.  
  337.  
  338. void FillSolid(){
  339. CRGB colour = CRGB( Red, Green, Blue);
  340. fill_solid(leds,NUM_LEDS ,CRGB( Red, Green, Blue));
  341.  
  342. }
  343.  
  344. void party(){
  345. colour = random16(0,255);
  346. fill_solid(leds,NUM_LEDS ,CHSV(colour, 255, 255));
  347. delay(100);
  348. }
  349.  
  350. void rave(){
  351. uint8_t Hand = (millis() /100) % 2;
  352. static uint8_t y = 99;
  353. if( y != Hand) {
  354. y = Hand;
  355. if(Hand == 1){colour = random16(0,255); w=6;}
  356. } fill_solid(leds,NUM_LEDS ,CHSV(colour, 255, 255));}
  357.  
  358. void red()
  359. {
  360. CRGB purple = CHSV( HUE_RED, 255, 255);
  361. CRGB green = CHSV( HUE_GREEN, 255, 255);
  362. CRGB black = CRGB::Black;
  363.  
  364. currentPalette = CRGBPalette16(
  365. green, purple, green, purple,
  366. green, purple, green, purple,
  367. green, purple, green, purple,
  368. green, purple, green, purple );
  369. }
  370.  
  371. void SetupTotallyRandomPalette()
  372. {
  373. for( int i = 0; i < 16; i++) {
  374. currentPalette[i] = CHSV( random8(), 255, random8());
  375. }
  376. }
  377.  
  378. void SetupPurpleAndGreenPalette()
  379. {
  380. CRGB purple = CHSV( HUE_PINK, 255, 255);
  381. CRGB green = CRGB::White;
  382. CRGB black = CRGB::Black;
  383. CRGB blue = CHSV( HUE_AQUA,255,255);
  384. CRGB yellow = CHSV( HUE_YELLOW,255,255);
  385. CRGB orange = CHSV( HUE_ORANGE,255,255);
  386. currentPalette = CRGBPalette16(
  387. green, yellow, black, black,
  388. purple, purple, black, black,
  389. blue, blue, orange, black,
  390. purple, orange, yellow, black );
  391. }
  392.  
  393. void ripple() {
  394. for (int i = 0; i < NUM_LEDS; i++) leds[i] = CRGB(0,0,0); // Rotate background colour.
  395. delay(3000 /UPDATES_PER_SECOND);
  396. switch (step) {
  397.  
  398. case -1: // Initialize ripple variables.
  399. center = random(NUM_LEDS);
  400. colour = random16(0,255);
  401. step = 0;
  402. break;
  403.  
  404. case 0:
  405. leds[center] = CHSV(colour, 255, 255);
  406. // Display the first pixel of the ripple.
  407. step ++;
  408. break;
  409.  
  410. case maxsteps: // At the end of the ripples.
  411. step = -1;
  412. break;
  413.  
  414. default: // Middle of the ripples.
  415. leds[wrap(center + step)] += CHSV(colour, 255, myfade/step*2); // Display the next pixels in the range for one side.
  416. leds[wrap(center - step)] += CHSV(colour, 255, myfade/step*2);
  417. leds[wrap(center + (step+1))] += CHSV(colour, 255, myfade/step*2); // Display the next pixels in the range for one side.
  418. leds[wrap(center - (step-1))] += CHSV(colour, 255, myfade/step*2);// Display the next pixels in the range for the other side.
  419. leds[wrap(center + (step+2))] += CHSV(colour, 255, myfade/step*2); // Display the next pixels in the range for one side.
  420. leds[wrap(center - (step-2))] += CHSV(colour, 255, myfade/step*2);
  421. step ++; // Next step.
  422. break;
  423. } // switch step
  424. } // ripple()
  425.  
  426. int wrap(int step) {
  427. if(step < 0) return NUM_LEDS + step;
  428. if(step > NUM_LEDS - 1) return step - NUM_LEDS;
  429. return step;
  430. } // wrap()
  431.  
  432.  
  433. enum { SteadyDim, GettingBrighter, GettingDimmerAgain };
  434. uint8_t PixelState[NUM_LEDS];
  435.  
  436. void InitPixelStates()
  437. {
  438. memset( PixelState, sizeof(PixelState), SteadyDim); // initialize all the pixels to SteadyDim.
  439. fill_solid( leds, NUM_LEDS, BASE_COLOR);
  440. }
  441.  
  442. void OneColour()
  443. {
  444. CRGB black = CRGB::Black;
  445. CRGB color = CRGB(Red,Green,Blue);
  446. currentPalette = CRGBPalette16(
  447. color, black, black, black,
  448. black, black, black, black,
  449. color, black, black, black,
  450. black, black, black, black );
  451. }
  452.  
  453.  
  454. void OneColourlots()
  455. {
  456. CRGB black = CRGB::Black;
  457. CRGB color = CRGB(Red,Green,Blue);
  458. currentPalette = CRGBPalette16(
  459. color, black, black, black,
  460. color, black, black, black,
  461. color, black, black, black,
  462. color, black, black, black );
  463. }
  464.  
  465. void OneColourlotss()
  466. {
  467. CRGB black = CRGB::Black;
  468. CRGB color = CRGB(Red,Green,Blue);
  469. currentPalette = CRGBPalette16(
  470. color, black, color, black,
  471. color, black, color, black,
  472. color, black, color, black,
  473. color, black, color, black );
  474. }
  475.  
  476. void TwinkleMapPixels()
  477. {
  478. for( uint16_t i = 0; i < NUM_LEDS; i++) {
  479. if( PixelState[i] == SteadyDim) {
  480. // this pixels is currently: SteadyDim
  481. // so we randomly consider making it start getting brighter
  482. if( random8() < CHANCE_OF_TWINKLE) {
  483. PixelState[i] = GettingBrighter;
  484. }
  485.  
  486. } else if( PixelState[i] == GettingBrighter ) {
  487. // this pixels is currently: GettingBrighter
  488. // so if it's at peak color, switch it to getting dimmer again
  489. if( leds[i] >= PEAK_COLOR ) {
  490. PixelState[i] = GettingDimmerAgain;
  491. } else {
  492. // otherwise, just keep brightening it:
  493. leds[i] += DELTA_COLOR_UP;
  494. }
  495.  
  496. } else { // getting dimmer again
  497. // this pixels is currently: GettingDimmerAgain
  498. // so if it's back to base color, switch it to steady dim
  499. if( leds[i] <= BASE_COLOR ) {
  500. leds[i] = BASE_COLOR; // reset to exact base color, in case we overshot
  501. PixelState[i] = SteadyDim;
  502. } else {
  503. // otherwise, just keep dimming it down:
  504. leds[i] -= DELTA_COLOR_DOWN;
  505. }
  506. }
  507. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement