An ESP32 web server is a server that runs on an ESP32 microcontroller and allows other devices to connect to it using the HTTP protocol. The ESP32 can be programmed to serve web pages, which can be accessed from a web browser on a computer or mobile device connected to the same network as the ESP32.
To create an ESP32 web server, you will need to:
It is important to note that the ESP32 has limited resources, so the web server should be designed to be as efficient as possible and handle a moderate amount of traffic. Also, it is important to secure the web server by using authentication and encryption to prevent unauthorized access to the device.
To control LEDs using Wi-Fi with an ESP32 web server, you will need to:
In this project we will turn LEDs on and off by WIFI with the ESP32 card and a smartphone.
ESP32
ESP32 is a series of low-cost, low-power system-on-a-chip microcontrollers with integrated Wi-Fi and dual-mode Bluetooth. The ESP32 series of microcontrollers is developed by Espressif Systems, a Chinese company based in Shanghai.
The ESP32 is designed for use in a wide range of applications, including Internet of Things (IoT) devices, home automation systems, and wearable electronics. It features a dual-core processor, 4MB of flash memory, and 520KB of SRAM. It also includes a number of peripheral interfaces, such as SPI, I2C, UART, and PWM, as well as a built-in touch sensor and Hall effect sensor.
The ESP32 also supports a range of wireless connectivity options, including Wi-Fi (IEEE 802.11b/g/n), Bluetooth 4.2 and 5.0, and Bluetooth Low Energy (BLE). The Wi-Fi module supports both station and access point modes, making it easy to connect to existing networks or create your own.
The ESP32 can be programmed using the Arduino IDE, the ESP-IDF, or the MicroPython firmware. It also supports OTA(over the air) firmware updates, making it easy to update the device’s firmware without having to physically access it.
Overall, the ESP32 is a powerful, low-cost, and versatile microcontroller that can be used for a wide range of applications.
Red LED – Yellow LED – Green LED
An LED, or light-emitting diode, is a type of semiconductor device that emits light when a current is passed through it. LEDs are widely used in a variety of applications, including electronic devices, automotive lighting, and traffic signals.
LEDs are made up of a semiconductor material, such as silicon or gallium arsenide, that forms a diode. When a voltage is applied to the diode, electrons are able to recombine with holes, which releases energy in the form of light. The color of the light emitted by an LED depends on the materials used to make the diode. For example, red LEDs are made from gallium arsenide phosphide, while blue and green LEDs are made from gallium nitride.
LEDs are known for their energy efficiency, long lifespan, and fast switching speeds. They use less power than traditional incandescent bulbs, and last for tens of thousands of hours. They also have the ability to switch on and off very quickly, which makes them ideal for use in high-speed communication systems and digital displays.
LEDs are very versatile, they can be used in different ways and forms, they can be used in single or multiple units, they can be used in different voltages and currents. And they can be controlled by a microcontroller like the ESP32, to create different effects.
resistance of 220Ω
Resistance is the property of a material that opposes the flow of electric current through it. It is measured in units of ohms (Ω). The resistance of a material is determined by its composition and the temperature at which it is operating.
In the case of an LED, using a current-limiting resistor is important to protect the LED from damage and to ensure that it operates within its safe range of forward current and voltage.
Connecting wires
Connecting wires refers to the process of physically connecting wires or cables to a device or circuit in order to establish an electrical connection. This can be done by using various connectors such as plugs, sockets, or terminal blocks. The wires are typically color-coded to indicate their function, such as red for power, black for ground, and yellow for signals.
Test plate
A test plate is a type of circuit board that is used to test electronic components. It typically consists of a flat board made of a non-conductive material, such as plastic or fiberglass, with a number of holes or pads that are used to connect electronic components. The test plate allows you to connect electronic components and test them easily.
To perform the assembly, you can connect the yellow LED resistor to pin D22, the green LED resistor to pin D21 and the red LED resistor to pin D23 of the ESP32 board.
Copy the following code to the ESP32 boot.py file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
try: import usocket as socket except: import socket from machine import Pin import network import esp esp.osdebug(None) import gc gc.collect() ssid = "************" #ssid du wifi password = "************" #mot de passe du wifi station = network.WLAN(network.STA_IF) station.active(True) station.connect(ssid, password) while station.isconnected() == False: pass print('Connection successful') print(station.ifconfig()) led_verte = Pin(21, Pin.OUT) led_jaune = Pin(22, Pin.OUT) led_rouge = Pin(23, Pin.OUT) |
Copy the following code into the ESP32 main.py file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
web_page(): html = """<html><head> <title>ESP Web Server</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="icon" href="data:,"> <style>html{font-family: Helvetica; display:inline-block; margin: 0px auto; text-align: center;} h1{color: #0F3376; padding: 2vh;}p{font-size: 1.5rem;}.button{display: inline-block; background-color: #009933; border: none; border-radius: 4px; color: white; padding: 16px 40px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;} .button2{background-color: #e7bd3b;} .button3{background-color: #ff0000;}</style></head><body> <h1>ESP Web Server</h1> <p><a href="/?led=verte"><button class="button">LED verte</button></a></p> <p><a href="/?led=jaune"><button class="button button2">LED jaune</button></a></p> <p><a href="/?led=rouge"><button class="button button3">LED rouge</button></a></p></body></html>""" return html s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(('', 80)) s.listen(5) allumer_verte=0 allumer_jaune=0 allumer_rouge=0 while True: conn, addr = s.accept() print('Got a connection from %s' % str(addr)) request = conn.recv(1024) request = str(request) print('Content = %s' % request) verte = request.find('/?led=verte') jaune = request.find('/?led=jaune') rouge = request.find('/?led=rouge') if verte == 6: if allumer_verte==0: print('VERTE') led_verte.value(1) allumer_verte=1 else: led_verte.value(0) allumer_verte=0 if jaune == 6: if allumer_jaune==0: print('JAUNE') led_jaune.value(1) allumer_jaune=1 else: led_jaune.value(0) allumer_jaune=0 if rouge == 6: if allumer_rouge==0: print('ROUGE') led_rouge.value(1) allumer_rouge=1 else: led_rouge.value(0) allumer_rouge=0 response = web_page() conn.send('HTTP/1.1 200 OK\n') conn.send('Content-Type: text/html\n') conn.send('Connection: close\n\n') conn.sendall(response) conn.close() |