Python 使用Raspberry Pi 3 B+;
我目前正在使用Raspberry Pi 3 B+和Android应用程序支持构建一个自动垃圾箱,其中我将使用伺服电机作为盖子的执行器,并将Android应用程序作为无线遥控的一种形式。一切都进行得很顺利,直到我遇到一个问题,即每当我尝试按下Android应用程序上的按钮时,Python shell程序在测试期间都会出错。我使用了一个参考视频()并彻底跟踪了所有内容,直到我遇到了这个障碍 对我来说,不断出现的结果是:Python 使用Raspberry Pi 3 B+;,python,raspberry-pi,servo,Python,Raspberry Pi,Servo,我目前正在使用Raspberry Pi 3 B+和Android应用程序支持构建一个自动垃圾箱,其中我将使用伺服电机作为盖子的执行器,并将Android应用程序作为无线遥控的一种形式。一切都进行得很顺利,直到我遇到一个问题,即每当我尝试按下Android应用程序上的按钮时,Python shell程序在测试期间都会出错。我使用了一个参考视频()并彻底跟踪了所有内容,直到我遇到了这个障碍 对我来说,不断出现的结果是: Waiting for connection ...connected fro
Waiting for connection
...connected from :
其中,根据参考视频,假设结果为:
Waiting for connection
...connected from : ('192.168.1.70', 11937)
Increase: 2.5
如您所见,IP地址、端口和“增加”文本没有出现,这意味着代码有问题
根据观看视频的人的一些评论,这段代码已经过时,使用的是Python2,我们现在的最新版本是Python3,我们需要在条件中使用“.encode()”行。然而,作为一个Python新手,我恐怕还不具备将此应用于代码的知识
以下是视频中使用的代码:
导入伺服电机
从套接字导入*
不时地
将RPi.GPIO导入为GPIO
伺服电机设置()
ctrCmd=['Up','Down']
主机=“”
端口=21567
BUFSIZE=1024
地址=(主机,端口)
tcpSerSock=套接字(AF\u INET,SOCK\u STREAM)
tcpSerSock.bind(地址)
听一听(5)
尽管如此:
打印“等待连接”
tcpCliSock,addr=tcpSerSock.accept()
打印“…连接自:”,地址
尝试:
尽管如此:
数据=“”
数据=tcpCliSock.recv(BUFSIZE)
如果没有数据:
打破
如果数据==ctrCmd[0]:
伺服电机
打印“增加:”,Servomotor.cur\u X
如果数据==ctrCmd[1]:
伺服电机
打印“减少:”,Servomotor.cur_X
除键盘中断外:
伺服电机。关闭()
GPIO.cleanup()
tcpSerSock.close()代码>以下是一个版本,为了更清晰和更好的实践,对其进行了少量编辑:
import Servomotor
import RPi.GPIO as GPIO
import socket
# Setup the motor
Servomotor.setup()
# Declare the host address constant - this will be used to connect to Raspberry Pi
# First values is IP - here localhost, second value is the port
HOST_ADDRESS = ('0.0.0.0', 21567)
# Declare the buffer constant to control receiving the data
BUFFER_SIZE = 4096
# Declare possible commands
commands = 'Up', 'Down'
# Create a socket (pair of IP and port) object and bind it to the Raspberry Pi address
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(HOST_ADDRESS)
# Set the socket to listen to an incoming connection (1 at a time)
server_socket.listen(1)
# Never stop the server once it's running
while True:
# Inform that the server is waiting for a connection
print("Waiting for connection to the following address: {}...".format(HOST_ADDRESS))
# Perform a blocking accept operation to wait for a client connection
client_socket, client_address = server_socket.accept()
# Inform that the client is connected
print("Client with an address {} connected".format(client_address))
# Keep exchanging data
while True:
try:
# Receive the data (blocking receive)
data = client_socket.recv(BUFFER_SIZE)
# If 0-byte was received, close the connection
if not data:
break
# Attempt to decode the data received (decode bytes into utf-8 formatted string)
try:
data = data.decode("utf-8").strip()
except UnicodeDecodeError:
# Ignore data that is not unicode-encoded
data = None
# At this stage data is correctly received and formatted, so check if a command was received
if data == commands[0]:
Servomotor.ServoUp()
print("Increase: {}".format(Servomotor.cur_X))
elif data == commands[1]:
Servomotor.ServoDown()
print("Decrease: {}".format(Servomotor.cur_X))
elif data:
print("Received invalid data: {}".format(data))
# Handle possible errors
except ConnectionResetError:
break
except ConnectionAbortedError:
break
except KeyboardInterrupt:
break
# Cleanup
Servomotor.close()
GPIO.cleanup()
client_socket.close()
# Inform that the connection is closed
print("Client with an address {} disconnected.".format(client_address))
为了向您展示实际的代码,我在我的机器上托管了一个本地服务器,并使用Putty连接到它。以下是我输入的命令:
以下是服务器的输出(我已将伺服相关函数替换为打印语句):
以下是一个版本,为了更清晰和更好的实践,对其进行了少量编辑:
import Servomotor
import RPi.GPIO as GPIO
import socket
# Setup the motor
Servomotor.setup()
# Declare the host address constant - this will be used to connect to Raspberry Pi
# First values is IP - here localhost, second value is the port
HOST_ADDRESS = ('0.0.0.0', 21567)
# Declare the buffer constant to control receiving the data
BUFFER_SIZE = 4096
# Declare possible commands
commands = 'Up', 'Down'
# Create a socket (pair of IP and port) object and bind it to the Raspberry Pi address
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(HOST_ADDRESS)
# Set the socket to listen to an incoming connection (1 at a time)
server_socket.listen(1)
# Never stop the server once it's running
while True:
# Inform that the server is waiting for a connection
print("Waiting for connection to the following address: {}...".format(HOST_ADDRESS))
# Perform a blocking accept operation to wait for a client connection
client_socket, client_address = server_socket.accept()
# Inform that the client is connected
print("Client with an address {} connected".format(client_address))
# Keep exchanging data
while True:
try:
# Receive the data (blocking receive)
data = client_socket.recv(BUFFER_SIZE)
# If 0-byte was received, close the connection
if not data:
break
# Attempt to decode the data received (decode bytes into utf-8 formatted string)
try:
data = data.decode("utf-8").strip()
except UnicodeDecodeError:
# Ignore data that is not unicode-encoded
data = None
# At this stage data is correctly received and formatted, so check if a command was received
if data == commands[0]:
Servomotor.ServoUp()
print("Increase: {}".format(Servomotor.cur_X))
elif data == commands[1]:
Servomotor.ServoDown()
print("Decrease: {}".format(Servomotor.cur_X))
elif data:
print("Received invalid data: {}".format(data))
# Handle possible errors
except ConnectionResetError:
break
except ConnectionAbortedError:
break
except KeyboardInterrupt:
break
# Cleanup
Servomotor.close()
GPIO.cleanup()
client_socket.close()
# Inform that the connection is closed
print("Client with an address {} disconnected.".format(client_address))
为了向您展示实际的代码,我在我的机器上托管了一个本地服务器,并使用Putty连接到它。以下是我输入的命令:
以下是服务器的输出(我已将伺服相关函数替换为打印语句):
你知道你在Raspberry Pi上使用的是哪种Python版本吗?是的,先生。在我的Raspberry Pi中是Python 3.5.3。你知道你在Raspberry Pi上使用的是哪一个Python版本吗?是的,先生。在我的Raspberry Pi中是Python 3.5.3。