我有2个Raspberry pi和一台笔记本电脑,试图在以下架构中进行通信。 Rpi-1通过Wifi与Rpi 2通讯 RPi-2然后将数据转发到PC。 Rpi-1充当末端节点(稍后将添加更多) Rpi-2充当数据集中器,聚集来自所有终端节点的数据。 我已经尝试过以这种方式使用TCP客户端服务器代码。 RPi-1的Rpi-1客户端, RPi1的Rpi2服务器和PC的客户端 PC作为RPi2的服务器 这在技术上正确吗?使RPi2成为不同机器的客户端和服务器
我曾经尝试过将RPi 1单独与Rpi 2进行通信,并且将Rpi2仅与PC进行通信。它可以正常工作,但是当我将两者集成时,它并没有向PC发送任何数据。
import socket
host = '172.17.116.207'# ip of the DC
port = 5560
def setupSocket():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
return s
def sendReceive(s, message):
s.send(str.encode(message))
print("Message transmitted")
reply = s.recv(1024)
print("We have received a reply.")
print("Send closing message.")
s.send(str.encode("EXIT"))
s.close()
reply = reply.decode('utf-8')
return reply
def transmit(message):
s = setupSocket()
response = sendReceive(s, message)
return response
#code for RPi1
from time import sleep
from monitor_client import transmit
sleepTime = 20
def PhasorDataMonitor():
message="aa0100341e3644853600000041b10000392b0000e36ace7ce36a31830444000009c4000042c80000447a0000461c40003c12d43f"
print("Transmitting Data.")
response = transmit(message)
print(response)
while True:
PhasorDataMonitor()
sleep(sleepTime)
#code for Rpi2
#Use this script to send and receive data over TCP/IP
import socket
#host_PC = '172.17.116.207'
host_PC = '172.17.116.35'
port = 5561
def setupSocket():
s_PC = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s_PC.connect((host_PC, port))
return s_PC
def sendReceive(s_PC, message):
s_PC.send(str.encode(message))
print("Message transmitted")
reply = s_PC.recv(1024)
print("We have received a reply.")
print("Send closing message.")
s_PC.send(str.encode("EXIT"))
s_PC.close()
reply = reply.decode('utf-8')
return reply
def transmit(message):
s_PC = setupSocket()
response = sendReceive(s_PC, message)
return response
#code in RPi2
import socket
from time import sleep
from monitor_client_DC import transmit
sleepTime = 5
host = '172.17.116.207'
port = 5560
def setupServer():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Socket created.")
try:
s.bind((host, port))
except socket.error as msg:
print(msg)
print("Socket bind complete.")
return s
def setupConnection():
s.listen(1)
conn, address = s.accept()
print("Connected to: " + address[0] + ":" + str(address[1]))
return conn
def dataTransfer(conn):
while True:
data = conn.recv(1024)
data = data.decode('utf-8')
print(data)
reply = 'Closing socket'
conn.sendall(str.encode(reply))
Print("Reply has been send.")
conn.close()enter code here
s = setupServer()
while True:
try:
conn = setupConnection()
dataTransfer(conn)
sleep(sleepTime)
message="aa0100341e3644853600000041b10000392b0000e36ace7ce36a31830444000009c4000042c80000447a0000461c40003c12d43f"
print("Transmitting Data to PC.")
response = transmit(message)
print(response)
sleep(sleepTime)
except:
break
#code in PC
import socket
host = '172.17.116.35'
#host = '192.168.43.199'
port = 5561
def setupServer():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Socket created.")
try:
s.bind((host, port))
except socket.error as msg:
print(msg)
print("Socket bind complete.")
return s
def setupConnection():
s.listen(1)
conn, address = s.accept()
print("Connected to: " + address[0] + ":" + str(address[1]))
return conn
def dataTransfer(conn):
while True:
data = conn.recv(1024)
data = data.decode('utf-8')
print(data)
reply = 'Unknow`enter code here` Command'
conn.sendall(str.encode(reply))
Print("Reply has been send.")
conn.close()
s = setupServer()
while True:
try:
conn = setupConnection()
dataTransfer(conn)
except:
break
我应该在PC上收到消息,但是直到Rpi2为止