first commit
This commit is contained in:
198
ESP32_MOTOR/ESP32_MOTOR.ino
Normal file
198
ESP32_MOTOR/ESP32_MOTOR.ino
Normal file
@@ -0,0 +1,198 @@
|
||||
int PWMA = 5; // Right side motor speed control (PWM)
|
||||
int PWMB = 4; // Left side motor speed control (PWM)
|
||||
int DA = 0; // Right side motor direction
|
||||
int DB = 2; // Left side motor direction
|
||||
|
||||
int PIN_GREEN = A0;
|
||||
int PIN_RED = D2;
|
||||
|
||||
#include <ESP8266WebServer.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
// Your WiFi credentials.
|
||||
// Set password to "" for open networks.
|
||||
const char* ssid = "Livebox-37cc";
|
||||
const char* pass = "8A6060920A8A86896F770F2C47";
|
||||
|
||||
IPAddress gateway(192, 168, 1, 1);
|
||||
IPAddress subnet(255, 255, 0, 0);
|
||||
IPAddress DNS(192, 168, 1, 1);
|
||||
|
||||
ESP8266WebServer server(80);
|
||||
|
||||
bool motor_is_running = false;
|
||||
String speed = "150"; // Variable globale pour stocker la vitesse
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
pinMode(PWMA, OUTPUT);
|
||||
pinMode(PWMB, OUTPUT);
|
||||
pinMode(DA, OUTPUT);
|
||||
pinMode(DB, OUTPUT);
|
||||
pinMode(PIN_GREEN, INPUT);
|
||||
pinMode(PIN_RED, INPUT);
|
||||
pinMode(2, OUTPUT); //Initialize GPIO2 pin as an output
|
||||
// Initialize motors to stop
|
||||
digitalWrite(PWMA, LOW);
|
||||
digitalWrite(PWMB, LOW);
|
||||
digitalWrite(DA, LOW);
|
||||
digitalWrite(DB, LOW);
|
||||
digitalWrite(2, HIGH); // Turn the LED off by making the voltage HIGH
|
||||
Serial.println("Motors initialized.");
|
||||
|
||||
delay(10);
|
||||
|
||||
// Connectez-vous au réseau WiFi
|
||||
WiFi.begin(ssid, pass);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(1000);
|
||||
Serial.println("Connexion au WiFi en cours...");
|
||||
}
|
||||
Serial.println("Connecté au réseau WiFi");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
// Définissez les gestionnaires pour les différentes URL
|
||||
server.on("/", HTTP_GET, handleRoot);
|
||||
server.on("/forward", HTTP_GET, handleForward);
|
||||
server.on("/backward", HTTP_GET, handleBackward);
|
||||
server.on("/stop", HTTP_GET, handleStop);
|
||||
|
||||
// Démarrer le serveur
|
||||
server.begin();
|
||||
Serial.println("Serveur Web démarré");
|
||||
}
|
||||
|
||||
//void loop() {
|
||||
// forward();
|
||||
// delay(2000);
|
||||
// stopMotors();
|
||||
// delay(2000);
|
||||
// backward();
|
||||
// delay(2000);
|
||||
// stopMotors();
|
||||
// delay(2000);
|
||||
// slowForward();
|
||||
// delay(2000);
|
||||
// stopMotors();
|
||||
// delay(2000);
|
||||
// round();
|
||||
// delay(2000);
|
||||
// stopMotors();
|
||||
// delay(2000);
|
||||
//}
|
||||
|
||||
void loop() {
|
||||
// Gérez les requêtes du serveur
|
||||
server.handleClient();
|
||||
|
||||
if (motor_is_running) {
|
||||
Serial.print(analogRead(PIN_GREEN));
|
||||
Serial.print(" ");
|
||||
Serial.println(digitalRead(PIN_RED));
|
||||
|
||||
delay(200);
|
||||
}
|
||||
}
|
||||
|
||||
void handleRoot() {
|
||||
// Générer la page HTML avec CSS
|
||||
String html = "<html><head><style>";
|
||||
html += "body { font-family: Arial, sans-serif; text-align: center; }";
|
||||
html += "h1 { color: #333; }";
|
||||
html += "form { margin: 20px auto; max-width: 300px; }";
|
||||
html += "input { padding: 8px; margin: 5px; }";
|
||||
html += "</style></head><body>";
|
||||
html += "<h1>Controle de la vitesse</h1>";
|
||||
html += "<form action='/forward' method='get'>";
|
||||
html += "Vitesse: <input type='text' name='speed' value='" + speed + "'>";
|
||||
html += "<input type='submit' value='Monter'>";
|
||||
html += "</form>";
|
||||
html += "<form action='/backward' method='get'>";
|
||||
html += "<input type='submit' value='Descendre'>";
|
||||
html += "</form>";
|
||||
html += "<form action='/stop' method='get'>";
|
||||
html += "<input type='submit' value='Arréter'>";
|
||||
html += "</form>";
|
||||
html += "</body></html>";
|
||||
|
||||
// Envoyer la page HTML au client
|
||||
server.send(200, "text/html", html);
|
||||
}
|
||||
|
||||
void handleForward() {
|
||||
// Récupérer la valeur de la vitesse depuis l'URL
|
||||
speed = server.arg("speed");
|
||||
Serial.println("Démarrage à la vitesse: " + speed);
|
||||
|
||||
forward(speed.toInt());
|
||||
// Vous pouvez ajouter ici le code pour traiter la vitesse comme vous le souhaitez
|
||||
|
||||
// Rediriger vers la page principale après le traitement
|
||||
server.sendHeader("Location", "/");
|
||||
server.send(302, "text/plain", "Redirection vers la page principale");
|
||||
}
|
||||
|
||||
void handleBackward() {
|
||||
// Récupérer la valeur de la vitesse depuis l'URL
|
||||
int tmp_speed = server.arg("speed");
|
||||
|
||||
if (tmp_speed > 100) {
|
||||
speed = tmp_speed;
|
||||
}
|
||||
Serial.println("Démarrage à la vitesse: " + speed);
|
||||
|
||||
backward(speed.toInt());
|
||||
// Vous pouvez ajouter ici le code pour traiter la vitesse comme vous le souhaitez
|
||||
|
||||
// Rediriger vers la page principale après le traitement
|
||||
server.sendHeader("Location", "/");
|
||||
server.send(302, "text/plain", "Redirection vers la page principale");
|
||||
}
|
||||
|
||||
void handleStop() {
|
||||
Serial.println("Arrêt");
|
||||
stopMotors();
|
||||
|
||||
// Vous pouvez ajouter ici le code pour gérer l'arrêt comme vous le souhaitez
|
||||
|
||||
// Rediriger vers la page principale après le traitement
|
||||
server.sendHeader("Location", "/");
|
||||
server.send(302, "text/plain", "Redirection vers la page principale");
|
||||
}
|
||||
|
||||
|
||||
void forward(int speed) {
|
||||
motor_is_running = true;
|
||||
Serial.println("Moving forward...");
|
||||
digitalWrite(DA, LOW); // Right motor forward
|
||||
digitalWrite(DB, LOW); // Left motor forward
|
||||
analogWrite(PWMA, speed); // Adjust the value (0-255) for speed control
|
||||
analogWrite(PWMB, speed); // Adjust the value (0-255) for speed control
|
||||
}
|
||||
|
||||
void stopMotors() {
|
||||
motor_is_running = false;
|
||||
Serial.println("Stopping motors...");
|
||||
digitalWrite(DA, LOW); // Right motor stop
|
||||
digitalWrite(DB, LOW); // Left motor stop
|
||||
digitalWrite(PWMA, LOW); // Right motor speed off
|
||||
digitalWrite(PWMB, LOW); // Left motor speed off
|
||||
}
|
||||
|
||||
void backward(int speed) {
|
||||
motor_is_running = true;
|
||||
|
||||
Serial.println("Moving backward...");
|
||||
digitalWrite(DA, HIGH); // Right motor backward
|
||||
digitalWrite(DB, HIGH); // Left motor backward
|
||||
analogWrite(PWMA, speed); // Adjust the value (0-255) for speed control
|
||||
analogWrite(PWMB, speed); // Adjust the value (0-255) for speed control
|
||||
}
|
||||
|
||||
//void round() {
|
||||
// Serial.println("Moving backward...");
|
||||
// digitalWrite(DA, HIGH); // Right motor backward
|
||||
// digitalWrite(DB, LOW); // Left motor forward
|
||||
// analogWrite(PWMA, 255); // Adjust the value (0-255) for speed control
|
||||
// analogWrite(PWMB, 255); // Adjust the value (0-255) for speed control
|
||||
//}
|
||||
Reference in New Issue
Block a user