与 Python 脚本远程交互的最简单方法 [英] Simplest Way to Remotely Interact With Python Script

查看:79
本文介绍了与 Python 脚本远程交互的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在网络上设置一个 Raspberri Pi,它运行 Python 脚本来控制 GPIO 引脚.我需要通过 GUI 或命令行从网络上的另一台计算机更改此脚本中的变量.实现这一点的最简单方法是什么(我是新手)?

I am setting up a Raspberri Pi on a network, which runs a Python script to control GPIO pins. I need to change a variable in this script from another computer on the network, via GUI, or command line. What is the simplest way possible to accoplish this (I am new to this)?

也许是一个简单的网页托管在 Pi 上,带有控制变量的按钮?

Maybe a simple webpage hosted on the Pi somehow with buttons that control the variables?

谢谢

抱歉,我试图保持简单.该脚本将在启动时启动并持续运行,监控温度并循环打开/关闭加热器(通过 GPIO 引脚)以保持存储在set_point"变量中的温度.

Sorry, I was trying to keep it simple. The script will be started at boot and run continuously, monitoring a temperature, and cycling a heater on/off (via GPIO pins) to maintain the temperature stored in a "set_point" variable.

推荐答案

这里有一个小网页,你可以安装在你的 Raspberry Pi 上,在你的 Apache/的文档根目录中保存为 index.php其他网络服务器.然后,您可以使用网络上的任何 iPhone 或计算机访问 URL 来控制所需的温度设置:

Here is a little webpage you could install on your Raspberry Pi, saved as index.php in the Document Root of your Apache/other web server. Then you can control the required temperature setting using any iPhone or computer on your network by going to URL:

<IP-ADDRESS-OF-PI>://index.php

看起来像这样:

显然,我不是一个设计酷炫网站的时尚设计师,所以它需要开始"才能看起来不错;-)

Obviously, I am not a trendy designer of cool-looking websites so it will need "tarting up" to look good ;-)

在电影的底部,有一个小的 Python 脚本正在运行 - 观看服务器上的 "setting.txt" 文件(在您的情况下是在 Raspberry Pi 上):

At the bottom of the movie, there is a little Python script running - watching the "setting.txt" file on the server (on the Raspberry Pi in your case):

from time import sleep
while True:
   file = open("setting.txt", "r") 
   print file.read() 
   file.close()
   sleep(1)

这是网页index.php:

<?php
// If we were POSTed, save the value in file on server
if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
   file_put_contents("setting.txt",$_POST['val']);
   return;
}
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Temperature Control</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script>

<?php
   // Get setting from previous run and load into Javascript variable "setting"
   if(!file_exists("setting.txt")){
      $s=10;
   } else {
      $s = file_get_contents("setting.txt");
   }
   printf("var setting=%d;",$s);
?>

  $( function() {
    $( "#slider-vertical" ).slider({
      orientation: "vertical",
      range: "min",
      // Constrain temperature setting to range [10,30]
      min: 10,
      max: 30,
      value: setting,
      change: function( event, ui ) {
        $( "#amount" ).val( ui.value );
        // Send value of slider back to server via AJAX POST
        $.post("index.php",{val:ui.value});
      }
    });
    // Update numeric value displayed on webpage
    $( "#amount" ).val( $( "#slider-vertical" ).slider( "value" ) );
  } );
  </script>
</head>
<body>

<p>
  <label for="amount">Setting:</label>
  <input type="text" id="amount" readonly style="border:0; color:#f6931f; font-weight:bold;">
</p>

<div id="slider-vertical" style="height:200px;"></div>

</body>
</html>

每次滑块移动时,它都会向服务器发送 AJAX POST,将滑块的值保存在名为 "setting.txt" 的文件中.这实际上是由同一个 PHP 接收的(您可以在前 4 行中看到),因此您只需维护一个文件.

Each time the slider moves, it does an AJAX POST back to the server sending the value of the slider to be saved in a file called "setting.txt". That is actually received by the same PHP (you can see that in the first 4 lines) so that you only have one file to maintain.

然后,您的 Python 脚本可以在每次通过其主循环,或时不时地,或在文件更改时(如您所愿)读取该文件中的值集.

Then your Python script can just read the value set from that file each time through its main loop, or every now and then, or when the file changes - as you wish.

这篇关于与 Python 脚本远程交互的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆