Send Data to and from Computer over Ethernet

jakerake

New member
Hey everyone. First off, this is my first project using any sort of networking so apologies if I am missing some basic things. I have a project where I would like to send and receive data over ethernet between my Teensy and my computer.

I am using a Teensy 4.1 with the native ethernet kit. This connects to my computer with an ethernet cable. My teensy is running a script using the NativeEthernet library, and my computer is running a Python program using the Sockets library. The code for both of these is pasted below. When I try to run these programs (I start by uploading/running the teensy script, then after a few seconds running the python script) the teensy never connects to my computer (server.available() stays at 0), and my python program times out when trying to connect (gives the error: Exception has occurred: TimeoutError [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond). The light on the teensy ethernet port does flash/light up green so am not sure what that indicates.

I am not sure what in the programs or setup is wrong or even really how I can begin to help troubleshoot it. My only idea is that the Python program is looking at my laptop's wifi adapter and not the ethernet port but I am not sure.

Thanks, and let me know if there is any more info I can provide!

Python Script (Running on computer)
Code:
import socket
import netifaces as ni
import time

# Teensy ip and port
HOST = "10.0.0.16"  # The server's hostname or IP address
PORT = 1234  # The port used by the server

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    # s.bind((eth_ip, PORT)) # try to use bind to make sure using ethernet port, but this did not work
    print("attempting connecting")
    s.connect((HOST, PORT))
    print("connected!")
    s.sendall(b"Hello, world\n")
    data = s.recv(PORT)

print(f"Received {data!r}")



Teensy Script
Code:
#include <NativeEthernet.h>

byte mac[] = {0x04, 0xe9, 0xe5, 0x14, 0x8f, 0x36}; // this is what the teensy is, but also found automatically in function below
byte ip[] = { 10, 0, 0, 16 };

// Port number for the communication
unsigned int port = 1234;
EthernetServer server(port);

// Initialize the Ethernet client
EthernetClient client;

void setup() {
  Serial.begin(9600); // open Serial port
  while (!Serial) {
    ; //wait for Serial window to open
  }
  
  // get mac address
  teensyMAC(mac);

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip); // use this if default

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet hardware not found.  Sorry, can't run without hardware. :(");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }

  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());

  delay(1000);
}

void loop() {
  // Wait for a client to connect
  EthernetClient client = server.available();
  Serial.println(client);
  // Check if a client has connected
  if (client) {
    // Read data from the client
    while (client.connected()) {
      if (client.available()) {
        // Read the data from the client
        String data = client.readStringUntil('\n');
        Serial.print("Client sent this signal: ");
        
        // Print the received data
        Serial.println(data);
      }
    }
  }
}


void teensyMAC(uint8_t *mac) {
    for(uint8_t by=0; by<2; by++) mac[by]=(HW_OCOTP_MAC1 >> ((1-by)*8)) & 0xFF;
    for(uint8_t by=0; by<4; by++) mac[by+2]=(HW_OCOTP_MAC0 >> ((3-by)*8)) & 0xFF;
    Serial.printf("MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}
 
Back
Top