Advertisement
Guest User

FastLED_MQTT

a guest
Aug 4th, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. /*
  2. ESP8266 MQTT FastLED Client
  3. Sets LED Color with MQTT
  4. */
  5.  
  6. #include <ESP8266WiFi.h>
  7. #include <PubSubClient.h>
  8. #include <FastLED.h>
  9. #include "MySecret.h"
  10.  
  11. #define DATA_PIN 2
  12. #define LED_TYPE WS2811
  13. #define COLOR_ORDER GRB
  14. #define NUM_LEDS 20
  15. CRGB leds[NUM_LEDS];
  16. #define BRIGHTNESS 64
  17. #define FRAMES_PER_SECOND 120
  18.  
  19. // create a new MySecret.h file with following content:
  20. // -cut------------------------------------------>
  21. // #define MY_WLAN_SSID "MyWLAN_SSID" // SSID
  22. // #define MY_WLAN_PASS "my_very_secret_password" // Password
  23. // #define MY_MQTT_SERVER "my_mqtt_server_ip" // MQTT Server IP
  24. // <-cut------------------------------------------
  25. // (and you do not check them into the repository!)
  26.  
  27. const char *ssid = "xxxxx"; // cannot be longer than 32 characters!
  28. const char *password = "xxxxx"; //
  29. const char *mqtt_server = "xxx.xxx.xxx.xxx"; //
  30.  
  31.  
  32. // Update these with values suitable for your network.
  33.  
  34. const char *inTopic = "cmnd/FastLedDisplay/rbw";
  35. const char *outTopic = "out_fastled_display_abfall";
  36.  
  37. WiFiClient espClient;
  38. PubSubClient client(espClient);
  39. long lastMsg = 0;
  40. char msg[50];
  41. int value = 0;
  42. char message_buff[100];
  43.  
  44. void setup_wifi() {
  45.  
  46. delay(10);
  47. // We start by connecting to a WiFi network
  48. Serial.println();
  49. Serial.print("Connecting to ");
  50. Serial.println(ssid);
  51.  
  52. WiFi.begin(ssid, password);
  53.  
  54. while (WiFi.status() != WL_CONNECTED) {
  55. delay(500);
  56. Serial.print(".");
  57. }
  58.  
  59. Serial.println("");
  60. Serial.println("WiFi connected");
  61. Serial.println("IP address: ");
  62. Serial.println(WiFi.localIP());
  63. }
  64.  
  65. void callback(char* topic, byte* payload, unsigned int length) {
  66. int i = 0;
  67. // create character buffer with ending null terminator (string)
  68. for (i = 0; i < length; i++) {
  69. message_buff[i] = payload[i];
  70. }
  71. message_buff[i] = '\0';
  72.  
  73. String msgString = String(message_buff);
  74. Serial.println("Message:" + msgString);
  75. changeColor(msgString);
  76. }
  77.  
  78. void changeColor(String rgbString)
  79. {
  80. //Parse RGB;
  81. int lIndex = rgbString.indexOf(',');
  82. int rIndex = rgbString.indexOf(',', lIndex + 1);
  83. int gIndex = rgbString.indexOf(',', rIndex + 1);
  84.  
  85. int l = rgbString.substring(0, lIndex).toInt();
  86. int r = rgbString.substring(lIndex + 1, rIndex).toInt();
  87. int g = rgbString.substring(rIndex + 1, gIndex).toInt();
  88. int b = rgbString.substring(gIndex + 1).toInt();
  89.  
  90. leds[l] = CRGB( r, g, b);
  91. FastLED.show();
  92. /*
  93. Serial.print("Set LED ");
  94. Serial.print(l);
  95. Serial.print("to r=");
  96. Serial.print(r);
  97. Serial.print(" g=");
  98. Serial.print(g);
  99. Serial.print(" b=");
  100. Serial.println(b);
  101. */
  102.  
  103. }
  104.  
  105. void reconnect() {
  106. // Loop until we're reconnected
  107. while (!client.connected()) {
  108. Serial.print("Attempting MQTT connection...");
  109. // Attempt to connect
  110. if (client.connect("ESP8266Client")) {
  111. Serial.println("connected");
  112. // Once connected, publish an announcement...
  113. client.publish(outTopic, "FastLED");
  114. // ... and resubscribe
  115. client.subscribe(inTopic);
  116. } else {
  117. Serial.print("failed, rc=");
  118. Serial.print(client.state());
  119. Serial.println(" try again in 5 seconds");
  120. // Wait 5 seconds before retrying
  121. delay(5000);
  122. }
  123. }
  124. }
  125.  
  126. void setup() {
  127.  
  128. Serial.begin(115200);
  129. resetLEDs();
  130.  
  131. setup_wifi();
  132. client.setServer(mqtt_server, 1883);
  133. client.setCallback(callback);
  134. }
  135.  
  136. void resetLEDs()
  137. {
  138. FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  139. FastLED.setBrightness(BRIGHTNESS);
  140. }
  141.  
  142. void loop() {
  143.  
  144. if (!client.connected()) {
  145. reconnect();
  146. }
  147.  
  148. client.loop();
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement