Skip to content

Instantly share code, notes, and snippets.

@Antoinebr
Last active November 28, 2017 21:50
Show Gist options
  • Save Antoinebr/1d228cfd6ea169f3f57b2a25ef882df2 to your computer and use it in GitHub Desktop.
Save Antoinebr/1d228cfd6ea169f3f57b2a25ef882df2 to your computer and use it in GitHub Desktop.
arduino-mqtt-payload-decode.h
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
char message[length + 1];
// copy contents of payload to message
memcpy(message, payload, length);
// add NULL terminator to message, making it a correct c string
message[length + 1] = '\0';
Serial.println("Message complet : ");
Serial.println(message);
Serial.println("...");
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
Serial.println();
char message[length + 1];
// copy contents of payload to message
memcpy(message, payload, length);
// add NULL terminator to message, making it a correct c string
message[length + 1] = '\0';
// we convert our message into a proper String
String myMsg = message;
Serial.println("Complete msg : ");
Serial.print(message);
Serial.println();
// We convert our String to int and send it to the servo
Servo1.write( myMsg.toInt() );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment