The ESP32 is a microcontroller board that has built-in WiFi capabilities. It is equipped with a dual-core processor, and can be programmed using the Arduino Integrated Development Environment (IDE) and the ESP-IDF (ESP32 IoT Development Framework).
The ESP32’s WiFi feature allows it to connect to wireless networks, such as home or office networks, and to other devices that are connected to the same network. This allows the ESP32 to communicate with other devices wirelessly, such as a computer, a smartphone, or another microcontroller board. This feature makes the ESP32 a popular choice for Internet of Things (IoT) projects, home automation, and other applications where wireless communication is needed.
The ESP32’s WiFi feature can be configured in different modes, such as Station mode, Access Point (AP) mode and both Station and AP mode (also known as P2P mode). Station mode allows the ESP32 to connect to a wireless network as a client, while AP mode allows the ESP32 to create its own wireless network that other devices can connect to. P2P mode allows the ESP32 to work as both a client and an access point, so it can connect to an existing network and create its own network at the same time.
The ESP32 also supports various security protocols to protect the communication, such as WPA2-PSK, WPA-PSK, and WEP.
A toy boat equipped with an ESP32 microcontroller board and WiFi capabilities can be a fun and educational project that allows you to control the boat wirelessly using a smartphone. The ESP32 microcontroller board is a low-cost and powerful device that can be programmed using the Arduino Integrated Development Environment (IDE) and the ESP-IDF (ESP32 IoT Development Framework).
The ESP32 board can be used to control the motors of the toy boat. It can also be programmed to communicate with other devices wirelessly, such as a remote control, a smartphone, or a computer, to control the boat’s movements or to receive status updates from the boat.
To build a toy boat equipped with an ESP32 board and WiFi capabilities, you will need to have a basic understanding of electronics and programming. You will also need to have access to the necessary tools and materials, such as a soldering iron, wires, and a power supply.
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.
ESP32
The ESP32 is a series of low-cost, low-power system on a chip (SoC) microcontrollers with integrated Wi-Fi and Bluetooth capabilities. It is developed by Espressif Systems and is based on the ESP32 microcontroller. The ESP32 is a popular choice for IoT projects, home automation, and other applications that require wireless connectivity. It is also commonly used in combination with other sensors and devices to build IoT systems.
pump of water of 5V
To pump water to a toy boat using a 5V power source, you would need a pump that is designed to run on that voltage and a way to connect the pump to the power source and the boat. You may also need additional components such as tubing or valves to direct the flow of water to the boat. It is important to make sure that the pump is rated for the type of water it will be used with (e.g. freshwater or saltwater) and that it is rated for the volume of water that needs to be pumped.
relay
A relay is an electronic switch that is used to control the operation of a water pump or other electrical devices. It is an electromechanical device that uses an electromagnetic coil to open or close a set of contacts, which in turn can control the power supplied to the water pump.
When the relay receives a control signal, such as a current flowing through the electromagnetic coil, it causes the contacts to change state, either opening or closing the circuit that controls the water pump. This allows the water pump to be turned on or off remotely, or based on certain conditions, such as a sensor reading or a timer.
supply power module 5V/3.3V
A power supply module that provides 5V/3.3V is a device that can output both 5V and 3.3V DC voltage from a single input source. These modules are commonly used in electronic projects where different components require different voltages to operate.
9V battery
A 9V battery is a type of primary battery that provides a nominal voltage of 9 volts. It is commonly used in small electronic devices such as radios, smoke detectors, and portable guitar amplifiers.
toy boat
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) |