New in Cpp / Arduino.. Need simple help...

Hi,

just trying to make a simple WebClient connection to 'checkip.dyndns.org' and read the IP address and display it on my LiquidCrystal LED DIsplay...

I'm new in Arduino & CPP. I'm learning by doing.. have already a script which is doing what i need, but not 100% perfect. (btw. i know programming, but not cpp what i think is similar to perl, python, php, ect., so i can understand what you mayme mean in your answer.. :slight_smile: )

My problem is, i can not display the output (char c = client.read(); on the LiquidCrystal as Text) ... here my code (btw. maybe cou can clean/make it beter?):

// include the library code:
#include <LiquidCrystal.h>
#include <SPI.h>
#include <Ethernet.h>


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
//IPAddress server(74,125,232,128);    // numeric IP for example.com (no DNS)
char server[] = "checkip.dyndns.org";  // name address for example.com (using DNS)
IPAddress ip(192,168,1,5);

// Initialize the Ethernet client library
EthernetClient client;
// initialize the LiquidCrystal with the numbers of the interface pins
LiquidCrystal lcd(9, 8, 5, 4, 3, 2);

void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  //lcd.setBrightness(20); //Doesn't work. Using Poti..
}




void loop() {
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) { Ethernet.begin(mac, ip); };
  delay(1000);
  
  // Print a message to the LCD.
  lcd.clear();
  lcd.print("Connecting...");
  
  // if you get a connection, report back via LiquidCrystal at bottom line:
  if (client.connect(server, 80)) {
    lcd.setCursor(0, 1);
    lcd.print("Connected...");
    // Make a HTTP request:
    client.println("GET / HTTP/1.1");
    client.println("Host: checkip.dyndns.org");
    client.println("Connection: close");
    client.println();
  } 
  else {
    // kf you didn't get a connection to the server print it at LiquidCrystal:
    lcd.clear();
    lcd.print("Connection Error");
  }

  // If client connected, recieve data abd print it at LiquidCrystal and wait 5 sec.
  if (client.available()) {
    char c = client.read();
    lcd.clear();
    lcd.print(c); //ERROR IN HERE.. 'c' IS NOT A VALID STRING FOR LiquidCrystal
    delay(5000);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    lcd.clear();
    lcd.print("disconnecting.");
    //client.stop();
  }

  // wait 5 sec. to renew IP
  delay(5000);
}


/*
void lcdmsg( col, row, msg) {
 lcd.clear();
 lcd.setCursor(col, row);
 lcd.print(msg);
}
*/

Hopy you can help me so i can learn more about CPP.

Thanks in advance.

Greetings from Germany,
Kris

Your are reading a single char, not a string ( or an array of chars ). And then you are giving that char to a function which expects either a String object or a pointer to an array of chars.

It is not clear to me what you expect to be receiving here, but I would be writing something that looks more like this

unsigned long start_time = millis() ;
bool completed = false ;
char buf[40] ;
byte bpos=0 ;

while ( !completed )
{
    while ( client.available() && bpos<40 )
    {
         char c = client.read() ;
         if ( c=='\r' || c=='\n' ) 
         {
              completed=true ;
              buf[bpos]='\0' ;
              break ;
         }
         else
         {
             buf[bpos]= c ;
             bpos++ ;
         }
    }
    unsigned long time_now = millis() ;
    if ( time_now - start_time > 5000 ) break ;
}
if ( bpos == 40 ) buf[39]='\0' ;

if ( bpos > 0 ) lcd.print( buf ) ;
else lcd.print("Got nothing");

Actually, it appears that a single char is a valid argument to one of the versions of the lcd. print( ) function, so I don't know what the compiler is objecting to.

Presumably, you are expecting more than 1 character response. So waiting a mandatory 5 seconds between each character you receive, is probably not what you actually intended to do.

  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) { Ethernet.begin(mac, ip); };

You should be starting the Ethernet shield in setup(), not in loop().

  delay(1000);

On every pass through loop()? There's a word for that. It rhymes with stupid.

    client.println("Host: checkip.dyndns.org");
    client.println("Connection: close");

The server you are connecting to is the host, so the host line is not needed. The default is to close the connection when done, so that line is not needed.

    char c = client.read();
    lcd.clear();
    lcd.print(c); //ERROR IN HERE.. 'c' IS NOT A VALID STRING FOR LiquidCrystal
    delay(5000);

You've already cleared the LCD. It is not necessary to clear it after read each and every character.

It is certainly not necessary to delay 5 second between characters.

It IS necessary to display the ACTUAL, COMPLETE error.