Pages: [1]   Go Down
Author Topic: Trying to display an requisted IP address on an LCD screen but the script stops  (Read 246 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello i'm new when it comes to programming with Arduino and I have a queston about a script i can't seem to get working.
I am using an Arduino Uno with an Ethernet shield and I am trying to requist an IP using the DHCP server and displaying it on a LCD screen.

I have 2 seperate scripts. The first where I show some random variables on my LCD screen and the second is where I requist an IP.
Both work, I can print the IP the Arduino got assigned and I get a reply when I ping it. But when I try to combine both scripts it just stops at the point where I use Ethernet.localIP(); as far as I can tell.

For the DHPC script I have followed this tutorial:
https://www.inkling.com/read/arduino-cookbook-michael-margolis-2nd/chapter-15/recipe-15-2

And for the LCD screen i'm following chapter 11 of the Arduino projects book and this:
https://www.inkling.com/read/arduino-cookbook-michael-margolis-2nd/chapter-11/recipe-11-1


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

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetClient client;
IPAddress myIPAddress;

void setup()
{
  Serial.begin(9600);
  
  lcd.begin(16,2);
  lcd.clear();
  
   // start ethernet using mac & DHCP
   // if this is not possible
   if(!connection)
   {
      Serial.println("Failed to configure Ethernet using DHCP");
      lcd.print("DHCP requist");
      lcd.setCursor(0, 1);
      lcd.print("Failed");
      
      // no point in carrying on, so stay in endless loop:
      while(true);  
   }
   else
   {
    // show that the IP needs to be requisted from the DHCP server
     Serial.println("Requisting IP");
     lcd.print("Requisting IP");
     lcd.setCursor(0,1);
     lcd.print("Waiting for DHCP");
   }
  
   // requist IP
   ip();
  
   // show the IP
   lcd.clear();
   lcd.setCursor(0, 0);
   lcd.print("The IP is:");
   lcd.setCursor(0, 1);
   lcd.print(myIPAddress);
  
   Serial.println(myIPAddress);
}

// using ethernet with mac & dhcp
boolean connection()
{
  if(Ethernet.begin(mac) == 0)
  {
    return false;
  }
  else
  {
    return true;
  }
}

// requisting ip
void ip()
{
   delay(1000);
   Serial.println("Now requisting IP"); // I receive this print
   myIPAddress = Ethernet.localIP();
   Serial.println("IP requist succesfull"); // I don't receive this or anyting after this
}

void loop()
{
  
}

Logged

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

What is supposed to be serving you the IP address? Do you have a DHCP server set up?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What is supposed to be serving you the IP address? Do you have a DHCP server set up?

The LCD screen is supposed to show the IP that was assigned.

And yes the DHCP server works.
If I use only this code on my Arduino then I get an IP wich I get a reply from if I ping it.

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

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

EthernetClient client;

void setup()
{
  Serial.begin(9600);
  // start ethernet using mac & DHCP
  if(Ethernet.begin(mac) == 0)
  {
    Serial.println("Failed to configure Ethernet using DHCP"); 
    while(true)   // no point in carrying on, so stay in endless loop:
      ;
  }
  delay(1000); // give the Ethernet shield a second to initialize
 
  Serial.print("This IP address: ");
  IPAddress myIPAddress = Ethernet.localIP();
  Serial.print(myIPAddress);

}

void loop()
{

}
Logged

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

Quote
If I use only this code on my Arduino then I get an IP wich I get a reply from if I ping it.
So, modify that code to display that value on the LCD.

Code:
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Paying attention to which pins you can't use (the SPI pins, for example) will pay dividends.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So, modify that code to display that value on the LCD.

Thats is exactly what I have been trying to do, see code I posted above, but for some reason anything that comes after this line myIPAddress = Ethernet.localIP(); doesn't get executed.

Part of the script I posted above:
Code:
// requisting ip
void ip()
{
   delay(1000);
   Serial.println("Now requisting IP"); // I receive this print
   myIPAddress = Ethernet.localIP();
   Serial.println("IP requist succesfull"); // I don't receive this or anyting after this
}
Logged

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

Your LCD is connected to the SPI pins, if you are using a 328-based Arduino. That CAN NOT be.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your LCD is connected to the SPI pins, if you are using a 328-based Arduino. That CAN NOT be.

I finnaly understand what you meant. I changed the two SPI pins 12 and 11 to 9 and 8 and now it works.

Thank you for your reply.
Logged

Pages: [1]   Go Up
Jump to: