
In this project we will turn LEDs on and off by WIFI with the ESP32 card and a smartphone.
ESP32 board
220Ω resistors
Connecting wires
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() |