I originally wanted to use fill_solid(leds, NUM_LEDS /number of leds/, 0xFF44DD); but it seems maybe the for loop is better? Is there a way to pass the hex value directly or the rgb ?
But it seems that fnc_ColorChange that the colors are 000
but I'm too amateur to know why.
// Version 0.1 T.S Setup
// Version 0.2 T.S Adding MQTT
/////////////////////
// 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 = "";
const char* pass = "";
char callbackString;
/////////////////////
// Variables //
//////////////////////
char payload_buff[100];
char topic_buff[50];
int strcomparison;
bool ranCallback;
/////////////////////
// MQTT Definitions //
//////////////////////
#define mqtt_server ""
#define mqtt_user ""
#define mqtt_password ""
#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();
// char *callbackString = (char *) payload;
// callbackString[length] = '\0'; // Adds a terminator to end of string based on length of current payload
// char *payload_buff = (char *) payload;
// payload_buff[length] = '\0';
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
/* //Old Method of String Comparison
int strcomparison = strcmp(topic, TopicOnOff);
if (strcomparison == 0) { // Work to be done here
Serial.println("Matched OnOff");
if (length == 2) {
digitalWrite(D1, HIGH);
Serial.println("Sending PIN High as message was On");
}
if (length == 3) {
digitalWrite(D1, LOW);
Serial.println("Sending PIN Low as message was Off");
}
} //end If
strcomparison = strcmp(topic, TopicColor); // Work to be done here
if (strcomparison == 0) {
Serial.println("Matched Color");
} //end If
*/
void fnc_ColorChange() {
//fill_solid(leds, NUM_LEDS /*number of leds*/, 0xFF44DD);
Serial.print("Inside fnc_ColorChange attempted to change color");
Serial.print("");
Serial.print("payload_buff is ");
Serial.print(payload_buff);
Serial.print("before attempted conversion");
// 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("After conversion the colors are");
Serial.print(r);
Serial.print(g);
Serial.print(b);
Serial.println("");
for(int i = 0; i < NUM_LEDS; i++) {
leds[i].setRGB( r, g, b);
}
Serial.println("About to attempt FastLED.show()");
FastLED.show();
}
void fnc_OnOff() {
}
// This is called by main loop when ranCallback is true
void fnc_processMQTTmessage() {
Serial.println("Now inside 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("tisnow_1313136575", 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
}