Pages: [1]   Go Down
Author Topic: Wiznet 5100 shield, Converted Webserver example, and getting weird results!  (Read 1203 times)
0 Members and 1 Guest are viewing this topic.
Canton, Connecticut, United States, Planet Earth, SOL Galaxy, 3rd star on the left.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 74
To Infinity, And BEYOND!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay, here's a interesting problem. I used the Web server example, to monitor 8 simple pushbuttons, and display their state
over the ethernet to a computer. My only modification, was converting the analogRead to digital Read, and directing input
from the 8 available digital pins, to the ethernet output. Using 8 sets of simple button circuits...



and using the following code..

Code:
/*
  button Web Server
 
 A simple web server that shows the value of the digital input pins.
 using an Arduino Wiznet Ethernet shield.
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 * digital inputs 2 through 9, 10K pulled to GND, pushbutton to +5V

 main code created 18 Dec 2009 by David A. Mellis
 modified 4 Sep 2010 by Tom Igoe
 * modified 22 Aug 2011 from analog read to digital read by Stephen Griswold
 */

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

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1, 177 };

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
Server server(80);

void setup()
{
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
          for (int digitalChannel = 2; digitalChannel < 10; digitalChannel++) {
            pinMode(digitalChannel, INPUT); //make sure pins are in imput mode
          }
}

void loop()
{
  // listen for incoming clients
  Client client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          // output the value of each digital input pin
          for (int digitalChannel = 2; digitalChannel < 10; digitalChannel++) {
            client.print("digital input ");
            client.print(digitalChannel);
            client.print(" is ");
            client.print(digitalRead(digitalChannel));
            client.println("<br />");
          }
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}

Problem is... the program seems to be ignoring the button states, and sending a semi random pattern.
 (with all the Port# = stuff removed)

 01111100 00111100 00011110 00001111 10001111 11000011 11110001 11110000   from the digitalRead.

Every time I reload the page on the receiving computer, it gives a different set, where it should be all zeros (if no buttons pushed,) or a 1 where one of the buttons is..

I've even tried revering the state, pulling the digital pins high with the 10K resistors, and the switch to GND, But it still gives the same result.

This is just the beginning of a experiment to use the ethernet shield as a link through a WiFi router, to monitor a robot project, and as most newbies, I'm building the main program in modules, getting each part to work. I'm using a Duemilanove as the test-bed, and plan on moving the code segments (modules) as I finish them, to a MEGA-2560 once I'm sure the different parts work. (more digital pins available)

It almost seems like there's some kind of artifact signals left from taking to the D10 to 13 pins, to the ethernet shield.
I don't know if this has been covered yet, or not.. and it's very frustrating!

Stephen (gelfling6)
Logged

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

Dumping all the code that is not related to the problem, does a simple Serial.print() of the pin state work?
Logged

Canton, Connecticut, United States, Planet Earth, SOL Galaxy, 3rd star on the left.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 74
To Infinity, And BEYOND!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

<hangs head low, and utters D'OH!!! >

I stripped it down to to the port reading, converted the output to serial debug, then slowed it down to 2400-baud (so I could read it..)
And.... same problem...  though, I had put the parts back almost exactly the same, I spaced the buttons a little further apart. (long breadboard.)
and this time, pin-9 was a solid 1..  despite the button pressing.  Then... I looked a little more carefully at the board.  You know the one, with it looks like 2 power rails across the top & bottom...  ...  smiley-confuse  and.. a big space between the left & right sides..   smiley-eek-blue  smiley-red 4 rails..


Okay, ran a jumper from the left +5V rail, to the right, and another from the left GND rail to the right.  Everything is all logic-1 now.   wait a sec...
4 pin push-buttons.. <insert foot kicking self>  unplugged switch for pin 2, turned it 90 degrees.. re-inserted.. Pin 2 showed logic-0.. pressed the button, Logic-1.   smiley-roll-blue  re-installed the ethernet server code, placed the ethernet shield between the wires & arduino. tried again.
All showed logic-1, except pin 2.  pressed the button, reloaded the page, it turned logic 1.  unplugged and turned the other 7 switches, reloaded the page again. all logic 0.  pressed the button for pin 4, reloaded page, everything 0 except 4, which was logic-1.   smiley-small

Problem solved.  It wasn't software, just mis-match of hardware...  I don't know why I didn't think to do a direct read.. Now the fun part, trying to write a HTML page that'll monitor the robot's progress, and keep sending it to the computer. Hopefuly, small enough to fit in the arduino's memory. (I have the ethershield, which doesn't have the SD card.)

Feeling a little foolish, but thankfull someone kicked me in the right direction.

Stephen (gelfling6)
Logged

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

Quote
Feeling a little foolish, but thankfull someone kicked me in the right direction.
No problem. I don't mind kicking people when they need it.

Quote
Now the fun part, trying to write a HTML page that'll monitor the robot's progress, and keep sending it to the computer.
Generating the page should not be a problem. Keep in mind, though, that if the Arduino is a server, it can't push the page anywhere. If the Arduino is a client, it doesn't push the page, either.

You'll need to have the Arduino act as a server, and have the client refresh the page often. With the appropriate meta tags in the page (no, don't ask me what they are), the client can refresh automatically at some interval.
Logged

Canton, Connecticut, United States, Planet Earth, SOL Galaxy, 3rd star on the left.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 74
To Infinity, And BEYOND!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hmm..   That's something I'd like to have the arduino do, refreshing the page info at a semi-regular interval..  I'm looking at a few books on HTML, but so-far, only getting as far as a load-once, and wait page as well as the base example.  What I'm looking to do, is have it report back distance readings from a PING))) clone (actually a VEX ping) as the robot travels around the room, and tells where it has to change direction because of something in the way, and how far away, or if following along a wall, that it needs to turn to continue along the wall.. I know, it might be more than the Arduino can swallow as far as HTML..
Right now, the test circuit was done with a wired connection between the shield to a router, and the computer to the same router,  just finished testing it across WiFi. (using an old (and small) D-Link 802.11B router.) Not looking for high speed, just simple reporting back info.  Everything else I've tried, seemed impossible (a pair of parallax 433Mhz radios, but having trouble writing soft serial. Apparently got a pair of Series-2 XBEEs, not the Series-1, so that was another fail.)  Was kind of hoping this might work better.
Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9675
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Simple meta-refresh server code:

Code:
// for W5100 ethernet shield
// the IP address will be dependent on your local network/router
// port 80 is default for HTTP, but can be changed as needed
// use IP address like http://192.168.1.102/ in your brouser

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 102 };
Server server(80);

void setup()
{
  // start the server
  Ethernet.begin(mac, ip);
  server.begin();
}

void loop()
{
  // listen for incoming clients
  Client client = server.available();
  if (client) {
     while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // see if HTTP request has ended with blank line
        if (c == '\n') {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
         
          //meta-refresh page every 2 seconds
          client.print("<HEAD>");
          client.print("<meta http-equiv=\"refresh\" content=\"2\">");
          client.print("<TITLE />Zoomkat's meta-refresh test</title>");
          client.print("</head>");
                   
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(analogRead(analogChannel));
            client.println("<br />");
            }
          break;
           }
        }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

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

Quote
What I'm looking to do, is have it report back distance readings from a PING))) clone (actually a VEX ping) as the robot travels around the room, and tells where it has to change direction because of something in the way, and how far away, or if following along a wall, that it needs to turn to continue along the wall.
In this case, set up your PC as a server, and the Arduino as a client. The client makes a GET request with parameters, like:
GET /plotthis.php?x=14.5&y=28.1&dist=3.8&turn=90

Create a script or application on the server that the Arduino can call, and that understands the data that the Arduino will include with it's requests. The server doesn't actually need to return anything. It just uses that the data that was in the GET request for its own purposes - plotting, logging, etc.

The Arduino makes a GET request whenever there is new information that the server should know about.

Having the client (the PC) pull information from the server (the Arduino) at its pace will either miss a lot of changes or require a lot of traffic to get the same information over and over.

Having the client (the Arduino) push information to the server (the PC) means that the minimum information travels over the network, and that no information is lost (assuming that the PC can keep up, which is really a no-brainer, and that the network bandwidth is sufficient, which is questionable given the relatively slow speed of the Ethernet shields for the Arduino).
Logged

Canton, Connecticut, United States, Planet Earth, SOL Galaxy, 3rd star on the left.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 74
To Infinity, And BEYOND!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hmm.. I'll have to check out that code you sent, Zoomkat.   Paul, though a client on the Arduino might be more efficient, I can't say the same for my programming on the PC side..  what I'd like, is have the arduino as the server side, and just using the laptop running a browser, running as a monitor.  (i.e. receiving simple info, displaying it, but also have the ability to take over manual control (i.e. WHOA! STOP! You're going the wrong way!!) of the robot when it runs into trouble.  I might be asking too much of the arduino, though...  Kinda wish the ethershield board did have an SD socket, and I could push all of the server HTML or program onto the SD card, and just have the arduino process it.  1st, let's see what I can make of Zoomkat's code.. Just got out of a quick meeting at work, and checking the email right now at a library.

Stephen
Logged

Pages: [1]   Go Up
Jump to: