Thanks PaulRB
Had to go through the list of forum points to check what I may have done.
For those that may stumble here - Forum Recommendations
http://forum.arduino.cc/index.php/topic,148850.0.html
I thought by posting the snippet of code I'm working on would make it easier as the rest works fine.
But to point #16 my code is below
For point #15 -
Rather than posting what I think are the options, I'll state my issue as
I'm sending in either a char as "0xff00ff" or as "rgb (50,100,150)" to my board using MQTT and the fill_solid in MQTT compaints about a unint8 or CRGB. I've tried to send it the rgb codes and using a loop to put the r,g,b values there. It's creating a watchdog soft reset. If I disable the soft watchdog, it triggers a hard reset.
Point #13 - Forum Etiquette - This question is specific to the fastLED function so I created a seperate topic in the LED section as my previous question was related to MQTT.
"
Point #6 - I think my topic is descriptive
Point #7 - Yep, used the code/code
Point #10 - My spelling may be poor at times, but I try to follow sentence structure.
Point #11 - Equipment is a ESP8266, WS2812B, 5V adapter, an SMD level-shifter.
"However, with coding problems, if possible post a "minimal" sketch that demonstrates the problem "
I'll be sure to follow these closer where applicable.
// Version 0.1 Setup
// Version 0.2 Adding MQTT
// Version 0.3 Turn LED a color by MQTT message - Currently not working
/////////////////////
// Inclusions //
/////////////////////
#include <FastLED.h>
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
/////////////////////
// Definitions //
/////////////////////
#define NUM_LEDS 300
#define LED_TYPE WS2812B
#define DATA_PIN 4
CRGB leds[NUM_LEDS];
//////////////////////
// WiFi Definitions //
//////////////////////
const char* ssid = "|$n0w|";
const char* pass = "$n0wb@llz";
char callbackString;
/////////////////////
// Variables //
//////////////////////
char payload_buff[100];
char topic_buff[50];
int strcomparison;
bool ranCallback;
/////////////////////
// MQTT Definitions //
//////////////////////
#define mqtt_server "cloudmqtt.com"
#define mqtt_user “mysensorname”
#define mqtt_password “sensorpassword
#define TopicOnOff "/home/1-ESP8266-OutDoor/OnOff"
#define TopicColor "/home/1-ESP8266-OutDoor/Color"
///////////////////////
// Other Definitions //
///////////////////////
WiFiClient espClient;
PubSubClient client(espClient);
// This is the MQTT Message Arrival Function which puts messages into topic_buff and payload_buff for action
void callback(char* topic, byte* payload, unsigned int length) {
Serial.println("CallBack Function Trigged");
Serial.print("Message arrived for topic [");
Serial.print(topic);
Serial.print("] ");
Serial.print("with a length of ");
Serial.print(length);
Serial.println();
byte len = _min(strlen(topic), sizeof(topic_buff) - 1);
memcpy(topic_buff, topic, len);
topic_buff[len] = 0;
len = _min(length, sizeof(payload_buff) - 1);
memcpy(payload_buff, payload, len);
payload_buff[len] = 0;
Serial.print("The char* version of the payload is ");
Serial.print(payload_buff);
ranCallback = true;
// Set this so I can process the message
Serial.println("");
Serial.println("End of Callback ");
// trying to move the char* to a string for easy comparison and use outside of this function
// topic_buff = topic;
} // End of Callback
void fnc_ColorChange() {
//fill_solid(leds, NUM_LEDS /*number of leds*/, 0xFF44DD);
Serial.print("Inside fnc_ColorChange attempted to change color");
// Get rid of '#' and convert it to integer
long number = strtol( &payload_buff[1], NULL, 16);
// Split them up into r, g, b values
long r = number >> 16;
long g = number >> 8 & 0xFF;
long b = number & 0xFF;
// String below causes issues
// String value = String((char*)payload_buff);
// split string at every "," and store in proper variable
// convert final result to integer
/*
valueR = value.substring(0,value.indexOf(',')).toInt();
valueG = value.substring(value.indexOf(',')+1,value.lastIndexOf(',')).toInt();
valueB = value.substring(value.lastIndexOf(',')+1).toInt();
*/
Serial.println("The colors received are");
Serial.print(r);
Serial.print(g);
Serial.print(b);
Serial.println("");
// This currently causes a watchdog soft reset. Disable watchdog causes a hard reset. Time to execute?
for(int i = 0; i < NUM_LEDS; i++) {
leds[i].setRGB( r, g, b);
FastLED.show();
}
}
void fnc_OnOff() {
}
// This is called by main loop when ranCallback is true
void fnc_processMQTTmessage() {
Serial.print("Topic_buff is");
Serial.print(topic_buff);
Serial.println();
Serial.print("payload_buff is ");
Serial.print(payload_buff);
Serial.println();
// Will execute if the topic is OnOff
strcomparison = strcmp(topic_buff, TopicOnOff);
if (strcomparison == 0) { // Work to be done here
Serial.println("Matched topic OnoFF, calling function");
fnc_OnOff();
}
// Will execute if the topic is Color
strcomparison = strcmp(topic_buff, TopicColor);
if (strcomparison == 0) { // Work to be done here
Serial.println("Matched topic Color, calling function");
fnc_ColorChange();
}
}
void setup() {
// Initial Array
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
// Start WiFi Connection
Serial.begin(115200);
Serial.println("");
pinMode(D1, OUTPUT);
WiFi.begin(ssid, pass);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// END WiFi Connection
//MQTT Connection
client.setServer(mqtt_server, 15557); // 15557 is the port given my cloudmqtt
client.setCallback(callback);
}
void reconnect() { // This is for the MQTT connection
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
// If you do not want to use a username and password, change next line to
// if (client.connect("ESP8266Client")) {
// //client.connect("clientID", "mqtt_username", "mqtt_password");
// The clientID must be unique otherwise disconnections will happen!
if (client.connect(“account_name”, mqtt_user, mqtt_password)) {
Serial.println("connected");
client.subscribe(TopicOnOff);
client.subscribe(TopicColor);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void clearMQTT() { // Clear the MQTT Payload
memset(payload_buff, 0, sizeof(payload_buff));
memset(topic_buff, 0, sizeof(topic_buff));
} // End of clearPaylod()
void loop() {
Serial.println("Entering loop()");
// First make sure MQTT is connected
{
if (!client.connected()) {
reconnect(); }
} //Close If
// Serial.println("Attempting a client.publish to " TopicOnOff);
// client.publish(TopicTemperature, temperatureString);
// Start MQTT Message Function //
Serial.println("Checking for new messages");
client.loop(); // This will check the callback function to see if there is a message"
// Taking action on MQTT Messages if ranCallback is set by client.loop()
Serial.println();
// This is here to confirm what the payload_buff looks like in loop() before processing - currently not working...
Serial.print("payload_buff is ");
Serial.print(payload_buff);
Serial.print(" before fnc_processMQTTmessage inside loop()");
if (ranCallback == true) {
Serial.println();
Serial.println("ranCallback is true, processing message");
fnc_processMQTTmessage ();
}
// Setting ranCallback to False to prevent rerun
ranCallback = false;
// Clearing Array
Serial.println();
Serial.println("Clearing message buffers");
clearMQTT();
Serial.println("-------------End of Loop---------------");
delay(3000);
// End of Loop
}