#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <WiFiClientSecure.h>
#include "certs.h"
const char* mqtt_server = "broker.mqttdashboard.com";
char messageBuffer[50];
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long currentTime;
unsigned long startTime;
unsigned long period = 1000;
bool power = false;
void setup_wifi()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop()
{
if (!client.connected())
{
reconnect();
}
client.loop();
if (power == true)
{
RainbowEffect();
}
}
void reconnect()
{
while (!client.connected())
{
if (client.connect("Reception"))
{
Serial.println("connected");
client.subscribe("RGB/Output");
}
else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void callback(char* topic, byte* payload, unsigned int length)
{
//Serial.println();
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
Serial.print("length : ");
Serial.println(length);
memcpy(messageBuffer, payload, length);
messageBuffer[length] = '\0';
Serial.print("the payload is : ");
Serial.println(messageBuffer);
if (strcmp(messageBuffer, "on") == 0)
{
Serial.print("turning ");
Serial.println(messageBuffer);
digitalWrite(LED_BUILTIN, LOW);
power = true; //activate rainbow
}
else if (strcmp(messageBuffer, "off") == 0)
{
Serial.print("turning ");
Serial.println(messageBuffer);
digitalWrite(LED_BUILTIN, HIGH);
OffRainbow();
power = false; //stop the rainbow
}
}
void OffRainbow()
{
Serial.print("RainbowOff");
Serial.println();
}
void RainbowEffect()
{
currentTime = millis();
if (currentTime - startTime >= period)
{
Serial.print("RainbowOn");
Serial.println();
startTime = currentTime;
}
}
Code can show payloads in Serial, but
if (strcmp(messageBuffer, "on") == 0)
{
Serial.print("turning ");
Serial.println(messageBuffer);
digitalWrite(LED_BUILTIN, LOW);
power = true; //activate rainbow
}
else if (strcmp(messageBuffer, "off") == 0)
{
Serial.print("turning ");
Serial.println(messageBuffer);
digitalWrite(LED_BUILTIN, HIGH);
OffRainbow();
power = false; //stop the rainbow
}
not working
What do you see if you print the result of the strcmp() ?
I have used something similar to your code, the only difference being that my equivalent of messageBuffer is declared as a pointer
What does your serial output look like?
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
Serial.print("length : ");
Serial.println(length);
memcpy(messageBuffer, payload, length);
messageBuffer[length] = '\0';
Serial.print("the payload is : ");
Serial.println(messageBuffer);
Serial output is like payload is on/off. basically the code above whenever I give command on mqtt box.
I really wanted to see the actual output from the serial monitor. I note that you have wisely included some square brackets so it's possible to detect newlines or spaces so I wondered whether the payload does contain such things.
What are you using to send the payload ?
MQTTBox maybe ?
What settings are you using ?
I have just tested your code using MQTTBox and a Wemos D1 mini ESP8266 and it works for me
MQTT Box. What settings are you referring to?
What settings are you referring to?
QoS, Retain and Payload Type
I took a screenshot the Qos, Retain and Payload type
That looks the same as my setup apart from the fact that I have Retain selected but as you are getting payloads that would not seem to be a problem.
Some things to try to help track down the problem
1 - print strlen(messageBuffer) after printing messageBuffer in callback()
2 - print each element of messageBuffer individually
for (int c = 0; c < strlen(messagBuffer); c++)
{
Serial.print(messageBuffer[c]);
}
Serial.println();
3 - print what is returned by strcmp(messageBuffer, "on") and strcmp(messageBuffer, "off")
void callback(char* topic, byte* payload, unsigned int length)
{
//Serial.println();
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
Serial.print("length : ");
Serial.println(length);
memcpy(messageBuffer, payload, length);
messageBuffer[length] = '\0';
Serial.print("the payload is : ");
Serial.println(messageBuffer);
Serial.print(strlen(messageBuffer));
for (int c = 0; c < strlen(messageBuffer); c++)
{
Serial.print(messageBuffer[c]);
}
Serial.println(strcmp(messageBuffer, "on"));
Serial.println(strcmp(messageBuffer, "off"));
Serial.println();
if (strcmp(messageBuffer, "on") == 0)
{
Serial.print("turning ");
Serial.println(strcmp(messageBuffer, "on"));
digitalWrite(LED_BUILTIN, LOW);
power = true; //activate rainbow
}
else if (strcmp(messageBuffer, "off") == 0)
{
Serial.print("turning ");
Serial.println(strcmp(messageBuffer, "off"));
digitalWrite(LED_BUILTIN, HIGH);
OffRainbow();
power = false; //stop the rainbow
}
}
Like this?
This is what I got from the serial monitor
failed, rc=-2 try again
failed, rc=-2 try again
failed, rc=-2 try again
failed, rc=-2 try again
failed, rc=-2 try again
failed, rc=-2 try again
failed, rc=-2 try again
failed, rc=-2 try again
failed, rc=-2 try again
failed, rc=-2 try again
failed, rc=-2 try again
failed, rc=-2 try again
failed, rc=-2 try again
failed, rc=-2 try again
failed, rc=-2 try again
So you're not getting a connection to the broker. Have you ever done so?
None of that printout came from the callback function. As suggested, it looks like you did not get a connection to the broker
wildbill:
So you're not getting a connection to the broker. Have you ever done so?
Before adding the additional stuff it was able to connect just fine
Do you have a copy of the old one to test?
As I previous said the code in the original post works for me
Are you sure that the 8266 is connecting to your wireless router ?
hmmm its weird now it doesn't connect
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <WiFiClientSecure.h>
#include "certs.h"
const char* mqtt_server = "broker.mqttdashboard.com";
char messageBuffer[50];
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long currentTime;
unsigned long startTime;
unsigned long period = 1000;
bool power = false;
void setup_wifi()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop()
{
if (!client.connected())
{
reconnect();
}
client.loop();
if (power == true)
{
RainbowEffect();
}
}
void reconnect()
{
while (!client.connected())
{
if (client.connect("Reception"))
{
Serial.println("connected");
client.subscribe("RGB/Output");
}
else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again");
}
}
}
void callback(char* topic, byte* payload, unsigned int length)
{
//Serial.println();
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
Serial.print("length : ");
Serial.println(length);
memcpy(messageBuffer, payload, length);
messageBuffer[length] = '\0';
Serial.print("the payload is : ");
Serial.println(messageBuffer);
if (strcmp(messageBuffer, "on") == 0)
{
Serial.print("turning ");
Serial.println(strcmp(messageBuffer, "on"));
digitalWrite(LED_BUILTIN, LOW);
power = true; //activate rainbow
}
else if (strcmp(messageBuffer, "off") == 0)
{
Serial.print("turning ");
Serial.println(strcmp(messageBuffer, "off"));
digitalWrite(LED_BUILTIN, HIGH);
OffRainbow();
power = false; //stop the rainbow
}
}
void OffRainbow()
{
Serial.print("RainbowOff");
Serial.println();
}
void RainbowEffect()
{
currentTime = millis();
if (currentTime - startTime >= period)
{
Serial.print("RainbowOn");
Serial.println();
startTime = currentTime;
}
}
Try this
void setup_wifi()
{
Serial.print("connecting to ");
Serial.println(SSID);
WiFi.begin(SSID, password);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("WiFi connected to ");
Serial.println(SSID);
Serial.print("IP address : ");
Serial.println(WiFi.localIP());
delay(2000);
}
Unlike your version it does not just blindly assume that WiFi has been connected
ok it connects
........
WiFi connected to ASUS
IP address : 192.168.1.88
connected
Message arrived [RGB/Output] length : 6
the payload is : off
off
Message arrived [RGB/Output] length : 5
the payload is : on
on
This is what I got from the Serial
void callback(char* topic, byte* payload, unsigned int length)
{
//Serial.println();
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
Serial.print("length : ");
Serial.println(length);
memcpy(messageBuffer, payload, length);
messageBuffer[length] = '\0';
Serial.print("the payload is : ");
Serial.println(messageBuffer);
for (int c = 0; c < strlen(messageBuffer); c++)
{
Serial.print(messageBuffer[c]);
}
Serial.println();
if (strcmp(messageBuffer, "on") == 0)
{
Serial.print("turning ");
Serial.println(strcmp(messageBuffer, "on"));
digitalWrite(LED_BUILTIN, LOW);
power = true; //activate rainbow
}
else if (strcmp(messageBuffer, "off") == 0)
{
Serial.print("turning ");
Serial.println(strcmp(messageBuffer, "off"));
digitalWrite(LED_BUILTIN, HIGH);
OffRainbow();
power = false; //stop the rainbow
}
}
So, there's three extra characters in the payload. Most likely CR and LF in some combination by the look of the output.
What's the sending side passing to MQTT?