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

时间:2015-07-02 20:03:17

标签: python sockets networking tcp arduino

我正在使用覆盆子pi进行图像处理,我希望它能够通过LAN与我的arduino交谈,以根据pi的指令操纵光束。我经常看到的唯一事情是Pi和Arduino之间的直接联系。这对我来说似乎很幼稚,但我试图让他们使用Arduino作为服务器进行通信,使用以太网库进行编程,并通过套接字库将Raspberry Pi作为客户端进行通信。我在路由器上给了他们两个静态IP,并使用下面的代码尝试通过,但当我的python线来到命令连接到Arduino时,我遇到INSERT INTO table2 (name, param, xvalue, yvalue) SELECT 'John', 'age', child, adult FROM table1 WHERE name = 'John'; &#39 ; s通过特定端口的IP。

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

Raspberry Pi:

socket.error: [Errno 113] No route to host

Arduino的:

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

1 个答案:

答案 0 :(得分:6)

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

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

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

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

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

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

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