diving further into my MQTT study, i want to have my photon use multiple values from a string. for simplicity of discussion, kets use a rgb led as an example.
topic message = “r=255, b=255, g=255, l=255,p=s”
in photon i want to have it do the following:
r=255 becomes red=255
b=…
g=…
l=255 becomes luminosity=255
p=s becomes pattern=solid, additional options would be chase, rainbow, etc, all referencing their own voids.
holy crap that was fast help. that should work perfectly, i don’t know json, so skipping that. yes , i meant functions, it’s late, i’m going to bed now, lol.
Single characters would require single quotes, just the way as I have used above.
And the backslash ( \ ) in my example is the escape character to tell the compiler not to take the following character literally but has a special meaning (in this case a zero-byte).
For your literal s case you'd just write
char pattern = 's';
You also haven't copied my sscanf() statement correctly
the first parameter cannot be an integer but must be a pointer to a string/character array.
Try that instead
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
returned = sscanf(p, "r=%d, g=%d, b=%d, l=%d, r=%d, p=%c", &red, &green, &blue, &luminosity, &remote, &pattern);
}
void loop() {
if (remote == 1) { // if we got new data from remote
remote = 0; // mark done
// do the rest
}
}
Also note that equality checks are not done like this
Alright, uploaded, compiled, but not “working” published the message from my client, and nadda, values aren’t updated (verified by the publishing of current values on the photon). It was working before the sscanf change when i just used atoi on a single parameter, so i think its jsut a formatting issue.
attempted to publish
r=255
r=100, g=200, b=255, l=100, r=1, p=s