Teensy 4.1 Static IP Address

Status
Not open for further replies.
From the NativeEthernet example "AdvancedChatServer":

Code:
#include <SPI.h>
#include <NativeEthernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network.
// gateway and subnet are optional:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);
IPAddress myDns(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 0, 0);


// telnet defaults to port 23
EthernetServer server(23);

EthernetClient clients[8];

void setup() {
  // You can use Ethernet.init(pin) to configure the CS pin
  //Ethernet.init(10);  // Most Arduino shields
  //Ethernet.init(5);   // MKR ETH shield
  //Ethernet.init(0);   // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit Featherwing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit Featherwing Ethernet

  // initialize the Ethernet device
  Ethernet.begin(mac, ip, myDns, gateway, subnet);

Hope that helps . . .

Mark J Culross
KD5RXT
 
Thank you Mark!

I’ve still got a few questions from this code now:

is the byte_mac the mac address of the teensy?

if so, how do you get the mac address from the teensy?

is the "IPAddress ip" the address that you are trying to set the teensy to?

Thank you!
 
Thank you Mark!

I’ve still got a few questions from this code now:

is the byte_mac the mac address of the teensy?

if so, how do you get the mac address from the teensy?

is the "IPAddress ip" the address that you are trying to set the teensy to?

Thank you!

It's been awhile since I've done much of anything with the Teensy & ethernet (too busy playing with my TeensyMIDIPolySynth synthesizer !!), but <here's> a reference that shows how to read the MAC address which is uniquely burned into each Teensy. And, yes, I do believe that the IP address used in the function call is the one that you would like to set/use.

Good luck & have fun !!

Mark J Culross
KD5RXT
 
Thank you Mark and TeensyWolf!

I believe I was able to get my Teensy's MAC address.

Does someone mind explaining more about what the following variables are in the code shared by Mark?

IPAddress ip(192, 168, 1, 177);
IPAddress myDns(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 0, 0);
 
Thank you Mark and TeensyWolf!

I believe I was able to get my Teensy's MAC address.

Does someone mind explaining more about what the following variables are in the code shared by Mark?

IPAddress ip(192, 168, 1, 177);
IPAddress myDns(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 0, 0);

If I understand your question correctly, the short answer is as follows:

IPAddress ip( [ whatever IP address that you want your Teensy to respond to / act as on your network ] );
IPAddress myDns( [ the IP address of whatever DNS server device that you want your Teensy to send DNS queries to - typically your network router ] );
IPAddress gateway( [ the the IP address of whatever device that you want your Teensy to send routing requests to for addresses that your Teensy does not itself already know how to route ] );
IPAddress subnet( [ the netmask of the network that your Teensy sits within / on ] );

If you have another device (e.g. PC) on the same network that you intend to put the Teensy on, you should be able to get at least the "gateway" & "subnet" values from that other device, & possibly the "myDNS" value. The "ip" value for the Teensy should be unique & different from any other device on the same network.

Hope that helps & if I answered the wrong question, please ask again !!

Mark J Culross
KD5RXT
 
That helps a lot! Thank you very much Mark :)

The other device that I am intending to connect my Teensy to is a Raspberry Pi 4B. I will look into getting those "gateway" and "subnet" values from the Raspberry Pi - if anyone has any tips for that, I would love to hear them!

Cheers,
Hannah
 
Useful commands to get network info on/from a RPi

That helps a lot! Thank you very much Mark :)

The other device that I am intending to connect my Teensy to is a Raspberry Pi 4B. I will look into getting those "gateway" and "subnet" values from the Raspberry Pi - if anyone has any tips for that, I would love to hear them!

Cheers,
Hannah

For the RPi (& most other linux devices), simply typing "ifconfig" without the quotes in a terminal window will show you lots of network interface information (if you get a "not found" error, try "/sbin/ifconfig" instead). For a wired LAN, your expected output should look something like this:

Code:
pi@testingHost:~ $ ifconfig
eth0      Link encap:Ethernet  HWaddr b8:27:eb:42:52:85  
          inet addr:192.168.0.9  Bcast:192.168.0.127  Mask:255.255.255.128
          inet6 addr: fe80::7dc1:32ab:b2e0:23c1/64 Scope:Link
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:218837 errors:0 dropped:0 overruns:0 frame:0
          TX packets:127796 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:21571976 (20.5 MiB)  TX bytes:15916499 (15.1 MiB)

- the "inet addr:192.168.0.9" shows the IP address of the RPi that you are checking on
- the "Mask:255.255.255.128" shows you the netmask in use
- if you are connecting via WiFi, you should look at the information for the "wlan0" (wireless) network interface instead of the "eth0" (wired) network interface.


Similarly, typing "cat /etc/resolv.conf" (which translates to "show me the contents of the /etc/resolv.conf file") will show you information for any DNS server(s) in use. Your expected output should look something like this:

Code:
pi@testingHost:~ $ cat /etc/resolv.conf 
# Generated by resolvconf
nameserver 192.168.0.1

- this shows that DNS requests will be forwarded to 192.168.0.1 (which in my case is the IP address of my home router).



Similarly, typing "route" will show how traffic will be routed out of the RPi that you are checking on. Your expected output will look something like this:

Code:
pi@testingHost:~ $ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.0.1     0.0.0.0         UG    0      0        0 wlan0
192.168.0.0     *               255.255.255.128 U     0      0        0 wlan0

- the first line (starts with "defualt") shows that any traffic that my RPi does not already know how to route for itself will be forwarded to "192.168.0.1" (again, my home router) as its "gateway". This line also indicates that this gateway can be reached by using the "wlan0" (wireless) interface.
- notice that the second line once again shows the netmask (255.255.255.128) being used on this network interface.

Hope this also helps to clarify some of what you are looking for. Feel free to ask any other questions which may come to mind.

Mark J Culross
KD5RXT
 
Status
Not open for further replies.
Back
Top