MQTT to IR

    payload[length] = '\0';

Puts it in the proper place.

Then, you can just pass payload (cast to char *) to atoi:
[/quote]

I am not really a C expert, but didn't just potentially overwrite some random part of memory?