0

So first off please bear with me, this is my first time working with any of this. I was able to write a simple python code that could send info to the arduino and control its servo:

def main(inp):
    import serial
    ser = serial.Serial("/dev/cu.usbmodemfa131")  # open arduino serial port
    ser.write(inp)      # write a string
    ser.close()             # close port

and i was able to make an html/javascript file with a number slider that i can open with node.js on my computer. my first thought was to perhaps have this python script called every time the user changed the number on the slider and therefore have the arduino's servo move accordingly, however this seemed hard to do in javascript. how can i have the user inputs on the website write to the arduino's serial port?

this is the html file, the slider was made using jquery.

<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Slider Demo</title>
  <link rel="stylesheet" href="jquery-ui.min.css">
  <style>#slider { margin: 50px; }  </style>
  <script src="external/jquery/jquery.js"></script>
  <script src="jquery-ui.min.js"></script>  
</head>
<body>

<div class="center" id="slider"></div>
<center>
<div id="slider-result">90</div>   
</center>
<input type="hidden" id="hidden"/>
<script>

$( "#slider" ).slider({
                animate: true,
                range: "min",
                value: 90,
                min: 0,
                max: 180,
                step: 1,
})

                //this gets a live reading of the value and prints it on the page
                slide: function( event, ui ) {
                    $( "#slider-result" ).html( ui.value );
                },

                //this updates the hidden form field so we can submit the data using a form
                change: function(event, ui) { 
                $('#hidden').attr('value', ui.value);
                }

                });
</script>

</body>
</html>
megan
  • 31
  • 2

1 Answers1

0

From browser is difficult to access pc resources ( like access to printer ) , you can do a server that listen to requests sent from browser and make something based on request , in your case you can send a XmlHttpRequest POST to '/action ' with number input as parameter using jquery , and make a route in node server listening that action (app.post('/action',function()..) ) , then inside the function ,you get the parameters from the post request and call your python script ( or use node serial port to control arduino ) . This is a general idea but i hope could help you.

cshion
  • 1,173
  • 1
  • 10
  • 19