Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 24, 2024, 10:38:06 22:38


Login with username, password and session length


Pages: [1] 2  All
Print
Author Topic: How to connect Ethernet Shield as client with PC, with router in between?  (Read 10079 times)
0 Members and 1 Guest are viewing this topic.
xpress_embedo
Active Member
***
Offline Offline

Posts: 173

Thank You
-Given: 122
-Receive: 254


« on: February 11, 2015, 08:39:38 08:39 »

Hello!! Everyone!

Earlier i had sent data from PC, to Arduino using Ethernet, so i use a Arduino Mega and Ethernet Shield for this purpose, i connected Ardiuino Mega + Ethernet Shield directly with my PC and changed the IP address of my PC as per the server address set in the following sketch, and then used HW Hercules software to communicate with Arduino with PC over Ethernet.
(Post related to this is available at this link, http://www.sonsivri.to/forum/index.php?topic=58953.msg169218#msg169218)

And code is as follow:-
Code:
#include <SPI.h>
#include <Ethernet.h>

#define ETH_CS    10
#define SD_CS  4

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {0xB0,0xCD,0xAE,0x0F,0xDE,0x10};

IPAddress ip(192,168,1,17);

IPAddress server(192,168,1,10);

EthernetClient client;

void setup()
{
  Serial.begin(9600);
  Serial.println("Starting Ethernet Connection");
  pinMode(ETH_CS,OUTPUT);
  pinMode(SD_CS,OUTPUT);
  digitalWrite(ETH_CS,LOW); // Select the Ethernet Module.
  digitalWrite(SD_CS,HIGH); // De-Select the internal SD Card
  if (Ethernet.begin(mac) == 0)  // Start in DHCP Mode
  {
    Serial.println("Failed to configure Ethernet using DHCP, using Static Mode");
    // If DHCP Mode failed, start in Static Mode
    Ethernet.begin(mac, ip);
  }
  Serial.print("My IP address: ");
 
  for (byte thisByte = 0; thisByte < 4; thisByte++)
  {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print(".");
  }
  Serial.println();
 
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 5000))
  {
    Serial.println("connected");
  }
  else
  {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available())
  {
    char c = client.read();
    client.write(c);
    Serial.print(c);
  }

  // as long as there are bytes in the serial queue,
  // read them and send them out the socket if it's open:
  while (Serial.available() > 0)
  {
    char inChar = Serial.read();
    if (client.connected())
    {
      client.print(inChar);
    }
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    // do nothing:
    while(true);
  }
}

But Now the case is little bit different, my Arduino+Ethernet Shield is connected with my PC directly, but there is a router in between it.
How can i receive the data from Arduino Mega on PC and Vice Versa with router in between, my setup is as follow:

Logged
pickit2
Moderator
Hero Member
*****
Online Online

Posts: 4668

Thank You
-Given: 834
-Receive: 4322


There is no evidence that I muted SoNsIvRi


« Reply #1 on: February 11, 2015, 10:25:42 10:25 »

If you add a router between your shield and PC, you need to bridge your PC to router.
Logged

Note: I stoped Muteing bad members OK I now put thier account in sleep mode
xpress_embedo
Active Member
***
Offline Offline

Posts: 173

Thank You
-Given: 122
-Receive: 254


« Reply #2 on: February 11, 2015, 11:46:27 11:46 »

Can you please provide me any link to do this bridging, i am searching on google with the above keywords but doesn't understand much.
Logged
pickit2
Moderator
Hero Member
*****
Online Online

Posts: 4668

Thank You
-Given: 834
-Receive: 4322


There is no evidence that I muted SoNsIvRi


« Reply #3 on: February 11, 2015, 01:16:16 13:16 »

http://windows.microsoft.com/en-gb/windows/create-network-bridge#1TC=windows-7

http://windows7themes.net/en-us/networking-questions-how-to-bridge-connections-in-windows-8-properly/
« Last Edit: February 11, 2015, 01:19:39 13:19 by pickit2 » Logged

Note: I stoped Muteing bad members OK I now put thier account in sleep mode
xpress_embedo
Active Member
***
Offline Offline

Posts: 173

Thank You
-Given: 122
-Receive: 254


« Reply #4 on: February 11, 2015, 03:50:39 15:50 »

As per the links provided by you, the bridge is used to connect two networks, and i don't have two networks, have just one network.

I think i am not able to explain my problem properly.

My PC is connected to a router, and this router is having one input port and 5 output port + Wifi, my PC is connected one one of the output ports.
I had to connect the Ethernet Shield to the router and wants to receive the data coming from Ethernet Shield to PC and from PC to Ethernet Shield.


Logged
kreutz
Active Member
***
Offline Offline

Posts: 183

Thank You
-Given: 994
-Receive: 125


« Reply #5 on: February 11, 2015, 05:21:31 17:21 »

Are you using the same CAT5 cable you used before (when connecting directly the arduino shield to the PC) in this new arrangement? If so, then try a normal patch cable because the original was probably a "crossover" and it isn't going to work on older routers.

note: modified following pickit2 remark for newer routers and pcs automatically adjusting for crossover cables.
« Last Edit: February 11, 2015, 08:23:50 20:23 by kreutz » Logged
pickit2
Moderator
Hero Member
*****
Online Online

Posts: 4668

Thank You
-Given: 834
-Receive: 4322


There is no evidence that I muted SoNsIvRi


« Reply #6 on: February 11, 2015, 07:03:23 19:03 »

then you don't follow what I'm saying.

1. you plug into pc Ethernet and it was working.
2. adding your router it's not working.
3. solution bridge your Ethernet to your router.

but then maybe I'm wrong, but if I remember a project I did about 10 years ago, that's what got it working.

As to the cat5 cable most modern routers don't mind you using a straight through or crossover cable.
Logged

Note: I stoped Muteing bad members OK I now put thier account in sleep mode
metal
Global Moderator
Hero Member
*****
Offline Offline

Posts: 2420

Thank You
-Given: 862
-Receive: 678


Top Topic Starter


« Reply #7 on: February 11, 2015, 07:40:45 19:40 »

you are not wrong.. bridging enables the PC and a device connected to this PC to have the same gateway, use the same DHCP on that router/server. So, instead of you connecting the arduino to the router, you bridge two network interfaces on your PC, and connect the arduino to your PC. Just go to network connections and highlight both network connections, the one conected to your router and the one connected to your arduino, right click and select bridge connections. Once bridge is created, right click on the network interface on your computer that is connected to the router, on sharing tab, check "Allow other users to connect to through this computer's internet connection" and from home networking connection: select network bridge, that's it. This is usually called "MAC miniport" on windows 7
Logged
kreutz
Active Member
***
Offline Offline

Posts: 183

Thank You
-Given: 994
-Receive: 125


« Reply #8 on: February 11, 2015, 08:14:40 20:14 »

Pickit2, Do you mean using "port forwarding" in the router?
Logged
pickit2
Moderator
Hero Member
*****
Online Online

Posts: 4668

Thank You
-Given: 834
-Receive: 4322


There is no evidence that I muted SoNsIvRi


« Reply #9 on: February 11, 2015, 10:00:17 22:00 »

It has nothing to do with port forwarding, Metal nailed it.

All you need is a hint, to where it look, I don't give the answers to problems.
Plus I made a good living by knowing how to do it.
Logged

Note: I stoped Muteing bad members OK I now put thier account in sleep mode
kreutz
Active Member
***
Offline Offline

Posts: 183

Thank You
-Given: 994
-Receive: 125


« Reply #10 on: February 11, 2015, 10:23:40 22:23 »

I know what bridging means, but I can't see how it will help with the current configuration he has.
Using bridging on the PC requires two nic ports (or a nic port and wifi)  and then connecting the arduino ethernet shell to one of those, like it was before he used the router. I am sure that solution is going to work, but what I mean is to use the same configuration as he has now with both, the pc and the arduino shell, connected to their respective router ports and forwarding port 5000 to the arduino shell ip on the router configuration.
 
« Last Edit: February 11, 2015, 10:31:30 22:31 by kreutz » Logged
pickit2
Moderator
Hero Member
*****
Online Online

Posts: 4668

Thank You
-Given: 834
-Receive: 4322


There is no evidence that I muted SoNsIvRi


« Reply #11 on: February 11, 2015, 10:36:41 22:36 »

I know what bridging means, but I can't see how it will help with the current configuration he has.
Using bridging requires two nic ports (or a nic port and wifi) on the PC
That's it, before he plugged into the pc, and now wants to add a Wi-Fi, Router/Server or other device.
he most likely wants to use a port on the router or even use Wi-Fi link.
so you need to bridge so it looks to the system he is plugging into the original port on the pc.

Now that's a very common network solution, I have 4 Wi-Fi routers linked this way.
Logged

Note: I stoped Muteing bad members OK I now put thier account in sleep mode
kreutz
Active Member
***
Offline Offline

Posts: 183

Thank You
-Given: 994
-Receive: 125


« Reply #12 on: February 11, 2015, 10:48:53 22:48 »

I agree your way it is going to work and is really simple to implement but, on reply #4, xpress_embedo is asking for help finding a solution with the network configuration he shows on the attached drawing.
Logged
pickit2
Moderator
Hero Member
*****
Online Online

Posts: 4668

Thank You
-Given: 834
-Receive: 4322


There is no evidence that I muted SoNsIvRi


« Reply #13 on: February 11, 2015, 11:17:44 23:17 »

Yes that's still a true statement, in his first post, he had wi-fi device in the same configuration.
The problem he is facing is simple, he has added a router to the pc, removing the option to use that port.

Answers relate to that fact. and don't cost anything, to implement.


Logged

Note: I stoped Muteing bad members OK I now put thier account in sleep mode
h0nk
Senior Member
****
Offline Offline

Posts: 257

Thank You
-Given: 226
-Receive: 231



« Reply #14 on: February 11, 2015, 11:55:37 23:55 »


Hello xpress_embedo,

You should check the connection speed of Your ethernet shield at Your router.
I would assume that the shield want to work as 10baseT-Device.
When autonegotion fails, no connect would be possible, until You are able to
configure this port manually as 10baseT.

Best Regards
Logged
kreutz
Active Member
***
Offline Offline

Posts: 183

Thank You
-Given: 994
-Receive: 125


« Reply #15 on: February 12, 2015, 02:29:16 02:29 »

I don't know which version of the shield he has but the one I found on the web has 10/100 capability and auto-negociates.
Logged
robotai
Junior Member
**
Offline Offline

Posts: 60

Thank You
-Given: 27
-Receive: 23


« Reply #16 on: February 12, 2015, 04:07:50 04:07 »

@xpress_embedo:
  Just to confirm if your Arduino and PC both use the "static" IPs? It seems your code on Arduino includes DHCP mode. So on "Cable Direct Link" setup, both will use "static" IP. However, on connecting through router, Arduino should retrieve IP address from DHCP Server on Router and the IP would very possibly not the same sub-net with your PC. Cause connection setup unsuccessful.
Logged
xpress_embedo
Active Member
***
Offline Offline

Posts: 173

Thank You
-Given: 122
-Receive: 254


« Reply #17 on: February 12, 2015, 05:24:04 05:24 »

Are you using the same CAT5 cable you used before (when connecting directly the arduino shield to the PC) in this new arrangement? If so, then try a normal patch cable because the original was probably a "crossover" and it isn't going to work on older routers.

note: modified following pickit2 remark for newer routers and pcs automatically adjusting for crossover cables.
The cable i am using comes with the router, and initially i though when connecting ethernet shield with PC, it is not supposed to work, but i works, then i came to know that modern PC can do, cross-over internally, and i had recently buy this, so it is working when connecting directly.
And when connecting this with router using same cable, then an IP address is assigned to Ethernet Shield, so i think cable connections are Ok.



Posted on: February 12, 2015, 05:56:27 05:56 - Automerged



then you don't follow what I'm saying.

1. you plug into pc Ethernet and it was working.
2. adding your router it's not working.
3. solution bridge your Ethernet to your router.

but then maybe I'm wrong, but if I remember a project I did about 10 years ago, that's what got it working.

As to the cat5 cable most modern routers don't mind you using a straight through or crossover cable.

I did exactly, but the problem is that, there is only network available in my network and sharing center and when i right click on it and pressed the bridge connection, i get an error, showing for bridging, two connections are required.


Posted on: February 12, 2015, 05:58:16 05:58 - Automerged

you are not wrong.. bridging enables the PC and a device connected to this PC to have the same gateway, use the same DHCP on that router/server. So, instead of you connecting the arduino to the router, you bridge two network interfaces on your PC, and connect the arduino to your PC. Just go to network connections and highlight both network connections, the one conected to your router and the one connected to your arduino, right click and select bridge connections. Once bridge is created, right click on the network interface on your computer that is connected to the router, on sharing tab, check "Allow other users to connect to through this computer's internet connection" and from home networking connection: select network bridge, that's it. This is usually called "MAC miniport" on windows 7

There is only one network available in my control panel, i don't how i will get two networks as i have only one.

Posted on: February 12, 2015, 06:05:02 06:05 - Automerged


I know what bridging means, but I can't see how it will help with the current configuration he has.
Using bridging on the PC requires two nic ports (or a nic port and wifi)  and then connecting the arduino ethernet shell to one of those, like it was before he used the router. I am sure that solution is going to work, but what I mean is to use the same configuration as he has now with both, the pc and the arduino shell, connected to their respective router ports and forwarding port 5000 to the arduino shell ip on the router configuration.
 

You get it right, i don't have two NIC ports and my PC doesn't have wifi, Router is connected to external network and then through router a CAT5 cable is connected to my PC, and from the same router a CAT5 cable is connected to router, so there is no possibility of bridging.
Port forwarding was the term i initially think.
And i use Wifi to use internet on mobile and tab.

Posted on: February 12, 2015, 06:08:49 06:08 - Automerged

Hello xpress_embedo,

You should check the connection speed of Your ethernet shield at Your router.
I would assume that the shield want to work as 10baseT-Device.
When autonegotion fails, no connect would be possible, until You are able to
configure this port manually as 10baseT.

Best Regards


I will check this and reply here, but i don't think that this is a problem.
But let me check first

Posted on: February 12, 2015, 06:14:19 06:14 - Automerged


@xpress_embedo:
  Just to confirm if your Arduino and PC both use the "static" IPs? It seems your code on Arduino includes DHCP mode. So on "Cable Direct Link" setup, both will use "static" IP. However, on connecting through router, Arduino should retrieve IP address from DHCP Server on Router and the IP would very possibly not the same sub-net with your PC. Cause connection setup unsuccessful.

Both are using DHCP, my PC as well as Arduino Ethernet Shield, and IP's to both are assigned dynamically.
I connected my Arduino Ethernet Shield with router and print the IP address assigned to it by DHCP and if it fails then i had written a static IP address to run in Static Mode.
But when connected to router, and IP Address is assigned to Ethernet Shield successfully.

Posted on: February 12, 2015, 06:18:09 06:18 - Automerged

Actually i am doing a project, in which Arduino collects the sensor data and on request from Serial Port, it send data to Serial Port, i want to access this data remotely from any PC connected to the same network like the configuration shown in my Post #1 and Post #4.
I did it successfully by connecting Arduino Ethernet Shield with my PC NIC port, but due to this i will not be access internet on PC, because i just have only one NIC port, if somehow, router forwards those message from my PC to Ethernet Shield and from Ethernet Shield to my PC, then it will be of great help.
Thank you guys, for your efforts and suggestion you are providing.
Logged
robotai
Junior Member
**
Offline Offline

Posts: 60

Thank You
-Given: 27
-Receive: 23


« Reply #18 on: February 12, 2015, 08:25:44 08:25 »

If I didn't miss your point, you need to set your Arduino in "Server Mode" with "static" IP address. Your PCs can retrieve IP addresses from DHCP server. Then all PCs can connect to Arduino as "client" without any problem.

If both PCs and Arduino are running in dynamic IP addresses, you will need to implement "broadcast" mechanism so PCs and Arduino can "find" each other (or through dynamic DNS way)
Logged
xpress_embedo
Active Member
***
Offline Offline

Posts: 173

Thank You
-Given: 122
-Receive: 254


« Reply #19 on: February 12, 2015, 12:00:14 12:00 »

If I didn't miss your point, you need to set your Arduino in "Server Mode" with "static" IP address. Your PCs can retrieve IP addresses from DHCP server. Then all PCs can connect to Arduino as "client" without any problem.

If both PCs and Arduino are running in dynamic IP addresses, you will need to implement "broadcast" mechanism so PCs and Arduino can "find" each other (or through dynamic DNS way)

Hi robotai,
Arduino Ethernet Shield must work in client mode not as server, and then router will assign Ip address to it, which is working properly, means my router is assigning a IP to my Arduino Ethernet Shield, which is a client.
My router has also assigned Ip address to my PC using DHCP.
You can check the program written above in my first post, in that i had used my Ethernet Shield as client.

What is this broadcast mechanism??
I thought this might be quite simple to do, as Ethernet basically means remote access, where one can access the data remotely, i am getting data on my PC from Arduino when connected directly as client but don't getting how it can receive data when both are connected to router.
Logged
kreutz
Active Member
***
Offline Offline

Posts: 183

Thank You
-Given: 994
-Receive: 125


« Reply #20 on: February 12, 2015, 01:29:01 13:29 »

Can you ping your shield from your pc? also, are you using the ip address and port number when you try to reach your shield from the pc? (for instance 192,168,1,17:5000 )

Posted on: February 12, 2015, 02:05:47 14:05 - Automerged

I guess you are a little confused about the "web client" and "web server" issue. It doesn't have anything to do with DHCP and assigning or not a dynamic ip address. You use your shield as a "web client" when you access a web server (from the shield) over the network and retrieve information from a webpage. On the contrary, you use a shield as a "web server" when you want a PC to retrieve information (such as voltages, temperatures, etc) from your arduino. If you want to use your arduino as a client having a dynamic ip is not an issue, but it is when you use the shield as a web server, in this case you need to have a fixed ip because you need to know your shield's ip in order to retrieve the information from the pc, and it doesn't help if that ip is changing due to DHCP assigning a new ip eventually.
Logged
xpress_embedo
Active Member
***
Offline Offline

Posts: 173

Thank You
-Given: 122
-Receive: 254


« Reply #21 on: February 12, 2015, 03:57:27 15:57 »

Hi!! Thanks to everyone.

It just started working, let me share what i had done.

First of all i open the command prompt and by typing the command ipconfig, i noted my IP address of PC, which is assigned by my Router.
It is as follow:
Ip->192.168.0.102
Subnet->255.255.255.0
Gateway->192.168.0.1
Then i checked the IP address assigned to my Ethernet Shield, by Router, it is 192.168.0.101

I did nothing after this, i just opened my Sketch and in place of Server IP address i write 192.168.0.102, which is IP address assigned to my PC by Router.

Then upload the sketch and just opened the 5000 port in HW Hercules software, within a second i got a message 192.168.0.101 connected and now i am able to successfully send and receive data from my PC to Ethernet Shield and from Ethernet Shield to PC both are connected to router.

I also checked ping 192.168.0.101 -t from command line and i get proper response for this.

With this setup, i am able to use internet on PC, as well as receive Messages from Ethernet Shield.
But as IP has been assigned dynamically to PC, it can change at any time or in the next cycle and no one will check it again and again, and re-upload the Server IP address in Arduino every-time.
So i did one more test, i noted down the Ip assigned to PC and set that manually in Network Configuration.
By doing this, my PC has static Ip address.
Now Ethernet Shield and my PC are working properly, but internet on PC is not working.
Though its not an issue, i just want to get Ethernet Shield and PC communicating with each other, both connected to router.

« Last Edit: February 12, 2015, 04:00:59 16:00 by xpress_embedo » Logged
kreutz
Active Member
***
Offline Offline

Posts: 183

Thank You
-Given: 994
-Receive: 125


« Reply #22 on: February 12, 2015, 06:29:57 18:29 »

In order for the pc to connect to internet you need to assign also the DNS server ip in the tcp properties, since you are now not receiving it from the DHCP server (router).
Logged
pickit2
Moderator
Hero Member
*****
Online Online

Posts: 4668

Thank You
-Given: 834
-Receive: 4322


There is no evidence that I muted SoNsIvRi


« Reply #23 on: February 13, 2015, 12:14:08 00:14 »

...
Logged

Note: I stoped Muteing bad members OK I now put thier account in sleep mode
robotai
Junior Member
**
Offline Offline

Posts: 60

Thank You
-Given: 27
-Receive: 23


« Reply #24 on: February 13, 2015, 03:04:48 03:04 »

Hi robotai,
Arduino Ethernet Shield must work in client mode not as server, and then router will assign Ip address to it, which is working properly, means my router is assigning a IP to my Arduino Ethernet Shield, which is a client.

I think you confused with DHCP Server-Client and Application Server-Client. Hope below wiki provide a little explanation
http://en.wikipedia.org/wiki/Client%E2%80%93server_model

If keep using your current architecture. Your major issue is only "How Arduino knows PC IP address is?" when PC is varying.

My suggestion is to implement Arduino in "Server" mode. Seek for Ethernet "Server" class on Arduino API, you should have a clue on what may be. Hope this help.
Logged
Pages: [1] 2  All
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC