Set Static IP for Arduino Mega

Hello,

I have a Mega and the ENC2860.

I need to set a static IP to the Arduino so I can ping it on the network.

I have tried the following but I still can not ping 192.168.1.200 using terminal

#include <EtherCard.h>
#include "ModbusIP_ENC28J60.h"
  // ethernet interface ip address
  static byte myip[] = { 192,168,1,200 };
  // gateway ip address 192.168.1.200 
  static byte gwip[] = { 192,168,1,1 };
  ether.staticSetup(myip, gwip);

Any insight would be amazing, I have to deliver this in the next 72 hours.

Matt

It depends on your router.
Sometimes during testing, the router gets suspicious and blocks the IP.
I always set the Arduino to DHCP and set a fixed IP address in the router.

How do you know if your local IP is accepted by the router ?
Is that really the gateway IP ?

If you are using Windows, then request the gateway and find a free local IP. Perhaps a command window with 'ipconfig /all'.

When you take your project with you and connect it to another router, then it might not work if the router has other settings.

Hello,

there is no router, I literally have the ethernet plugged into my computer and am trying to ping it or change it, nothing I do seems to make a difference.

I did plug it into my laptop and it shows as another IP but still that same generic 169.254.x.x

It is possible: Direct connection between laptop and arduino via ethernet - Stack Overflow.
I don't know how the ENC2860 library should be used for that.
Some old computers might need a Ethernet crossover cable. I thought that was something from the past, but perhaps your computer needs it.

There is a similar question at the moment here (with a W5100): Direct connection from Arduino to computer with ethernet shield - Programming Questions - Arduino Forum.

You have not posted all your code, but in the small amount you have posted I don't see a call to ether.begin which I believe should come before ether.staticSetup.

Koepel & CountryPaul,

Thanks for the input, I am going to make a new cable and try it out then post my code.

I appreciate your input dearly.

Matt

Hello Again,

So, I have this huge sketch and yes I just tossed those lines in the first post in the sketch hoping that would do the trick. I was wrong.

So I want to start very basically, I have the below sketch. I uploaded it to the arduino, connected the crossover cable from the Enc to the back of my computer and in the terminal, I ran 'ipconfig getifaddr en0' the result was '169.254.16.93'.

I am sure you look at this and think 'duh' but honestly I am lost, tried for days, and can't wrap my head around what is going on.

Should I ping my computer and try to get the netmask, dns and gw?

#include <EtherCard.h>      // https://github.com/jcw/ethercard]
byte Ethernet::buffer[700];

void setup () {
  Serial.begin(9600);
  const static uint8_t myip[] = {192,168,0,100}; // Example Supplied
  const static uint8_t gwip[] = {192,168,0,254}; // Example Supplied
  const static uint8_t dnsip[] = {192,168,0,1};  // Example Supplied
  const static uint8_t netmask[] = {192,168,0,1};  // Example Supplied
  ether.staticSetup(myip, gwip, dnsip, netmask);
}

void loop () {}

My first reaction is that your PC is using 169.254.16.93 as the ip address for Ethernet - though I am unsure that the "getifaddr en0" means if you are using windows - what OS are you using on the PC?

If the PC Ethernet address is 169.254.16.93 and assuming it has a netmask of 255.255.255.0 other ip addresses on ethernet will need to be 169.254.16.nn in order to communicate with it. Alternatively, set the ip address on your PC to 192.168.0.nn where nn is not used elsewhere 200 might be suitable.

Thank you for the explanation, so to clarify I can not set it to a totally random IP, it has to be a variation of what the local network is set to?

I am sorry for my ignorance on this.

FYI

PING 169.254.16.200 (169.254.16.200): 56 data bytes
Request timeout for icmp_seq 0
Request timeout for icmp_seq 1
Request timeout for icmp_seq 2
#include <EtherCard.h>      // https://github.com/jcw/ethercard]
byte Ethernet::buffer[700];

void setup () {
  Serial.begin(9600);
  const static uint8_t myip[] = {169,254,16,200}; // Suggested
  const static uint8_t gwip[] = {169,254,16,0}; // GW?
  const static uint8_t dnsip[] = {};  // Not sure what to do here
  const static uint8_t netmask[] = {255,255,0,0};  // From Comp
  ether.staticSetup(myip, gwip, dnsip, netmask);
}

void loop () {}

You still don't appear to have a ether.begin() before you call ether.staticSetup. Not sure whether using a blank dnsip will cause problems, you could use the PC ip. Using an address ending in .0 may cause problems (as can an ip address ending in 255) this is because they are often used as network identifier and broadcast address respectively so are best avoided in this case - they often work without problems, but it just add an additional possible problem to this configuration. The address 169.254.x.x is an automatic private address range and indicates a problem - usually caused by the computer trying to use DHCP but with no DHCP server accessible. I think it would be safer to use 192.168.0.n address and use 192.168.0.93 for the PC (you will have to set it manually) and 192.168.0.200 for your arduino and 192.168.0.254 for the gateway.

Very often routers (gateways) are given the address ending in .254., Often routers for home use contain dns servers so again use 192.168.0.254 for the dnsip.

Hello,
In case you have only your Arduino hardware cabled to the port of your pc and nothing else (even no Wifi active), then you need to set you PC to a fixed IP address and you need to set your Arduino to another fixed ip adress. Then you set both network masks to the same value and you are done.
From then on you should be able to ping your Arduino from your PC. And if it does not work, well try disabling virus scanners, firewalls etc.

However: if you have WiFi on an other NIC in your pc: make sure your networks are separate ones (IP anded with Mask should be different).

If you are connected to a (HOME) network, you should be able to ping from your pc to your router. And you should be able to identify a free IP address, then you should be able to set your Arduino to this free address.

I have written some software to look at Arduino variables over ethernet from the PC. The tutorial to use this software explains in detail how to set up your Arduino (fixed IP) with a PC (variable IP) to make ping work. Good luck.

WawiLib getting started ethernet

Best regards Johi.

Thank you Johi,

Can I achieve that with this sample code by simply changing the IP address?

#include <EtherCard.h>      // https://github.com/jcw/ethercard]
byte Ethernet::buffer[700];

void setup () {
  Serial.begin(9600);

  const static uint8_t myip[] = {169,254,16,200}; // ?
  const static uint8_t gwip[] = {169,254,16,0}; // ?
  const static uint8_t dnsip[] = {169,254,16,0};  // ?
  const static uint8_t netmask[] = {255,255,0,0};  // ?
  ether.staticSetup(myip, gwip, dnsip, netmask);
}

void loop () {}

Screenshot for reference, note that I have changed the network to the actual settings I will be using.

Are there any led lights on the Ethernet interfaces that indicate whether there is an live circuit or not? Many switches and ethernet interfaces show no lights if there is no connection but a led if there is a connection, often different colour leds for different speeds. You may need to plug both the PC (Mac) and the Arduino into a switch just to check the Ethernet connection.
Does the same thing happen with the laptop as with your mac?
Can you try more than one ethernet cable (just in case)?

Yes the lights are active on both ends.

Tried with crossover and straight cables.

Is that with the Arduino & Mac, and also with Arduino and Laptop?

Yes, Mac and Laptop and even the Motorola iNET II.

None of your code includes a call to ethernet.begin that I can see, so in theory you are not actually starting the Ethernet communications on your Arduino.

I think the best option is to try another sketch to see if you can get ping working.
There is a simple example in the link below, you will obviously have to change the ip address to one suitable for your network - probably the one you have used in your own sketch.

Arduino ENC28J60 Ethernet Module - Ping Test from PC to Arduino | Hands-On Embedded

Thank you for that, even with that tut I get nowhere, what the heck.