Pages: [1]   Go Down
Author Topic: 'client' was not declared in this scope  (Read 1690 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I will be grateful for help with this problem.

My goal, of course, is to write some fairly clean code, using functions as a way of cleaning things up.  I would like to do some of the "client.print" activity within a function, as shown here.

Unfortunately it won't compile.  The compiler says "'client' was not declared in this scope", pointing at the first use of 'client' in the function "analytics".

I assume there is something I must do to explain to the compiler that it should be able to make use of "client" within the function.

Quote


#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[] = { 0x90, 0xA2, 0xDA, 0x00, 0x80, 0xB8 };
IPAddress ip(192,168,113,90);

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

EthernetServer server(81);

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

void loop()
{
  
  // listen for incoming clients
  EthernetClient 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();

client.print("Getting ready for 'hello world'");
       
analytics();       
          
          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();
  }
}

void analytics()
{

client.print("Hello, world!");

}

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24450
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Put your declaration of client outside of loop ().
The scope error is a clue.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

Thank you for your posting which says to "Put your declaration of client outside of loop ()."

I am sorry but I am so new at this that I do not know how to do what you said.  I would be grateful if you can say something like "insert a new line outside of loop() that says X".



Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24450
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
EthernetServer server(81);
 EthernetClient client;
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

Thank you for your posting.  Indeed your suggestion of "EthernetClient client;" made the compile eror go away. 

When I run this sketch, the LED lights up, but "hello world" does not show up on the web page.  The "getting ready" shows up on the web page, but the "hello world" from the function does not.

I would be grateful if you can tell me what I am doing wrong.

Quote
#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[] = { 0x90, 0xA2, 0xDA, 0x00, 0x80, 0xB8 };
IPAddress ip(192,168,113,90);

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

EthernetServer server(81);
EthernetClient client;


void setup()
{
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  pinMode(4,OUTPUT);
}

void loop()
{
  
  // listen for incoming clients

EthernetClient 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();


    client.println("Getting ready for 'hello world' <br />");

  
analytics();
        
          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();
  }
}


void analytics()
{
  digitalWrite(4,HIGH); //turn on a yellow LED
  delay(1000);
  digitalWrite(4,LOW);
  
client.print("Hello, world!");

}


Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24450
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You've still got a client declaration inside loop.
Change it to an assignment.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Tesla Member
***
Karma: 118
Posts: 8962
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but "hello world" does not show up on the web page.

It appears that you may have previously closed the connection to the client (client.stop()smiley-wink before you print "hello world. Also, you are using quote tags instead of code tags (#) to post your code
Logged

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

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

Thank you for your posting.

For the benefit of other new users such as myself, the fix turned out to be the following:

  • In loop(), find the line "EthernetClient client = server.available();" and change it to "client = server.available();".  In other words, delete "EthernetClient ".
  • Outside of loop(), insert a new line "EthernetClient client;"

The idea was that I had started with the Example of Web Server, the sketch that permits a visitor to visit a web site (inside the Arduino) and to see the values of the five A-to-D inputs.  I wanted to do some of the "print.client" activity in a function instead of in the main program.  This fix permits such a thing.

Again, thank you for posting.

Oh, and if anyone wishes to visit the web page, it is at http://wx1.oppedahl.com:81/.  Just now it is very much "under construction" but maybe later people will find it to be interesting.  I will be looking for volunteers to send me emails with data-pairs, each data pair providing an A-to-D value and the corresponding human-readable temperature on a webcam image.
Logged

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

Carl,
Thank you for posting the solution in easy to understand instructions and the explanation.
Helped me do a task and understand a little more about Arduino coding (and I'm sure others, too).
KJP
Logged

Pages: [1]   Go Up
Jump to: