Skip to content

Instantly share code, notes, and snippets.

@efatsi
Last active March 30, 2017 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save efatsi/879839d7d537552d5356c176f02505a2 to your computer and use it in GitHub Desktop.
Save efatsi/879839d7d537552d5356c176f02505a2 to your computer and use it in GitHub Desktop.
String json = String("{\"count\":" + String(count) + "}");
int stringLength;
if (count < 10) {
stringLength = 13;
} else if (count < 100) {
stringLength = 14;
} else if (count < 1000) {
stringLength = 15;
} else {
stringLength = 16;
}
char input[stringLength];
json.toCharArray(input, stringLength);
int inputLen = sizeof(input);
int encodedLen = base64_enc_len(inputLen);
char encoded[encodedLen];
base64_encode(encoded, input, inputLen);
String encodedString = String(encoded);
if (count < 10) {
String morphed = encodedString.substring(0, encodedString.length() - 5) + String("=");
Particle.publish("magnetPass", morphed);
} else if (count < 100) {
String morphed = encodedString.substring(0, encodedString.length() - 4);
Particle.publish("magnetPass", morphed);
} else if (count < 1000) {
String morphed = encodedString.substring(0, encodedString.length() - 2) + String("==");
Particle.publish("magnetPass", morphed);
} else {
String morphed = encodedString.substring(0, encodedString.length() - 5) + String("=");
Particle.publish("magnetPass", morphed);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment