2

I am using ESP8266 01 board to send some data via TCP to server on local network. My code is:

#include <ESP8266WiFi.h>
const char* ssid = "ssid";
const char* password = "pas";
const char* host = "192.168.1.103";
void setup() {
 Serial.begin(115200);
 delay(10);
 // Wifi connection
 Serial.println();
 Serial.println();
 Serial.print("Connecting to ");
 Serial.println(ssid);
 WiFi.begin(ssid, password);
 while (WiFi.status() != WL_CONNECTED) {
 delay(500);
 Serial.print(".");
 }
 Serial.println("");
 Serial.println("WiFi connected"); 
 Serial.println("IP address: ");
 Serial.println(WiFi.localIP());
 delay(5000);
 Serial.print("connecting to ");
 Serial.println(host);
 // Use WiFiClient class to create TCP connections
 WiFiClient client;
 const int port = 8888;
 if (!client.connect(host, port)) 
 {
 Serial.println("connection failed");
 return;
 }
}
void loop() {
 while(Serial.available() == 0)
 {
 //wait for arduino serial data
 }
 while(Serial.available())
 {
 // send data to server
 client.print(Serial.read());
 }
}

After chacking code in Arduino IDE I am getting error message:

In function 'void loop()':
espwifitcp1:57: error: 'client' was not declared in this scope
 client.print(Serial.read());
 ^
exit status 1
'client' was not declared in this scope

It appears that if I declare WiFiClient client; inside setup() function it can't be accessed outside it. Do I correctly understand the problem? I have tried to declare object outside setup() and loop() functions but it got even more errors. Any suggestions?

asked May 1, 2017 at 0:00

2 Answers 2

3

You have declared WiFiClient client; in the scope of setup() only. Move it's declaration to the global scope by placing it above the declaration of void setup()

answered May 1, 2017 at 0:06
1
  • Or write WiFiClient instead of WifiClient X_X Commented Feb 16, 2020 at 15:38
-1

in the header include WifiClient.h

Juraj
18.3k4 gold badges31 silver badges49 bronze badges
answered Jun 7, 2019 at 8:34
1
  • 1
    ESP8266WiFi.h includes WiFiClient.h Commented Jun 7, 2019 at 8:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.