Last active
November 28, 2017 21:50
-
-
Save Antoinebr/1d228cfd6ea169f3f57b2a25ef882df2 to your computer and use it in GitHub Desktop.
arduino-mqtt-payload-decode.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("..."); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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