Converting between MQTT and another IoT messaging protocol

I have an arduino working as a "converter" converting between MQTT and another messaging protocol, "IoTMSG2". But at the moment I have to hard code any new messages into the converter. I'd love to be able to just prepend/prefix an MQTT message with something like 'Z' (for example!) and for the converter to strip the 'Z' but realise that that particular message should be converted to a IoTMSG2 message.

Any ideas how I could go about this? This is how I'm going about it at the moment, with hard-coded references.

MQTT to IoTMSG2

void callback(char * topic, byte * payload, unsigned int length) 
{
        char p[length + 1];
        memcpy(p, payload, length);
        p[length] = NULL;

        if (!strcmp(p, "closedoor")) {IoTMSG2.publish(DEVICE_NAME, "CLOSE DOOR", 60, PRIVATE);}}

IoTMSG2 to MQTT

void eventHandler(const char * event,
        const char * data) {
if (strcmp(data, "CLOSE DOOR") == 0) 
{client.publish("security/door", "CLOSE");} 
}

Thank you!

Perhaps you see a really obvious relationship between "closedoor" and "CLOSE DOOR", but I don't.

Perhaps you see a really obvious relationship between "CLOSE DOOR" and "security/door" + "CLOSE", but I don't.

If you can explain the relationship, without relying on the phrase "word in English", we can help you with auto-conversion.

Hi Paul, sorry to have confused matters.

Basically I'm trying to convert between MQTT and "IoTMSG2". I posted those two code snippets merely to show how my code works at the moment.

The relationship is:

Message comes in from the IoT platform into my Arduino as a message type "IoTMSG2". I am subscribed to these messages. At the moment, I have hardcdoed a check for the specific message "CLOSE DOOR" into the subscription.

if (strcmp(data, "CLOSE DOOR") == 0)

if that exact message comes in, my script immediately republishes an MQTT message on topic security/door with message "CLOSE.

client.publish("security/door", "CLOSE"

This was just posted as an example.

Same with messaging coming in from the MQTT platform. I am subscribed to these messages also in the same script.

So if the message "closedoor" comes in on MQTT:

if (!strcmp(p, "closedoor"))

my script immediately republishes an IoTMSG2 message

IoTMSG2.publish(DEVICE_NAME, "CLOSE DOOR", 60, PRIVATE)

However...all this close door / open door stuff isn't so relevant to what I'm actually asking about (as I said, just an example). What I'm wanting to do, is

a) if an MQTT message comes in prefixed with Z,

e.g. Zclosedoor, the script would strip the 'Z' and immediately republish as IoTMSG2 e.g. IoTMSG2.publish(DEVICE_NAME, "closedoor", 60, PRIVATE) see how the message is exactly the same (minus the stripped 'z')

And in reverse if b) an IoTMSG2 message comes in prefixed with Z, (strcmp(data, "ZCLOSE DOOR") == 0), strip the Z and republish as MQTT client.publish("security/door", "CLOSE DOOR")

Does this make more sense?

THANK YOU

void eventHandler(const char * event, const char * data)
{
   if (strcmp(data, "CLOSE DOOR") == 0)
   {
      client.publish("security/door", "CLOSE");
   }
}

This is your IoTMSG2 to MQTT handler, properly formatted.

If you want to simply publish (some of) the contents of the array pointed to by the pointer named data, using the publish() method, then do that.

void eventHandler(const char * event, const char * data)
{
   if(data[0] == 'z')
   {
      client.publish("security/door", &data[1]);
   }
}

This will check that the 1st character pointed to is a 'z', and, if it is, publish() the rest of the pointed to data.

Thank you - looks like that should work nicely - just what I was after - and looks like I should be able to adapt it for 'p' ! :-)

Thanks!