ESP8266 Web server

Hello, has anyone worked with this?

Is it 5 Volt I/O logic or not?

that board has ESP8266 MCU on board, which means it works on 3.3V logic levels

Does this work: https://www.instructables.com/id/Using-the-ESP8266-module/

like a webserver? I mean to hit the IP on the browser and get response? And I mean to program it with "clear" C/C++ Arduino code, not AT commands as it shows here: http://www.ardumotive.com/iot-wifi-temp-and-humidity.html

yes It does

you'll likely need a handy programming adapter like this

and in the Arduino IDE, you select 'Generic ESP8266 Module' under Tools > Boards

The Node MCU or the Wemos boards (like the Wemos D1) are much easier to use than the bare modules. I suggest going with them.

I bought this:

The question is, in order to program it, do I need any special device? Or I just connect to to the Arduino through the Serial (TX,RX) and send the AT commands?

The one you bought has s/w on it to use it as a slave to another Arduino.
You can re-program it with new code but then you need a usb to 3v serial adapter.
Also, in order to use with another Arduino, you need to make sure the other Arduino is using 3v signals.

I would use a module like what was in your fist post and run all the sketch code on the esp part rather than have some of on the esp module and most of it another Arduino.
With that other module, you can use the Arduino IDE with a USB cable to upload sketches to it and run all the code on it with no other Arduino.

--- bill

Also, in order to use with another Arduino, you need to make sure the other Arduino is using 3v signals.

Thanks for your answer! It has a level shifter module… 3.3 <–> 5, so I believe it safe to connect it directly to Arduino’s I/O UART pins, right?

OP- What are you trying to do?

SteveMann:
OP- What are you trying to do?

Basically to learn how to use ESP8266 as a webserver + using a LED to switch it on/off from my browser.

alex5678:
Thanks for your answer! It has a level shifter module… 3.3 <–> 5, so I believe it safe to connect it directly to Arduino’s I/O UART pins, right?

It would appear so, as long as you use their adapter.
If you want to write sketches for it vs use AT commands from another Arduino to control the esp module, you will need a USB to TTL adapter to allow you to download sketches to it using the IDE.

alex5678:
Basically to learn how to use ESP8266 as a webserver + using a LED to switch it on/off from my browser.

My recommendation would be to not mess with an other Arduino using AT commands to talk to / control the esp board.
Not sure what Arduino you were planning on using, but the esp module is MUCH more powerful than any of the AVR based Arduinos. Tons more memory and the ability to have a local file system in the FLASH, which makes things like web server sketch code MUCH easier and simpler.
I would highly recommend writing the sketch and running it directly on the esp module as there is no need to use another Arduino since the esp modules can run sketches directly.
esp modules are much faster, have tons more resources and can do things that are not possible on other Arduino modules.
IMO, for something like a web server, using another Arduino to talk AT commands or LUA to the esp just complicates things and gets in the way, not to mention will be slower and have less capabilities than just running all the code on the esp.

I would recommend getting a Wemos D1 mini or a NodeMCU (the one in your original post)
Those boards can be programmed using the IDE and micro usb cable and have more i/o pins than the module you have.

My preference is the Wemos D1 mini for my own projects.
It is small with enough pins for most projects and there are shields and base boards which can be useful for some projects.

— bill

I get this error when compiling:

fatal error: ESP8266WiFi.h: No such file or directory

I am trying to include the library from here:

and it says that the folder does not contain a libary!!!

Has anyone, any idea what to do??

@update: Now it shows me this:

java.io.IOException: The compressed file does not contain a library
	at processing.app.Base.handleAddLibrary(Base.java:2417)
	at processing.app.Base$6.actionPerformed(Base.java:1124)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
	at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
	at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:842)
	at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:886)
	at java.awt.Component.processMouseEvent(Component.java:6539)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
	at java.awt.Component.processEvent(Component.java:6304)
	at java.awt.Container.processEvent(Container.java:2239)
	at java.awt.Component.dispatchEventImpl(Component.java:4889)
	at java.awt.Container.dispatchEventImpl(Container.java:2297)
	at java.awt.Component.dispatchEvent(Component.java:4711)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
	at java.awt.Container.dispatchEventImpl(Container.java:2283)
	at java.awt.Window.dispatchEventImpl(Window.java:2746)
	at java.awt.Component.dispatchEvent(Component.java:4711)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
	at java.awt.EventQueue.access$500(EventQueue.java:97)
	at java.awt.EventQueue$3.run(EventQueue.java:709)
	at java.awt.EventQueue$3.run(EventQueue.java:703)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
	at java.awt.EventQueue$4.run(EventQueue.java:733)
	at java.awt.EventQueue$4.run(EventQueue.java:731)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
The compressed file does not contain a library

possibly you haven't installed ESP8266 cores into the Arduino IDE

follow this link

Thank you, I have done this! I am compiling the code in Arduino pro mini 5V/16Mhz. And the ESP8266 is connected through TX, RX to the nano. Any ideas?

So you entered this board manager url:
http://arduino.esp8266.com/stable/package_esp8266com_index.json
in your IDE preferences?

Then installed installed the esp8266 board package using the IDE Boards Manager ?

What version of the esp8266 platform have you installed?

I was using 2.7.1 and just now updated to the latest 2.7.4 and it seems to work ok for me.

— bill

alex5678:
Thank you, I have done this! I am compiling the code in Arduino pro mini 5V/16Mhz. And the ESP8266 is connected through TX, RX to the nano. Any ideas?

I don't use esp8266 devices this way. Never have.
IMO, it doesn't make sense as it is way too limiting and impossible to use some of more advanced useful features in the esp8266 platform like the SPIFFS file system or to integrate the WiFi manager.
I run all the code on the esp8266 and use the native esp8266 APIs and libraries and don't have any other processor involved.

If you are compiling code for an AVR based Arduino, you don't need the ESP8266 platform tools installed and you cannot use any of the esp8266 platform APIs or esp8266 libraries on the Arduino pro min even if you did install them.
If you are trying to use LUA then you need LUA tools.

Based on what you said you wanted to do in post #9, then see my post #10.

I still recommend that you use only the esp8266 and run all the sketch code directly on the esp8266 device.
I'd use one of the esp8266 devices that includes a usb port like a NodeMCU or a WeMOS D1 mini.
Those are the simplest to play with as you can write sketch code use the Arduino to upload it to the esp8266 module and all the esp8266 libraries and examples can be used.
There are also MANY esp8266 projects out there that you can play round with that work this way using native code , including web servers, weather stations, IFTT examples, and tons of other IoT projects.

If you want to get more advanced, you could do things like:

  • configure wireless uploading to the esp8266
  • configure things for gdb to get source level debugging.

--- bill

bperrybap:
I don't use esp8266 devices this way. Never have.
IMO, it doesn't make sense as it is way too limiting and impossible to use some of more advanced useful features in the esp8266 platform like the SPIFFS file system or to integrate the WiFi manager.
I run all the code on the esp8266 and use the native esp8266 APIs and libraries and don't have any other processor involved.

Amen to that!

This is the actual programming adapter for the ESP-01:

Alex bought the wrong one!

You can use the cheaper - USB or otherwise - adapters to actually run your code and swap in and out of the programming adapter to load it.

Ok I bought the programming USB. I am trying this code form here:

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

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

// Replace with your network credentials
const char* ssid     = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_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 output5State = "off";
String output4State = "off";

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

// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0; 
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;

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
    currentTime = millis();
    previousTime = currentTime;
    while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
      currentTime = millis();         
      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("");
  }
}

Where I changed the ssid and the password to my home’s network. And although I see the blue LED of the programmer flashing, I get this message:

esptool.py v2.8
Serial port COM8
Connecting........_____....._____....._____....._____....._____....._____.....____Traceback (most recent call last):
  File "C:\Users\alex\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.3/tools/upload.py", line 65, in <module>
    esptool.main(cmdline)
  File "C:/Users/alex/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.7.3/tools/esptool\esptool.py", line 2890, in main
    esp.connect(args.before)
  File "C:/Users/alex/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.7.3/tools/esptool\esptool.py", line 483, in connect
    raise FatalError('Failed to connect to %s: %s' % (self.CHIP_NAME, last_error))
esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header
esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header

Someone??

"Connecting......................................____"

This usually means you selected the wrong serial port in the IDE, or something else has control of that port.

What is a programming USB? Do you mean the programming adapter Paul recommended? Make sure the switch is in the PROG position when you plug the adapter into the USB port.

Also, run the Device Manager and expand the Ports line as shown below. This will show you the port number when you plug in the adapter.

device_manager.jpg

device_manager.jpg