Greetings,
I adapted the 2.1 – Web Server – Control Outputs project with the elegant ota project to control a robot and everything worked fine. When I added pwn control on motor speed, only one motor worked, the left one.
thanks,
/*********
Rui Santos
Complete instructions at https://RandomNerdTutorials.com/build-web-servers-ebook/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*********/
#include <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <AsyncElegantOTA.h>
#include “SPIFFS.h”
// Replace with your network credentials
const char *ssid = “”;
const char *password = “”;
// Set GPIO´s
// Motor direita
int motor1Pin1 = 19;
int motor1Pin2 = 21;
int enable1Pin = 22; // ena
// Motor esquerda
int motor2Pin1 = 18;
int motor2Pin2 = 17;
int enable2Pin = 2; // enb 23
//pwm setings
String pwmSliderValue = “0”;
const int frequencyHz = 5000;
const int pwmChannel = 0;
const int pwmChannel1 = 1;
const int resolution = 8;
//slider valor
const char *INPUT_PARAMETER = “value”;
// Stores LED state
String ledState;
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
// Initialize SPIFFS
void initSPIFFS()
{
if (!SPIFFS.begin())
{
Serial.println(“An error has occurred while mounting SPIFFS”);
}
Serial.println(“SPIFFS mounted successfully”);
}
// Initialize WiFi
void initWiFi()
{
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print(“Connecting to WiFi ..”);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(‘.’);
delay(1000);
}
Serial.println(WiFi.localIP());
}
// Replaces placeholder with movimento do robo
String processor(const String &var)
{
/*if (var == “STATE”)
{
if (movimento)
{
ledState = “Movimento”;
}
else
{
ledState = “Parado”;
}
return ledState;
}*/
if (var == “SLIDERVALUE”)
{
return pwmSliderValue;
}
return String();
}
void frente()
{
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
//movimento = true;
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
ledcWrite(pwmChannel, pwmSliderValue.toInt());
ledcWrite(pwmChannel1, pwmSliderValue.toInt());
}
void tras()
{
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
//movimento = true;
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
ledcWrite(pwmChannel, pwmSliderValue.toInt());
ledcWrite(pwmChannel1, pwmSliderValue.toInt());
}
void parar()
{
//movimento = false;
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
}
void direita()
{
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
// movimento = true;
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
int velocidade = pwmSliderValue.toInt();
int vel_dir = velocidade * 20 / 100;
int vel_esq = velocidade;
ledcWrite(pwmChannel, vel_dir);
ledcWrite(pwmChannel1, vel_esq);
}
void esquerda()
{
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
//movimento = true;
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, HIGH);
int velocidade = pwmSliderValue.toInt();
int vel_dir = velocidade;
int vel_esq = velocidade * 20 / 100;
ledcWrite(pwmChannel, vel_dir);
ledcWrite(pwmChannel1, vel_esq);
}
void setup()
{
Serial.begin(115200);
initWiFi();
initSPIFFS();
// Set up GPIO’s
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(enable1Pin, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(enable2Pin, OUTPUT);
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, LOW);
digitalWrite(motor2Pin2, LOW);
ledcSetup(pwmChannel, frequencyHz, resolution);
ledcSetup(pwmChannel1, frequencyHz, resolution);
ledcAttachPin(enable1Pin, pwmChannel);
ledcAttachPin(enable2Pin, pwmChannel1);
// Route for root / web page
server.on(“/”, HTTP_GET, [](AsyncWebServerRequest *request)
{ request->send(SPIFFS, “/index.html”, “text/html”, false, processor); });
server.serveStatic(“/”, SPIFFS, “/”);
server.on(“/slider”, HTTP_GET, [](AsyncWebServerRequest *request)
{
String inputMessage;
// GET input1 value on <ESP_IP>/slider?value=<inputMessage>
if (request->hasParam(INPUT_PARAMETER))
{
inputMessage = request->getParam(INPUT_PARAMETER)->value();
pwmSliderValue = inputMessage;
ledcWrite(pwmChannel, pwmSliderValue.toInt());
}
else
{
inputMessage = “No message sent”;
}
Serial.println(inputMessage);
request->send(SPIFFS, “/index.html”, “text/html”, false, processor);
});
// Route to set robo para frente
server.on(“/frente”, HTTP_GET, [](AsyncWebServerRequest *request)
{
frente();
request->send(SPIFFS, “/index.html”, “text/html”, false, processor);
});
// Route to set robo para esquerda
server.on(“/esquerda”, HTTP_GET, [](AsyncWebServerRequest *request)
{
esquerda();
request->send(SPIFFS, “/index.html”, “text/html”, false, processor);
});
// Route to set robo para direita
server.on(“/direita”, HTTP_GET, [](AsyncWebServerRequest *request)
{
direita();
request->send(SPIFFS, “/index.html”, “text/html”, false, processor);
});
// Route to set robo para parar
server.on(“/parar”, HTTP_GET, [](AsyncWebServerRequest *request)
{
parar();
request->send(SPIFFS, “/index.html”, “text/html”, false, processor);
});
// Route to set robo para tras
server.on(“/tras”, HTTP_GET, [](AsyncWebServerRequest *request)
{
tras();
request->send(SPIFFS, “/index.html”, “text/html”, false, processor);
});
// Start ElegantOTA
AsyncElegantOTA.begin(&server);
// Start server
server.begin();
}
void loop()
{
}
Hi.
I’m not sure if this is the issue, but when you get the slider value, you’re only writing to one channel. If you want to control both motors, you need to write to both pwm channels. See the following snipet of your code (you’re only setting one channel):
inputMessage = request->getParam(INPUT_PARAMETER)->value(); pwmSliderValue = inputMessage; ledcWrite(pwmChannel, pwmSliderValue.toInt()); }
Or completely remove the following line because you’e already setting the PWM value on the direita() and esquerda() functions.
ledcWrite(pwmChannel, pwmSliderValue.toInt());
Regards,
Sara