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;
}
}