2

We are trying to use Ethernet communication in order to have multiple Arduinos (acting as servers) push alerts to a Raspberry Pi acting as a client) using socket programming. We have the following code but it only prints data1 after some alert is triggered in data2, whereas it should be pushing the alert regardless of whether it is the first or second Arduino sending it an alert. We cannot figure out why this prioritization is occurring or how to fix it.

We are using the following Python code on the Raspberry Pi.

from socket import *
import select
data = None 
timeout = 3 # timeout in seconds
host1 = "192.168.1.101"
host2 = "192.168.1.102"
print ("Connecting to " + host1)
print ("Connecting to " + host2)
port1 = 5000
port2=65432
s1 = socket(AF_INET, SOCK_STREAM)
s2 = socket(AF_INET, SOCK_STREAM)
print ("Socket made")
ready1 = select.select([s1],[],[],timeout)
ready2 = select.select([s2],[],[],timeout)
s1.connect((host1,port1))
s2.connect((host2,port2))
print ("Connection made to " + host1)
print ("Connection made to " + host2)
while(1):
    print("Data received")
    data1=s1.recv(4096)
    data2=s2.recv(4096)
    if (len(data2)>0 or len(data1)>0):
        print(data1)
        print(data2)
maria khan
  • 31
  • 1
  • You should check your `ready1` and `ready2` variables before trying to fetch the data. See https://stackoverflow.com/a/2721734/2015686 – chill0r Mar 20 '22 at 13:46
  • Can you pleasd elaborate further on how we can check the ready variables. We tried to check ready1[0] and ready2[0] before receivinf and printing data accordingly but there is no response. – maria khan Apr 04 '22 at 22:04

0 Answers0