0
私はアンドロイドクライアントとPythonサーバーを使用してソケットネットワークアプリケーションを作成しています。クライアント側は2つの値 "1"と "2"を送信するのにほぼ完璧です。サーバー側では、値が "1"に等しいか、サーボが右から左に移動するか、または2に等しい値が左から右に移動するかどうかをチェックする値がサーバーに送られます。コードをご覧ください:制御サーボモータへのTCP接続
# server.py
import socket
import RPi.GPIO as IO
import time
IO.setmode(IO.BOARD)
IO.setup(12,IO.OUT)
pwm2=IO.PWM(12,50)
# create a socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# get local machine name
host = "192.168.1.10"
# bind to the port
s.bind((host, 18050))
# queue up to 5 requests
s.listen(1)
while True:
print("Listenng to the client")
# establish a connection
clientsocket,addr = s.accept()
print("Got a connection from %s" % str(addr))
d=int(clientsocket.recv(1024))
if d==1:
print(d, "right to left")
pwm2.start(12)
time.sleep(3)
pwm2.stop()
elif d==2:
print(d, "left to right")
pwm2.start(2)
time.sleep(3)
pwm2.stop()
IO.cleanup()
s.close()
実装では、奇妙な動作が常に発生します。場合によってはモーターが動かないこともあります。また、右から左、左から右に1回だけ移動します。結果は以下の通りである:?
>>
Listenng to the client
Got a connection from ('192.168.1.9', 1313)
(1, 'right to left') # Movement occurs
Listenng to the client
Got a connection from ('192.168.1.9', 1337)
(2, 'left to right') # Movement occurs
Listenng to the client
Got a connection from ('192.168.1.9', 1383)
(1, 'right to left') # No Movement occurs
Listenng to the client
Got a connection from ('192.168.1.9', 1416)
(2, 'left to right') # No Movement occurs
Listenng to the client
Got a connection from ('192.168.1.9', 1445)
(1, 'right to left') # No Movement occurs
Listenng to the client
Got a connection from ('192.168.1.9', 1528)
(2, 'left to right') # No Movement occurs
Listenng to the client
Got a connection from ('192.168.1.9', 1574)
(1, 'right to left') # No Movement occurs
Listenng to the client
してください任意の提案...全体的なアイデアは、Androidの活動から1または2を送信することにより、サーボを制御することです。あなたが貸し付けるかもしれない助けを前もってありがとう。
ありがとう。私はあなたの助けに感謝します。 2番目の提案は、私が求めているものです。いいぞ! – Hayder