This could potentially be a stupid question, but I've been working for two weeks on it with no success. I'm using an Arduino101 that connects via bluetooth to an EVOThings application on a phone. The application requests tweeter information from a Flask server hosted on Heroku and the idea is to relay the response to the Arduino through the bluetooth connection. However, although the response goes through successfully, I'm having trouble parsing the response on the client side to do further processing and send it to the Arduino101. Below is some of the code:
<script>
// Application object.
var app = {}
// Connected device.
app.device = null;
app.msg = '';
//Get the message.
app.getMsg = function()
{
$.ajax({ type: "GET",
contentType: "application/json; charset=utf-8",
url: 'http://ift.tt/2bmUWzE',
async: false,
dataType: "json",
//navigator.notification.alert("hello", function() {});
success : function(text)
{
// navigator.notification.alert("hello", function() {});
var res = JSON.stringify(text.data);
// navigator.notification.alert("hello", function() {});
//document.getElementById("try").innerHTML=res;
var message = '';
for(i = 0; i < res.length; i++){
message += res[i];
}
app.msg += message;
//navigator.notification.alert("hello", function() {});
}
});
}
// Turn on LED red.
app.ledRed = function()
{
app.device && app.device.writeDataArray(new Uint8Array([1]), '19b10001-e8f2-537e-4f6c-d104768a1214');
}
// Turn on LED green
app.ledGreen = function()
{
navigator.notification.alert(app.msg, function() {});
app.device && app.device.writeDataArray(new Uint8Array([2]), '19b10001-e8f2-537e-4f6c-d104768a1214');
}
// Turn on LED blue
app.ledBlue = function()
{
app.device && app.device.writeDataArray(new Uint8Array([3]), '19b10001-e8f2-537e-4f6c-d104768a1214');
}
// Turn on LED yellow
app.ledYellow = function()
{
app.device && app.device.writeDataArray(new Uint8Array([4]), '19b10001-e8f2-537e-4f6c-d104768a1214');
}
// Turn on LED cyan
app.ledCyan = function()
{
app.device && app.device.writeDataArray(new Uint8Array([5]), '19b10001-e8f2-537e-4f6c-d104768a1214');
}
// Turn on LED purple
app.ledPurple = function()
{
app.device && app.device.writeDataArray(new Uint8Array([6]), '19b10001-e8f2-537e-4f6c-d104768a1214');
}
// Turn on LED white
app.ledWhite = function()
{
app.device && app.device.writeDataArray(new Uint8Array([7]), '19b10001-e8f2-537e-4f6c-d104768a1214');
}
// Turn off LED.
app.ledOff = function()
{
app.device && app.device.writeDataArray(new Uint8Array([0]), '19b10001-e8f2-537e-4f6c-d104768a1214');
}
app.showMessage = function(info)
{
document.getElementById('info').innerHTML = info
};
// Called when BLE and other native functions are available.
app.onDeviceReady = function()
{
app.showMessage('Touch the connect button to begin.');
};
app.connect = function()
{
evothings.arduinoble.close();
app.showMessage('Connecting...');
evothings.arduinoble.connect(
'ARDUINO 101-3E4E', // Advertised name of BLE device.
function(device)
{
app.device = device;
app.showMessage('Connected! Touch buttons to turn LED on/off.');
},
function(errorCode)
{
app.showMessage('Connect error: ' + errorCode + '.');
});
};
document.addEventListener(
'deviceready',
function() { evothings.scriptsLoaded(app.onDeviceReady) },
false);
</script>
What I did was modify the EVOThing example for connecting the Arduino101 to the phone app with BLE by adding a function to do the request using AJAX and saving it in a global variable, which I then plan to send to the Arduino. But the msg is never stored in the variable to begin with. Actually, even using the alert function to debug the success code block in the AJAX post, I get nothing. The server side is getting the response and is returning a OK 200 code as seen below: Heroku Logs from server side
For some reason the msg coming from the server is never saved to the app.msg variable. Any help on why this setup doesn't work will be greatly appreciated.
Aucun commentaire:
Enregistrer un commentaire