问题
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)?
Maybe a simple webpage hosted on the Pi somehow with buttons that control the variables?
Thanks
EDIT: 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.
回答1:
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
It looks like this:
Obviously, I am not a trendy designer of cool-looking websites so it will need "tarting up" to look good ;-)
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)
Here is the web page 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>
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.
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.
回答2:
Your Question has very less information... Based on the Assumption you have python script remote.py and need to pass the values to the variable, You can do it with ssh command in python
import os
os.system ('ssh remoteuser at remote python remote.py arg1 "arg 2 has spaces"
arg3')
and if you want to use Web API calls please refer the below link, How to use Web APIs in pyton3
回答3:
Actually, you may want to check for new value for your variable every time script is run - so keep it in an accesible location on remote computer.
e.g - 1) If you share it in a file in shared folder:
use python script from raspberry-pi to read file on remote computer and get variable value.
2) If you have web server on remote computer - i.e. you make a request and get value of variable as response.
Use below python code:
import requests
response = requests.post("http://remote-pc/varvalues",{"get_value_for_variable":'var-name'})
print(response.content)
回答4:
I recommend you edit the variables in your python script by SSHing to your raspberry pi from another computer and then editing the variable in a terminal based editor like nano
or vim
. Here's how you can do this:
First
Obtain the local IP address of your raspberry pi and run the following command on a computer that is on the same network. For example:
$ ssh brian@10.0.1.164
Assuming the raspberry pi has an IP address of 10.0.1.164
and the raspberry pi also has a user named brian
. If you don't know the pi's IP address, run sudo ifconfig
on the pi and get the number next to wlan0 (if you are using wifi the number should start with 10.0
or 192.168
). You will then be prompted to enter the user's password.
Second
Once you are connected via ssh, open the python file you want with nano
or vim
(command line-based text editors) with:
nano /users/brian/Documents/python-project/myfile.py
Where /users/brian/Documents/python-project/myfile.py
is the location of the file on your raspberry pi.
That's the simplest way to get started, of course you could build a more complicated web app to accomplish this somehow, but it is better to start with something that is quick and easy.
回答5:
Here's another way, using Redis which is a very fast, very simple "data structure server". It can serve strings, integers, atomic integers, lists, queues, hashes, sets, sorted sets and so on and there are clients for C, C++, PHP, Python, the command-line etc.
So, if you install Redis on your Raspberry Pi, and start it up with systemctl
, you can set and get values. So, let's run a Python script that loops around checking the temperature setting every second:
#!/usr/local/bin/python3
import redis
from time import sleep
# Connect to local Redis server
con = redis.Redis("localhost")
while True:
print(con.get("setting"))
sleep(1)
Then, on any other machine on the network, you can just use the command line client to set a value for the setting
string, e.g:
redis-cli -h <IP-ADDRESS-OF-PI> <<< "SET setting 31"
Here is an animation of it in action. The top half would be your control program running on the Raspberry Pi, getting the temperature each time through its loop. The bottom half is any other machine on the network, just setting the required temperature at the command line:
Of course, you could also transfer data the other way - from Pi to network clients - by storing the actual, measured temperature on the Pi in another variable (SET actual 23
) and getting it on your clients with "GET actual"
.
来源:https://stackoverflow.com/questions/47919009/simplest-way-to-remotely-interact-with-python-script