Create a webserver for controlling a device problem with compiling

Hello people at the Arduino forum. I am using ESP8266 (NodeMCU v1.0, rev 2). I need to control 4 buttons, but for some reason I can not compile the code (later the buttonns will increase to 40).

Can someone help me debug this code.

https://www.dropbox.com/s/gqbmf9cjxuga4qn/Code.txt?dl=0

Please post your program here. See How to use the Forum.

...R

It is more than "9000 characters" which is the reason why I used dropbox.

the code on drop box misses all /, " and *

Now its 8500 characters, so it should work.

// Load Wi-Fi library
#include <ESP8266WiFi.h>

// Replace with your network credentials
const char* ssid     = "MY WIFI NAME";
const char* password = "MY WIFI PASSWORD";

// Set web server port number to 80
WiFiServer server(80);

// Variable to store the HTTP request
String header;

// Auxiliar variables to store the current output state
String output7State = "off";
String output6State = "off";
String output5State = "off";
String output4State = "off";

// Assign output variables to GPIO pins
const int output7 = 7;
const int output6 = 6
const int output5 = 5;
const int output4 = 4;

void setup() {
  Serial.begin(115200);
  // Initialize the output variables as outputs
  pinMode(output7, OUTPUT);
  pinMode(output6, OUTPUT);
  pinMode(output5, OUTPUT);
  pinMode(output4, OUTPUT);
  // Set outputs to LOW
  digitalWrite(output7, LOW);
  digitalWrite(output6, LOW);
  digitalWrite(output5, LOW);
  digitalWrite(output4, LOW);

  // Connect to Wi-Fi network with SSID and password
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  // Print local IP address and start web server
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();
}

void loop(){
  WiFiClient client = server.available();   // Listen for incoming clients

  if (client) {                             // If a new client connects,
    Serial.println("New Client.");          // print a message out in the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == '\n') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();
            
            // turns the GPIOs on and off
      if (header.indexOf("GET /7/on") >= 0) {
              Serial.println("GPIO 7 on");
              output7State = "on";
              digitalWrite(output7, HIGH);
            } else if (header.indexOf("GET /7/off") >= 0) {
              Serial.println("GPIO 7 off");
              output7State = "off";
              digitalWrite(output7, LOW);
            } else if (header.indexOf("GET /6/on") >= 0) {
              Serial.println("GPIO 6 on");
              output6State = "on";
              digitalWrite(output6, HIGH);
            } else if (header.indexOf("GET /6/off") >= 0) {
              Serial.println("GPIO 6 off");
              output6State = "off";
              digitalWrite(output6, LOW);
            }
      
            if (header.indexOf("GET /5/on") >= 0) {
              Serial.println("GPIO 5 on");
              output5State = "on";
              digitalWrite(output5, HIGH);
            } else if (header.indexOf("GET /5/off") >= 0) {
              Serial.println("GPIO 5 off");
              output5State = "off";
              digitalWrite(output5, LOW);
            } else if (header.indexOf("GET /4/on") >= 0) {
              Serial.println("GPIO 4 on");
              output4State = "on";
              digitalWrite(output4, HIGH);
            } else if (header.indexOf("GET /4/off") >= 0) {
              Serial.println("GPIO 4 off");
              output4State = "off";
              digitalWrite(output4, LOW);
            }
            
            // Display the HTML web page
            client.println("<!DOCTYPE html><html>");
            client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
            client.println("<link rel=\"icon\" href=\"data:,\">");
            // CSS to style the on/off buttons 
            // Feel free to change the background-color and font-size attributes to fit your preferences
            client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
            client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
            client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
            client.println(".button2 {background-color: #77878A;}</style></head>");
      client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
            client.println(".button3 { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
            client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
            client.println(".button4 {background-color: #77878A;}</style></head>");
            
            // Web Page Heading
            client.println("<body><h1>ESP8266 Web Server</h1>");
            
                  // Display current state, and ON/OFF buttons for GPIO 7  
            client.println("<p>GPIO 7 - State " + output7State + "</p>");
            // If the output7State is off, it displays the ON button       
            if (output7State=="off") {
              client.println("<p><a href=\"/7/on\"><button class=\"button4\">ON</button></a></p>");
            } else {
              client.println("<p><a href=\"/7/off\"><button class=\"button4 button3\">OFF</button></a></p>");
            } 
               
            // Display current state, and ON/OFF buttons for GPIO 6  
            client.println("<p>GPIO 6 - State " + output6State + "</p>");
            // If the output6State is off, it displays the ON button       
            if (output6State=="off") {
              client.println("<p><a href=\"/6/on\"><button class=\"button3\">ON</button></a></p>");
            } else {
              client.println("<p><a href=\"/6/off\"><button class=\"button4 button3\">OFF</button></a></p>");
            }
      
            // Display current state, and ON/OFF buttons for GPIO 5  
            client.println("<p>GPIO 5 - State " + output5State + "</p>");
            // If the output5State is off, it displays the ON button       
            if (output5State=="off") {
              client.println("<p><a href=\"/5/on\"><button class=\"button\">ON</button></a></p>");
            } else {
              client.println("<p><a href=\"/5/off\"><button class=\"button button2\">OFF</button></a></p>");
            } 
               
            // Display current state, and ON/OFF buttons for GPIO 4  
            client.println("<p>GPIO 4 - State " + output4State + "</p>");
            // If the output4State is off, it displays the ON button       
            if (output4State=="off") {
              client.println("<p><a href=\"/4/on\"><button class=\"button\">ON</button></a></p>");
            } else {
              client.println("<p><a href=\"/4/off\"><button class=\"button button2\">OFF</button></a></p>");
            }
            client.println("</body></html>");
            
            // The HTTP response ends with another blank line
            client.println();
            // Break out of the while loop
            break;
          } else { // if you got a newline, then clear currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    // Clear the header variable
    header = "";
    // Close the connection
    client.stop();
    Serial.println("Client disconnected.");
    Serial.println("");
  }
}

one more ; and it compiles

Juraj:
one more ; and it compiles

On which line?

Edit: after putting the semicolons on line "const int output6 = 6;". I still get "error exit status 1".

Are you using the Arduino IDE? In v 1.8.5 the line with the error is highlighted when I compile for an ESP8266.

exit status 1
expected ',' or ';' before 'const'

When I add the ; it compiles.

ArakelTheDragon:
It is more than "9000 characters"

Then add your .ino file as an attachment.

...R

Arduino IDE 1.8.5.
Windows 7 Pro 64bit, original.

Here is the “.ino” file and here is the error:

"C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WiFi\src/ESP8266WiFiGeneric.h:27:22: fatal error: functional: No such file or directory

#include "

The whole error is:

Arduino: 1.8.5 (Windows 7), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, 4M (1M SPIFFS), v2 Lower Memory, Serial, None, All Flash Contents, 115200"

In file included from sketch\Working good web server controls 2 LEDs only on port 80 10.11.2018.c:7:0:

C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WiFi\src/ESP8266WiFi.h:27:8: error: expected identifier or '(' before string constant

 extern "C" {

        ^

In file included from C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266/IPAddress.h:25:0,

                 from C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WiFi\src/ESP8266WiFi.h:31,

                 from sketch\Working good web server controls 2 LEDs only on port 80 10.11.2018.c:7:

C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266/Printable.h:25:1: error: unknown type name 'class'

 class Print;

 ^

C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266/Printable.h:33:1: error: unknown type name 'class'

 class Printable {

 ^

C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266/Printable.h:33:17: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token

 class Printable {

                 ^

In file included from C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WiFi\src/ESP8266WiFi.h:31:0,

                 from sketch\Working good web server controls 2 LEDs only on port 80 10.11.2018.c:7:

C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266/IPAddress.h:29:1: error: unknown type name 'class'

 class IPAddress: public Printable {

 ^

C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266/IPAddress.h:29:16: error: expected '=', ',', ';', 'asm' or '__attribute__' before ':' token

 class IPAddress: public Printable {

                ^

C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\cores\esp8266/IPAddress.h:98:1: error: unknown type name 'IPAddress'

 extern const IPAddress INADDR_NONE;

 ^

In file included from C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WiFi\src/ESP8266WiFi.h:33:0,

                 from sketch\Working good web server controls 2 LEDs only on port 80 10.11.2018.c:7:

C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WiFi\src/ESP8266WiFiType.h:101:5: error: unknown type name 'WiFiMode'

     WiFiMode oldMode;

     ^

C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WiFi\src/ESP8266WiFiType.h:102:5: error: unknown type name 'WiFiMode'

     WiFiMode newMode;

     ^

C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WiFi\src/ESP8266WiFiType.h:107:5: error: unknown type name 'String'

     String ssid;

     ^

C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WiFi\src/ESP8266WiFiType.h:108:5: error: unknown type name 'uint8'

     uint8 bssid[6];

     ^

C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WiFi\src/ESP8266WiFiType.h:109:5: error: unknown type name 'uint8'

     uint8 channel;

     ^

C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WiFi\src/ESP8266WiFiType.h:114:5: error: unknown type name 'String'

     String ssid;

     ^

C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WiFi\src/ESP8266WiFiType.h:115:5: error: unknown type name 'uint8'

     uint8 bssid[6];

     ^

C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WiFi\src/ESP8266WiFiType.h:116:5: error: unknown type name 'WiFiDisconnectReason'

     WiFiDisconnectReason reason;

     ^

C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WiFi\src/ESP8266WiFiType.h:121:5: error: unknown type name 'uint8'

     uint8 oldMode;

     ^

C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WiFi\src/ESP8266WiFiType.h:122:5: error: unknown type name 'uint8'

     uint8 newMode;

     ^

C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WiFi\src/ESP8266WiFiType.h:127:5: error: unknown type name 'IPAddress'

     IPAddress ip;

     ^

C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WiFi\src/ESP8266WiFiType.h:128:5: error: unknown type name 'IPAddress'

     IPAddress mask;

     ^

C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WiFi\src/ESP8266WiFiType.h:129:5: error: unknown type name 'IPAddress'

     IPAddress gw;

     ^

C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WiFi\src/ESP8266WiFiType.h:134:5: error: unknown type name 'uint8'

     uint8 mac[6];

     ^

C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WiFi\src/ESP8266WiFiType.h:135:5: error: unknown type name 'uint8'

     uint8 aid;

     ^

C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WiFi\src/ESP8266WiFiType.h:140:5: error: unknown type name 'uint8'

     uint8 mac[6];

     ^

C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WiFi\src/ESP8266WiFiType.h:141:5: error: unknown type name 'uint8'

     uint8 aid;

     ^

C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WiFi\src/ESP8266WiFiType.h:147:5: error: unknown type name 'uint8'

     uint8 mac[6];

     ^

In file included from C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WiFi\src/ESP8266WiFiSTA.h:28:0,

                 from C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WiFi\src/ESP8266WiFi.h:34,

                 from sketch\Working good web server controls 2 LEDs only on port 80 10.11.2018.c:7:

C:\Users\ArakelTheDragon\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.2\libraries\ESP8266WiFi\src/ESP8266WiFiGeneric.h:27:22: fatal error: functional: No such file or directory

 #include <functional>

                      ^

compilation terminated.

exit status 1
Error compiling for board NodeMCU 1.0 (ESP-12E Module).

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Wifi_control_LEDs.ino (8.25 KB)

you have still something strange in you ino file before #include. delete all lines until #include and eventually type again the #include line

Does not work. The strange part is that when I copy the following code into the first “.ino” file it does not work. When I copy it in the blink example, it works.

/*********
  Rui Santos
  Complete project details at http://randomnerdtutorials.com  
*********/

// Load Wi-Fi library
#include <ESP8266WiFi.h>

// Replace with your network credentials
const char* ssid     = "UPC3042654";
const char* password = "PAMEKZNN";

// Set web server port number to 80
WiFiServer server(80);

// Variable to store the HTTP request
String header;

// Auxiliar variables to store the current output state
String output5State = "off";
String output4State = "off";

// Assign output variables to GPIO pins
const int output5 = 5;
const int output4 = 4;

void setup() {
  Serial.begin(115200);
  // Initialize the output variables as outputs
  pinMode(output5, OUTPUT);
  pinMode(output4, OUTPUT);
  // Set outputs to LOW
  digitalWrite(output5, LOW);
  digitalWrite(output4, LOW);

  // Connect to Wi-Fi network with SSID and password
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  // Print local IP address and start web server
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();
}

void loop(){
  WiFiClient client = server.available();   // Listen for incoming clients

  if (client) {                             // If a new client connects,
    Serial.println("New Client.");          // print a message out in the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == '\n') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();
            
            // turns the GPIOs on and off
            if (header.indexOf("GET /5/on") >= 0) {
              Serial.println("GPIO 5 on");
              output5State = "on";
              digitalWrite(output5, HIGH);
            } else if (header.indexOf("GET /5/off") >= 0) {
              Serial.println("GPIO 5 off");
              output5State = "off";
              digitalWrite(output5, LOW);
            } else if (header.indexOf("GET /4/on") >= 0) {
              Serial.println("GPIO 4 on");
              output4State = "on";
              digitalWrite(output4, HIGH);
            } else if (header.indexOf("GET /4/off") >= 0) {
              Serial.println("GPIO 4 off");
              output4State = "off";
              digitalWrite(output4, LOW);
            }
            
            // Display the HTML web page
            client.println("<!DOCTYPE html><html>");
            client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
            client.println("<link rel=\"icon\" href=\"data:,\">");
            // CSS to style the on/off buttons 
            // Feel free to change the background-color and font-size attributes to fit your preferences
            client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
            client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
            client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
            client.println(".button2 {background-color: #77878A;}</style></head>");
            
            // Web Page Heading
            client.println("<body><h1>ESP8266 Web Server</h1>");
            
            // Display current state, and ON/OFF buttons for GPIO 5  
            client.println("<p>GPIO 5 - State " + output5State + "</p>");
            // If the output5State is off, it displays the ON button       
            if (output5State=="off") {
              client.println("<p><a href=\"/5/on\"><button class=\"button\">ON</button></a></p>");
            } else {
              client.println("<p><a href=\"/5/off\"><button class=\"button button2\">OFF</button></a></p>");
            } 
               
            // Display current state, and ON/OFF buttons for GPIO 4  
            client.println("<p>GPIO 4 - State " + output4State + "</p>");
            // If the output4State is off, it displays the ON button       
            if (output4State=="off") {
              client.println("<p><a href=\"/4/on\"><button class=\"button\">ON</button></a></p>");
            } else {
              client.println("<p><a href=\"/4/off\"><button class=\"button button2\">OFF</button></a></p>");
            }
            client.println("</body></html>");
            
            // The HTTP response ends with another blank line
            client.println();
            // Break out of the while loop
            break;
          } else { // if you got a newline, then clear currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    // Clear the header variable
    header = "";
    // Close the connection
    client.stop();
    Serial.println("Client disconnected.");
    Serial.println("");
  }
}

when I copy the following code into the first ".ino" file

What is the "first .ino file"?

The code attached on post#9 compiles for me.

from sketch\Working good web server controls 2 LEDs only on port 80 10.11.2018.c:7:

Is your sketch named:
Working good web server controls 2 LEDs only on port 80 10.11.2018.c
and not:
Working good web server controls 2 LEDs only on port 80 10.11.2018.ino
?

Using the .c extension will skip all of the Arudino core additions.