【问题标题】:Communicating between Raspberry Pi and Arduino over LAN通过 LAN 在 Raspberry Pi 和 Arduino 之间进行通信
【发布时间】:2015-07-02 20:03:17
【问题描述】:

我正在使用树莓派进行图像处理,我希望它能够通过 LAN 与我的 arduino 通信,以根据树莓派的指令控制光束。我经常看到的唯一事情是 Pi 和 Arduino 之间的直接联系。我可能看起来很幼稚,但我试图让他们使用 Arduino 作为服务器进行通信,使用以太网库进行编程,并通过套接字库将 Raspberry Pi 作为客户端。我在我的路由器上给了他们两个静态 IP,并使用以下代码尝试通过,但是当我的 python 行到达命令通过特定端口连接到 Arduino 的 IP 时,我遇到了socket.error: [Errno 113] No route to host

关于如何更正确地建立这种联系有什么想法吗?我的主要目标是能够通过 LAN 完成此操作,因此 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

阿杜诺:

//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);
}

【问题讨论】:

  • 您是否尝试使用 PC 发送和接收数据只是为了确定问题出在哪里??
  • 我没有,但我最初认为他们的协议可能不兼容或其他什么。您认为我应该能够进行这种直接连接吗?无论如何,我会试试这个并回来

标签: python sockets networking tcp arduino


【解决方案1】:

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

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

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

3) 我的 Arduino 的 loop() 中的 if (client == True) 测试不工作。 R-Pi 可以毫无错误地连接和发送消息,但 Arduino 似乎没有正确响应。一旦我从if 声明中提取所有内容,R-Pi 就开始接收我的消息,我看到所有内容都有响应。这是我的最终代码:

R-Pi:

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")


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

阿杜诺:

#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.
//  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    • 2020-12-27
    相关资源
    最近更新 更多