import network
import socket
from time import sleep
import machine
from machine import PWM ,SPI
from machine import Pin
import utime
ssid = 'Airbox-B6B8'
password = 'qynL7GcFbWrd'
led = Pin("LED" , Pin.OUT)
in1 = Pin(0,Pin.OUT)
in2 = Pin(1,Pin.OUT)
in3 = Pin(2,Pin.OUT)
in4 = Pin(3,Pin.OUT)
on-led =Pin(4 , Pin.OUT)
off-led =Pin(5 , Pin.OUT)
servo = PWM(Pin(6))
servo.freq(50)
servo.duty_u16(0)
angle = 0
i = 90
def map(value, in_min, in_max, out_min, out_max):
value = max(in_min, min(in_max, value))
return (value - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
def rotate(angle):
servo.duty_u16(int(map(angle , 0 , 180 , 1500 , 8000)))
class SSD1351:
def __init__(self, width, height, spi, dc, cs, res):
self.width = width
self.height = height
self.spi = spi
self.dc = dc
self.cs = cs
self.res = res
self.buffer = bytearray(width * height * 2) # 2 octets par pixel (16 bits de couleur)
def command(self, data):
self.dc.off()
self.cs.off()
self.spi.write(data)
self.cs.on()
def data(self, data):
self.dc.on()
self.cs.off()
self.spi.write(data)
self.cs.on()
def reset(self):
self.res.off()
time.sleep_ms(50)
self.res.on()
time.sleep_ms(50)
def init_display(self):
self.reset()
self.command(b'\xFD\x12') # Set Command Lock
# Configurer d'autres commandes d'initialisation ici
def show(self):
self.command(b'\x15\x00\x7F\x75\x00\x7F') # Set Column Address
self.command(b'\x75\x00\x7F\x15\x00\x7F') # Set Row Address
self.data(self.buffer)
def draw_rectangle(self, x, y, width, height, color):
for i in range(height):
for j in range(width):
self.pixel(x + j, y + i, color)
spi = SPI(1, baudrate=10000000, polarity=0, phase=0, sck=Pin(10), mosi=Pin(11))
dc = Pin(9, Pin.OUT)
cs = Pin(8, Pin.OUT)
res = Pin(12, Pin.OUT)
oled = SSD1351(128, 128, spi, dc, cs, res)
# Initialiser l'écran OLED
oled.init_display()
def connect(): # function to connect to wifi
wlan = network.WLAN(network.STA_IF) # Station mode / acces point mode
wlan.active(True) #activate Wlan
wlan.connect(ssid, password) #wlan connect with name and password
while not wlan.isconnected():
print('Waiting for connection...')
sleep(0.1)
ip = wlan.ifconfig()[0]
print(f'Connected on {ip}')
return ip # return the ip address
def open_socket(ip):
address = (ip, 80) # (ip address , port) 80 because the protocol is HTTP
connection = socket.socket() #creating socket of connection
connection.bind(address)
connection.listen(1)
return connection
#----------------------------------------------------------------------------------------------------------
def webpage():
html = """
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Commande de Robot</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-image: url('votre_photo.jpg'); /* Chemin de votre photo d'arrière-plan */
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
#controls {
text-align: center;
}
.remote {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-gap: 10px;
}
.control-button {
width: 70px;
height: 70px;
margin: 10px;
border-radius: 50%;
border: 3px solid #4CAF50;
background-color: white;
font-size: 24px;
color: #4CAF50;
cursor: pointer;
transition: background-color 0.3s ease, color 0.3s ease;
}
.control-button:hover {
background-color: #4CAF50;
color: white;
}
#start, #stop, #erreur {
padding: 10px 20px;
margin: 10px;
font-size: 18px;
border: none;
border-radius: 5px;
background-color: #4CAF50;
color: white;
cursor: pointer;
transition: background-color 0.3s ease;
}
#start:hover, #stop:hover, #erreur:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div id="controls">
<h1>Commande de Robot</h1>
<div class="remote">
<button id="gauche" class="control-button">◄</button>
<button id="avancer" class="control-button">▲</button>
<button id="droite" class="control-button">►</button>
<button id="stop" class="control-button">■</button>
<button id="reculer" class="control-button">▼</button>
<button id="haut-droit" class="control-button">↗</button>
<button id="haut-gauche" class="control-button">↖</button>
<button id="arriere-droit" class="control-button">↘</button>
<button id="arriere-gauche" class="control-button">↙</button>
</div>
<button id="start" onclick="startStop()">On</button>
<button id="erreur" onclick="corrigerErreur()">Erreur Corrigée</button>
</div>
<script>
let robotActive = false;
function startStop() {
robotActive = !robotActive; // Inverser l'état du robot à chaque clic
const startButton = document.getElementById('start');
if (robotActive) {
startButton.textContent = 'Stop'; // Changer le texte du bouton en "Stop"
} else {
startButton.textContent = 'On'; // Changer le texte du bouton en "On"
}
}
function corrigerErreur() {
robotActive = true; // Activer le robot
document.getElementById('start').textContent = 'Stop'; // Changer le texte du bouton "Start" en "Stop"
}
const stopButton = document.getElementById('stop');
stopButton.addEventListener('click', () => {
fetch(`/commande?command=stop`)
.then(response => {
console.log('Commande "Stop" envoyée avec succès');
robotActive = false; // Désactiver le robot
document.getElementById('start').textContent = 'On'; // Réinitialiser le texte du bouton "Start"
})
.catch(error => console.error('Erreur lors de l\'envoi de la commande "Stop" :', error));
});
const buttons = {
// Ajoutez ici les autres boutons de contrôle
};
Object.values(buttons).forEach(button => {
button.addEventListener('click', () => {
if (!robotActive) return; // Si le robot n'est pas activé, ne pas exécuter la commande
const command = button.id;
fetch(`/commande?command=${command}`)
.then(response => console.log(`Commande ${command} envoyée avec succès`))
.catch(error => console.error(`Erreur lors de l'envoi de la commande ${command} :`, error));
});
});
</script>
</body>
</html>
"""
return str(html)
def serve(connection):
while True:
client = connection.accept()[0]
request = client.recv(1024)
request = str(request)
if /star in requert :
on-led.on()
off-led.off()
spi = machine.SPI(1, baudrate=10000000, sck=machine.Pin(10), mosi=machine.Pin(11))
# Initialisation de l'écran OLED RGB (0.95 inch)
oled = ssd1351.SSD1351_SPI(128, 128, spi, machine.Pin(8), machine.Pin(9), machine.Pin(12))
# Effacer l'écran
oled.fill(0)
# Dessiner le corps du robot (rectangle)
oled.rect(30, 30, 70, 70, 0xFFFF)
# Dessiner la tête du robot (cercle)
oled.circle(65, 20, 15, 0xFFFF)
# Dessiner les yeux du robot (deux cercles)
oled.fill_circle(60, 15, 2, 0xFFFF)
oled.fill_circle(70, 15, 2, 0xFFFF)
# Dessiner les bras du robot (deux lignes)
oled.line(30, 50, 10, 80, 0xFFFF)
oled.line(100, 50, 120, 80, 0xFFFF)
# Dessiner les jambes du robot (deux lignes)
oled.line(50, 100, 40, 120, 0xFFFF)
oled.line(80, 100, 90, 120, 0xFFFF)
# Mise à jour de l'écran pour afficher le robot
oled.show()
# Attendre quelques secondes
time.sleep(5)
# Effacer l'écran
oled.fill(0)
oled.show()
if avancer in requert :
in1.on()
in2.off()
in3.on()
in4.off()
elif:
in1.off()
in2.off()
in3.off()
in4.off()
if recuter in requert :
in1.off()
in2.on()
in3.off()
in4.on()
elif :
in1.off()
in2.off()
in3.off()
in4.off()
if /droite in requert :
i = i + 1
rotate(i)
utime.sleep_ms(50)
elif :
while i > 90:
i = i - 1
rotate(i)
utime.sleep_ms(40)
if /gauche in requert :
i = i - 1
rotate(i)
utime.sleep_ms(50)
elif :
while i < 90:
i = i + 1
rotate(i)
utime.sleep_ms(40)
if /haut-droit in requert :
i = i + 1
rotate(i)
utime.sleep_ms(50)
in1.on()
in2.off()
in3.on()
in4.off()
elif :
while i > 90:
i = i - 1
rotate(i)
utime.sleep_ms(40)
if /haut-gauche in requert :
i = i - 1
rotate(i)
utime.sleep_ms(50)
in1.on()
in2.off()
in3.on()
in4.off()
elif :
while i < 90:
i = i + 1
rotate(i)
utime.sleep_ms(40)
if /arriere-droit in requert :
i = i - 1
rotate(i)
utime.sleep_ms(50)
in1.off()
in2.on()
in3.off()
in4.on()
elif :
while i < 90:
i = i + 1
rotate(i)
utime.sleep_ms(40)
if /arriere-gauche in requert :
i = i + 1
rotate(i)
utime.sleep_ms(50)
in1.off()
in2.on()
in3.off()
in4.on()
elif :
while i > 90:
i = i - 1
rotate(i)
utime.sleep_ms(40)
if /stop in requert :
in1.off()
in2.off()
in3.off()
in4.off()
off-led.off()
on-led-off()
# Dessiner un rectangle rouge (couleur 0xF00) de 20x20 pixels à partir du coin supérieur gauche de l'écran
oled.draw_rectangle(0, 0, 20, 20, 0xF00)
elif /erreur in requert :
for i in range(128 * 128):
oled.buffer[i * 2] = 0xF8 # Composante rouge
oled.buffer[i * 2 + 1] = 0x00 # Composante verte et bleue
# Afficher le contenu sur l'écran
oled.show()
if /star in requert :
off-led.on()
on-led.off()
#---------------------------------------------------------------------------------------------------------------
html = webpage()
response = "HTTP/1.1 200 OK\r\n"
response += "Content-Type: text/html\r\n\r\n"
response += html
client.send(response)
client.close()
try:
ip = connect()
connection = open_socket(ip)
serve(connection)
except KeyboardInterrupt:
machine.reset()