The executive summary is a problem with JSON data array that does not onload to a Yun webpage.
The end game is a remote controller through a webpage that most browsers open. I have built a Buffalo II DAC that has I2C capabilities for volume and features control. I have adapted an Arduino sketch provided by HiFiduino and added webserver functions through the Yun.
Buttons on the webpage for volume, mute, and filter selection are working. But when I add the ability to display volume and sample rate status on the webpage the webserver hangs.
The JSON data array is stored using Bridge.put():
Bridge.put("smplrate", String(sr));
Bridge.put("volume", String(currAttnu/2));
} // End of loop()
This is very stable code and stores the array in myYun.local/data/get/
{"value":{"smplrate":"44101","volume":"18"},"response":"get"}
I have incorporated a getJSON method from How to use getJSON ? - Arduino Yún - Arduino Forum that is in a .js file (DAC2.js):
$(function() {
refresh();
});
function refresh()
{
$.getJSON("/data/get", function(json)
{
document.getElementById('mysmplrt').innerHTML=json.value.smplrate;
document.getElementById('myvol').innerHTML=json.value.volume;
});
setTimeout("refresh()",3000); //Pole every three seconds.
}
This function is evoked by onload:
The complete html code is:
<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<html>
<head>
<title>Buffalo DAC Remote Controller</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name=\"apple-mobile-web-app-capable\" content=\"yes\" />
<meta name=\"apple-mobile-web-app-status-bar-style\" content=\"black\" />
<meta name=\"format-detection\" content=\"telephone=no\" />
<!--link rel=\"apple-touch-icon\" href=\"NDD-60x60.png\" /-->
<script type="text/javascript" src="js/zepto.min.js"></script>
<script type="text/javascript" src="js/DAC1.js"></script>
<script type="text/javascript" src="js/DAC2.js"></script>
<style>
body { margin:0; padding:0; border:0; background-color: 4475CD; font-family:\"Arial\",Arial,sans-serif; }
</style>
</head>
<body onload="setInterval(function(){ reply_click(); refresh();}, 500);">
<center>
<div style=border:0px solid #4475CD;>
<div style=background-color:#6495ED; height:480px;>
<div style=font-size:145%; text-align:center; color:2455aD; padding:0px 0px 0px 0px;></style>
<h1><i>Buffalo DAC
Remote Controller</i></h1>
</div>
<div style=\"font-size:120%; text-align:center; color:000066; padding:0px 0px 0px 0px;\">
Sample Rate is: <span id='mysmplrt'>-----</span>
DAC Volume is: <span id='myvol'>--</span>
</div>
<div style=\"font-size:175%; text-align:center; color:000066; padding:20px 0px 0px 0px;\">
<form id="DAC_Selection" action="">
<button id="1" onClick="reply_click(this.id)"> Volume UP </button>
<button id="0" onClick="reply_click(this.id)">Volume DOWN</button>
<button id="2" onClick="reply_click(this.id)"> MUTE </button>
<button id="3" onClick="reply_click(this.id)"> Filter </button>
</form>
</div>
</center>
</body>
</html>
For completeness, here is the DAC1.js code:
$(function() {
reply_click();
});
function reply_click(clicked_id) {
//Every one second, this function poles the button 'id' from web page and sends to Yun
//a request to control volume or filter functions depending on which button is pressed.
if (clicked_id =='1') //if the user selected for Volume UP, send request to Arduino
{
$('#DAC_content').load('/arduino/cmd/1');
}
if (clicked_id =='0') //if the user selected for Volume DOWN, send request to Arduino
{
.
.
.
}
if (clicked_id ==' ') //if the selected is null, send request to Arduino
{
$('#DAC_content').load('/arduino/cmd/ ');
}
setTimeout("reply_click()",1000); //Pole every one second.
}
Again, the simple button code does work but the addition of the JSON retrieval code causes a webserver hang. I suspect that either there is an "onload" problem or that I am overloading the Yun memory space. The uploaded sketch fills 82% of the memory. What is the Yun's memory limit for storing sketches?
Is my use of "onload" correct? Programming syntax? I have worked this issue for a week so I may be missing obvious problems.
Your feedback will be highly valued.
Many thanks for reading this.
Ron