I know I asked about how to get multiple MQTT subscriptions a while back.
I have sort of worked it out, I hope.
It seems to be working, but it doesn't seem to be able to separate the TOPICS.
Though this part is the part in questions for now:
if (topic == "TEST")
{
header(3);
header(2);
header(1);
header(0);
}
Which doesn't seem to work/be invoked if I send a message with the TOPIC as TEST.
Yet, the topic is shown in this part:
Serial.print("Message arrived [");
Serial.print(topic); // <<---- Topic printed here.
Serial.print("] ");
for (int i = 0; i < length; i++) // <<----- Payload.
{
Serial.print((char)payload[i]);
}
Serial.println();
Where the topic is clearly printed out, and I see [TEST] printed. Given the [ and ] are added, as you can see.
As it is, I can send a <1> or <0> in either IFF, TEST or the third topic to which I am subscribed.
Any topic the LED turn on/off.
But, I wasn't really wanting that.
I only want the LED to turn on/off with a certain topic.
You can see in the code I have bits where it conditionally has code similar to the part shown above for TEST, but it doesn't work.
This is the entire routine:
And below is an extract of how I subscribe to multiple channels, in case I am not doing it right.
void callback(char* topic, byte* payload, unsigned int length)
{
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++)
{
Serial.print((char)payload[i]);
}
Serial.println();
if (topic == "TEST")
{
header(3);
header(2);
header(1);
header(0);
}
char x;
byte y = 0;
// // ************** 2019 02 23 IFF channel detection.
// if (topic == "IFF")
// {
// IFF Channel message received.
if ((char)payload[0] == 'X')
{
//
IPAddress ip = WiFi.localIP();
snprintf(ip_addr, 60, "{\"WIFI_DEVICE\":\"GPS\",\"IP_Address\":\"%ld.%ld.%ld.%ld\"}" , ip[0], ip[1], ip[2], ip[3]);
Serial.println(ip_addr);
client.publish(idTopic, ip_addr);
blinker();
}
// }
// Switch on the LED if an 1 was received as first character
if ((char)payload[0] == '1')
{
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is acive low on the ESP-01)
}
else
if ((char)payload[0] == '0')
{
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
}
else
/*
if ((char)payload[0] == '2')
{
IPAddress ip = WiFi.localIP();
snprintf(ip_addr, 60, "{\"WIFI_DEVICE\":\"GPS\",\"IP_Address\":\"%ld.%ld.%ld.%ld\"}" , ip[0], ip[1], ip[2], ip[3]);
Serial.println(ip_addr);
client.publish(idTopic, ip_addr);
}
else
*/
if ((char)payload[0] == '3')
{
display_active = 1;
wipe(15);
}
else
if ((char)payload[0] == '4')
{
{
Serial.print(" First digit received ");
Serial.println((char)payload[1]);
y = (payload[1]);
if ((char)payload[2] != 'M')
{
Serial.print(" Second digit received ");
Serial.println((char)payload[2]);
y = (payload[1]*10) + (payload[2]);
}
Serial.print(" Constructed digit ");
if (y > 9)
{
//
Serial.print(" * ");
Serial.print((char)(y/10));
}
Serial.println((char)(y%10));
}
display_active = 1;
display_brightness(y);
}
else
if ((char)payload[0] == '5') // Not used any more I hope.
{
display_active = 1;
display_brightness(0);
}
else
if ((char)payload[0] == '6')
{
//
//display_brightness(15); // WRONG. Need to change all values sent to 15.
//
Serial.println("****** DISPLAY TURNED OFF ********");
display_active = 0;
}
else
if ((char)payload[0] == '7')
{
// Display E's on screen.
test();
}
if ((char)payload[0] == '8')
{
// Another test routine.
test2();
}
if ((char)payload[0] == '9')
{
// Another test routine.
test4();
}
}
Subscribe to multiple channels:
const char* idTopic = "STATUS/WIFIDEVICEID";
const char* inTopic1 = "IFF";
const char* inTopic2 = "DISPLAY_BRIGHTNESS";
const char* outTopic = "outTopic";
void reconnect()
{
// Loop until we're reconnected
Serial.println("Reconnecting");
while (!client.connected())
{
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(mqtt_server, willTopic, willQoS, willRetain, willMessage))
{
Serial.println("connected");
// Once connected, publish an announcement...
client.publish(outTopic, "hello world");
// ... and resubscribe
client.subscribe(inTopic1);
client.subscribe(inTopic2);
client.subscribe("TEST/#");
// Publish IP address back to server for future use.
IPAddress ip = WiFi.localIP();