Skip to content

[MKC-1824] Correct errors in Alvik User Manual #2156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ ucPack libraries from the [ucPack repository](https://github.com/arduino/ucPack-
6. Select the "Arduino-alvik" and move it inside the "lib" folder in your Alvik.
![Setting the FW path on the Labs for micropython](assets/moving_alvik_folder.png)

7. Go back to the main folder and select the "ucPack-mpy-main" folder and move it next to the arduino_alvik inside the "lib" folder in your Nano ESP32.
7. Go back to the main folder and select the "ucPack" folder found inside "ucPack-mpy-main" and move it next to the arduino_alvik inside the "lib" folder in your Nano ESP32.
![Setting the FW path on the Labs for micropython](assets/moving_ucPack.png)

8. Now go back to the main root of the files system on the Nano ESP32. Then in your local folder navigate to the examples folder once there, select the following files and move them to the main folder of the ESP32.
Expand All @@ -319,7 +319,7 @@ With this last step, your Nano ESP32 has been set up with the Alvik out of the b

### Updating Alvik's Body (STM32)

1. Download the [pre-compiled firmware](https://github.com/arduino-libraries/Arduino_AlvikCarrier/releases/latest) from the [Alvik Carrier GitHub reposiitory](https://github.com/arduino-libraries/Arduino_AlvikCarrier)
1. Download the [pre-compiled firmware](https://github.com/arduino-libraries/Arduino_AlvikCarrier/releases/latest) from the [Alvik Carrier GitHub repository](https://github.com/arduino-libraries/Arduino_AlvikCarrier)

This step will download a "firmware_x_x_x.bin" file, save it in your Alvik folder

Expand All @@ -329,7 +329,7 @@ With this last step, your Nano ESP32 has been set up with the Alvik out of the b
3. Let's move now the "firmware_x_x_x.bin" to the main root.
![Setting the FW path on the Labs for micropython](assets/moving_fw_bin.png)

4. Turn ON your alvik, go to the Editor tab and tun the following commands by typing them and clicking on the "Play" button
4. Turn ON your alvik, go to the Editor tab and run the following commands by typing them and clicking on the "Play" button

```
from arduino_alvik import update_firmware
Expand Down Expand Up @@ -1094,7 +1094,6 @@ By following these steps, you can control the LEDs on the Arduino Alvik robot to

## Talking With Other Devices!


### WiFi

The ESP32 on the Arduino Alvik robot includes built-in Wi-Fi capabilities, enabling it to connect to wireless networks and communicate with other devices over the internet. This can be particularly useful for remote control applications. In this example, we'll set up the Alvik robot to connect to a Wi-Fi network, host a web server, and provide a simple web interface with buttons to control the robot's movements.
Expand All @@ -1107,7 +1106,6 @@ The provided code will:

### Step By Step Setup


1. Replace `Insert_SSID_Here` and `Password_Here` with your Wi-Fi credentials.
2. Upload the script to the Arduino Nano ESP32.
3. Connect to the robot's IP address (printed on the console) using a web browser.
Expand All @@ -1124,7 +1122,7 @@ from arduino_alvik import ArduinoAlvik
from time import sleep_ms

# Wi-Fi credentials
SSID = "InErt_SSID_Here"
SSID = "Insert_SSID_Here"
PASSWORD = "Password_Here"

# Connect to Wi-Fi
Expand Down Expand Up @@ -1186,13 +1184,16 @@ html = """
We finally set up the web server, note that the selected port should be valid for your network with no conflicts. This can be tuned to the setting that best works for your network configuration.

```python
#EADDRINUSE erroris prompted change port
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
# Specify a port
port = 8080
addr = socket.getaddrinfo('0.0.0.0', port)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(5)

print('Listening on', addr)
s.settimeout(5)

print(f'Listening on {sta_if.ifconfig()[0]}:{port}')
```

**Handling HTTP Requests**
Expand All @@ -1201,26 +1202,47 @@ The following code handles incoming HTTP requests and moves the robot in the spe

```python
def handle_request(conn):
request = conn.recv(1024)
request = str(request)
print("Request:", request)

if '/up' in request:
alvik.set_wheels_speed(30, 30)
elif '/down' in request:
alvik.set_wheels_speed(-30, -30)
elif '/left' in request:
alvik.set_wheels_speed(-30, 30)
elif '/right' in request:
alvik.set_wheels_speed(30, -30)
else:
alvik.brake()
try:
request = conn.recv(1024).decode('utf-8')
first_line = request.split('\n')[0] # Get the first line of the request
path = first_line.split(' ')[1] # Extract the path (e.g., "/up")

# Strip query strings (e.g., "/up?")
if '?' in path:
path = path.split('?')[0]
print(f"Request path: {path}")

# Ignore favicon requests
if path == '/favicon.ico':
conn.send('HTTP/1.1 204 No Content\n')
conn.send('Connection: close\n\n')
return

# Control the robot based on the request path
if path == '/up':
alvik.set_wheels_speed(30, 30)
elif path == '/down':
alvik.set_wheels_speed(-30, -30)
elif path == '/left':
alvik.set_wheels_speed(-30, 30)
elif path == '/right':
alvik.set_wheels_speed(30, -30)
else:
alvik.brake()

# Send the response for valid paths
conn.send('HTTP/1.1 200 OK\n')
conn.send('Content-Type: text/html\n')
conn.send('Connection: close\n\n')
conn.sendall(html)

conn.send('HTTP/1.1 200 OK\n')
conn.send('Content-Type: text/html\n')
conn.send('Connection: close\n\n')
conn.sendall(html)
conn.close()
except OSError as e:
if e.errno == 104: # ECONNRESET error
print("Connection reset by client.")
else:
print(f"Error: {e}")
finally:
conn.close()
```

**Main Loop**
Expand All @@ -1231,9 +1253,17 @@ The following code runs the main loop to handle incoming connections and process
# Main loop to handle incoming connections
try:
while True:
conn, addr = s.accept()
print('Connection from', addr)
handle_request(conn)
try:
conn, addr = s.accept()
print('Connection from', addr)
handle_request(conn)
except OSError as e:
if e.errno == 116: # ETIMEDOUT error
print("Waiting for connection...")
elif e.errno == 104: # ECONNRESET error
print("Connection reset by client.")
else:
print(f"Accept error: {e}")
except KeyboardInterrupt:
print("Server stopped")
s.close()
Expand All @@ -1242,6 +1272,135 @@ except KeyboardInterrupt:

This section ensures the server keeps running, accepting incoming connections, and handling requests until the script is interrupted. If an `EADDRINUSE` error occurs, change the port number from 80 to an unused port, like 8080, in the `addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]` line. Also, make sure the device you access the web interface on should be on the same network as the Alvik.


**Full Code**
```python
import network
import socket
from arduino_alvik import ArduinoAlvik
from time import sleep_ms

# Wi-Fi credentials
SSID = "Arduino_IoT"
PASSWORD = "ArduinoIoT2021"

# Connect to Wi-Fi
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect(SSID, PASSWORD)

# Wait for connection
while not sta_if.isconnected():
pass

print("Connected to WiFi. IP address:", sta_if.ifconfig()[0])

# Initialize the robot
alvik = ArduinoAlvik()
alvik.begin()
sleep_ms(5000)

# HTML for the web interface
html = """
<!DOCTYPE html>
<html>
<head>
<title>Alvik Robot Control</title>
</head>
<body>
<h1>Control Alvik Robot</h1>
<form action="/up">
<button type="submit">Up</button>
</form>
<form action="/down">
<button type="submit">Down</button>
</form>
<form action="/left">
<button type="submit">Left</button>
</form>
<form action="/right">
<button type="submit">Right</button>
</form>
</body>
</html>
"""

# Specify a port
port = 8080
addr = socket.getaddrinfo('0.0.0.0', port)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(5)

s.settimeout(5)

print(f'Listening on {sta_if.ifconfig()[0]}:{port}')

# Function to handle incoming HTTP requests
def handle_request(conn):
try:
request = conn.recv(1024).decode('utf-8')
first_line = request.split('\n')[0] # Get the first line of the request
path = first_line.split(' ')[1] # Extract the path (e.g., "/up")

# Strip query strings (e.g., "/up?")
if '?' in path:
path = path.split('?')[0]
print(f"Request path: {path}")

# Ignore favicon requests
if path == '/favicon.ico':
conn.send('HTTP/1.1 204 No Content\n')
conn.send('Connection: close\n\n')
return

# Control the robot based on the request path
if path == '/up':
alvik.set_wheels_speed(30, 30)
elif path == '/down':
alvik.set_wheels_speed(-30, -30)
elif path == '/left':
alvik.set_wheels_speed(-30, 30)
elif path == '/right':
alvik.set_wheels_speed(30, -30)
else:
alvik.brake()

# Send the response for valid paths
conn.send('HTTP/1.1 200 OK\n')
conn.send('Content-Type: text/html\n')
conn.send('Connection: close\n\n')
conn.sendall(html)

except OSError as e:
if e.errno == 104: # ECONNRESET error
print("Connection reset by client.")
else:
print(f"Error: {e}")
finally:
conn.close()

# Main loop to handle incoming connections
try:
while True:
try:
conn, addr = s.accept()
print('Connection from', addr)
handle_request(conn)
except OSError as e:
if e.errno == 116: # ETIMEDOUT error
print("Waiting for connection...")
elif e.errno == 104: # ECONNRESET error
print("Connection reset by client.")
else:
print(f"Accept error: {e}")
except KeyboardInterrupt:
print("Server stopped")
s.close()
alvik.stop()

```

### ESP-NOW

The ESP32 on the Arduino Alvik robot also supports ESP-NOW, a fast, connectionless communication protocol that enables direct, quick, and low-power control of smart devices without the need for a router. ESP-NOW can work alongside Wi-Fi and Bluetooth LE, making it versatile for various applications. It supports the ESP8266, ESP32, ESP32-S, and ESP32-C series. In this example, we'll set up the Alvik robot to receive commands via ESP-NOW.
Expand Down
Loading