A very simple code error

Hi All
I have tried void server and a few other combinations all with the same error, I know it’s just bad coding but I do not know how to solve it. could somebody point me in the right direction.
Thank you in advance.

void printP1();
void server.available(); // Line with error

void printP1(){
WiFiClient client = server.available(); //Line with error
client.println(“HTTP/1.1 200 OK”);
client.println(“Content-Type: text/html”);
client.println(“Connection: close”); // the conn

platformio run
src\main.cpp: In function ‘void printP1()’:
src\main.cpp:73:21: error: ‘server’ was not declared in this scope
WiFiClient client = server.available();

Error GCC expected initializer before ‘.’ token 9:12
Error Build error: expected initializer before ‘.’ tokene 9:12
Error GCC ‘server’ was not declared in this scope 73:21
Error Build error: ‘server’ was not declared in this scopee 73:21

Your function name may not include the . character as per C++ language definitions.

I think what you’re trying to do is instantiating an object named server and call its member function .available(). If you have a server object (globally) then you do not need to add a function prototype for that member function.

Can you post your full code?

what i copy is not what is shown?

why is a simple cut and paste not working?

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

void printP1();
void server.available();

String theme = “darkly”;
const char* ssid = "
const char* password =
float humid,bmp_t,bmp_a,bmp_p;
float hic,fahr,hif,dPt;
int lightv,light2;
float temp,temp2;

IPAddress ip(192, 168, 1, 202); // ip of Client
IPAddress gateway(192,168,1,254); // gateway of your network
IPAddress subnet(255,255,255,0);
IPAddress dns(192, 168, 1, 254);

void setup() {
Serial.begin(115200);
Serial.println(“Booting”);
WiFi.config(ip, dns, gateway, subnet);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println(“Connection Failed! Rebooting…”);
delay(5000);
ESP.restart();
}
ArduinoOTA.onStart( {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = “sketch”;
else // U_SPIFFS
type = “filesystem”;

// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);

});
ArduinoOTA.onEnd( {
Serial.println(“\nEnd”);
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf(“Progress: %u%%\r”, (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println(“Auth Failed”);
else if (error == OTA_BEGIN_ERROR) Serial.println(“Begin Failed”);
else if (error == OTA_CONNECT_ERROR) Serial.println(“Connect Failed”);
else if (error == OTA_RECEIVE_ERROR) Serial.println(“Receive Failed”);
else if (error == OTA_END_ERROR) Serial.println(“End Failed”);
});
ArduinoOTA.begin();
Serial.println(“Ready”);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}

void loop() {
// put your main code here, to run repeatedly:
ArduinoOTA.handle();
delay(2000);
printP1();
}

void printP1(){
WiFiClient client = server.available();
client.println(“HTTP/1.1 200 OK”);
client.println(“Content-Type: text/html”);
client.println(“Connection: close”); // the connection will be closed after completion of the response//

client.println();
client.println(“”);

client.println(“”);

client.println(“”);
client.println(“”);

client.println(“ESP8266 & DHT22 Sensor LoRa ”);

client.println(“\n”);

client.println(“

”);
client.println(“
”);
client.println(“
”);
client.println(“

ESP8266 & DHT22 Sensor LoRa

”);
client.println(“
”);
client.println(“
”);
client.print("Humidity (%) : “);
client.print(humid, 2);
client.print(” Atmospheric pressure : ");
client.println(bmp_p, 2);

client.print("Temperature (C) : “);
client.print(temp, 2);
client.print(” Temperature (C) : ");
client.println(bmp_t, 2);
client.print("Temperature (F) : “);
client.print((float)fahr, 2);
client.print(” Temperature (F) : ");
bmp_t=bmp_t*1.8+32;
//bmp_t=bmp_t+32;
client.println(bmp_t, 2);

client.print(“Heat Index : “);
client.print(hic);
client.print(” (C) “);
client.print(hif);
client.println(” (F)”);
client.print(“Dew PointFast (C) : “);
client.println(dPt);
client.print(“Light (%) : “);
client.println(lightv);
client.println(”

”);
client.println(”
”);
client.println(“
”);
client.println(“
”);
client.println(“”);
client.println(“”);

client.println(“”);
client.println(“”);
client.println(“* {box-sizing: border-box}”);

client.println(“.container {”);
client.println(“width: 100%;”);
client.println(“background-color: #1c1c23;”);
client.println(“}”);

client.println(“.skills {”);
client.println(“text-align: center;”);
client.println(“padding-right: 20px;”);
client.println(" line-height: 20px;“);
//client.println(“color: white;”);
client.println(”}");

client.print(“.html {width: “);
client.print(humid, 2);
client.println(”%; background-color: #4CAF50;}”);

client.print(“.css {width: “);
client.print(temp2, 2);
client.println(”%; background-color: #2196F3;}”);

client.print(“.js {width: “);
client.print(light2);
client.println(”%; background-color: #f44336;}”);

client.println(“”);
client.println(“”);
client.println(“”);

client.println(“

Humidity

”);
client.println(“
”);
client.print(“
”);
client.print(humid, 2);
client.println(“%
”);
client.println(“
”);

client.println(“

Temperature

”);
client.println(“
”);
client.print(“
”);
client.print(temp, 2);
client.println(“C
”);
client.println(“
”);

client.println(“

Light

”);
client.println(“
”);
client.print(“
”);
client.print(lightv);
client.println(“%
”);
client.println(“
”);
}

Sorry but I cannot post my code here what is here is junk
how do you post full code here?

You can edit your previous posts and to use markdown.

In the line before the code add a new line with three backticks followed by “cpp”) and at the end add a new line three backticks. See the Markdown reference.


#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>


void printP1();
void server.available();

String  theme = "darkly";
const char* ssid = 
const char* password = 
float humid,bmp_t,bmp_a,bmp_p;
float hic,fahr,hif,dPt;
int lightv,light2;
float temp,temp2;

IPAddress ip(192, 168, 1, 202);  // ip of Client
IPAddress gateway(192,168,1,254);           // gateway of your network
IPAddress subnet(255,255,255,0);
IPAddress dns(192, 168, 1, 254);

void setup() {
  Serial.begin(115200);
  Serial.println("Booting");
  WiFi.config(ip, dns, gateway, subnet);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("Connection Failed! Rebooting...");
    delay(5000);
    ESP.restart();
  }
  ArduinoOTA.onStart([]() {
    String type;
    if (ArduinoOTA.getCommand() == U_FLASH)
      type = "sketch";
    else // U_SPIFFS
      type = "filesystem";

    // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
    Serial.println("Start updating " + type);
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
    else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
    else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
    else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
    else if (error == OTA_END_ERROR) Serial.println("End Failed");
  });
  ArduinoOTA.begin();
  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
    // put your main code here, to run repeatedly:
    ArduinoOTA.handle();
    delay(2000);
    printP1();
}

void printP1(){
WiFiClient client = server.available();
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");  // the connection will be closed after completion of the response//

client.println();
client.println("<!DOCTYPE html>");

client.println("<html charset=UTF-8><head><meta http-equiv='refresh' content='20' name='viewport' content='width=device-width, initial-scale=1'/>");

client.println("<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script><script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js'></script>");
client.println("<link href='https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/");
client.println(theme);
client.println("/bootstrap.min.css' rel='stylesheet'>");


client.println("<title>ESP8266 & DHT22 Sensor LoRa </title>");


client.println("</head>\n<body>");

client.println("<div class='container'>");
client.println("<div class='panel panel-default' margin:45px>");
client.println("<div class='panel-heading'>");
client.println("<H2>ESP8266 & DHT22 Sensor LoRa </H2>");
client.println("<div class='panel-body' style='background-color: black'>");
client.println("<pre>");
client.print("Humidity (%)       : ");
client.print(humid, 2);
client.print("               Atmospheric pressure  : ");
client.println(bmp_p, 2);

client.print("Temperature (C)    : ");
client.print(temp, 2);
client.print("               Temperature (C)       : ");
client.println(bmp_t, 2);
client.print("Temperature (F)    : ");
client.print((float)fahr, 2);
client.print("               Temperature (F)       : ");
bmp_t=bmp_t*1.8+32;
//bmp_t=bmp_t+32;
client.println(bmp_t, 2);

client.print("Heat Index         : ");
client.print(hic);
client.print(" (C) ");
client.print(hif);
client.println(" (F)");
client.print("Dew PointFast (C)  : ");
client.println(dPt);
client.print("Light (%)          : ");
client.println(lightv);
client.println("</pre>");
client.println("</div>");
client.println("</div>");
client.println("</div>");
client.println("<html>");
client.println("<head>");

client.println("<meta name='viewport' content='width=device-width, initial-scale=1'>");
client.println("<style>");
client.println("* {box-sizing: border-box}");

client.println(".container {");
client.println("width: 100%;");
client.println("background-color: #1c1c23;");
client.println("}");

client.println(".skills {");
client.println("text-align: center;");
client.println("padding-right: 20px;");
client.println(" line-height: 20px;");
//client.println("color: white;");
client.println("}");

client.print(".html {width: ");
client.print(humid, 2);
client.println("%; background-color: #4CAF50;}");

client.print(".css {width: ");
client.print(temp2, 2);
client.println("%; background-color: #2196F3;}");

client.print(".js {width: ");
client.print(light2);
client.println("%; background-color: #f44336;}");


client.println("</style>");
client.println("</head>");
client.println("<body>");

client.println("<p>Humidity</p>");
client.println("<div class='container'>");
client.print("<div class='skills html'>");
client.print(humid, 2);
client.println("%</div>");
client.println("</div>");

client.println("<p>  Temperature</p>");
client.println("<div class='container'>");
client.print("<div class='skills css'>");
client.print(temp, 2);
client.println("C</div>");
client.println("</div>");

client.println("<p>  Light</p>");
client.println("<div class='container'>");
client.print("<div class='skills js'>");
client.print(lightv);
client.println("%</div>");
client.println("</div>");
}

got it thank you for the pointer

You have not declared your server object. You must first instantiate such a object of the wanted type.

From reading your code I presume that you want a TCP server on which you will reply with the HTTP response. For this, I added the declaration WiFiServer server(80); at the beginning. Also don’t forget to call server.begin() after connecting to the WiFi and that server.available() returns a WiFiClient object on which you have to figure out if it’s connected or not.

Try the code.

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

void printP1();

String  theme = "darkly";
const char* ssid = "BTHub5-RKSM";
const char* password = "********* REPLACE WITH PW *******";
float humid,bmp_t,bmp_a,bmp_p;
float hic,fahr,hif,dPt;
int lightv,light2;
float temp,temp2;

IPAddress ip(192, 168, 1, 202);  // ip of Client
IPAddress gateway(192,168,1,254);           // gateway of your network
IPAddress subnet(255,255,255,0);
IPAddress dns(192, 168, 1, 254);

//Declare a server to be opened at port 80 (HTTP)
WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  Serial.println("Booting");
  WiFi.config(ip, dns, gateway, subnet);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("Connection Failed! Rebooting...");
    delay(5000);
    ESP.restart();
  }
  ArduinoOTA.onStart([]() {
    String type;
    if (ArduinoOTA.getCommand() == U_FLASH)
      type = "sketch";
    else // U_SPIFFS
      type = "filesystem";

    // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
    Serial.println("Start updating " + type);
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
    else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
    else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
    else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
    else if (error == OTA_END_ERROR) Serial.println("End Failed");
  });
  ArduinoOTA.begin();
  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  //Start the TCP server
  server.begin();
}

void loop() {
    // put your main code here, to run repeatedly:
    ArduinoOTA.handle();
    delay(2000);
    printP1();
}

void printP1(){
	//Check if a client has connected. Answer HTTP then.
	WiFiClient client = server.available();
	//There may or may not be a client connected.
	//Only act on connected clients.
	if(!client)
		return;

	//Wait until some data was received
	while(!client.available()){
		delay(1);
	}

	//We are now certain that there is a client connected. Send him the HTTP response.
	client.println("HTTP/1.1 200 OK");
	client.println("Content-Type: text/html");
	client.println("Connection: close");  // the connection will be closed after completion of the response//

	client.println();
	client.println("<!DOCTYPE html>");

	client.println("<html charset=UTF-8><head><meta http-equiv='refresh' content='20' name='viewport' content='width=device-width, initial-scale=1'/>");

	client.println("<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script><script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js'></script>");
	client.println("<link href='https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/");
	client.println(theme);
	client.println("/bootstrap.min.css' rel='stylesheet'>");


	client.println("<title>ESP8266 & DHT22 Sensor LoRa </title>");


	client.println("</head>\n<body>");

	client.println("<div class='container'>");
	client.println("<div class='panel panel-default' margin:45px>");
	client.println("<div class='panel-heading'>");
	client.println("<H2>ESP8266 & DHT22 Sensor LoRa </H2>");
	client.println("<div class='panel-body' style='background-color: black'>");
	client.println("<pre>");
	client.print("Humidity (%)       : ");
	client.print(humid, 2);
	client.print("               Atmospheric pressure  : ");
	client.println(bmp_p, 2);

	client.print("Temperature (C)    : ");
	client.print(temp, 2);
	client.print("               Temperature (C)       : ");
	client.println(bmp_t, 2);
	client.print("Temperature (F)    : ");
	client.print((float)fahr, 2);
	client.print("               Temperature (F)       : ");
	bmp_t=bmp_t*1.8+32;
	//bmp_t=bmp_t+32;
	client.println(bmp_t, 2);

	client.print("Heat Index         : ");
	client.print(hic);
	client.print(" (C) ");
	client.print(hif);
	client.println(" (F)");
	client.print("Dew PointFast (C)  : ");
	client.println(dPt);
	client.print("Light (%)          : ");
	client.println(lightv);
	client.println("</pre>");
	client.println("</div>");
	client.println("</div>");
	client.println("</div>");
	client.println("<html>");
	client.println("<head>");

	client.println("<meta name='viewport' content='width=device-width, initial-scale=1'>");
	client.println("<style>");
	client.println("* {box-sizing: border-box}");

	client.println(".container {");
	client.println("width: 100%;");
	client.println("background-color: #1c1c23;");
	client.println("}");

	client.println(".skills {");
	client.println("text-align: center;");
	client.println("padding-right: 20px;");
	client.println(" line-height: 20px;");
	//client.println("color: white;");
	client.println("}");

	client.print(".html {width: ");
	client.print(humid, 2);
	client.println("%; background-color: #4CAF50;}");

	client.print(".css {width: ");
	client.print(temp2, 2);
	client.println("%; background-color: #2196F3;}");

	client.print(".js {width: ");
	client.print(light2);
	client.println("%; background-color: #f44336;}");


	client.println("</style>");
	client.println("</head>");
	client.println("<body>");

	client.println("<p>Humidity</p>");
	client.println("<div class='container'>");
	client.print("<div class='skills html'>");
	client.print(humid, 2);
	client.println("%</div>");
	client.println("</div>");

	client.println("<p>  Temperature</p>");
	client.println("<div class='container'>");
	client.print("<div class='skills css'>");
	client.print(temp, 2);
	client.println("C</div>");
	client.println("</div>");

	client.println("<p>  Light</p>");
	client.println("<div class='container'>");
	client.print("<div class='skills js'>");
	client.print(lightv);
	client.println("%</div>");
	client.println("</div>");

	//Make sure all the data got out
	client.flush();
	//Kill the connection to the client. (This may or may not be necessary, since the browser may already have closed the connection)
	client.stop();
}

However, when you want to open a web server, why not use the dedicated ESP8266WebServer class? Very easy to use and more powerful (can handle different URLs, etc…). Look at the example sketch and the github repository.

PS: I’ve also removed the WiFi password from the code. You can edit your previous posts (using the pencil button under your post) to remove that data, too.

1 Like

WOW thank you for your time and help.
I also understand what you’re saying which make the change for me.
I will look at the example and thank you for pointing me in the right direction
:grinning: