jQuery mobile + Rest

Hi,
I am super new to Arduino and web programming so go easy on me. I wanted to create a simple application where I could flip a switch in a jQuery mobile application and it would change the state of a digital pin on the Yun.

I have part of the process working. So far I have:

  • Compiled the Bridge example on the Yun and can send url commands like /arduino/digital/13/1 successfully
  • Created a simple jquery form html page with a flipswitch which I wish to use to change the pin state

What I am trying to understand is the (presumably) javascript that is triggered when the switch is moved sending the required url. I have read a little about json, jquery and looked at a few examples but can't quite understand the mechanism.

My index.html is as follows:

<!DOCTYPE html>
<html>
<head>
	<title>Coffee Timer</title>
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css" />
	<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
	<script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>
</head>
<body>

<div data-role="page">
  <div data-role="header">
    <h1>Welcome</h1>
  </div>

 <form>
    <label for="OnOff">Power</label>
    <input type="checkbox" data-role="flipswitch" name="OnOff" id="OnOff">
</form>
</body>
</html>

I would appreciate some guidance on closing the loop so I can get playing with the next steps.

Thanks.

http://forum.arduino.cc/index.php?topic=217756.msg1591479#msg1591479

In this demo I have used AngularJS, but you can choose your favorite technology (JQuery, Zepto, Backbone, and so on) since you will just need to make a GET request to move a servo.

Hi,
Just for completeness I thought I would come back with where I have got to. As I have never typed a line of JS before it took a nit of learning before I could get the following together:

<script type="text/javascript">
  $.ajaxSetup({ cache: false });
  $(document).ready(function(){
            //setup initial pin/ switch state
            $.get("http://arduino.local/arduino/digital/13",function(state)
            {if (state == 1)
                {$("#OnOff").val('1').flipswitch("refresh");
                }
            else //should probably use case
                {$("#OnOff").val('0').flipswitch("refresh");
                }
            });
            
            //set pin when switch change received
            $(document).on('change', '#OnOff', function(){    
                var datastring = $("#OnOff").val();
                $.ajax
                ({
                    type: "GET",
                    url: "http://arduino.local/arduino/digital/13/" + datastring,
                    dataType: 'JSON',
                    });
                });
            });  
</script>

As you can see, I tried using both the simple get command and the full blown ajax query. I think that the datatype:JSON is superfluous as there is no data in the body(??) but it is working.

One thing I need to work on is some sort of heart beat or check that the request was successful. ATM I send a request to change state and assume that all was well. Anyway, at least I have the basic idea/ comm's working now.

Thanks.

I've just done something very similar:
http://forum.arduino.cc/index.php?PHPSESSID=m9l0k3kgd285b1die9913pfo75&topic=226349.0
Hope this helps!