Communication between Raspberry Pi and Arduino via LAN

I am doing image processing with a raspberry pi and I want it to be able to talk to my arduino over the LAN to control the light beam based on pi instructions. The only thing I've seen a lot are direct connections between Pi and Arduin. This may seem naive to me, but I tried to communicate with them using Arduino as a server, programmed with the Ethernet library, and a Raspberry Pi as a client via the socket library. I gave them a static IP on my router and used the following code to try to get through, but I was greeted socket.error: [Errno 113] No route to host

when my python line came to the command to connect to the Arduino IP on a specific port.

Any ideas on how I could make this connection more correct? The main goal for me is to do this over LAN, so USB connections and direct I2C connections don't help, although they would definitely get the job done.

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

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

      

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 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 here yet...");
    }
    delay(1500);
}

      

+3


source to share


1 answer


After digging around, I found my solution and a few more details. This way both the R-Pi and Arduino can do TCP and I found the source of my errors.

1) I suppose the delay I set after server.begin () in my Arduino code gave me an error as it pauses the program execution and probably pauses the listening process.

2) The while loop in my R-Pi client code was giving me a broken pipe error.

3) The test if (client == True)

in my Arduino loop()

didn't work. The R-Pi was able to connect and send messages without error, but the Arduino didn't seem to respond as expected. As soon as I pulled everything out of the instructions if

, the R-Pi started getting my messages and I saw how everything responded. Here's my final code:



R-R: 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

      

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

      

+6


source







All Articles