Go Down

Topic: Direct connection from Arduino to computer with ethernet shield (Read 2943 times) previous topic - next topic

Cosmonaut

I am trying to create a tethered robot with an onboard Arduino, connected directly to my computer via ethernet. I have a knockoff HanRun HR911105A 14/23 ethernet shield connected to an Uno. My computer is a MacBook connected via a Thunderbolt-ethernet dongle.

I would essentially like to create a server on the board that I can connect to by typing in the Uno's IP address in a browser. I don't know if this is possible or not, but after following a few tutorials, I can't get this to work.

Does the MAC address have to be specific to that board? I have heard that with some of these, you can just assign your own.

Currently the address is set as:
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED

And the Arduino's IP:
192.168.1.177

In System Preferences I have the computer connected through the Thunderbolt port using DHCP and a manual address of 192.168.1.20

I have no experience in this and would love any help setting up this server. This is for a class project and I'm stumped.

Thanks!

pert

I have a knockoff HanRun HR911105A 14/23 ethernet shield connected to an Uno.
That's just the model of the RJ-45 jack on the board. We need to know the model of the Ethernet controller chip on the shield. It should be Wiznet W5100, W5200, W5500, or ENC28j60.

Does the MAC address have to be specific to that board? I have heard that with some of these, you can just assign your own.
You can make up any address you like as long as it's unique on the network,

Cosmonaut

Quote
That's just the model of the RJ-45 jack on the board. We need to know the model of the Ethernet controller chip on the shield. It should be Wiznet W5100, W5200, W5500, or ENC28j60.
Man, now I feel like an idiot...

Looking at the chip, it seems to be a W5100.

And I'm not sure I'm structuring the MAC right. Are those 0's supposed to be replaced by numbers?

pert

The MAC should be fine unless you happen to have another device on the network using that one. The MAC they use in the example code isn't completely random, check out what the letters spell...an old tired programmer joke.

They are in hexadecimal notation. For more information see the Hexadecimal section of:
https://www.arduino.cc/en/Reference/IntegerConstants

Cosmonaut

Is the WebServer sketch (or this similar one) the wrong thing to be using for direct communication between the board and the computer?

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address
byte ip[] = {
  192, 168, 2, 201 };   // static ip of Arduino
byte gateway[] = {
  192, 168, 2, 254 };  // gateway address
byte subnet[] = {
  255, 255, 255, 0 };  //subnet mask
EthernetServer server(80);   //web server port

void setup(){
  //enable serial monitor
  Serial.begin(9600);
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {

        // start of web page
         client.println("HTTP/1.1 200 OK");
         client.println("Content-Type: text/html");
         client.println("<html><head></head><body>");
         client.println();
         client.print("<form method=get>");
         client.print("<input type='radio' name=r value='1'> One<br>");
         client.print("<input type='radio' name=r value='2' checked> Two<br>");
         client.print("<input type='radio' name=r value='3'> Three<br>");
         client.print("<input type=submit value=submit></form>");
         client.print("</body></html>");
         //stopping client
         client.stop();
       }
     }
   }
}


This is what I'm trying to run right now.

GypsumFantastic

Is the WebServer sketch (or this similar one) the wrong thing to be using for direct communication between the board and the computer?
No that should be fine. You might need to use a crossover ethernet cable (or a crossover adapter) though.

This is because the wiring you need to connect two computers is slightly different from the wiring you need to connect a computer to a hub.

Some ethernet interfaces automatically reconfigure themselves to deal with this difference, some make you use the different cable. I don't know if yours will be one of the automatic ones.

pert

The datasheet says that W5100 does have auto-MDIX so no crossover cable should be necessary. I've never actually tried this though so I could be wrong.

Cosmonaut

Turns out I don't need a crossover. My computer can handle that fine as well.

The problem was that my subnet mask was wrong. Changed it and everything worked fine.

I'm running a modified version of this code:
Code: [Select]
#include <SPI.h>
#include <Ethernet.h>



String HTTP_req;
boolean LED_status = 0;



byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};   //physical mac address
byte ip[] = {192, 168, 1, 1};   // static ip of Arduino
byte gateway[] = {192, 168, 2, 254};  // gateway address
byte subnet[] = {255, 255, 255, 0};  //subnet mask
EthernetServer server(80);   //web server port



void setup()
{
  Ethernet.begin(mac, ip, gateway, subnet);  //start Ethernet
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
}



void loop()
{
  EthernetClient client = server.available();  // Create a client connection
 
  if (client) // got client?
  { 
    boolean currentLineIsBlank = true;
    while (client.connected())
    {
      if (client.available()) // client data available to read
      {   
        char c = client.read(); // read 1 byte (character) from client
        HTTP_req += c;  // save the HTTP request 1 char at a time
        if (c == '\n' && currentLineIsBlank)
        {
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");
          client.println(); // send web page
          client.println("<!DOCTYPE html>");
          client.println("<html>");
          client.println("<head>");
          client.println("<title>Arduino LED Control</title>");
          client.println("</head>");
          client.println("<body>");
          client.println("<h1>LED</h1>");
          client.println("<p>Click to switch LED on and off.</p>");
          client.println("<form method=\"get\">");
          ProcessCheckbox(client);         
          client.println("</form>");
          client.println("</body>");
          client.println("</html>");
          Serial.print(HTTP_req);
          HTTP_req = "";    // finished with request, empty string
          break;
        }

        if (c == '\n')
        {
          currentLineIsBlank = true;
        }
       
        else if (c != '\r')
        {
          currentLineIsBlank = false;
        }
           
      } // end if (client.available())
    } // end while (client.connected())
   
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
       
  } // end if (client)
}

void ProcessCheckbox(EthernetClient cl)
{
  if (HTTP_req.indexOf("LED1=2") > -1)
  {  // see if checkbox was clicked
    if (LED_status)
      {
        LED_status = 0;
      }
   
    else
    {
      LED_status = 1;
    }
  }


  if (LED_status) // switch LED on
  {   
    digitalWrite(2, HIGH); // checkbox is checked
    cl.println("<input type=\"checkbox\" name=\"LED1\" value=\"2\" \
    onclick=\"submit();\" checked>LED1");
  }

  else // switch LED off
  {
      digitalWrite(2, LOW); // checkbox is unchecked
      cl.println("<input type=\"checkbox\" name=\"LED1\" value=\"2\" \
      onclick=\"submit();\">LED1");
  }
}


From https://startingelectronics.org/tutorials/arduino/ethernet-shield-web-server-tutorial/web-server-LED-control/

I just can't seem to add a second button. Whenever I do, they both activate, or none at all, and turn on the wrong LED. I tried making a second ProcessCheckbox function changing all the values so they would correspond to another pin but that doesn't work.

Go Up