【问题标题】:Controlling Arduino with local webpage/node.js使用本地网页/node.js 控制 Arduino
【发布时间】:2015-11-12 04:04:07
【问题描述】:

所以首先请多多包涵,这是我第一次使用这些。 我能够编写一个简单的 python 代码,可以将信息发送到 arduino 并控制它的伺服:

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

我能够制作一个带有数字滑块的 html/javascript 文件,我可以在我的计算机上使用 node.js 打开该文件。我的第一个想法可能是每次用户更改滑块上的数字时调用这个 python 脚本,从而让 arduino 的伺服相应地移动,但这在 javascript 中似乎很难做到。如何让网站上的用户输入写入 arduino 的串行端口?

这是 html 文件,滑块是使用 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>

【问题讨论】:

    标签: javascript python node.js arduino arduino-yun


    【解决方案1】:

    从浏览器很难访问 pc 资源(例如访问打印机),您可以做一个服务器来监听从浏览器发送的请求并根据请求进行某些操作,在您的情况下,您可以发送 XmlHttpRequest POST 到 '/action ' 使用 jquery 将数字输入作为参数,并在节点服务器中创建一个路由,监听该操作 (app.post('/action',function()..) ),然后在函数内部,您从发布请求中获取参数并调用您的python 脚本(或使用node serial port 来控制arduino)。这是一个普遍的想法,但我希望可以帮助你。

    【讨论】:

      猜你喜欢
      • 2023-03-08
      • 1970-01-01
      • 2013-03-12
      • 1970-01-01
      • 2012-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多