IOS Swift Call Function by Device


#1

I’m working on an app for my Particle devices. I want to call a function, but I’m having a hard time wrapping my head around this code:

var myPhoton1 : ParticleDevice?

            let funcArgs = ["D7", 1] as Any
    var task = myPhoton1!.callFunction("digitalWrite", withArguments: funcArgs as! [Any]) { (resultCode : NSNumber?, error : Error?) -> Void in
        if (error == nil) {
            print("LED on D7 successfully turned on")
        }
    }
    var bytesToReceive : Int64 = task.countOfBytesExpectedToReceive
    // ..do something with bytesToReceive

My question is how do I specify the particle device that i want to call the function on? I have the app getting all of the devices and displaying on a table view with an array. Any help would be appreciated. Thank you.

PS: still learning the Swift coding environment - be gentle please…


#2

This should help
https://docs.particle.io/reference/SDKs/ios/#get-a-list-of-all-devices

You ask for the list of devices and pick the one you want.


#3

@ScruffR Thanks for the references - it took me awhile to get it right in my headspace, but it is working. Thank you!

I can call the function via the IOS app - but have run into an issue:

  1. It doesn’t just send one event through - i get up to six events in one function call. Not sure why that is.

If I use the console method of calling the function, it only triggers once. What am I missing?


#4

@ScruffR - I had a duh moment - disregard last post. I had it in a loop with 6 devices. Thanks again! It’s working.