'client' was not declared in this scope

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.

#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!”);

}

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

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".

EthernetServer server(81); EthernetClient client;

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.

#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’
");

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!”);

}

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

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():wink: before you print "hello world. Also, you are using quote tags instead of code tags (#) to post your code

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.

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

This also works for WiFiClient.

Outside of Loop put WiFiClient client;
Inside of loop put client = server.available();

Thank you oppedahl.

not sure what it wants here? if anyone can help i would greatly appreciate it.

'MqttClient' was not declared in this scope

ifdef USE_EMULATION

UdpDisconnect();

endif // USE_EMULATION

if (Settings.flag.mqtt_enabled) { MqttClient.disconnect(); }