Digital write returns success but does not affect the device

I am using the iOS SDK. SparkCloud is connected. I have a valid SparkDevice object.

SparkDevice 0x1740cca90, type: Spark device, id: xxxxxxx, name: XXX, connected: true, variables: {\n}, functions: (\n digitalread,\n digitalwrite,\n analogread,\n analogwrite\n), version: (null), requires update: false, last app: (null), last heard: 2015-06-18 23:42:52 +0000

I am trying to turn on a LED and reflect its state on the screen.

[_myPhoton callFunction:@"digitalwrite" withArguments:@[@"D7", @1] completion:^(NSNumber *resultCode, NSError *error) {
    if (!error) {
        _blueLEDState = _tmpBlueLEDState;
        _blueLED.backgroundColor = [_blueLEDState boolValue] ? _blueOn : _blueOff;
    }
}];

error comes back as nil, which would indicate success. resultCode has a value of -2. What does that mean?

The device remains unaffected by this call. It is running on an iPhone, and the Particle app running on the same phone is working fine. Will the source to the Particle all be released so I can see how they did it, or is there any way I can diagnose why this call is not succeeding?

The example code for the SDK gave an argument of @1 to turn on the LED. The argument is supposed to be @“HIGH” or @“LOW”, not @1 or @0. I fixed that and my code works now. Huzzah!

2 Likes

Glad you found the solution.

I am trying to do the same with function name “led” and argument as “on”/“off” to turn ON/OFF the led D7 on electron

with application code:

void setup() {
    pinMode(led,OUTPUT); // Our LED pin is output (lighting up the LED)
    Particle.variable("analogvalue", &analogvalue, INT);
    Particle.function("led",ledToggle);
}

int ledToggle(String command) {

    if (command=="on") {
        digitalWrite(led,HIGH);
        Particle.publish("G1", "tData", 60, PRIVATE);
        return 1;
    }
    else if (command=="off") {
        digitalWrite(led,LOW);
        
        Particle.publish("B", 
              "v:" + String::format("%.2f",fuel.getVCell()) + 
              ",c:" + String::format("%.2f",fuel.getSoC()),
              60, PRIVATE);
        return 0;
    }
    else if(command=="sync")
    {
        //Do something
    }
    else {
        
        return -1;
    }
}

ios code:

        SparkCloud.sharedInstance().getDevices { (sparkDevicesList : [AnyObject]?, error :NSError?) -> Void in
            // 2
            if let sparkDevices = sparkDevicesList as? [SparkDevice]
            {
                print(sparkDevices)
                // 3
                for device in sparkDevices
                {
                    if device.name == "myDevice"
                    {
                        // 4
                        device.callFunction("led", withArguments: ["off"], completion: { (resultCode :NSNumber?, error : NSError?) -> Void in
                            // 5
                            print("Called a function on myDevice with error ")
                            print(error)
                        })
                    }
                }
            }
        }

Result

[<SparkDevice 0x7e01db70, type: Photon, id: XXXXXXXX, name: myDevice, connected: true, variables: {
    analogvalue = int32;
}, functions: (
    led
), version: (null), requires update: false, last app: (null), last heard: 2016-04-23 03:57:37 +0000>]

Called a function on myDevice with error
nil

What is wrong here? Could you please suggest me a solution?

Thanks,
Jay

Not a solution for your issue, but a side note on your firmware

You are using String::format() here

Particle.publish("B", 
              "v:" + String::format("%.2f",fuel.getVCell()) + 
              ",c:" + String::format("%.2f",fuel.getSoC()),
              60, PRIVATE);

But why so complicated?

You can just write

Particle.publish("B", String::format("v:%.2f,c:%.2f", fuel.getVCell(), fuel.getSoC()), PRIVATE);

That’s the whole point of String::format().

1 Like

Thanks @ScruffR.