
In this project, we will order a small boat equipped with the ESP32 card via the Wifi network.
This is why we are going to create two micropython programs for the ESP32 board.
For the servomotor:
we connect the yellow wire to pin 2 of the ESP32 board
connect the red wire to the 5V pin of the power supply module
connect the black wire to the GND pin of the ESP32 board.
For relay:
we connect pin (S) to pin 23 of the ESP32 board
we connect pin (+) to the 3.3V pin of the ESP32 board
we connect the pin (-) to the GND pin of the ESP32 board
Connect the COM pin to the (+) terminal of the water pump
Connect the NO pin to the 5V terminal of the power supply module
For the water pump: Connect the (-) terminal to the GND pin of the ESP32 board.
Here are 3 micropython programs that allow you to connect the ESP32 card to the smartphone via the wifi network and to receive a message containing the command order of the boat.
boot.py:
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 |
# Complete project details at https://RandomNerdTutorials.com import machine try: import usocket as socket except: import socket from machine import Pin import network from servo import Servo import esp esp.osdebug(None) import gc gc.collect() ssid = '*************' # pour la connexion de la carte ESP32 au réseau wifi password = '*************' station = network.WLAN(network.STA_IF) station.active(True) station.connect(ssid, password) while station.isconnected() == False: pass print('Connection successful') print(station.ifconfig()) pompe = Pin(2, Pin.OUT) servo_pin = machine.Pin(23) my_servo = Servo(servo_pin) |
main.py:
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 |
# Complete project details at https://RandomNerdTutorials.com def web_page(): if pompe.value() == 1: gpio_state="ON" else: gpio_state="OFF" 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: #e7bd3b; border: none; border-radius: 4px; color: white; padding: 16px 30px; text-decoration: none; font-size: 20px; margin: 2px; cursor: pointer;} .button2{background-color: red;}</style></head><body> <h1>ESP Web Server</h1> <p>GPIO state: <strong>""" + gpio_state + """</strong></p><p><a href="/?pompe=on"><button class="button">Avant</button></a></p> <table><tr><td><p><a href="/?pompe=gauche"><button class="button">Gauche</button></a></p></td><td><p><a href="/?pompe=off"><button class="button button2">Stop</button></a></p></td> <td><p><a href="/?pompe=droite"><button class="button">Droite</button></a></p></td> </tr></table></body></html>""" return html s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(('', 80)) s.listen(5) 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) pompe_on = request.find('/?pompe=on') pompe_off = request.find('/?pompe=off') pompe_droite = request.find('/?pompe=droite') pompe_gauche = request.find('/?pompe=gauche') if pompe_on == 6: print('LED ON') pompe.value(1) my_servo.write_angle(90) if pompe_off == 6: print('LED OFF') pompe.value(0) if pompe_droite == 6: print('LED ON') pompe.value(1) my_servo.write_angle(45) if pompe_gauche == 6: print('LED ON') pompe.value(1) my_servo.write_angle(135) 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() |
servo.py:
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 |
from machine import PWM import math # originally by Radomir Dopieralski http://sheep.art.pl # from https://bitbucket.org/thesheep/micropython-servo class Servo: """ A simple class for controlling hobby servos. Args: pin (machine.Pin): The pin where servo is connected. Must support PWM. freq (int): The frequency of the signal, in hertz. min_us (int): The minimum signal length supported by the servo. max_us (int): The maximum signal length supported by the servo. angle (int): The angle between the minimum and maximum positions. """ def __init__(self, pin, freq=50, min_us=600, max_us=2400, angle=180): self.min_us = min_us self.max_us = max_us self.us = 0 self.freq = freq self.angle = angle self.pwm = PWM(pin, freq=freq, duty=0) def write_us(self, us): """Set the signal to be ``us`` microseconds long. Zero disables it.""" if us == 0: self.pwm.duty(0) return us = min(self.max_us, max(self.min_us, us)) duty = us * 1024 * self.freq // 1000000 self.pwm.duty(duty) def write_angle(self, degrees=None, radians=None): """Move to the specified angle in ``degrees`` or ``radians``.""" if degrees is None: degrees = math.degrees(radians) degrees = degrees % 360 total_range = self.max_us - self.min_us us = self.min_us + total_range * degrees // self.angle self.write_us(us) |