members
Basics: Project 075z ESP8266 ESP-12E module - MicroPython Web Server
of Acoptex.com in ESP8266 ESP-12
Basics: Project 075z
Project name: ESP8266 ESP-12E module - MicroPython Web Server
Tags: ESP, ESP8266, WI FI module, ESP-12E, Ai Thinker, AI-Cloud, SOC, GPIO, General Purpose Input Output, System On a Chip, IOT, internet of things, FTDI232, FTDI 232, LoLin NODEMCU V3, NODEMCU, V3, Development Board with USB-to-Serial Onboard, MicroPython, MicroPython Web Server
Attachment: bootsketch, mainsketch
In this project, you need these parts :
1. ESP8266 ESP-12E module with micro USB cable 1pc
2.Jumper cables F-M
3. Resistor 2 pcs (220 Ohm each)
4.LED 2 pcs (can be of different color)
5. Breadboard 1 pc
6. uPyCraft IDE (you can download and read more about it here)
General
We will learn how to build a web server to control the ESP8266 ESP-12E module outputs using MicroPython.
We will build a web server to control the LEDs and use sockets with the Python socket API.
In order to program the ESP8266 ESP-12E module with MicroPython you need to:
- Install uPyCraft IDE in your PC;
- Upload MicroPython firmware to your ESP8266 ESP-12E module
Understanding the ESP8266 ESP-12E WI FI module (LoLin NODEMCU V3)
You can read more about it here.
TX - transmit pin. GPIO pin
RX - receive pin. GPIO pin
3V3 (or 3V or 3.3V) - power supply pin (3-3.6V).
GND ( or G) - ground pin.
RST - reset pin. Keep it on high (3.3V) for normal operation. Put it on 0V to reset the chip.
EN - Chip enable. Keep it on high (3.3V) for normal operation.
Vin - External power supply 5VDC.
D0-D8 - GPIO (General Purpose Input Output) pins
D5-D8 - SPI interface
D1-D2– I²C/TWI Interface
SC (or CMD) - (Chip Select) - the pin that the master can use to enable and disable specific devices. GPIO pin
SO (or SDO) - Master In Slave Out (MISO) - SPI communication. The Slave line for sending data to the master. GPIO pin
SK (or CLK) - SCK (Serial Clock) - SPI communication.The clock pulses which synchronize data transmission generated by the master. GPIO pin
S1 (or SD1) - Master Out/Slave In (MOSI). SPI communication. The Master line for sending data to the peripherals. GPIO pin
S2 (or SD2) - GPIO pin
S3 (or SD3) - GPIO pin
VU (or VUSB) - external power 5VDC.
A0 - ADC output.
RSV - reserved
Wiring
Step by Step instruction
- Install uPyCraft IDE on your PC;
- Plug the ESP8266 ESP-12E module to your PC and wait for the drivers to install (or install manually any that might be required).
- ESP8266 ESP-12E module - Uploading MicroPython firmware.
- Open uPyCraft IDE. Double-click uPyCraft_V1.1.exe file. A new window opens with the uPyCraft IDE software.
- Go to Tools -> Board. If you are using ESP8266 ESP-12E module select esp8266.
- Go to Tools -> Serial and select your ESP8266 ESP-12E module COM port (in our case it’s COM8). If you don’t see the COM Port, you need to install the ESP32 CP210x USB to UART Bridge VCP Drivers , check your USB cable too (it should be data cable).
- You have established a serial communication with your board. The >>> should appear in the Shell window after a successful connection with your board.
- Press New file button to create a new file.
- Press Save file button to save the file in your PC. Input file name main.py and press ok button.
- You should see the following in your uPyCraft IDE -the boot.py file in your device and a new tab with the main.py file.
- Press Download and run button to upload the file to your ESP8266 ESP-12E module. The device directory should now load the main.py file. Your ESP8266 ESP-12E module has two files - boot.py and main.py stored now.
- You should see a message download ok in the Shell window.
- Copy the following bootsketch to the Editor on the boot.py file. Modify SSID and password for you local network to be able to connect to your router.
- Press Save button to save the changes.
- Press Stop button to stop any script from running in your board.
- Press Download and Run button to upload the script to the ESP8266 ESP-12E module.
- You should see a message download ok in the Shell window.
- Copy the following mainsketch to the Editor on the main.py file.
- Press Save button to save the changes.
- Press Stop button to stop any script from running in your board.
- Press Download and Run button to upload the script to the ESP8266 ESP-12E module.
- You should see a message download ok in the Shell window.
- Press the ESP8266 ESP-12E module on-board EN button to reboot it.
- After a few seconds it should establish a connection with your router and print the IP address on the Shell.
- Open your browser (IE, Google Chrome or Microsoft Edge) and type your ESP8266 ESP-12E module IP address you have just observed (In our case it is 192.168.0.125). You should see the web server page as shown below. We tested with Microsoft Edge and Google Chrome (was loading faster then with Google Chrome)
- When you press the ON button, you make a request on the ESP8266 ESP-12E module IP address followed by /led1=on or /led2=on. The corresponding LED turns on and its GPIO state updated on the page.
- When you press the OFF button, you make a request on the ESP8266 ESP-12E module IP address followed by /led1=off or /led2=off. The corresponding LED turns off and its GPIO state updated on the page.
Code
1. bootsketch
We create our web server using sockets and the Python socket API. First we import the socket library:
try: import usocket as socket
except: import socket
We need to import the Pin class from the machine module to be able to interact with the GPIOs: from machine import Pin
We need to import the network library. The network library allows us to connect the ESP8266 ESP-12E module to a Wi-Fi network: import network
The following lines turn off vendor OS debugging messages: import esp esp.osdebug(None)
We run a garbage collector: import gc gc.collect()
A garbage collector is a form of automatic memory management. This is a way to reclaim memory occupied by objects that are no longer in used by the program. This is useful to save space in the flash memory.
The following variables hold your network credentials: ssid='' and password=''. You need to replace them with your local network access data.
Then we set the ESP8266 ESP-12E module as a Wi-Fi station: station = network.WLAN(network.STA_IF) and activate it: station.active(True)
ESP8266 ESP-12E module connects to your router using the SSID and password defined earlier: station.connect(ssid, password)
The following statement ensures that the code doesn’t proceed while the ESP is not connected to your network: while station.isconnected() == False: pass
After a successful connection print network interface parameters like the ESP8266 ESP-12E module IP address – use the ifconfig() method on the station object:
print('Connection successful')
print(station.ifconfig())
Create a Pin objects called led1 and led2 - outputs for the ESP8266 ESP-12E module GPIO5 and GPIO4 accordingly:
led1 = Pin(5, Pin.OUT)
led2 = Pin(4, Pin.OUT)
2. mainsketch
The script starts by creating a function called web_page(). This function returns a variable called html that contains the HTML text to build the web page: def web_page():
The web page displays the current GPIO states. So, before generating the HTML text, we need to check the LEDs state. We save LEDs state on the gpio_state1 and gpio_state2 variable:
if led1.value() == 1:
gpio_state1="ON"
else:
gpio_state1="OFF"
if led2.value() == 1:
gpio_state2="ON"
else:
gpio_state2="OFF"
The gpio_state1 and gpio_state2 variable is incorporated into the HTML text using "+" signs to concatenate strings.
After creating the HTML to build the web page we need to create a listening socket to listen for incoming requests and send the HTML text in response. The following picture shows how to create sockets for server-client interaction:
Create a socket using socket.socket() and specify the socket type. We create a new socket object called s with the given address family and socket type. This is a STREAM TCP socket: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Then we bind the socket to an address (network interface and port number) using the bind() method. The bind()method accepts a tupple variable with the ip address, and port number: s.bind(('', 80))
In our project, we are passing an empty string ' ' as an IP address and port 80. In this case, the empty string refers to the localhost IP address (this means the ESP8266 ESP-12E module IP address).
The next line enables the server to accept connections; it makes a socket to listen. The argument specifies the maximum number of queued connections. The maximum is 5: s.listen(5)
In the while loop is where we listen for requests and send responses. When a client connects, the server calls the accept() method to accept the connection. When a client connects, it saves a new socket object to accept and send data on the conn variable, and saves the client address to connect to the server on the addr variable: conn, addr = s.accept()
Then we print the address of the client saved on the addrvariable: print('Got a connection from %s' % str(addr))
The data exchange between the client and server done using the send() and recv() methods.
The following line gets the request received on the newly created socket and saves it in the request variable.
request = conn.recv(1024)
The recv() method receives the data from the client socket (remember that we’ve created a new socket object on the conn variable). The argument of the recv() method specifies the maximum data that can be received at once.
The next line simply prints the content of the request:
print('Content = %s' % str(request))
Then we create a variable called response that contains the HTML text returned by the web_page() function: response = web_page()
Finally we send the response to the socket client using the send() method: conn.send(response) and close the created socket: conn.close()
Summary
We have learnt how to build a web server to control the ESP8266 ESP-12E module outputs using MicroPython.
Libraries
- None
Sketch
- See attachments on the begining of this project
Other projects of Acoptex.com










jobs.viewed