Software Serial Esp8266 Web Average ratng: 6,3/10 2076 reviews

Over the past few years, the ESP8266 has been a growing star among IoT or WiFi-related projects. It’s an extremely cost-effective WiFi module that – with a little extra effort – can be programmed to build a standalone web server. How cool is that!

Check out the full changelog Bugs, Issues, Suggestions?Have an idea of how to make The Homebrewery better?Or did you find something that wasn't quite right? You're done!If you want to save ink or have a monochrome printer, add the Ink Friendly snippet to your brew before you print Big things coming in v3.0.0With the next major release of Homebrewery, v3.0.0, this tool will no longer support raw HTML input for brew code. All brews made previous to the release of v3.0.0 will still render normally. New Things All The Time!What's new in the latest update? Printable monster stat blocks 5e. I love what you've done with it, and I have no idea how unbalancing it'd be, but seeing as it's one of the biggest features is the the xenomorphs can strike from anymore.Hit print and enjoy!

How to use SoftwareSerial with ESP8266. Ask Question Asked 8 months ago. Active 7 months ago. It looks like you're using the hardware serial pins with the software serial port. Browse other questions tagged arduino esp8266 software-serial or ask your own question. Feb 11, 2019  Now open your serial monitor and press reset button of your ESP8266 and now your ESP starts connecting to your network, once connected it will give you IP of this ESP. Then open the browser and copy the IP address from serial monitor.

What is a Web server and how it works?

Web server is a place which stores, processes and delivers web pages to Web clients. Web client is nothing but a web browser on our laptops and smartphones. The communication between client and server takes place using a special protocol called Hypertext Transfer Protocol (HTTP).

In this protocol, a client initiates communication by making a request for a specific web page using HTTP and the server responds with the content of that web page or an error message if unable to do so (like famous 404 Error). Pages delivered by a server are mostly HTML documents.

ESP8266 Operating Modes

One of the greatest features ESP8266 provides is that it cannot only connect to an existing WiFi network and act as a Web Server, but it can also set up a network of its own, allowing other devices to connect directly to it and access web pages. This is possible because ESP8266 can operate in three different modes: Station mode, Soft Access Point mode, and both at the same time. This provides possibility of building mesh networks.

Station (STA) Mode

The ESP8266 that connects to an existing WiFi network (one created by your wireless router) is called Station (STA)

In STA mode ESP8266 gets IP from wireless router to which it is connected. With this IP address, it can set up a web server and deliver web pages to all connected devices under existing WiFi network.

Soft Access Point (AP) Mode

The ESP8266 that creates its own WiFi network and acts as a hub (Just like WiFi router) for one or more stations is called Access Point (AP). Unlike WiFi router, it does not have interface to a wired network. So, such mode of operation is called Soft Access Point (soft-AP). Also the maximum number of stations that can connect to it is limited to five.

In AP mode ESP8266 creates a new WiFi network and sets SSID (Name of the network) and IP address to it. With this IP address, it can deliver web pages to all connected devices under its own network.

Wiring – Connecting LEDs to ESP8266 NodeMCU

Now that we know the basics of how web server works, and in which modes ESP8266 can create a web server, it’s time to connect some LEDs to ESP8266 NodeMCU that we want to control over WiFi.

Start by placing the NodeMCU on to your breadboard, ensuring each side of the board is on a separate side of the breadboard. Next, connect two LEDs to digital GPIO D6 and D7 through a 220Ω current limiting resistor.

When you’re done you should have something that looks similar to the illustration shown below.

Concept Behind Controlling Things From ESP8266 Web Server

So, you might be thinking, “How am I going to control things from a web server that merely processes and delivers web pages?” Well, then you need to understand what’s going on behind the scene.

When you type a URL in a web browser and hit ENTER, the browser sends a HTTP request (a.k.a. GET request) to a web server. It’s a job of web server to handle this request by doing something. You might have figured it out by now that we are going to control things by accessing a specific URL. For example, suppose we entered a URL like http://192.168.1.1/ledon in a browser. The browser then sends a HTTP request to ESP8266 to handle this request. When ESP8266 reads this request, it knows that user wants to turn the LED ON. So, it turns the LED ON and sends a dynamic webpage to a browser showing LED status : ON As easy as Pie!

ESP8266 as HTTP Server using WiFi Access Point (AP) mode

Now let’s move on to the interesting stuff!

As the heading suggests, this example demonstrates how to turn the ESP8266 into an access point (AP), and serve up web pages to any connected client. To start with, plug your ESP8266 NodeMCU into your computer and Try the sketch out; and then we will dissect it in some detail.

Accessing the Web Server in AP mode

After uploading the sketch, open the Serial Monitor at a baud rate of 115200. And press the RESET button on ESP8266. If everything is OK, it will show HTTP server started message.

Next, find any device that you can connect to a WiFi network – phone, laptop, etc. And look for a network called NodeMCU. Join the network with password 123456789.

After connecting to your NodeMCU AP network, load up a browser and point it to 192.168.1.1 The NodeMCU should serve up a web page showing current status of LEDs and two buttons to control them. If take a look at the serial monitor at the same time, you can see status of NodeMCU’s GPIO pins.

Now, click the button to turn LED1 ON while keeping an eye on the URL. Once you click the button, the ESP8266 receives a request for /led1on URL. It then turns the LED1 ON and serves a web page with status of LED updated. It also prints the status of GPIO pin on the serial monitor.

You can test LED2 button and check that it works in a similar way.

Now, let’s take a closer look at the code to see how it works, so that you are able to modify it to fulfill your needs.

Detailed Code Explanation

The sketch starts by including ESP8266WiFi.h library. This library provides ESP8266 specific WiFi methods we are calling to connect to network. Following that we also include the ESP8266WebServer.h library, which has some methods available that will help us setting up a server and handle incoming HTTP requests without needing to worry about low level implementation details.

As we are setting the ESP8266 NodeMCU in Access Point (AP) mode, it will create a WiFi network. Hence, we need to set its SSID, Password, IP address, IP subnet mask and IP gateway.

Next, we declare an object of ESP8266WebServer library, so we can access its functions. The constructor of this object takes port (where the server will be listening to) as a parameter. Since 80 is the default port for HTTP, we will use this value. Now you can access the server without needing to specify the port in the URL.

Next, we declare the NodeMCU’s GPIO pins to which LEDs are connected and their initial state.

Inside Setup() Function

We configure our HTTP server before actually running it. First of all, we open a serial connection for debugging purpose and set GPIO ports to OUTPUT.

Then, we set up a soft access point to establish a Wi-Fi network by proving SSID, Password, IP address, IP subnet mask and IP gateway.

In order to handle incoming HTTP requests, we need to specify which code to execute when a particular URL is hit. To do so, we use on method. This method takes two parameters. First one is a URL path and second one is the name of function which we want to execute when that URL is hit.

For example, the first line of below code snippet indicates that when a server receives an HTTP request on the root (/) path, it will trigger the handle_OnConnect() function. Note that the URL specified is a relative path.

Likewise, we need to specify 4 more URLs to handle two states of 2 LEDs.

We haven’t specified what the server should do if the client requests any URL other than specified with server.on() . It should respond with an HTTP status 404 (Not Found) and a message for the user. We put this in a function as well, and use server.onNotFound() to tell it that it should execute it when it receives a request for a URI that wasn’t specified with server.on

Now, to start our server, we call the begin method on the server object.

Inside Loop() Function

To handle the actual incoming HTTP requests, we need to call the handleClient() method on the server object. We also change the state of LED as per the request.

Next, we need to create a function we attached to root (/) URL with server.on. Remember? At the start of this function, we set the status of both the LEDs to LOW (Initial state of LEDs) and print it on serial monitor. In order to respond to the HTTP request, we use the send method. Although the method can be called with a different set of arguments, its simplest form consists of the HTTP response code, the content type and the content.

In our case, we are sending the code 200 (one of the HTTP status codes), which corresponds to the OK response. Then, we are specifying the content type as “text/html“, and finally we are calling SendHTML() custom function which creates a dynamic HTML page containing status of LEDs.

Likewise, we need to create four functions to handle LED ON/OFF requests and 404 Error page.

Displaying the HTML Web Page

SendHTML() function is responsible for generating a web page whenever the ESP8266 web server gets a request from a web client. It merely concatenates HTML code into a big string and returns to the server.send() function we discussed earlier. The function takes status of LEDs as a parameter to dynamically generate the HTML content.

The first text you should always send is the <!DOCTYPE> declaration that indicates that we’re sending HTML code.

Next, the <meta> viewport element makes the web page responsive in any web browser. While title tag sets the title of the page.

Styling the Web Page

Next, we have some CSS to style the buttons and the web page appearance. We choose the Helvetica font, define the content to be displayed as an inline-block and aligned at the center.

Following code then sets color, font and margin around the body, H1, H3 and p tags.

Some styling is applied to the buttons as well with properties like color, size, margin, etc. The ON and OFF button has different background color while :active selector for buttons ensure button click effect

Setting the Web Page Heading

Next, heading of the web page is set; you can change this text to anything that suits your application.

Displaying the Buttons and Corresponding State

To dynamically generate the buttons and LED status, we use if statement. So, depending upon the status of the GPIO pins, ON/OFF button is displayed.

ESP8266 as HTTP Server using WiFi Station (STA) mode

Now let’s move on to our next example which demonstrates how to turn the ESP8266 into Station (STA) mode, and serve up web pages to any connected client under existing network.

Before you head for uploading the sketch, you need to make some changes to make it work for you. You need to modify the following two variables with your network credentials, so that ESP8266 can establish a connection with existing network.

Once you are done, go ahead and try the sketch out.

Accessing the Web Server in STA mode

After uploading the sketch, open the Serial Monitor at a baud rate of 115200. And press the RESET button on ESP8266. If everything is OK, it will output the dynamic IP address obtained from your router and show HTTP server started message.

Next, load up a browser and point it to the IP address shown on the serial monitor. The NodeMCU should serve up a web page showing current status of LEDs and two buttons to control them. If take a look at the serial monitor at the same time, you can see status of NodeMCU’s GPIO pins.

Now, click the button to turn LED1 ON while keeping an eye on the URL. Once you click the button, the ESP8266 receives a request for /led1on URL. It then turns the LED1 ON and serves a web page with status of LED updated. It also prints the status of GPIO pin on the serial monitor.

Newman, Henry Takei, Perry R. Klokkevold, Fermin A. Carranza pdf.

You can test LED2 button and check that it works in a similar way.

Code Explanation

If you observe this code with the previous code, the only difference is that we are not setting the soft Access Point, Instead we are joining existing network using WiFi.begin() function.

While the ESP8266 tries to connect to the network, we can check the connectivity status with WiFi.status() function

Just for your information, this function returns the following statuses:

  • WL_CONNECTED: assigned when connected to a Wi-Fi network
  • WL_NO_SHIELD: assigned when no Wi-Fi shield is present
  • WL_IDLE_STATUS: a temporary status assigned when WiFi.begin() is called and remains active until the number of attempts expires (resulting in WL_CONNECT_FAILED) or a connection is established (resulting in WL_CONNECTED)
  • WL_NO_SSID_AVAIL: assigned when no SSID are available
  • WL_SCAN_COMPLETED: assigned when the scan networks is completed
  • WL_CONNECT_FAILED: assigned when the connection fails for all the attempts
  • WL_CONNECTION_LOST: assigned when the connection is lost
  • WL_DISCONNECTED: assigned when disconnected from a network

Once the ESP8266 is connected to the network, the sketch prints the IP address assigned to ESP8266 by displaying WiFi.localIP() value on serial monitor.

The only difference between AP & STA mode is one creates the network and other joins the existing network. So, rest of the code for handling HTTP requests and serving web page in STA mode is same as that of AP mode explained above. This includes:

  • Declaring NodeMCU’s GPIO pins to which LEDs are connected
  • Defining multiple server.on() methods to handle incoming HTTP requests
  • Defining server.onNotFound() method to handle HTTP 404 error
  • Creating custom functions that are executed when specific URL is hit
  • Creating HTML page
  • Styling the web page
  • Creating buttons and displaying their status

Introduction: The ESP8266 Part 1 - Serial WIFI Module for Arduino

This is the part 1 of 3 instructables to help you to use the ESP8266 with Arduino. In this This first tutorial you will learn how to set-up and test the module connected to an Arduino.

The ESP8266 is perhaps the most versatile serial module to connect 'things' at the Internet, that why it is so popular in the world of IoT. It is a complete module, which includes a microprocessor which can be programmed directly via the Arduino IDE (C++), or in other environments to build (usually using a high level language itself, the 'LUA'). To control 'things' there is no need to have the Arduino itself to the interface because the ESP8266 has 2 GPIOs (two 'pin' input / output). A lot of inportant info is possible to be found at ESP82 Forum:

Link to ESP8266 Forum:

Features:

  • 802.11 b / g / n
  • Wi-Fi Direct (P2P), soft-AP
  • Integrated TCP / IP protocol stack
  • Integrated TR switch, balun, LNA, power amplifier and matching network
  • Integrated PLLs, regulators, DCXO and power management units
  • + 19.5dBm output power in 802.11b mode
  • Power down leakage current of <10uA
  • 1MB Flash Memory
  • Integrated low power 32-bit CPU Could be used the application processor
  • SDIO 1.1 / 2.0, SPI, UART STBC, 1 × 1 MIMO, MIMO 2 × 1
  • A-MPDU & A-MSDU aggregation & 0.4ms guard interval
  • Wake up and transmit packets in <2ms
  • Standby power consumption of <1.0mW (DTIM3)

The above spec includes a lot of technical stuff that you do not really need in the great part of your projects, but it is good to have a hand 'just in case'.

Until today, I only studied the ESP8266 connected to the Arduino, replacing the need for a relatively expensive Wifi shield (Shield, is a PCB that you install at a top of an Arduino to expand its capabilities).

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: The ESP 8266 Specifications and Pins

Well, the first thing you have to do is test the module with AT commands, which is the standard of communication. In general , the module comes from manufactures speaking at a 115,200 baud rate. Sometimes this is complicated, as in the case of Arduino UNO, once only for HW Serial '0' (pins 0 and 1) can work at that speed. The problem is that the PC Serial Monitor also use the same serial port (PC is used here as a generic term, but I hope my Mac does not listen to me ;-). The solution for Arduino UNO is to use the 'SoftwareSerial' library to designate other two generic pins (GPIOs) to be used as a serial port (SW). This works OK as long as the transmission speed is less than 19,200 baud. Perfect! But how to do in the case of ESP8266 come programmed at a faster rate? The way is to reprogram it, of course! BUT, not all Firmware that is loaded into from factory, accept reprogramming module. So the ideal is to upgrade the FW first. Several posts at internet explain how to do it. Here, not to wrap your head around with right speed, firmware, etc., I will simplify it using an Arduino MEGA, that has 4 HW serial ports. So, no worries.

The ports of MEGA:

  • TX0 / RX0 > Pin 1, 0 (same as the UNO) > 'Serial 0'
  • TX1 / RX1 > Pin 18, 19 > 'Serial1'
  • TX2 / RX2 > Pin 16, 17 > 'Serial2'
  • TX3 / RX3 > Pin 14, 15 > 'Serial3'

For my tests, I will use the Serial 2 (pins 16 and 17)

Let's have a close view at the module:

  • Power Source: 3.3V This is very important because the module does not work with 5V and can burn if you insist (rhymed!). The input pins also do not support 5V and when receiving an Arduino signal, it is important to use first a 'voltage level converter' (cute name for the good and old resistors voltage divider). Another important point is to have an independent source of 3.3V. Not always the Arduino can supply the required current for the correct module operation.
  • The module has 6 pins:
    • TX: that it will be connected to RX2's MEGA (can be connected directly, since MEGA has no problem understanding 3.3V as HIGH)
    • RX : TX2 connected to the MEGA via a Level Converter
    • VCC : 3.3V
    • GND : Ground. It is worth remembering that you must connect the GND of ESP8266 to the GND of the MEGA.
    • CH_PD (*) : connected to pin 4 of the MEGA SW reset to start communication
    • RST : Reset generally connects to VCC
    • GPIO0 : open
    • GPIO2 : open

(*) In several sites on the Internet, this pin is directly connected to VCC. In my case, without the 'reset' by SW (puts the pin momentarily LOW), the ESP8266 did not work.

There are adapters on the market to turn the module breadboard friendly, as the physical distance between the ESP8266 pins are not compatible with breadboard holes. I used a simple 'Male / Female' cable type FTDI (see below) for the connection. The colors shown are compatible with the connection diagram.

Step 2: The Circuit to Be Used in Tests

Nothing special in HW diagram. The red plaque is an independent 3.3V source that is coupled to the breadboard. Observe that the yellow wire connected to the TX2 of Arduino, goes through the voltage divider (1K and 2.2K resistors), so when TX2 is HIGH (5V), the ESP8266 receive approx. 3.3V (HIGH for it).

Step 3: TESTING ESP8266 WITH AT COMMANDS

The idea of this sketch is to run tests and set-up the module, allowing you to enter AT commands via the Serial Monitor and see the answer:

In the part of 'comments', are listed the most basic AT commands. When loading the program you will see a lot of trash at Serial Monitor, after the name of the module and finally the word 'ready'. Do not worry, at this time you can send AT commands.

Start with simple 'AT', the module should return 'OK', test other commands and so on.

The picture shows what should appear in the Serial Monitor after you test various commands.

  • AT commands for test examples:
    • AT > ESP8266 returns OK
    • AT + RST > ESP8266 restart and returns OK
    • AT + GMR > ESP8266 returns AT Version; SDK version; id; OK
    • AT + CWMODE? => ESP8266 returns mode type
    • AT + CWLAP > ESP8266 returns close access points
    • AT + CIFSR > ESP8266 returs designed IP
    • AT + CIPMUX = 1 > Set ESP8266 is multiples connections
    • AT + CIOBAUD = 9600 > Change Baudrate > ESP8266 returs OK
    • AT + CIPSERVER = 1.80 > set mode SERVER port: 4040
    • AT + CWMODE = 3 > Connect ESP8266 as combined mode (Access Point (2) and Server (1))
    • AT + CWSAP = 'Acc_Point_name', 'password', wifi_Channel, cript # > j.
    • AT + CWSAP = 'ESP_8266_AP,' 1234 ', 3.0
    • AT + CWJAP = 'SSID', 'password' > Connect to WiFi network

* = AT + CWJAP 'ROVAI TIMECAP', '-1 mjr747'

****************************************************************** /

Bellow the documents related to the tests: the Arduino sketch, and two documents in PDF (one with a complete list of commands and the other with a more complete guide for the module set-up as different modes: web server, access point, etc.):

Attachments

  • ESP8266_WiFi_Module_Quick_Start_Guide_v_1.0.4.pdf

Step 4: Conclusion

In the next 2 Instructables, I will develop how to use an Arduino/ESP8266 as a WebServer and how to trigger LEDs (or anything) remotely using the internet.

Saludos

For more information, please visit my blog:

Be the First to Share

Recommendations

Shield for Arduino From Old Russian VFD Tubes: Clock, Thermometer, Volt Meter.. in Clocks
4 365
Paws to Wash - Cat Meets Covid Handwashing Project in Arduino
  • Work From Home Speed Challenge

  • Sculpting Challenge

  • Clocks Contest