Teensy 4.1 Ethernet

Status
Not open for further replies.

caliope

Member
I have Ethernet hardware functioning on Teensy 4.1, using NativeEthernet.h.

Webserver example yields the following output:

Code:
Ethernet WebServer Example
using HW_OCOTP_MAC* - see https://forum.pjrc.com/threads/57595-Serial-amp-MAC-Address-Teensy-4-0
MAC: 04:e9:e5:0b:c2:21
IP Address 10.22.1.164
10.22.1.164
server is at 10.22.1.164

My hope is to communicate to the Teensy through my LAN using a .NET application.

Code:
           TcpClient client = new TcpClient("10.22.1.164", 20);
            NetworkStream stream = client.GetStream();
            Byte[] data = new Byte[1024];
            String responseData = String.Empty;
            Int32 bytes;
            while (true)
            {
                bytes = stream.Read(data, 0, data.Length);
                if (bytes > 0)
                {
                    responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                    Console.WriteLine("Received: {0}", responseData);
                }
            }

Although I have the Teensy now connected to LAN, I can neither ping the Teensy server nor find it with the .Net code.

Any Pointers would be appreciated.
thanks
 
ping should work, you may need to configure netmask and broadcast and router IP. also your .Net example uses port 20, but WebServer will be on port 80
 
Changed from WebServer to Web Client, DHCP assigned by Router, and works well.

Code:
Initialize Ethernet with DHCP:
using HW_OCOTP_MAC* - see https://forum.pjrc.com/threads/57595-Serial-amp-MAC-Address-Teensy-4-0
MAC: 04:e9:e5:0b:c2:21
  DHCP assigned IP 192.168.0.3
connecting to www.google.com...
connected to 172.217.14.196

disconnecting.
Received 280677 bytes in 6.1474, rate = 45.66 kbytes/second


next task is to broadcast sensor data from Teensy over LAN to Listening devices.
 
Cant seem to crack this one:

.Net indicates we are connected to the Teensy yet we do not receive acknowledgement from Teensy that there is a connection.

Code:
void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();

  if (client == true) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
...

variable "client" never goes true.

c# code
Code:
        private void Form1_Load(object sender, EventArgs e)
        {
            TcpClient client = new TcpClient("192.168.0.164", 80);
            bool con = client.Connected;
            string message = "GET / HTTP/1.1";
            Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
            NetworkStream stream = client.GetStream();

            stream.Write(data, 0, data.Length);
            //Byte[] data = new Byte[1024];
            String responseData = String.Empty;
            int bytes;
            while (true)
            {
                bytes = stream.Read(data, 0, data.Length);
                if (bytes > 0)
                {
                    responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                    Console.WriteLine("Received: {0}", responseData);
                }
            }
            stream.Close();
            client.Close();

        }

to completely confuse me Arduino states the server.available() will always evaluate false if no client has data available for reading. https://www.arduino.cc/en/Reference/ServerAvailable

Assume the client is expected to send a request string. Thats all I can think of.

thanks
 
server.available() returns a pointer or NULL, not a boolean, so your test should be if (client) { ....

see examples/ in NativeEthernet lib or read more closely the URL in the previous post
 
Last edited:
Status
Not open for further replies.
Back
Top