Go Down

Topic: using ethernet to replace the bridge in the bridge example (Read 338 times) previous topic - next topic

rcvolley

so I'm trying to replace the bridge with eithernet to try and get the arduino to be able to interact with another network if its connected through the eithernet port I'm able to ping the arduino but when I try to type in the ip adress on the correct wireless network nothing happenes.

this is my full code it works if I replce everything eithernet with the bridge library I'll also post that code below

Code: [Select]
#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <Console.h>


byte mac[] = {0x90, 0xA2, 0xDA, 0xF8, 0x23, 0x02};
IPAddress dnServer(155, 101, 100, 10);
IPAddress gateway(155, 101, 101, 1);
IPAddress ip(155, 101, 101, 51);
EthernetServer server(80);

unsigned long value;
int pin;
int onoff;
unsigned long value2;
int pin2;
int onoff2;
EthernetClient client;
void setup() {
  Ethernet.begin(mac, ip, dnServer, gateway);
  server.begin();
  Console.begin();

}

void loop() {
  
  if (client) {
    process(client);
    client.stop();
  }
  analogWrite(pin, value);
  analogWrite(pin2, value2);
  if (onoff == 1) {
    digitalWrite (4, HIGH);
  }
  else {
    digitalWrite (4, LOW);
  }
  if (onoff2 == 1) {
    digitalWrite (2, HIGH);
  }
  else {
    digitalWrite (2, LOW);
  }
}
void process(EthernetClient client) {

  String command = client.readStringUntil('/');
  if (command == "analog") {
    analogCommand(client);
  }
}
void analogCommand(EthernetClient client) {
  pin = client.parseInt();
  if (client.read() == '/') {
    onoff = client.parseInt();
    if (client.read() == '/') {
      value = client.parseInt();
      if (client.read() == '/') {
        pin2 = client.parseInt();
        if (client.read() == '/') {
          onoff2 = client.parseInt();
          if (client.read() == '/') {
            value2 = client.parseInt();
          }
        }
      }
    }
  }
  client.println("always use pin 5");
  client.println("dont go above 123");
  client.println("1 on   0 off");
}

 

this is the code with the bridge instead of the eithernet this works as long as im connected to the arduinos wifi.

Code: [Select]
#include <Bridge.h>
#include <BridgeServer.h>
#include <BridgeClient.h>
#include <Console.h>

BridgeServer server;
unsigned long value;
int pin;
int onoff;
unsigned long value2;
int pin2;
int onoff2;

void setup() {
  Bridge.begin();
  server.listenOnLocalhost();
  server.begin();
  Console.begin();


}

void loop() {
  BridgeClient client = server.accept();
  if (client) {
    process(client);
    client.stop();
  }
  analogWrite(pin, value);
  analogWrite(pin2, value2);
  if (onoff == 1) {
    digitalWrite (4, HIGH);
  }
  else {
    digitalWrite (4, LOW);
  }
  if (onoff2 == 1) {
    digitalWrite (2, HIGH);
  }
  else {
    digitalWrite (2, LOW);
  }
}
void process(BridgeClient client) {

  String command = client.readStringUntil('/');
  if (command == "analog") {
    analogCommand(client);
  }
}
void analogCommand(BridgeClient client) {
  pin = client.parseInt();
  if (client.read() == '/') {
    onoff = client.parseInt();
    if (client.read() == '/') {
      value = client.parseInt();
      if (client.read() == '/') {
        pin2 = client.parseInt();
        if (client.read() == '/') {
          onoff2 = client.parseInt();
          if (client.read() == '/') {
            value2 = client.parseInt();
          }
        }
      }
    }
  }
  client.println("always use pin 5");
  client.println("dont go above 123");
  client.println("1 on   0 off");
}

ShapeShifter

The Ethernet library in general, and the EthernetServer/EthernetClient classes in particular, do not work with the Yun. They are intended for an Ethernet shield where the sketch is in control of all networking operations.

The Yun is very different from a regular Arduino with an Ethernet or WiFi shield. The Yun consists of a microprocessor for running sketches, but it also has a full Linux system to manage the WiFi and Ethernet connections, the SD card, and many other functions. All networking is managed by the Linux side, and the sketch communicates using the Bridge library.

It's unfortunate that all of the Yun documentation stresses only WiFi connections, as it makes it seem like all of those examples only work over WiFi. In reality, wherever the documentation or an example talks about WiFi, you can substitute the words "network" or "Ethernet" and the meaning doesn't change. As far as the Bridge library is concerned, there is no difference between WiFi or Ethernet - the same sketch can work with one or the other, or even both at the same time.

You have a sketch that works with WiFi. That same sketch will also work with the Ethernet port without modifications. You only need a valid Ethernet connection, and you need to use the correct IP address (the WiFi and Ethernet interfaces will each have their own address.)

The key is to have a valid Ethernet connection. By default, the Ethernet connection is set up to acquire an address using DHCP. That means that it is expecting to be connected to a router or a network that has a DHCP server. If you have an existing network, and you connect the Yun's Ethernet port to it, it should get an address and just plain work. On the other hand, if you are just directly connecting a computer directly to the Yun without there being a router on the network, then you will likely have to do some configuring: either run a DHCP server on your computer so the Yun can get an address, or configure the Yun and computer with compatible static addresses, or configure the Yun to run a DHCP server so your computer can get an address.

The gist of the story is that if you can ping the Yun over the Ethernet port, then the same sketch that works for WiFi should also work for Ethernet.

rcvolley

Thank you so much for your help I'm trying to plug it into a network where it has to have a specific IP address or it won't work I plug it into the network type in the IP address http://ip adress/arduino/analog/5/1/15/4/1/100 and then the rest of the information and it says it doesn't exist I'm on the same network that I plugged it into and I have set the eithernet ip adress to the one I need to have it be useing the advanced settings  is there something I'm missing?

ShapeShifter

Can you ping the Yun at that IP address?

Can you access the Yun's configuration pages at that address? (http://ip address)

Don't bother trying to test your sketch until you can answer yes to these two questions. If you can't answer yes to them, then there is an issue with the way you have configured your static address. If you can't ping it or access the configuration pages, there is no way the sketch is going to be able to communicate.

If you can get to the Yun's configuration pages using WiFi, what does it show for the Ethernet status on the summary status page (the first page you land on after logging in)? Does it shows that the Ethernet port is connected? Does it show the correct IP address and subnet mask?



And don't forget: any time you make changes, and the Linux side has to reboot, it can take at least a full minute before it is rebooted and ready to talk on the network. Don't fall into the trap of changing the address, rebooting, and then immediately testing if it is talking - give it time to finish booting.


rcvolley

so I can ping it only when I'm on the Arduino internet when I ping it on the internet I plugged the ethernet cord into it won't ping it at all though it says its connected and that all the information is correct so I'm not sure what to do next.

Go Up