• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Web Server – Control Outputs project

Q&A Forum › Category: ESP32 › Web Server – Control Outputs project
0 Vote Up Vote Down
Ricardo Borges asked 4 years ago

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()
{
}

3 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

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

0 Vote Up Vote Down
Ricardo Borges answered 4 years ago

 

Hi, Sara
the issue has been resolved by setting the missing pwmChannel1.
thanks
 

Ícone "Verificada pela comunidade"

 

 
 

 

 

0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Great!
I’ll mark this issue as resolved.
If you need further help, you just need to open a new question in our forum.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.