Get deviceName if deviceName is empty

I’m trying to write some code to get my device name, but only if the deviceName variable is empty.

My code uses device name to send an SMS notification to a non tech-savy user once a day.

My code has been working, but it asks for and gets that data every time my device wakes, (every 20 min) and I want to minimize that data usage. Therefore I want to put the device name in a retained variable, and check to see if I already have it before expend the data to get it.

My problem is that when I use the if statement to check for the name, it does not do the subscribe and publish that I need to get the device name.

I’ve tried it several ways, but can’t make it work.

Can someone help me see my error? Maybe this is something you can’t do in setup?

Thx

/*
 * Test to get deviceName into retained memory and sent via Tropo
 */

STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY));

const char webhookName[] = "troposendSMS";
char* cellNumber[] = {"+12345678901"};
retained char deviceName[32];
bool SMSsent = false;
char message[200];

void setup() {
    if (deviceName == "") {
        Particle.subscribe("particle/device/name", saveName);
        delay(1000);
        Particle.publish("particle/device/name");
        delay(5000);
    }
}

void saveName(const char *topic, const char *data) {
    strncpy(deviceName, data, sizeof(deviceName)-1);
}

void loop() {
    if (SMSsent == false) {
        sendSMS();
    }
}

void sendSMS() {
    sprintf(message, "{\"n\":\"%s\", \"m\":\"%s\"}", cellNumber[0], deviceName);
    Particle.publish(webhookName, message);
    SMSsent = true;
}

You can’t compare C-strings (aka char arrays) with the == equality operator since that only compares the addresses stored in the pointers (which almost never will be the same).
A proper string compare would be done via strcmp() but in your case you just want to know whether empty or not and for that you can either use the string length (strlen()) which should be 0 on an empty string or you just check the first character (if (deviceName[0] == '\0'))

1 Like

Aha!
Thank you, @ScruffR.
Tha
J