Pages: [1] 2 3   Go Down
Author Topic: Ethernet & LCD Shield project  (Read 1554 times)
0 Members and 1 Guest are viewing this topic.
Wales, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey folks,

First-timer with the Arduino here smiley
I'm trying to put together a quick project using the Arduino, an Ethernet shield and an LCD keypad shield from hobbytronics.

My program verifies and loads properly but when it runs it only goes so far in the setup and stops and loop() doesn't run at all.

What I'm trying to achieve is to use the ethernet shield to go online and get a value from a special page on my website.
Through each step the LCD is supposed to show where it's up to (for my diagnostic purposes, but will repurpose it later).

Here is the code I'm loading to the Arduino.
As above, it gets as far as "continuing..", the LCD backlight dims slightly and then.. nothing.
Code:
#include <SPI.h>
#include <Ethernet.h>
#include <LiquidCrystal.h>

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(69,163,169,77);
EthernetClient client;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);


void setup() {
  lcd.begin(16, 2);
 
  lcd.setCursor(0,0);
  lcd.print("Web Client:");
  lcd.setCursor(0,1);
  lcd.print("Ready...");
 
  delay(2000);
  lcd.setCursor(0,1);
  lcd.print("Continuing..");

  if (Ethernet.begin(mac) == 0) {
    lcd.setCursor(0,1);
    lcd.print("DHCP Config Err");
  } else {
    lcd.setCursor(0,1);
    lcd.print("Else Happened");
  }
  delay(1000);
 
  lcd.setCursor(0,1);
  lcd.print("Connecting...");

  if (client.connect(server, 80)) {
    lcd.setCursor(0,1);
    lcd.print("Connected");

    client.println("GET /onLive.php HTTP/1.0");
    client.println();
  }
  else {
    lcd.setCursor(0,1);
    lcd.print("Connection Fail");
  }
}

void loop()
{
  lcd.setCursor(0,1);
  lcd.print("Starting Loop");

  while(client.available() > 0)
  {
     char c = client.read();
     if(c == '#')
     {
  while(client.available() == 0) { }
  c = client.read();
  if(c == '1')
           lcd.setCursor(0,1);
           lcd.print("File Found!!");
  } else {
           lcd.setCursor(0,1);
           lcd.print("File Missing..");
     }
  }
  if (!client.connected()) {
   
    lcd.setCursor(0,1);
    lcd.print("Disconnecting..");
    client.stop();
   
    lcd.setCursor(0,1);
    lcd.print("Resetting");

    delay(3000);
    setup();
  }
}

Any help with this for a newbie would be greatly appreciated =)

Thanks!
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 116
Posts: 5400
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would remove the lcd and check the ethernet shield alone. IDE v1.0 has a bug that will cause the Ethernet.begin(mac) to not return.

Add under you mac address declaration
Code:
IPAddress ip(192,168,0,2);

Then use this instead of the dhcp
Code:
Ethernet.begin(mac,ip);

Do you get to "Connecting...."?

This is a test for the 605 bug. If you get to "Connecting", then it may be the 605 bug. You will have other problems if this is the case. But there is a fix. You can now download IDE v1.0.1 from the Arduino download page. That version has the patch already.

If I do not disable the SD SPI, I have trouble with DHCP also. Before the Ethernet.begin(mac) call
Code:
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);

edit: My bad. Those should have been commas in the ip, not periods. Just a habit.
Logged

Wales, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the reply,

I've made the adjustment and it seems to be doing the trick.
I also tweaked the code a bit to work with a local server where I just echo out the value I want the arduino to look for.

This gets as far as "connecting" and goes no further..
Sometimes I get a "connect failed" but it mostly sticks to "connecting".

The code is below if you don't mind having another look.

Code:
#include <SPI.h>
#include <Ethernet.h>
#include <LiquidCrystal.h>

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0,50);

IPAddress server(192,168,0,90);
EthernetClient client;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);


void setup() {
  lcd.begin(16, 2);
 
  lcd.setCursor(0,0);
  lcd.print("Web Client:");

  Ethernet.begin(mac,ip);
  delay(1000);
  lcd.setCursor(0,1);
  lcd.print("Connecting      ");
  delay(1000);

  if (client.connect(server, 80)) {
    lcd.setCursor(0,1);
    lcd.print("Connect OK      ");

    client.println("GET /ethernet.php HTTP/1.0");
    client.println();
  }
  else {
    lcd.setCursor(0,1);
    lcd.print("Connect Failed  ");
  }
}

void loop()
{
  while(client.available() > 0)
  {
     char c = client.read();
     if(c == '#')
     {
  while(client.available() == 0) { }
  c = client.read();
  if(c == '1')
           lcd.setCursor(0,1);
           lcd.print("File Found!!    ");
           delay(5000);
           
  } else {
           lcd.setCursor(0,1);
           lcd.print("File Missing..  ");
           delay(5000);
     }
  }
 
  delay(500);
 
  if (!client.connected()) {
    client.stop();

    delay(1000);
    setup();
  }
}
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 116
Posts: 5400
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK. Then try this. Remove the LCD shield and upload this. Open the serial monitor and watch.
Code:
#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0,50);

IPAddress server(192,168,0,90);
EthernetClient client;

void setup() {
  Serial.begin(9600);
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Serial.print("Starting ethernet...");
  Ethernet.begin(mac,ip);
  Serial.println("started");
  delay(1000);

  Serial.println("Connecting...");

  if (client.connect(server, 80)) {
    Serial.println("Connected");
    client.println("GET / HTTP/1.0");
    client.println();

    while(client.connected()) {
      while(client.available()) {
        Serial.write(client.read());
      }
    }
    client.stop();
    Serial.println("disconnected");
  }
  else {
    Serial.println("failed");
  }
}

void loop() {
}

What is the output? It may be "Connected", then lines and lines of trash forever.
Logged

Wales, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I've now tried that, and I get a failed response;
Quote
Starting ethernet...started
Connecting...
failed

After a reset or two I do get a response:
Quote
Starting ethernet...started
Connecting...
Connected
HTTP/1.0 200 OK
X-Powered-By: PHP/5.4.0
Content-type: text/html
Connection: Close
Date: Tue, 22 May 2012 19:10:51 GMT
Server: Abyss/2.7.0.0-X1-Win32 AbyssLib/2.7.0.0

disconnected

But more often than not it fails after hanging on connecting... for a long while.
Just for curiosity's sake, I added the the LCD shield on, and I get an instant failed each and every time.
Remove the LCD, and it's back to occasionally getting a result but mostly failing.

EDIT::
Scratch that, It's now getting a result everytime now.. but still fails with the LCD.
« Last Edit: May 22, 2012, 02:17:23 pm by Rirawen » Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 116
Posts: 5400
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK. I increased the delay after the begin to give the w5100 a little more time to set up. Install the LCD shield and upload this. It only starts the LCD and prints one line. Open the serial monitor and watch.
Code:
#include <SPI.h>
#include <Ethernet.h>
#include <LiquidCrystal.h>

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0,50);

IPAddress server(192,168,0,90);
EthernetClient client;

void setup() {
  Serial.begin(9600);
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  lcd.begin(16, 2);
  lcd.setCursor(0,0);
  lcd.print("Web Client:");

  Serial.print("Starting ethernet...");
  Ethernet.begin(mac,ip);
  Serial.println("started");
  delay(2000);

  Serial.println("Connecting...");

  if (client.connect(server, 80)) {
    Serial.println("Connected");
    client.println("GET / HTTP/1.0");
    client.println();

    while(client.connected()) {
      while(client.available()) {
        Serial.write(client.read());
      }
    }
    client.stop();
    Serial.println("disconnected");
  }
  else {
    Serial.println("failed");
  }
}

void loop() {
}

Do you get "Web Client:" on the LCD and the webpage download on the serial monitor?
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 116
Posts: 5400
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I see the problem. The shield uses digital pin 10 for the backlight. It can't do that. That is the SS pin for the w5100.
Logged

Wales, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I had to add "LiquidCrystal lcd(8, 9, 4, 5, 6, 7);" just before lcd.begin() otherwise it wouldn't compile.

With the LCD shield on the board, it fails everytime.
Without it, I get a response from the server.

I'm hoping this is just a config error and not a faulty lcd board.. =/
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 116
Posts: 5400
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are correct. I was in the process of correcting that when I found the datasheet on the LCD shield. It uses digital pin 10 for the LCD backlight. Unfortunately, that is the Slave Select pin for the w5100 ethernet device. That is why the backlight is dim. It is getting a "PWM" signal from the w5100 slave select.

edit: Digital pin 4 is also the Slave Select pin for the microSD card. Using that for other functions has caused me problems with the ethernet shield, even tho by the schematic, it should not make a difference if there is no SD card inserted.

Are you willing to experiment by trying a little "pin bending"? If so, try bending digital pins 10 and 4 on the LCD shield so they do not insert into the ethernet shield. Then try the program again. Don't worry about the LCD display. Does the serial monitor show the ethernet functions are working?

Change the pin setup to this for a test. Note the change to pin 3.
Code:
LiquidCrystal lcd(8, 9, 3, 5, 6, 7);
« Last Edit: May 22, 2012, 03:00:01 pm by SurferTim » Logged

Wales, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I changed the void loop to re-run setup - past behavior has shown me the ethernet fails between 1-2 times before working fine.
Since my LCD is using additional standoffs I have no problem pin bending smiley

So, with 10 and 4 moved, it seems to work fine - it starts, connects, spits out some server data and disconnects, then repeats.


EDIT:
Just spotted your chance to the LCD..
Made the alterations and tested working, I get good activity on the monitor.
« Last Edit: May 22, 2012, 03:12:35 pm by Rirawen » Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 116
Posts: 5400
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Did you do the lcd pin change too? Note the change to pin 3.
Code:
LiquidCrystal lcd(8, 9, 3, 5, 6, 7);

Now take a small piece of wire, and connect digital pin 4 to digital pin 3 on the LCD shield. Run the sketch again. Do both devices work? There will not be any backlight yet.

Logged

Wales, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm a little confused, when you say there will not be any backlight yet, are you assuming it should be off completely?
The backlight, even with pins 4, 10 bent out and the change to the sketch, the backlight still remains lit.
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 116
Posts: 5400
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You would think after all these years, I would know how to read a datasheet, but I still have problems! That is the backlight control. If it is LOW, then there will be backlight. If the pin is HIGH, there will be no backlight.

But that is not the important part. Does the LCD show the correct output?

Logged

Wales, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

No, while the ethernet functions seem to work the LCD has remained blank throughout all testing.
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 116
Posts: 5400
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you have a jumper wire on the LCD shield from digital 4 (not connected to ethernet shield) to digital 3 (still connected to ethernet shield)?
« Last Edit: May 22, 2012, 03:27:54 pm by SurferTim » Logged

Pages: [1] 2 3   Go Up
Jump to: