通过LAN在Raspberry Pi和Arduino之间进行通信

Dav*_*ran 3 python sockets networking tcp arduino

我正在用覆盆子pi进行图像处理,我希望它能够通过LAN与我的arduino交谈,根据pi的指令操纵光束.我见过的唯一的事情是Pi和Arduino之间的直接联系.这对我来说似乎很幼稚,但我试图让他们使用Arduino作为服务器进行通信,使用以太网库进行编程,并通过套接字库将Raspberry Pi作为客户端进行通信.我在路由器上给了他们两个静态IP,并使用下面的代码尝试通过,socket.error: [Errno 113] No route to host但当我的python线来到命令通过特定端口连接到Arduino的IP时,我遇到了.

关于如何更正确地进行此连接的任何想法?我的主要目标是能够通过局域网进行此操作,因此USB连接和直接I2C连接没有帮助,尽管他们肯定会完成工作.

树莓派:

from socket import *
import select

data = None

timeout = 3 # timeout in seconds
msg = "test"

host = "192.168.1.113"
print ("Connecting to " + host)

port = 23

s = socket(AF_INET, SOCK_STREAM)
print "Socket made"

ready = select.select([s],[],[],timeout)


s.connect((host,port))
print("Connection made")

while True:

    if data != None:
        print("[INFO] Sending message...")
        s.sendall(msg)
        data = None
        print("[INFO] Message sent.")
        continue

    if ready[0]:        #if data is actually available for you
        data = s.recv(4096)
        print("[INFO] Data received")
        print data
        continue
Run Code Online (Sandbox Code Playgroud)

Arduino的:

//90-a2-da-0f-25-E7
byte mac[] = {0x90, 0xA2, 0xDA, 0x0f, 0x25, 0xE7};

//ip Address for shield
byte ip[] = {192,168,1,113};

//Use port 23
EthernetServer server = EthernetServer(23);

void setup() {
  //init device
  Ethernet.begin(mac,ip);
  //start listening for clients
  server.begin();

  Serial.begin(9600);     //For visual feedback on what's going on
  while(!Serial){
    ;   //cause Leonardo
  }
  delay(1000);
  if(server.available()){
  Serial.write("Client available");
}
}

void loop() {
  // put your main code here, to run repeatedly:
  EthernetClient client = server.available();
    if (client == true){
      Serial.write("Client Connected.");

      if(client.read() > 0){
        Serial.write(client.read());        
      }
      else{
        server.write("ping");
      }
    }
    else{
      Serial.println("No one's here yet...");
    }
    delay(1500);
}
Run Code Online (Sandbox Code Playgroud)

Dav*_*ran 6

在挖掘之后,我找到了我的解决方案,以及更多细节.所以R-Pi和Arduino都可以做TCP,我发现了我的错误来源.

1)我相信我在Arduino代码中的server.begin()之后设置的延迟给了我错误,因为它暂停了程序,因此可能会暂停监听过程.

2)我的R-Pi客户端代码中的while循环给了我一个管道错误.

3)if (client == True)我的Arduino测试loop()不起作用.R-Pi可以连接并发送没有错误的消息,但Arduino似乎没有正确响应.一旦我从if声明中删除了所有内容,R-Pi就开始接收我的消息了,我看到一切都响应了.这是我的最终代码:

R-Pi:来自套接字导入*导入选择

data = None

timeout = 3 # timeout in seconds
msg = "test"

host = "192.168.1.113"
print ("Connecting to " + host)

port = 23

s = socket(AF_INET, SOCK_STREAM)
print "Socket made"

ready = select.select([s],[],[],timeout)


s.connect((host,port))
print("Connection made")


if ready[0]:        #if data is actually available for you
    print("[INFO] Sending message...")
    s.sendall(msg)
    print("[INFO] Message sent.")

    data = s.recv(4096)
    print("[INFO] Data received")
    print data
Run Code Online (Sandbox Code Playgroud)

Arduino的:

#include <SPI.h>
#include <Ethernet.h>

//90-a2-da-0f-25-E7
byte mac[] = {0x90, 0xA2, 0xDA, 0x0f, 0x25, 0xE7};

//ip Address for shield
byte ip[] = {192,168,1,113};

//Use port 23 for telnet
EthernetServer server = EthernetServer(23);

void setup() {
  Serial.begin(9600);     //For visual feedback on what's going on
  while(!Serial){
    ;   //wait for serial to connect -- needed by Leonardo
  }

  Ethernet.begin(mac,ip); // init EthernetShield
  delay(1000);

  server.begin();
  if(server.available()){
    Serial.println("Client available");
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  EthernetClient client = server.available();
  message = client.read();

  server.write(message);
  server.write("Testu ");
  Serial.println(message);

//  if (client == true){                    <----- This check screwed it up. Not needed.
//    Serial.println("Client Connected.");
//    server.write(client.read());        //send back to the client whatever     the client sent to us.
//  }
}
Run Code Online (Sandbox Code Playgroud)