Hi Guys.
Thank you for all of the help.
before I can do the strncopy() I need to convert the payload byte to a char.
However, i'm stuck at a point where I cannot get my global payload_buff[100] to display when called from the main loop()
It shows up when serial printed in the callback function as On, Off, 12381823 as I set it in a MQTT message but in the loop() it's nothing at all
Here is the output
I've added in (expecting result here) in the 2 spots where there is nothing displayed.
Entering loop()
Checking for new messages
CallBack Function Trigged
Message arrived for topic [/home/1-ESP8266-OutDoor/OnOff] with a length of 2
The char* version of the payload is On
End of Callback
payload_buff is (expecting result here) before fnc_processMQTTmessage
ranCallback is true, processing message
Topic_buff is
payload_buff is (expecting result here)
Clearing message buffers
// 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 "qtt.com"
#define mqtt_user "1-ESP8266-OutDoor"
#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
// This previous code to be removed...
//need to convert the payload from bytes to strings for easy display
//work on the payload buffer
// int i = 0;
// for(i=0; i<100; i++) {
// payload_buff[i] = payload[i];
//}
// payload_buff[i] = '\0';
// strncpy ( payload, payload_buff, sizeof(payload) );
// Serial.print("The payload is ");
// Serial.print(payload_buff);
// Serial.println();
// Converting the byte* payload into a char array
char *payload_buff = (char *) payload;
payload_buff[length] = '\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() {
}
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("something", 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");
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
}
}