Sending Particle.publish data to xcode

Hey Particle Community. I am looking for a way to send numerical data from the Particle.publish, photon cloud to xcode, an application to create apps for IOS. Is there a way to do this? Thanks!

1 Like

Have you looked at the iOS SDK, and example Tinker app?

1 Like

Hey Moors7, I have made my own app on xcode already using the iOS SDK. I need to connect the photon to that app and send data to it because I might publish this app on the app store sometime in the future. So I need to find a way to send data from the photon to the app.
I was thinking, do you think there is a way to give xcode an access token from the console.particle so it could send data? I was thinking that if the xcode could receive a stream of data from the terminal on my computer it could receive the data.

Have you seen this?
https://docs.particle.io/reference/ios/#events-sub-system

Hey Particle Community. I am trying to connect a photon to my own IOS app that I am creating and launching to the app store next year.

Since there is already a particle mobile app that lets you connect your photon to your phone, asking the developers who made that app (or anyone else you might be able to help), what code did they you use in Xcode to allow the photon to connect to the IOS app and phone? Your help is greatly appreciated!

You really need to start your research here: https://docs.particle.io/reference/ios/

I am struggling with the same problem. I already read the reference documents you are suggesting us to read and tried the code. The documentation is half backed, an example showing data transfer - ideally Jason data transfer - would help everyone here.

It would help to what kind of connection you want, and how much data you need to transfer. From the iOS app, you can call functions, read variables, or listen to publishes. Do you need to initiate the transfer from the Particle device or from the iOS device?

The data volume is pretty small, 6 integer data points. I already publish to the particle cloud using json format. I would like to display those data points on the iOS device once the user launches the App. I am using Swift, which I am learning as I go… hence the need for an example.

Once the above gets resolved, I need to be able to send the user a “notification”, which will be a kind of alarm for the user to launch the app because some of the data points demand user attention.

In summary I need:

  1. an example how to read a json event from particle cloud,
  2. help on how to send a notification to an iOS user who has the app and opted in to get notification.

Thanks.

The docs have an example for reading a variable which I think would be the easiest way to get the data. Here is a slightly modified version that should work with json data,

myPhoton!.getVariable("myVariable", completion: { (result:Any?, error:Error?) -> Void in
    if let _ = error {
        print("Failed reading data from device")
    }
    else {
        if let json = result as? String {
            print(json)
            // convert the json to a Foundation object with the JsonSerialization class
        }
    }
})

The second part is more difficult. I’ve never written an app that uses notifications. Looking at Apple’s docs, it seems like a pretty complicated thing to set up (to give you an idea of what’s involved, take a look at this tutorial. One requirement for using push notifications is that you must be a paying member of the iOS Developer Program. Is this an app you want to put on the app store, or is it for personal use?

I am planing to put this app on the App Store, I am on the iOS Developer Program. Thank you for the link.

Hi Ric, your code helped me, but this was not what I was looking for. I was trying to read an “event” that was already published to the particle cloud, not to hit the device in the field. The documentation and lack of a examples, at least for a swift rookie, is fairly light… After a couple of days, I came up with a solution as follows:

    ParticleCloud.sharedInstance().subscribeToMyDevicesEvents(withPrefix: "USEREVENT", handler: { (event :ParticleEvent?, error : Error?) in
        if let _ = error {
            print ("could not subscribe to events")
        } else {
            DispatchQueue.main.async(execute: {
                if let str = event!.data {
                    let data = str.data(using: String.Encoding.utf8, allowLossyConversion: false)!
                    let json = try? JSONSerialization.jsonObject(with: data, options: []) as! [String: AnyObject]
                    if let FirstKeyData = json!["FirstKey"] as? String {
                        // Now FirstKeyData avaialble within App
                    }
                    if let SecondKeyData = json!["SecondKey"] as? String {
                        if let SecondKeyDataFloat = Float(SecondKeyData) {
                        // Now SecondKeyDataFloat is available as a numeric variable
                        } else {
                            print ("Not a valid number: \(SecondKeyData)")
                        }
                    }
                }
            })
        }
    })

Your app will have to be running at the time the event is published, so I didn’t think this approach would work. The events are not stored in the cloud (unless something has changed recently; I’ve been away for a while).

Nope, it's still true as you said.

Thank you for pointing this out. The app is working at the moment because I am publishing every 2 seconds. As the long term plan is to reduce the update rate, it does not work.

Your approach can work for the app, but it would create more traffic between device and cloud and likely be slower. At this point I will consider my own cloud instance.