Subscribe to a MQTT Topic. (Subscribe ke Topic MQTT)
subscribe
@/subscribe
Subscribe to a MQTT Topic. (Subscribe ke Topic MQTT)
MQTTnusabotid/nusabot-mqtt-broker/nusabot-device
Topicstring
The topic to publish to
Updatepulse
Update incomming messages. (Needs to be quick/Continuous)
OKpulse
Subscribed OK
Messagestring
Incomming message.
OUTnusabotid/nusabot-mqtt-broker/nusabot-device
To use the node in your project you should have the nusabotid/nusabot-mqtt library installed. Use the “File → Add Library” menu item in XOD IDE if you don’t have it yet. See Using libraries for more info.
C++ implementation
struct State {
String Ctopic;
char str[16];
CStringView view;
State() : view(str) { }
};
{{ GENERATED_CODE }}
void evaluate(Context ctx) {
if (!isInputDirty<input_Update>(ctx)){
emitValue<output_OUT>(ctx, getValue<input_MQTT>(ctx));
return;}
if (isSettingUp()){
auto MQTT = getValue<input_MQTT>(ctx);
auto state = getState(ctx);
auto topic = getValue<input_Topic>(ctx);
auto topicLength = length(topic);
char _topic[topicLength + 1];
dump(topic, _topic);
_topic[topicLength] = '\0';
state->Ctopic = String(_topic);
MQTT->subscribe(_topic);
return;
}
auto MQTT = getValue<input_MQTT>(ctx);
auto state = getState(ctx);
MQTT->loop();
for(int i=0; i<10; i++) //cycle thrue the buffer
{
if(state->Ctopic == Buff [i][0]){ //topics (global from callback) against Ctopic (Current Topic)
//Serial.println("incoming: " + Gtopic + " - " + Gpayload);
Buff [i][1].toCharArray(state->str, 16); //Check messages in buffer that matter to this node
Buff [i][0] = "";
Buff [i][1] = "";
emitValue<output_Message>(ctx, XString(&state->view));
emitValue<output_OK>(ctx, 1);
}
}
emitValue<output_OUT>(ctx, MQTT);
}