Please help! How do I send data from ios app to particle cloud?

Hello,

I was wondering if I could send data from phone to particle cloud.

Basically I’m getting some data from rfduino on ios app(cordova), and trying to send data to particle cloud and control LED lights over the internet based on that received data.

I’ve looked up some documentation but couldn’t find any information. Does anyone have any idea on this? I would greatly appreciate any information. Thank you!

Since it sounds like you’re using Apache Cordova, which is based on Javascript, I’d use Particle API JS. Now I haven’t actually tried it, the Particle API JS is designed for use in non-browser environments like node.js so it should probably work in Cordova. I think. Maybe. Maybe not. But start there.
https://docs.particle.io/reference/javascript/

There is an native iOS SDK but I’d try Javascript first if you’re using Cordova.
https://docs.particle.io/reference/ios/

Of course you could just code the cloud API requests directly as Javascript web queries, as well.
https://docs.particle.io/reference/api/

3 Likes

Thank you so much for your reply!

But since I’m very new here…I’m still not sure how to send and store data received from external sensor(not using photon yet this part) to particle cloud api from my cordova app.

When usually sending data object to api in javascript, I imagined something like this…

$.ajax({
type: “POST”,
dataType: “json”,
url: “/api/API”,
data: source,
success: function (data) {
alert(data);
},

but of course it doesn’t seem how it works in this case. Do you mind specifying the way I can send data in javascript? I see using POST I could upload data, but how do I include this in javascript?

I would appreciate your help. Thank you

Hopefully someone who actually knows what they’re talking about will chime in here. I’ve never actually written in app in Cordova, so I’m not positive how it actually works, but if you don’t get an answer sooner I’ll try to give it a shot this weekend since it seems like an interesting thing I’ve never done before.

I’ve spent whole day on this and didn’t get any progress… If anyone can share any idea on this I would very appreciate it. Thanks!

Taking look in the tutorial section of the forums is almost always worthwhile, especially if you’re new.

This one for example has proven very useful:

I suspect you are using an Cordova/Angular based framework. You’d best write a service / factory for that. For example:

.factory('iotService', function ($http) {
  var iotService = {};
  iotService.sendtoParticle = function (data) {
    return $http.post(url + '/directory/', data);
  };
  return iotService;
})

Then use it in your controller:

.controller('Ctrl', function (iotService) {

  iotService.sendtoParticle( your data here )
    .then(function (data) {
      // etc.
    })
    .catch(function (err) {
      //console.log(err);
    });

Hello,
@Moors7, @bartpeperkamp

thank you for your reply. So after following instruction you sent and Particle javascript documentation, I finally get it working on web app. but it still doesn’t work on my phonegap app.

So here I tried simply turning LED light on when I click the button on the app. Then I combined that code with my mobile app.

I haven’t tried service/factory yet, but before I tried that, I was wondering if you could quickly look at following code and see if something is missing.
I’m sure there is something wrong, but really don’t get what it is. I don’t get any error from console, either, which is very frustrating. (just in case I removed deviceId, token and etc here)

html:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1,  target-densitydpi=device-dpi" />
        
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
        <script type="text/javascript" src="particle.min.js">

        var Particle =require('particle-api-js');
        var particle = new Particle();

        particle.login({username: '', password: ''});
        particle.login({username: '', password: ''}).then(
          function(data){
            console.log('API call completed on promise resolve: ', data.body.access_token);
          },
          function(err) {
            console.log('API call completed on promise fail: ', err);
          }
        );

        var fnPr = particle.callFunction({ deviceId: '', name: 'led', argument: 'on', auth: "" });

        fnPr.then(
          function(data) {
            console.log('Function called succesfully:', data);
          }, function(err) {
            console.log('An error occurred:', err);
          });


        </script>

 

        <title>UV Intensity</title>
    </head>
    
    <body>
        <div class="app">

            <h1>Current UV Intensity</h1>
            <div id="mainPage">
                <ul id="deviceList">                    
                </ul>
                <button id="refreshButton">Refresh</button>
            </div>

            <div id="detailPage">
                <br>
                <div id="uvIndex">
                    <script type="text/javascript"></script>
                </div>
                
                <div id="ifCondtion"></div>
                
                    <button id="closeButton">Close</button>
            </div>  

            <br><br>
                <button id ="fnPr"> LED ON </button>
    
        </div>

        <script type="text/javascript" src="cordova.js"></script>
        <script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
        <script type="text/javascript">
            app.initialize();
            
        </script>
        
    </body>
</html>

javascript code:


// (c) 2013 Don Coleman

'use strict';

var arrayBufferToFloat = function (ab) {
    var a = new Float32Array(ab);
    return a[0];
};

var app = {
    initialize: function() {
        this.bindEvents();
        detailPage.hidden = true;
    },

    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
        refreshButton.addEventListener('touchstart', this.refreshDeviceList, false);
        closeButton.addEventListener('touchstart', this.disconnect, false);
        deviceList.addEventListener('touchstart', this.connect, false); 
        
    },
    onDeviceReady: function() {
        app.refreshDeviceList();
    },
    refreshDeviceList: function() {
        deviceList.innerHTML = ''; // empties the list
        rfduino.discover(5, app.onDiscoverDevice, app.onError);
    },
    onDiscoverDevice: function(device) {
        var listItem = document.createElement('li'),
            html = '<b>' + 'UVIndex'

        listItem.setAttribute('uuid', device.uuid);
        listItem.innerHTML = html;
        deviceList.appendChild(listItem);
    },

    
    connect: function(e) {
        var uuid = e.target.getAttribute('uuid'),
            onConnect = function() {
                rfduino.onData(app.onData, app.onError);
                app.showDetailPage();
            };

        rfduino.connect(uuid, onConnect, app.onError);


    },
    onData: function(data) {

        console.log(data);
        var uvdata = arrayBufferToFloat(data);
        uvIndex.innerHTML = uvdata.toFixed(2);
          
    },

    disconnect: function() {
        rfduino.disconnect(app.showMainPage, app.onError);
    },
    showMainPage: function() {
        mainPage.hidden = false;
        detailPage.hidden = true;
    },
    showDetailPage: function() {
        mainPage.hidden = true;
        detailPage.hidden = false;
    },
    onError: function(reason) {
        alert(reason); // real apps should use notification.alert
    }
};

And for the firmware code It will turn on light when I click the button.

Firmware code

int led1 = D0;


// Last time, we only needed to declare pins in the setup function.
// This time, we are also going to register our Spark function

void setup()
{


   pinMode(led1, OUTPUT);
 
   Spark.function("led",ledToggle);

   digitalWrite(led1, LOW);
  

}

void loop()
{
   // Nothing to do here
}


int ledToggle(String command) {


    if (command=="on") {
        digitalWrite(led1,HIGH);
     
        return 1;
    }
    else if (command=="off") {
        digitalWrite(led1,LOW);
        
        return 0;
    }
    else {
        return -1;
    }
}