Can't seem to send data to MKR1000 properly

I’m trying to have a bidirectional communication between MKR1000 and Python from my host computer.

Code from MKR1000:

#include <WiFi101.h>
#include <SPI.h>

// To connect to the server on laptop
char ssid[] = "id";
char pass[] = "pass";
int status = WL_IDLE_STATUS;

IPAddress server(172,20,10,3);
WiFiClient client;


// Random variable
int a, i, j, k;
byte buf0[7];
byte buf1[7];

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial1.begin(115200);

  pinMode(LED_BUILTIN, OUTPUT);
  
  status = WiFi.begin(ssid, pass);
  if (status != WL_CONNECTED) {
    while (status != WL_CONNECTED) {
      status = WiFi.begin(ssid, pass);
    }
  } else  {
    j = client.connect(server, 1234);
    while (j != 1)  {
      j = client.connect(server, 1234);
    }
  }
}

void loop()
{
	if (client.available()) {
		a = client.read();

		if (a == 0)	{ // Send new target to Due
      j = 0;
			while(j<6)	{
				if (client.available()>0)	{
					buf0[j] = client.read();
					j++;
				}
			}

      Serial1.write((byte) 0);
      // Send coordinate back to Due
      for (i = 0; i<6; i++) {
        Serial1.write(buf0[i]);
      }

		} else if (a == 1)	{
      // Get the coordinate from the Due
      Serial1.write((byte) 1);
      k = 0;
      while(k < 6) {
        if (Serial1.available() > 0)  {
          buf1[k] = Serial1.read();
          k++;
        }      
      }

			for (i=0;i<6;i++)	{
				client.write(buf1[i]);
			}

      client.write((byte) 0);  // fill in the blank size
      client.write((byte) 0);

		} else if (a == 2) { // identify yourself
      Serial.println("Identify myself.");
      client.write((byte) 1);
		}
	}
}

Code from Python:

import socket
import time

def coor2bytes(coor_fnc):
	coorByte = [0, 0, 0, 0, 0, 0]

	if (coor_fnc[0] >= 0):
		coorByte[0] = (coor_fnc[0] >> 8) & 0xFF # High byte of X
		coorByte[1] = coor_fnc[0] & 0xFF # Low byte of X
	else:
		coor_fnc[0] = coor_fnc[0]*(-1)
		coorByte[0] = (coor_fnc[0] >> 8) & 0xFF # High byte of X
		coorByte[0] = coorByte[0] ^ 0x80
		coorByte[1] = coor_fnc[0] & 0xFF # Low byte of X
	
	if (coor_fnc[1] >= 0):
		coorByte[2] = (coor_fnc[1] >> 8) & 0xFF # High byte of Y
		coorByte[3] = coor_fnc[1] & 0xFF # Low byte of Y
	else:
		coor_fnc[1] = coor_fnc[1]*(-1)
		coorByte[2] = (coor_fnc[1] >> 8) & 0xFF # High byte of X
		coorByte[2] = coorByte[2] ^ 0x80
		coorByte[3] = coor_fnc[1] & 0xFF # Low byte of X
		
	if (coor_fnc[2] >= 0):
		coorByte[4] = (coor_fnc[2] >> 8) & 0xFF # High byte of Phi
		coorByte[5] = coor_fnc[2] & 0xFF # Low byte of Phi
	else:
		coor_fnc[2] = coor_fnc[2]*(-1)
		coorByte[4] = (coor_fnc[2] >> 8) & 0xFF # High byte of Phi
		coorByte[4] = coorByte[4] ^ 0x80
		coorByte[5] = coor_fnc[2] & 0xFF # Low byte of Phi

	return coorByte

def bytes2coor(byte_fnc):
	receivedCoor_fnc = [0, 0, 0]

	receivedCoor_fnc[0] = ((-1)**(byte_fnc[0]>>7)) * ((byte_fnc[1]) | (((byte_fnc[0]&0x7f)<<8)))
	receivedCoor_fnc[1] = ((-1)**(byte_fnc[2]>>7)) * ((byte_fnc[3]) | (((byte_fnc[2]&0x7f)<<8)))
	receivedCoor_fnc[2] = ((-1)**(byte_fnc[4]>>7)) * ((byte_fnc[5]) | (((byte_fnc[4]&0x7f)<<8)))

	return receivedCoor_fnc


if __name__ == '__main__':
	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)


	s.bind((socket.gethostname(), 1234)) # bind(ip, port)
	print("Done binding.")
	s.listen(2)

	clientsocket, address = s.accept()
	print(f"Connection from {address} has been established!")
	# time.sleep(5)

	print("Ask for ID.")

	# Identify the client
	client_id = -1
	while(client_id == -1):
		clientsocket.send(bytes([2]))
		print("I asked.")
		client_id = clientsocket.recv(8)
		client_id = int.from_bytes(client_id, 'little')


	if client_id == 1:
		print("This is Alpha.")
	else:
		print("I have no idea who this is.")
		print("The id is: " + str(client_id))

	while True:
		print();
		print("What you want to do?")
		print("0. Send target")
		print("1. Get current coordinate")
		print("2. Set current coordinate (not yet implement)")
		try:
			a = int(input("I choose: "))
		except Exception:
			print("Error.")
			a = -1;

		if (a == 0):
			coor = [0, 0, 0]
			try:
				coor[0] = int(input("X: "))
				coor[1] = -int(input("y: "))
				coor[2] = int(input("phi: "))

				coorByte = coor2bytes(coor)

			except Exception:
					print("Error.")

			clientsocket.send(bytes([0]))
			clientsocket.send(bytes(coorByte))
			print("I already sent the target.")

		elif (a == 1):
			clientsocket.send(bytes([1]))
			bytesReceived = []
			full_msg = []

			while (len(full_msg) < 8):
				bytesReceived = clientsocket.recv(8)
				for x in range(len(bytesReceived)):
					full_msg.append(bytesReceived[x])
			
			receivedCoor = bytes2coor(full_msg)
			print("coordinate received: " + str(receivedCoor))

		else:
			print("Not yet implement.")

I know it’s a lot of code but really you can just have a look at the MKR code and this particular section on the Python:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)


	s.bind((socket.gethostname(), 1234)) # bind(ip, port)
	print("Done binding.")
	s.listen(2)

	clientsocket, address = s.accept()
	print(f"Connection from {address} has been established!")
	# time.sleep(5)

	print("Ask for ID.")

	# Identify the client
	client_id = -1
	while(client_id == -1):
		clientsocket.send(bytes([2]))
		print("I asked.")
		client_id = clientsocket.recv(8)
		client_id = int.from_bytes(client_id, 'little')

For some reason, I can’t seem to get the MKR response back, UNLESS I turn on the MKR first, wait for 1 or 2 seconds, and then run the Python script.

Any insight?

More testing lead me to believe that the MKR did actually response back to my command but Python failed to pick it up.