Arduino Nano + ENC28J60 + Wireless Network

Hey guys,

I'm not an advanced Arduino programmer, but right now I'm working on a project where I'm setting up a WLAN where the clients in the network are Arduino Nano V3s connected to the ENC28J60 Ethernet module. The ethernet module is connected to a wireless router, and this 'construction' is placed 3 times in the WLAN.

I started already with testing out if I could send data to another client in the network (laptop for testing purposes), which failed. I'm using the EtherCard library, and I don't really know what too account for when sending data using the Arduino.

Has anyone got any experience with this, and if so could you please reply with a code script where data is being send over WLAN using Static IP-adresses for the Arduino. Also a question that popped into my head, when trying to send data to another Arduino, does it have to be send to the IP-adress of the wireless router or the IP of the Arduino (Ethernet shield).

Any help or suggestions would be superb!

Kind regards!
Dries

The IP of the Arduino.
Do you use different mac addresses for everything ?
What does the serial monitor say for the IP address ?

Peter_n:
The IP of the Arduino.
Do you use different mac addresses for everything ?
What does the serial monitor say for the IP address ?

Normally I would use a different IP for every Arduino because a MAC-address is in theory 'unique'. Don't really know since I've got no experience with the Arduino and ENC28J60.

What should I print on the serial monitor to test? Just send the IP-address to the serial monitor?

#include <EtherCard.h>

// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
static byte myip[] = { 192,168,1,203 };

byte Ethernet::buffer[500];
BufferFiller bfill;

void setup () {
  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
    Serial.println( "Failed to access Ethernet controller");
  ether.staticSetup(myip);
}

Source: http://www.instructables.com/id/Add-Ethernet-to-any-Arduino-project-for-less-than-/step3/Arduino-Code/

My code has been modified a little and it's full of errors so I deleted it and decided to start fresh since it was a mess.
So the code must be that I send for example a character and a boolean to a laptop or another Arduino client. I will try to test-write some code in a couple of minutes and think about it and upload the code to this topic too.
By the way the idea to use this Wi-Fi came from: http://www.instructables.com/id/Cheap-and-Easy-Arduino-Wi-Fi-Hack/

Regards,
Dries

Yes, just send the IP to the serial monitor.
You can add DHCP to that EtherCard example, have a look at the DHCP example and modify your sketch.
You must use different mac addresses for everything,
With the same mac address, the router thinks it is the same client and gets confused.

Peter_n:
Yes, just send the IP to the serial monitor.
You can add DHCP to that EtherCard example, have a look at the DHCP example and modify your sketch.
You must use different mac addresses for everything,
With the same mac address, the router thinks it is the same client and gets confused.

Yeah I guessed as much conserning the router. I don't really need to use the DHCP-code I think since I need tp have the Arduinos to have a static IP, and a central router sets up te network so my guess is that it confirms the static IP of the Ethernet shield.

Problem I can't figure out: since the router connected to the Arduino is set up to be in client mode, and since it has an IP,is this going to be a problem? When I would send data to the Arduino, which IP do I send it to? The router or the Ethernet shield?

Regards,
Dries

When you are within the network of the router, you use the IP of the Arduino.
Can you log into the router and see all the clients ?
It would be for example something like this:
192,168,1,200 : computer
192.168.1.201 : other computer
192.168.1.203 : arduino 1
192.168.1.204 : arduino 2

If you are using the computer (at 192.168.1.200) you type in the browser: http: //192.168.1.203
However, if you are outside you home network, for example on the internet, you have to open a port in the router. In that case you can use the IP of your internet connection and let the router translate a specific port to the right local IP number.
http://whatismyipaddress.com/

You have a router that is a client ?
It depends on how you have set it up. Does that router have a DHCP server ?
Is your primary network 192.168.0.xxx ? and your local network after the router is 192.168.1.xxx ?
Or is also your primary network also 192.168.1.xxx ?

The easiest way is if your router does not use its DHCP server and also your primary network is 192.168.1.xxx.
In that case your primary network and the local network are fully transparant and the primary router (the one 'above' your router) handles the IP numbers also for the Arduino.

I have a router after a router after a router myself. My second router makes its own network with a DHCP server enabled, and a computer on the first network can not reach my Arduino. I have to configure the second router to make it possible to pass it to reach my second network. I have wireless devices on my second network, but also wireless routers as clients. They don't use DHCP, they just act as pass through wireless clients. So an Arduino connected to the second network has the same IP number as when it is connected to a wireless client device.
It's getting confusing, can you make a drawing of your network, with the IP numbers ?

Peter_n:
When you are within the network of the router, you use the IP of the Arduino.
Can you log into the router and see all the clients ?
It would be for example something like this:
192,168,1,200 : computer
192.168.1.201 : other computer
192.168.1.203 : arduino 1
192.168.1.204 : arduino 2

If you are using the computer (at 192.168.1.200) you type in the browser: http: //192.168.1.203
However, if you are outside you home network, for example on the internet, you have to open a port in the router. In that case you can use the IP of your internet connection and let the router translate a specific port to the right local IP number.
http://whatismyipaddress.com/

You have a router that is a client ?
It depends on how you have set it up. Does that router have a DHCP server ?
Is your primary network 192.168.0.xxx ? and your local network after the router is 192.168.1.xxx ?
Or is also your primary network also 192.168.1.xxx ?

The easiest way is if your router does not use its DHCP server and also your primary network is 192.168.1.xxx.
In that case your primary network and the local network are fully transparant and the primary router (the one 'above' your router) handles the IP numbers also for the Arduino.

I have a router after a router after a router myself. My second router makes its own network with a DHCP server enabled, and a computer on the first network can not reach my Arduino. I have to configure the second router to make it possible to pass it to reach my second network. I have wireless devices on my second network, but also wireless routers as clients. They don't use DHCP, they just act as pass through wireless clients. So an Arduino connected to the second network has the same IP number as when it is connected to a wireless client device.
It's getting confusing, can you make a drawing of your network, with the IP numbers ?

Yeah sorry it's not super clear to me as well since I've got not experience with this.
So this is what I think that my network setup should be:

So up top I have 3 Arduino Nanos with the ethernet shields that are connected to the wireless routers. These routers are right now modified/set up as clients in the network. This is what I meant when I said that they were clients. (Does this have to change?)
In the middle I've gotten 2 laptops where a program is going to be running where I do some processing on the data that the Arduinos are sending.
Below there is a wireless router which creates the wireless LAN where the other routers (clients) connect to.

Connection from the outside is not necessary since it's only private. No internet connection is needed or available for now, so it's just a local WLAN with only connection to those who are in it.

I hope this doesn't confuse you anymore, and please do correct anything if I'm wrong! :slight_smile:

Regards,
Dries

Thanks for the drawing !
You have local (wireless) network at 192.168.0.xxx
And everything is within that local network.
The Arduino boards and the laptop are clients of the router.

Addressing the Arduino from the laptop is by addressing IP number 192.168.0.104 from the laptop.
Since it is a local network, all clients can connect to each other.

There is one small thing: If you use a static IP address of 192.168.0.104, the router must allow that. If for example the router has given that address to something else in the past, it might not accept it. You should be able to log into the router and see the clients, you should see the Arduino IP numbers there.

Thanks for the help so far! It has helped me think about things more clearly! :slight_smile:

I've been thinking, since we give the ethernet shield an IP, in what configuration must I set up the router (the one who allows the Arduino to go wireless)?
Different modes:

  • Access Point: instantly adds a Wi-Fi hotspot to existing wired network ( -> not this I believe)
  • Client Mode (TV/Game Console Adapter): Gives wired-only devices access to an existing Wi-Fi network ( -> Currently set up like this)
  • Router Mode: Creates an instant private wireless network and share internet to multiple Wi-Fi devices, suitable for most hotel and home networks. ( -> not this I believe)
  • Repeater Mode: Extends existing Wi-Fi, improving signal strenght and coverage
  • Bridge Mode: Use host wireless network to establish a guest network with a different password

The thing is that I'm doubting that I should put it in Bridge mode, since the client itself is the ethernet module and not the router.

Regards,
Dries

The main router provides the wireless AP (Access Point), that is your wifi network.

The rest are clients.
So the 'router' which is connected to the Arduino should be set as client.
I have them as 'dummy' transparent clients, without DHCP server, without other things. I select "client", let them connect to the SSID of the wifi network, and fill in the password of the wifi network.
You may have to try a few things. In my situation the Arduino itself determines its IP number on the wifi network, and not the "router-as-client". I like that situation, since I can exchange the routers and Arduinos, and every Arduino keeps its own IP number.
However the "router-as-client" does have a settings page and that settings page has also a IP number.
I have a list to keep track of all my local IP numbers, and always select a new unused one. By now that list is 20 numbers long.

I have used this "router as client" with a few brands and with the manufacturers firmware and custom firmware.

I see :slight_smile:

Now I'm trying out simple code just for giving the Ethernet module an IP, and connecting it directly with a home router via cable (where my desktop is working from, so with internet connection and other clients). This would be so I could simply ping with my desktop to the Arduino, but unfortunately it isn't working.
This is my current code:

// Present a "Will be back soon web page", as stand-in webserver.
// 2011-01-30 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php
 
#include <EtherCard.h>

#define STATIC 0  // set to 1 to disable DHCP (adjust myip/gwip values below)

#if STATIC
// ethernet interface ip address
static byte myip[] = { 192,168,0,240 };
// gateway ip address
static byte gwip[] = { 192,168,0,1 };
#endif

// ethernet mac address - must be unique on your network
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

byte Ethernet::buffer[500]; // tcp/ip send and receive buffer

char page[] PROGMEM =
"HTTP/1.0 503 Service Unavailable\r\n"
"Content-Type: text/html\r\n"
"Retry-After: 600\r\n"
"\r\n"
"<html>"
  "<head><title>"
    "Testing website"
  "</title></head>"
  "<body>"
    "<h3>This is the Arduino</h3>"
    "<p><em>"
      "please work :)
"
      
    "</em></p>"
  "</body>"
"</html>"
;

void setup(){
  Serial.begin(57600);
  Serial.println("\n[backSoon]");
  
  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) 
    Serial.println( "Failed to access Ethernet controller");
#if STATIC
  ether.staticSetup(myip, gwip);
#else
  if (!ether.dhcpSetup())
    Serial.println("DHCP failed");
#endif

  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);  
  ether.printIp("DNS: ", ether.dnsip);  
}

void loop(){
  // wait for an incoming TCP packet, but ignore its contents
  if (ether.packetLoop(ether.packetReceive())) {
    memcpy_P(ether.tcpOffset(), page, sizeof page);
    ether.httpServerReply(sizeof page - 1);
  }
}

And I try to ping to 192.168.0.240 but it's unreachable.
Also my desktop has the IP: 192.168.135 so it's in the same range.

Regards,
Dries

I don't know.
Some routers have a limited range. For example from xxx.xxx.xxx.100 to xxx.xxx.xxx.200.
Can you try with DHCP ? That should show valid numbers for the gateway and the local ip.

Quick question, would the example "getDHCPandDNS" do this?

Yes, I think so. I have not used that myself, but it seems pretty straightforward.

Hey,
So after a while I figured out and got it working to give the Arduino a static IP and was able to ping to it's IP while it was connected to the client-router.
Code:

#include <EtherCard.h>
static byte mymac[] = {0xDD,0xDD,0xDD,0x00,0x00,0x01};
static byte myip[] = {192,168,0,240};
byte Ethernet::buffer[700];
 
void setup () {
 
  Serial.begin(57600);
  Serial.println("PING Demo");
 
  if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0) 
    Serial.println( "Failed to access Ethernet controller");
 
  if (!ether.staticSetup(myip))
    Serial.println("Failed to set IP address");
}
 
void loop() {
 
  ether.packetLoop(ether.packetReceive());  
  
}

I've been searching on how to send data over it, and came across UDP, any thoughts on different communication protocols?
It's not much data that needs to be send, but I can't really get it to work right now. The receiver must receive the data in VB.net, so it can be processed and send back.
I found a tutorial on youtube for simple UDP packet transmitter and receiver, but can't receive anything. link: Visual Basic How To: Simple UDP - YouTube
I've tried some code in Arduino, but it's all messed up again thanks to many modifications and frankly I don't think it's going to work with my current Arduino code. I've used the ether.sendUdp function, but can't receive any message in VB.net. http://jeelabs.net/pub/docs/ethercard/index.html
I've also installed Wireshark, and started filtering for 'UDP only', but no messages came through with the correct sender or destination IP (so all the UDP messages were from the router or other network material).

Please do link me good tutorials or recommend using a other/better communication protocol.

Hope my current situation is explained clear enough to understand :stuck_out_tongue:

Regards,
Dries

I have not used VB.NET and also have not used UDP with the EtherCard library, so I think I can't help you.

When testing something, create a new sketch to test that. If everything is working, combine it. You can add new files to the Arduino IDE, use the drop-down button on the right.

The normal situation would be this: Two Arduino boards connected to the same router. Both with the official W5100 ethernet shield. When data has to be transferred, and a few misses are okay, the UDP is right protocol for that. UDP is for example also used to get the time from a NTP server.

Perhaps you could try the HTTP protocol, or find a good and working project of EtherCard and UDP.

Hi there,

I've been messing around with UDP but can't really get it to work :open_mouth: So I've been thinking about setting up a webpage on each on my Nano's "in the field". And I'm guessing it's possible to give the elements IDs on the webpage, and try to get the data from the pages using the IDs.

Do you think this is possible or are you thinking of a problem?

Regards,
Dries