Advertisement
duquesne9

MQTT Multi strip ESP8266 - WORKING

Jul 7th, 2018
531
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.87 KB | None | 0 0
  1. /*
  2.  Basic ESP8266 MQTT example
  3.  This sketch demonstrates the capabilities of the pubsub library in combination
  4.  with the ESP8266 board/library.
  5.  It connects to an MQTT server then:
  6.   - publishes "hello world" to the topic "outTopic" every two seconds
  7.   - subscribes to the topic "inTopic", printing out any messages
  8.     it receives. NB - it assumes the received payloads are strings not binary
  9.   - If the first character of the topic "inTopic" is an 1, switch ON the ESP Led,
  10.     else switch it off
  11.  It will reconnect to the server if the connection is lost using a blocking
  12.  reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to
  13.  achieve the same result without blocking the main loop.
  14.  To install the ESP8266 board, (using Arduino 1.6.4+):
  15.   - Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs":
  16.        http://arduino.esp8266.com/stable/package_esp8266com_index.json
  17.   - Open the "Tools -> Board -> Board Manager" and click install for the ESP8266"
  18.   - Select your ESP8266 in "Tools -> Board"
  19. */
  20.  
  21. #include <ESP8266WiFi.h>
  22. #include <PubSubClient.h>
  23.  
  24. #include "FastLED.h"
  25.  
  26. // Update these with values suitable for your network.
  27.  
  28. const char* ssid = "SSID";
  29. const char* password = "PASSWORD";
  30. const char* mqtt_server = "Host.IP";
  31. const char* mqtt_username = "USERNAME";
  32. const char* mqtt_password = "PASSWORD";
  33. const int mqtt_port = 1883;
  34. const char* state = "";
  35. #define SENSORNAME "strip"
  36.  
  37.  
  38. #define NUM_LEDS_PER_STRIP 9
  39. CRGB leftWallLeds[NUM_LEDS_PER_STRIP];
  40. CRGB rightWallLeds[NUM_LEDS_PER_STRIP];
  41. CRGB bottomLeds[NUM_LEDS_PER_STRIP];
  42. CRGB tempLED;
  43.  
  44. //globals for fireplace
  45. byte hue = 0;
  46. byte counter=0;  
  47.  
  48. WiFiClient espClient;
  49. PubSubClient client(espClient);
  50.  
  51. long lastMsg = 0;
  52. char msg[50];
  53. int value = 0;
  54.  
  55.  
  56. void fireplace_brighter(){
  57.         hue++;
  58.   delay(35);
  59.   int i = random8(9);
  60.     leftWallLeds[i] = CHSV(random(1,15), 255, random(80,255));
  61.     rightWallLeds[i] = CHSV(random(10,25), 255, random(80,255));
  62.     bottomLeds[i] = CHSV(random(240,8), 255, random(80,255));
  63.      FastLED.show();
  64.  
  65.     delay(100);
  66.  
  67.      for(int x=0; x<NUM_LEDS_PER_STRIP; x++){
  68.     leftWallLeds[x].fadeToBlackBy(.5);
  69.      }
  70.      for(int x=0; x<NUM_LEDS_PER_STRIP; x++){
  71.     rightWallLeds[x].fadeToBlackBy(.5);
  72.      }
  73.      for(int x=0; x<NUM_LEDS_PER_STRIP; x++){
  74.     bottomLeds[x].fadeToBlackBy(.5);
  75.      }
  76. }//end fireplace_brighter
  77.  
  78. void fireplace_faster(){
  79.         hue++;
  80.   delay(random(10,30));
  81.   int i = random8(9);
  82.     leftWallLeds[i] = CHSV(random(1,15), 255, random(80,255));
  83.     rightWallLeds[i] = CHSV(random(10,25), 255, random(80,255));
  84.     bottomLeds[i] = CHSV(random(240,8), 255, random(80,255));
  85.      FastLED.show();
  86.  
  87.     delay(random(30,60));
  88.  
  89.      for(int x=0; x<NUM_LEDS_PER_STRIP; x++){
  90.     leftWallLeds[x].fadeToBlackBy(.5);
  91.      }
  92.      for(int x=0; x<NUM_LEDS_PER_STRIP; x++){
  93.     rightWallLeds[x].fadeToBlackBy(.5);
  94.      }
  95.      for(int x=0; x<NUM_LEDS_PER_STRIP; x++){
  96.     bottomLeds[x].fadeToBlackBy(.5);
  97.      }
  98. }//end fireplace_faster
  99.  
  100. void fireplace_dynamic(){
  101.         hue++;
  102.   delay(random(35));
  103.   int i = random8(9);
  104.     leftWallLeds[i] = CHSV(random(1,15), 255, random(30,255));
  105.     rightWallLeds[i] = CHSV(random(10,25), 255, random(30,255));
  106.     bottomLeds[i] = CHSV(random(240,8), 255, random(45,255));
  107.      FastLED.show();
  108.  
  109.     delay(50);
  110.  
  111.      for(int x=0; x<NUM_LEDS_PER_STRIP; x++){
  112.     leftWallLeds[x].fadeToBlackBy(.5);
  113.      }
  114.      for(int x=0; x<NUM_LEDS_PER_STRIP; x++){
  115.     rightWallLeds[x].fadeToBlackBy(.5);
  116.      }
  117.      for(int x=0; x<NUM_LEDS_PER_STRIP; x++){
  118.     bottomLeds[x].fadeToBlackBy(.5);
  119.      }
  120. }//end fireplace_dynamic
  121.  
  122. void fireplace_dimmer(){
  123.         hue++;
  124.   delay(50);
  125.   int i = random8(9);
  126.     leftWallLeds[i] = CHSV(random(1,15), 255, random(25,160));
  127.     rightWallLeds[i] = CHSV(random(115,125), 255, random(20,160));
  128.     bottomLeds[i] = CHSV(random(230,245), 255, random(30,160));
  129.      FastLED.show();
  130.  
  131.     delay(random(40,75));
  132.  
  133.      for(int x=0; x<NUM_LEDS_PER_STRIP; x++){
  134.     leftWallLeds[x].fadeToBlackBy(.5);
  135.      }
  136.      for(int x=0; x<NUM_LEDS_PER_STRIP; x++){
  137.     rightWallLeds[x].fadeToBlackBy(.5);
  138.      }
  139.      for(int x=0; x<NUM_LEDS_PER_STRIP; x++){
  140.     bottomLeds[x].fadeToBlackBy(.5);
  141.      }
  142. }//end fireplace_dimmer
  143.  
  144. void fireplace_neon(){
  145.         hue++;
  146.   delay(random(20,40));
  147.   int i = random8(9);
  148.     leftWallLeds[i] = CHSV(random(1,255), 255, random(50,200));
  149.     rightWallLeds[i] = CHSV(random(1,255), 255, random(50,200));
  150.     bottomLeds[i] = CHSV(random(150,35), 255, random(80,255));
  151.      FastLED.show();
  152.  
  153.     delay(random(50,75));
  154.  
  155.      for(int x=0; x<NUM_LEDS_PER_STRIP; x++){
  156.     leftWallLeds[x].fadeToBlackBy(.5);
  157.      }
  158.      for(int x=0; x<NUM_LEDS_PER_STRIP; x++){
  159.     rightWallLeds[x].fadeToBlackBy(.5);
  160.      }
  161.      for(int x=0; x<NUM_LEDS_PER_STRIP; x++){
  162.     bottomLeds[x].fadeToBlackBy(.5);
  163.      }
  164. }//end fireplace_neon
  165.  
  166.  
  167.  
  168. void fireplace_neon_inverse(){
  169.         hue++;
  170.   delay(random(20,40));
  171.   int i = random8(9);
  172.     leftWallLeds[i] = CHSV(random(150,35), 255, random(50,200));
  173.     rightWallLeds[i] = CHSV(random(150,35), 255, random(50,200));
  174.     bottomLeds[i] = CHSV(random(1,255), 255, random(80,255));
  175.      FastLED.show();
  176.      FastLED.show();
  177.  
  178.     delay(random(50,75));
  179.  
  180.      for(int x=0; x<NUM_LEDS_PER_STRIP; x++){
  181.     leftWallLeds[x].fadeToBlackBy(.5);
  182.      }
  183.      for(int x=0; x<NUM_LEDS_PER_STRIP; x++){
  184.     rightWallLeds[x].fadeToBlackBy(.5);
  185.      }
  186.      for(int x=0; x<NUM_LEDS_PER_STRIP; x++){
  187.     bottomLeds[x].fadeToBlackBy(.5);
  188.      }
  189. }//end fireplace_neon_inverse
  190.  
  191.  
  192.  
  193. void blackout() {
  194.   for(int i = 0; i < NUM_LEDS_PER_STRIP; i++) {
  195.     // set our current dot to red, green, and blue
  196.     leftWallLeds[i] = CRGB::Black;
  197.     rightWallLeds[i] = CRGB::Black;
  198.     bottomLeds[i] = CRGB::Black;
  199.     FastLED.show();
  200.    
  201.   }
  202.   //checkState();
  203. }
  204.  
  205. void setup_wifi() {
  206.  
  207.   delay(10);
  208.   // We start by connecting to a WiFi network
  209.   Serial.println();
  210.   Serial.print("Connecting to ");
  211.   Serial.println(ssid);
  212.  
  213.   WiFi.begin(ssid, password);
  214.  
  215.   while (WiFi.status() != WL_CONNECTED) {
  216.     delay(500);
  217.     Serial.print(".");
  218.   }
  219.  
  220.   randomSeed(micros());
  221.  
  222.   Serial.println("");
  223.   Serial.println("WiFi connected");
  224.   Serial.println("IP address: ");
  225.   Serial.println(WiFi.localIP());
  226. }
  227.  
  228. /*
  229. void checkState(){
  230.   if (state =="fireplace") {
  231.    
  232.     client.loop();
  233.     fireplace();
  234.   }
  235.  
  236.   if (state == "blackout"){
  237.     client.loop();
  238.     blackout();
  239.   }
  240.  
  241.  
  242. }
  243. */
  244.  
  245. void callback(char* topic, byte* payload, unsigned int length) {
  246.   Serial.print("Message arrived [");
  247.   Serial.print(topic);
  248.   Serial.print("] ");
  249.   for (int i = 0; i < length; i++) {
  250.     Serial.print((char)payload[i]);
  251.   }
  252.   Serial.println();
  253.  
  254.   // Switch on the LED if an 1 was received as first character
  255.   if ((char)payload[0] == '0') {
  256.     state = "blackout";
  257.   }
  258.   if ((char)payload[0] == '1') {
  259.    state =  "fireplace_brighter";
  260.   }
  261.   if ((char)payload[0] == '2') {
  262.     state = "fireplace_faster";
  263.   }
  264.   if ((char)payload[0] == '3') {
  265.     state = "fireplace_dynamic";
  266.   }
  267.  
  268.   if ((char)payload[0] == '4') {
  269.     state = "fireplace_dimmer";
  270.   }
  271.   if ((char)payload[0] == '5') {
  272.     state = "fireplace_neon";
  273.   }
  274.   if ((char)payload[0] == '6') {
  275.     state = "fireplace_neon_inverse";
  276.   }
  277.  
  278.   }
  279.  
  280.  
  281. void reconnect() {
  282.   // Loop until we're reconnected
  283.   while (!client.connected()) {
  284.     Serial.print("Attempting MQTT connection...");
  285.  
  286.     // Attempt to connect
  287.     if (client.connect(SENSORNAME, mqtt_username, mqtt_password)) {
  288.       Serial.println("connected");
  289.       // Once connected, publish an announcement...
  290.       client.publish("outTopic", "hello world");
  291.       // ... and resubscribe
  292.       client.subscribe("inTopic");
  293.     } else {
  294.       Serial.print("failed, rc=");
  295.       Serial.print(client.state());
  296.       Serial.println(" try again in 5 seconds");
  297.       // Wait 5 seconds before retrying
  298.       delay(5000);
  299.     }
  300.   }
  301. }
  302.  
  303. void setup() {
  304.   FastLED.addLeds<NEOPIXEL, D3>(leftWallLeds, NUM_LEDS_PER_STRIP);
  305.   FastLED.addLeds<NEOPIXEL, D5>(rightWallLeds, NUM_LEDS_PER_STRIP);
  306.   FastLED.addLeds<NEOPIXEL, D7>(bottomLeds, NUM_LEDS_PER_STRIP);
  307.  
  308.   Serial.begin(9600);
  309.   setup_wifi();
  310.   client.setServer(mqtt_server, mqtt_port);
  311.   client.setCallback(callback);
  312.  
  313.     }
  314.  
  315.  
  316. void loop() {
  317.  
  318.   if (!client.connected()) {
  319.     reconnect();
  320.   }
  321.   client.loop();
  322.   if (state == "fireplace_brighter"){
  323.     fireplace_brighter();
  324.     }
  325.   if (state == "fireplace_faster"){
  326.     fireplace_faster();
  327.     }
  328.   if (state == "fireplace_dynamic"){
  329.     fireplace_dynamic();
  330.     }
  331.   if (state == "fireplace_dimmer"){
  332.     fireplace_dimmer();
  333.     }
  334.   if (state == "fireplace_neon"){
  335.     fireplace_neon();
  336.     }
  337.   if (state == "fireplace_neon_inverse"){
  338.     fireplace_neon_inverse();
  339.     }
  340.   else if (state == "blackout"){
  341.     blackout();
  342.     }
  343. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement