Advertisement
duquesne9

MQTT Needs Loop - ESP8266

Jul 7th, 2018
580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.50 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. const char* ssid = "SSID";
  28. const char* password = "PASSWORD";
  29. const char* mqtt_server = "Host.IP";
  30. const char* mqtt_username = "USERNAME";
  31. const char* mqtt_password = "PASSWORD";
  32. const int mqtt_port = 1883;
  33.  
  34.  
  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.  
  57.  
  58.  
  59. void setup_wifi() {
  60.  
  61.   delay(10);
  62.   // We start by connecting to a WiFi network
  63.   Serial.println();
  64.   Serial.print("Connecting to ");
  65.   Serial.println(ssid);
  66.  
  67.   WiFi.begin(ssid, password);
  68.  
  69.   while (WiFi.status() != WL_CONNECTED) {
  70.     delay(500);
  71.     Serial.print(".");
  72.   }
  73.  
  74.   randomSeed(micros());
  75.  
  76.   Serial.println("");
  77.   Serial.println("WiFi connected");
  78.   Serial.println("IP address: ");
  79.   Serial.println(WiFi.localIP());
  80. }
  81.  
  82. void callback(char* topic, byte* payload, unsigned int length) {
  83.   Serial.print("Message arrived [");
  84.   Serial.print(topic);
  85.   Serial.print("] ");
  86.   for (int i = 0; i < length; i++) {
  87.     Serial.print((char)payload[i]);
  88.   }
  89.   Serial.println();
  90.  
  91.   // Switch on the LED if an 1 was received as first character
  92.   if ((char)payload[0] == '1') {
  93.         hue++;
  94.     delay(random(20,40));
  95.     int i = random8(8);
  96.     leftWallLeds[i] = CHSV(random(1,15), 255, random(30,160));
  97.     rightWallLeds[i] = CHSV(random(115,125), 255, random(30,160));
  98.     bottomLeds[i] = CHSV(random(230,245), 255, random(30,160));
  99.      FastLED.show();
  100.  
  101.     delay(random(50,75));
  102.  
  103.      for(int x=0; x<NUM_LEDS_PER_STRIP; x++){
  104.     leftWallLeds[x].fadeToBlackBy(.5);
  105.      }
  106.      for(int x=0; x<NUM_LEDS_PER_STRIP; x++){
  107.     rightWallLeds[x].fadeToBlackBy(.5);
  108.      }
  109.      for(int x=0; x<NUM_LEDS_PER_STRIP; x++){
  110.     bottomLeds[x].fadeToBlackBy(.5);
  111.      }
  112.    
  113. }
  114.  
  115.  
  116.   else {
  117.   for(int i = 0; i < NUM_LEDS_PER_STRIP; i++) {
  118.     // set our current dot to red, green, and blue
  119.     leftWallLeds[i] = CRGB::Black;
  120.     rightWallLeds[i] = CRGB::Black;
  121.     bottomLeds[i] = CRGB::Black;
  122.     FastLED.show();
  123.    
  124.   }
  125.   }
  126.  
  127. }
  128. void reconnect() {
  129.   // Loop until we're reconnected
  130.   while (!client.connected()) {
  131.     Serial.print("Attempting MQTT connection...");
  132.  
  133.     // Attempt to connect
  134.     if (client.connect(SENSORNAME, mqtt_username, mqtt_password)) {
  135.       Serial.println("connected");
  136.       // Once connected, publish an announcement...
  137.       client.publish("outTopic", "hello world");
  138.       // ... and resubscribe
  139.       client.subscribe("inTopic");
  140.     } else {
  141.       Serial.print("failed, rc=");
  142.       Serial.print(client.state());
  143.       Serial.println(" try again in 5 seconds");
  144.       // Wait 5 seconds before retrying
  145.       delay(5000);
  146.     }
  147.   }
  148. }
  149.  
  150. void setup() {
  151.   FastLED.addLeds<NEOPIXEL, D3>(leftWallLeds, NUM_LEDS_PER_STRIP);
  152.   FastLED.addLeds<NEOPIXEL, D5>(rightWallLeds, NUM_LEDS_PER_STRIP);
  153.   FastLED.addLeds<NEOPIXEL, D7>(bottomLeds, NUM_LEDS_PER_STRIP);
  154.  
  155.   Serial.begin(9600);
  156.   setup_wifi();
  157.   client.setServer(mqtt_server, mqtt_port);
  158.   client.setCallback(callback);
  159.  
  160.     }
  161.  
  162.  
  163. void loop() {
  164.  
  165.   if (!client.connected()) {
  166.     reconnect();
  167.   }
  168.   client.loop();
  169.  
  170.  
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement