How to include Device Name (Not just Device ID) in SMS when using Messaging Cloud API (Twilio)

First, I’d stay away from String and stick with char[] which can also be passed to Particle.publish().
And second, subscribing alone isn’t enough. You also need to ask the cloud to send you the name to your subscription.

char dev_name[32] = "";
bool publishName = false;

void handler(const char *topic, const char *data) {
  strncpy(dev_name, data, sizeof(dev_name)-1);
  Serial.printlnf("received %s: %s", topic, dev_name);
  publishName = true;
}

void setup() {
  Particle.subscribe("particle/device/name", handler);
  Particle.publish("particle/device/name");  // <-- ask the cloud for the name to be sent to you
}

void loop() {
  if (publishName) {
    Particle.publish("twilio_sms", dev_name, PRIVATE);
    delay(1000); // to ensure adhering to rate limit
    publishName = false;
  }
}
4 Likes