Pages: 1 [2]   Go Down
Author Topic: Getting started with Arduino Ethernet  (Read 2025 times)
0 Members and 1 Guest are viewing this topic.
California
Offline Offline
God Member
*****
Karma: 13
Posts: 629
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I had heard / read about a "serial terminal" window in the Arduino IDE, but what I read left a lot to be desired - how to make it work, is it done automagically, etc.  So, (if I am understanding all this correctly), unless I "do something different" - whatever that is - any requests, via sketches software, hardware, or whatever, to use a "serial terminal", (using the built-in serial libraries?), automagically get routed through the USB interface to the IDE's serial terminal window.  Right?

Not exactly.  Please review the following sketch (It is in Examples\Basics in the IDE) and tell me what you see.

Code:
/*
  AnalogReadSerial
  Reads an analog input on pin 0, prints the result to the serial monitor.
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
 
 This example code is in the public domain.
 */

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);        // delay in between reads for stability

Quote
While we're on this topic. . . (a bit off-topic, I apologize) . . . I just noticed on both my Uno and Mega boards that there are two pin-connections labeled "TX" and "RX", (on the Uno), "TX-0" and "RX-0", (on the Mega).  (I had my Uno hidden under a Seeed servo/DC Motor shield - so I did not really notice this until now.)  Do these run "in parallel" with the USB "serial" interface, or are they programmatically different?

Example:  I have a 16x2 serial display board that I bought with the other Arduino "toys" I picked up for myself.  It would be interesting to send, (at least), some status/display information to that display while the Ethernet board, (or some other shield, for that matter), is running.

Yes, you have a TTL/UART communication on the UNO and four on the MEGA.  For the MEGA, you will address Serial, Serial1, Serial2 and Serial3 for the individual UARTs.  Technically speaking, the UNO or MEGA's USB Serial is it's own UART via the Atmega8u2/16u2, but it uses the Serial (Rx0/Tx0) of the Mega or Uno to put things out through the USB via the Transistor to Transistor Level communication.

Clear as mud?  Just start with some sketches, mess with some of the parameters and admit you are an Arduino noob smiley-wink  We all were in the beginning.
Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
My point is this:
If these things are more carefully explained in the documentation accompanying a piece of software - or if there is even some kind of documentation at all - the noob like myself who does not mind reading the documentation before trying to load code, will have a chance to learn these things without taking up the valuable time of the experts on these fora, explaining and re-re-re-explaining what should be obvious, and obviously documented.
I won't disagree that the documentation would be improved. Where I have in issue, though, is with the idea that an Ethernet shield is going to make magic happen.

The process of adding an ethernet card to a PC, back in the days before every computer came with them, was only undertaken by those that understood that the card was not a solver bullet. Magic wasn't going to happen because the card got installed.

Back then, plugging in the card, then saying "Hey, everybody, I've got an ethernet card now, and magic isn't happening" got you strange looks. As it does today when you add an ethernet shield to an Arduino and expect magic to happen.

I still expect that you will have a fundamental understanding of client/server application development before you see the ethernet shield as a necessary component. I expect you to have at least passing familiarity with doing the same stuff with a PC that you want to do with an Arduino with ethernet shield.

Asking for links to tutorials to show you how to use the ethernet shield is an admission that the basic understanding is missing. It is that basic understanding that allows us to tell you to "tweak this, nudge that, and give this other thing a little twist to the left."

I'm fine with us agreeing to disagree.
Logged

Worcester, Ma.
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Paul,

When you posted your reply, I was ready to post a - lengthy - reply. . . .  However, I deleted it and waited a few days to read your post again.

Re-reading it today, it still infuriates me.  I cannot imagine how - on a forum like this - you can come on with such an absolutely arrogant attitude toward me - and possibly others here on these fora as well.

You said:

Quote
I still expect that you will have a fundamental understanding of client/server application development before you see the ethernet shield as a necessary component. I expect you to have at least passing familiarity with doing the same stuff with a PC that you want to do with an Arduino with ethernet shield.

I do not cede you the right to expect, or demand, anything from me.

You said:

Quote
Asking for links to tutorials to show you how to use the ethernet shield is an admission that the basic understanding is missing.

Likewise, I do not cede you the right to place limits upon the questions I may pose.

( :shaking head in wonder: )
I cannot understand how you can equate "asking for help/tutorials" so that I can see how it is done with the Arduino, with lacking any understanding about computers, networking, the Internet, etc. etc. etc.

It is exactly this kind of arrogance that drives people away from these fora, and makes them think two or three times about making use of the resources available here.  People don't come here to be spanked by the likes of you, they want help and encouragement.  And I am glad to say that everyone else on this forum has provided exactly that.  I have looked at code, seen how it is done, played with it, and had fun.

A request, if I may:  (Quoting a sign I have over my desk)
Be nice, or go away.

What say ye?

Jim (JR)
« Last Edit: September 30, 2012, 09:38:07 pm by jharris1993 » Logged

Worcester, Ma.
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Zoomkat,

First of all, thank you, (and everyone else for that matter), for the excellent code.

I believe I have found a small bug:

In your demo code, you wrote:
Code:
void setup(){

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }

  Serial.begin(9600);  <<--- In the wrong place?

  Serial.println("Better client test 9/22/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

You place the Serial.begin statement just prior to where you print your banner title.
Shouldn't it be placed just after the declaration of the "setup" method?  AFAIK, placed after the Ethernet.begin reference, the error code within it cannot print.  Right?

I had some fun with your code, moved that statement around, snooped out the Ethernet.cpp file, snipped some code from the "DhcpAddressPrinter" Ethernet example. . . . and added some code to give the user some feedback, and display the characteristics of his connection before it moves on to your test.

Here it is:

Code:
//zoomkat 9-22-12
//Updated 09/29/2012 by Jim Harris (JR) to provide a "confidence" message and IP info after connect
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName[] = "web.comporium.net"; // zoomkat's test web page server
EthernetClient client;

//////////////////////

void setup(){

/*
Added by JR on 9/29/2012
Moved Serial.begin so that it appears prior to any serial message reference.
Added startup confidence message - as the sketch appears to hang when seeking an IP address.
*/
    Serial.begin(9600);
    Serial.println("Attempting to retrieve an IP address via DHCP\n");
    //  Added a "timing" message to help manage the users expectations
    Serial.println("This might take a little while, so why not go and");
    Serial.println("get a cuppa coffee while you wait\n");
//  end add =========================================================

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }
/*
Added by JR on 09/29/2012
Added an "answer" to the "acquiring" message printed above.
*/
  Serial.println("Ahhh . .  Got one!");

/*
Here I snipped the central code block from the
"DhcpAddressPrinter" example found within the Ethernet library
folder, duplicated it four times, scrounged the Ethernet source,
and added the display of the IP address obtained, the subnet mask,
the gateway IP address, and the address of the assigned DNS server.
*/
  // print your local IP address:
  Serial.print("My IP address is: ");
  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.print("\n");  //  Added to space the various entries.

  // print your local subnet mask:
  Serial.print("My Subnet Mask is: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.subnetMask()[thisByte], DEC);
    Serial.print(".");
  }
  Serial.print("\n");

  // print your local gatewaay:
  Serial.print("My Local Gateway is: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.gatewayIP()[thisByte], DEC);
    Serial.print(".");
  }
  Serial.print("\n");

  // print your local DNS server IP:
  Serial.print("My Local DNS Server is: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.dnsServerIP()[thisByte], DEC);
    Serial.print(".");
  }
  Serial.print("\n\n");  //  Added to provide spacing from my text and zoomkat's original text.
//  End add ===============================================================

  Serial.println("Better client test 9/22/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  }  
}

//////////////////////////

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(serverName, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
    client.println("Host: web.comporium.net");
    client.println(); //end of get request
  }
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

Take a look and critique it.

Feel free to mess with it as I have done.

A question:

On my Windows 7 system (that I am using as the development system), ipconfig returns the following information about my hard-wired Ethernet connection:

Code:
Ethernet adapter Local Area Connection:

   Connection-specific DNS Suffix  . : vgorilla.com
   Description . . . . . . . . . . . : Realtek PCIe FE Family Controller
   Physical Address. . . . . . . . . : 00-1E-33-FB-62-21
   DHCP Enabled. . . . . . . . . . . : Yes
   Autoconfiguration Enabled . . . . : Yes
   IPv4 Address. . . . . . . . . . . : 172.31.100.100(Preferred)
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Lease Obtained. . . . . . . . . . : Sunday, September 30, 2012 4:52:01 PM
   Lease Expires . . . . . . . . . . : Monday, October 01, 2012 7:44:37 PM
   Default Gateway . . . . . . . . . : 172.31.100.1
   DHCP Server . . . . . . . . . . . : 172.31.100.1
   DNS Servers . . . . . . . . . . . : 172.16.250.1
                                       66.189.0.100
                                       24.159.64.23
                                       24.247.24.53
   NetBIOS over Tcpip. . . . . . . . : Enabled

Note that there are three external DNS servers returned by the DHCP configuration request, (the first one is hard-coded so that I can use a particular VPN connection more easily.)

When I run my updated code and display the DNS IP address, it only retrieves and displays ONE DNS IP address.  (The 66.189.0.100 address)

Even if I increase the loop count for gathering the DNS IP address to 8, it still displays only the one.  The second comes back as 194.0.0.0
Viz.:
Code:
Attempting to retrieve an IP address via DHCP. . . .
Ahh . . Got one!
My IP address is: 172.31.100.104.
My Subnet Mask is: 255.255.255.0.
My Local Gateway is: 172.31.100.1.
My Local DNS Server is: 66.189.0.100.194.0.0.0.  <-- yes, I know the formatting stinks

Better client test 9/22/12
Send an e in serial monitor to test

Why is that?  Can it retrieve more than one DNS address?  Would it even be useful in the Arduino context?

I have also been having fun with the serial library and a 16x2 LCD display.  When I get time, I'll see if I can get this thing to print to the external display instead of the built-in serial terminal.

What say ye?

Jim (JR)
« Last Edit: September 30, 2012, 09:36:50 pm by jharris1993 » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 551
Posts: 46240
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Likewise, I do not cede you the right to place limits upon the questions I may pose.
I'm not trying to impose any limits on the questions you ask. You asked for links to tutorials to using the ethernet shield. Then, you complained that they were inadequate/wrong/assumed stuff you didn't know/didn't apply.

I think that you have unrealistic expectations. You expect us to do your research for you, and only point you to hand-holding tutorials that explain in detail what you need to do, without trying to teach you anything else, while not saying exactly what it is you want to do. I don't see how we can be expected to do that.

Quote
A request, if I may:  (Quoting a sign I have over my desk)
Be nice, or go away.
I'm not going away. I just won't be responding to you, anymore.
Logged

Pages: 1 [2]   Go Up
Jump to: