SHARE
TWEET

MQTT stops communicating in loop - ESP8266

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