Executive Summary: I would like to make an MQTT message that I retrieved using callback() available inside my loop (from where it is then displayed on a giant NeoPixel array). Overall, this will be a neat projet! All will make sense once you glance at the code snippets.
Background: Last month I built the coolest monster display ever using NeoPixels
and it works like a charm. Lots of fun; highly recommendable project. But the displayed txt is hardcoded into the program.
I want to be able allow my guests to change the displayed text on this monster.
I settled upon using MQTT - useful to learn about that anyway. Set up my own broker on my NAS; I can pass messages around just fine. Did all my homework.
But I can't transfer the MQTT txt into my main program.
My sketch is pretty long; I can post it if wanted (you would probably get bugged down in the weeds)
Here are the salient parts:
void loop() {
client.loop(); // grab any new stringText from MQTT broker
// can display it just fine inside the callback function.
// want to have *m passed back from the callback function so that the displayed text can be changed
const char *m = "TEST NUMBER 5"; // Still hardwired. Bummer. Want this under external control!
//Note that text may be quite long (1000+ characters)
while (*m) { // while loop is black magic. Josh is a wizard!
for( uint8_t step=0; step<FONT_WIDTH+INTERCHAR_SPACE ; step++ ) {
cli();
sendString( m , step , 0x01, 0x00, 0x02 );
sei();
m++;
}
}
In the code, client.loop(); seems to really only call callback(). Not sure I understand the details, but it works, so I'll take it.
Anyway, the callback function itself was taken from the MQTT library GitHub - knolleary/pubsubclient: A client library for the Arduino Ethernet Shield that provides support for MQTT.
It looks like this: (NOTE: I already figured out how to change char array in it to a string!)
void callback(char* topic, byte* payload, unsigned int length) {
// checks if message has arrived & displays it: (straight from the MQTT library)
// next 4 lines were in the library - seems to be a very complicated way of doing this?
// for (int i=0;i<length;i++) {
// Serial.print((char)payload[i]);
// }
// Serial.println();
// here is how to change that into a char - makes it easier in my mind.
payload[length] = '\0'; // add a null to terminate
Serial.println("Re-cast as *string - is this this what I want? :");
char *m = (char *) payload;
Serial.print(m);
Serial.println();
}
Anytime I publish a new message, it pops up in my serial display. Cool.
What I want to achieve is this: every time there is a new message coming in from MQTT, change what is displayed on the NEOPIXEL display...
Or, pass *m from the callback function into loop
Been banging my head against this too long... any help is appreciated!
--cut here--
Actually, what I REALLY want is
-have an array of, say, 5 messages (A,B,C,D,E) that are displayed one after the other (I already know how to do that)
-MQTT a new message in; replace old message A with this new one (call it A'). Now display A', B, C, D, E
-MQTT a second message in; replace old message B with this newer one (B'). displaying A', B', C, D, E
-send more and more; after I am at A', B', C', D', E', the next MQTT messages will be A" so that I display A", B', C', D', E'.
And so on... But I have that under control (I think).
--cut here--
So it all hinges on my being able to "extract" the newest MQTT message from the callback function, which I am stumped with.
Thanks!
(using UNO with Ethernet shield)