Go Down

Topic: Use mqtt to select mode for FastLED flashing (Read 184 times) previous topic - next topic

cpyandy

Hi all,

I am using a WemosD1 to control Led strip,  the programming for FASTLed modes is complete and working. Now having issue of using MQTT to select the correct mode to operate my leds.

Note:

- WIfi connection has been made
- Connected to mqtt server
- Led modes (scenes) 0 ~ 6 switching is done with "switch" function in the main loop.
- Mqtt is not selecting the correct scenes.  The selected scene is always says default.




#include <SPI.h>
#include <PubSubClient.h>
#include <ESP8266WiFi.h>        // Include the Wi-Fi library
#include <FastLED.h>

#define DATA_PIN 0                          // hardware Data pin
#define NUM_LEDS 150                        // Total No. of LEDs

IPAddress server(10, 0, 0, xx);    //MQTT broker IP Address

struct CRGB leds[NUM_LEDS];
int currentScene=0;
int previousScene=0;
int selectedScene=0;

const char* ssid     = "xxxxxxxxx";     
const char* password = "xxxxxxxxx";


void callback(char* topic, byte* payload, unsigned int length)
{
//================Print whatever received to serial Monitor =========
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
 
  for (int i=0;i<length;i++)
  {
  Serial.print((char)payload);
  }
    Serial.println();


//=============== MQTT SUBSCRIBE================

String strTopic = String((char*)topic);

  if (strTopic == "cmnd/WemosD1/LEDscene")
  {

   
  if (((char)payload[0]) != previousScene)
 
  {
     
   if(((char)payload[0]) == 1)
    {
      selectedScene = 1;
      Serial.println("Scene Selection = 1");
     }
   
    else if (((char)payload[0]) == 2)
    {
      selectedScene = 2;
      Serial.println("Scene Selection = 2");

     }

    else if (((char)payload[0]) == 3)
    {
      selectedScene = 3;
      Serial.println("Scene Selection = 3");
     }

    else if (((char)payload[0]) == 4)
    {
      selectedScene = 4;
      Serial.println("Scene Selection = 4");
     }

    else if (((char)payload[0]) == 5)
    {
      selectedScene = 5;
      Serial.println("Scene Selection = 5");
     }   

    else
    {
      selectedScene = 0;
      Serial.println("Scene Selection = Default");
     } 

     
      previousScene = ((char)payload[0]);


  } 
}

}
    WiFiClient espClient;
    PubSubClient client(espClient);

cpyandy

I think I know what's the issue with the program, the mqtt message received is in ascii format (ie ascii 1 = 49 decimal). Hence I have come up with a fix for the issue:

Anyone know how to convert ascii message to decimal in a proper way?

I am having some slight delays sometimes in receiving mqtt messages on my Wemo, could that be the program?

Modified code below:
Code: [Select]
#include <SPI.h>
#include <PubSubClient.h>
#include <ESP8266WiFi.h>        // Include the Wi-Fi library
#include <FastLED.h>

#define DATA_PIN 0                          // hardware Data pin
#define NUM_LEDS 150                        // Total No. of LEDs

IPAddress server(10, 0, 0, 12);    //MQTT broker IP Address

struct CRGB leds[NUM_LEDS];
int currentScene=0;
int previousScene=0;
int selectedScene=0;

const char* ssid     = "xxxxxxxx";
const char* password = "xxxxxxxxxx";

void callback(char* topic, byte* payload, unsigned int length)
{
//================Print whatever received to serial Monitor =========
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
 
  for (int i=0;i<length;i++)
  {
  Serial.print((char)payload[i]);
  }
    Serial.println();


//=============== MQTT SUBSCRIBE================

String strTopic = String((char*)topic);

  if (strTopic == "cmnd/WemosD1/LEDscene")
  {
    currentScene = ((char)payload[0])-48;      // Don't know how to receive the message in decimal format

 
  if (currentScene != previousScene)

  {
     
   if(currentScene == 1)
    {
      selectedScene = 1;
      Serial.println("Scene Selection = 1");
     }
   
    else if (currentScene == 2)
    {
      selectedScene = 2;
      Serial.println("Scene Selection = 2");

     }

    else if (currentScene == 3)
    {
      selectedScene = 3;
      Serial.println("Scene Selection = 3");
     }

    else if (currentScene == 4)
    {
      selectedScene = 4;
      Serial.println("Scene Selection = 4");
     }

    else if (currentScene == 5)
    {
      selectedScene = 5;
      Serial.println("Scene Selection = 5");
     }   

    else
    {
      selectedScene = 0;
      Serial.println("Scene Selection = Default");
     } 
     
      previousScene = currentScene;


  } 
}

}
    WiFiClient espClient;
    PubSubClient client(espClient);





Go Up