0% found this document useful (0 votes)
2 views12 pages

Web Sever Test Code

This document contains an Arduino sketch for controlling motors and LEDs using an ESP32 microcontroller with Wi-Fi capabilities. It sets up a web server to receive commands for motor movement and LED state changes, allowing for remote control via a web interface. The code includes functions for moving the motors forward, backward, left, right, and stopping, as well as controlling the state of multiple LEDs and a buzzer.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views12 pages

Web Sever Test Code

This document contains an Arduino sketch for controlling motors and LEDs using an ESP32 microcontroller with Wi-Fi capabilities. It sets up a web server to receive commands for motor movement and LED state changes, allowing for remote control via a web interface. The code includes functions for moving the motors forward, backward, left, right, and stopping, as well as controlling the state of multiple LEDs and a buzzer.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

/*********

Rui Santos

Complete project details at https://randomnerdtutorials.com

*********/

// Import required libraries

#include "WiFi.h"

#include "ESPAsyncWebServer.h"

#include "SPIFFS.h"

#include <Tone32.h>

// Replace with your network credentials

const char* ssid = "Sun (3)";

const char* password = "0123456789";

#define BUZZER_PIN 34

// Set LED GPIO

const int ledPin = 2;

// Stores LED state

String ledState;

// Create AsyncWebServer object on port 80

AsyncWebServer server(80);

int Speed;

// Motor A

int motor1Pin1 = 27;

int motor1Pin2 = 26;

int enable1Pin = 14;


int SliderValue;

// Motor B

int motor2Pin1 = 35;

int motor2Pin2 = 33;

int enable2Pin = 32;

int enable3Pin = 22;

int enable4Pin = 23;

int enable5Pin = 12;

int enable6Pin = 13;

int valueString;

// Setting PWM properties

const int freq = 30000;

const int pwmChannel1 = 0;

const int pwmChannel2 = 1;

const int pwmChannel3 = 0;

const int pwmChannel4 = 0;

const int pwmChannel5 = 0;

const int pwmChannel6 = 0;

const int resolution = 8; //resolution of pulse width mod 8bit = 0-255 that we can vary the value

//int dutyCycle = 200;

// Declare the pins to which the LEDs are connected

int greenled = 4;

int redled = 15;

int blueled = 18;

int yellowled = 21;

String greenstate = "off";// state of green LED

String redstate = "off";// state of red LED

String bluestate = "off";// state of green LED

String yellowstate = "off";// state of red LED


// Replaces placeholder with LED state value

String processor(const String& var){

Serial.println(var);

if(var == "STATE"){

if(digitalRead(ledPin)){

ledState = "ON";

else{

ledState = "OFF";

Serial.print(ledState);

return ledState;

return String();

void setup(){

pinMode(greenled, OUTPUT);

pinMode(redled, OUTPUT);

pinMode(blueled, OUTPUT);

pinMode(yellowled, OUTPUT);

// sets the pins as outputs:

pinMode(motor1Pin1, OUTPUT);

pinMode(motor1Pin2, OUTPUT);

pinMode(enable1Pin, OUTPUT);

pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);

pinMode(enable2Pin, OUTPUT);

pinMode(enable3Pin, OUTPUT);

pinMode(enable4Pin, OUTPUT);

pinMode(enable5Pin, OUTPUT);

pinMode(enable6Pin, OUTPUT);

// configure LED PWM functionalitites

ledcSetup(pwmChannel1, freq, resolution);

ledcSetup(pwmChannel2, freq, resolution);

ledcSetup(pwmChannel3, freq, resolution);

ledcSetup(pwmChannel4, freq, resolution);

ledcSetup(pwmChannel5, freq, resolution);

ledcSetup(pwmChannel6, freq, resolution);

// attach the channel to the GPIO to be controlled

ledcAttachPin(enable1Pin, pwmChannel1);

ledcAttachPin(enable2Pin, pwmChannel2); // attach pwm to D25

ledcAttachPin(enable3Pin, pwmChannel3);

ledcAttachPin(enable4Pin, pwmChannel4); // attach pwm to D25

ledcAttachPin(enable5Pin, pwmChannel5);

ledcAttachPin(enable6Pin, pwmChannel6); // attach pwm to D25

// Serial port for debugging purposes

pinMode(BUZZER_PIN, OUTPUT); // set the buzzer pin as an output

Serial.begin(115200);

pinMode(ledPin, OUTPUT);
// Initialize SPIFFS

if(!SPIFFS.begin(true)){

Serial.println("An Error has occurred while mounting SPIFFS");

return;

// Connect to Wi-Fi

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {

delay(1000);

Serial.println("Connecting to WiFi..");

// Print ESP32 Local IP Address

Serial.println(WiFi.localIP());

// Route for root / web page

server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){

request->send(SPIFFS, "/index.html", String(), false, processor);

});

// Route to load style.css file

server.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request){

request->send(SPIFFS, "/style.css", "text/css");

});

// Route to set GPIO to HIGH

server.on("/FORWARD", HTTP_GET, [](AsyncWebServerRequest *request){

digitalWrite(ledPin, HIGH);
MotorForward(Speed);

Redled(Speed);

request->send(SPIFFS, "/index.html", String(), false, processor);

});

// Route to set GPIO to HIGH

server.on("/BACKWARD", HTTP_GET, [](AsyncWebServerRequest *request){

digitalWrite(ledPin, HIGH);

MotorBackward(Speed);

Blueled(Speed);

request->send(SPIFFS, "/index.html", String(), false, processor);

});

// Route to set GPIO to HIGH

server.on("/RIGHT", HTTP_GET, [](AsyncWebServerRequest *request){

digitalWrite(ledPin, HIGH);

MotorRight(Speed);

Yellowled(Speed);

request->send(SPIFFS, "/index.html", String(), false, processor);

});

// Route to set GPIO to HIGH

server.on("/LEFT", HTTP_GET, [](AsyncWebServerRequest *request){

digitalWrite(ledPin, HIGH);

Serial.println("Motor Left");

MotorLeft(Speed);

Greenled(Speed);

request->send(SPIFFS, "/index.html", String(), false, processor);

});
server.on("/HORN", HTTP_GET, [](AsyncWebServerRequest *request){

Serial.println("HORN");

playTone(1000, 1000); // play a 1 kHz tone for 1 second

request->send(SPIFFS, "/index.html", String(), false, processor);

});

// Route to set GPIO to LOW

server.on("/STOP", HTTP_GET, [](AsyncWebServerRequest *request){

digitalWrite(ledPin, LOW);

MotorStop();

Stopled();

request->send(SPIFFS, "/index.html", String(), false, processor);

});

server.on("/Slider", HTTP_GET, [](AsyncWebServerRequest *request){

if(request->hasParam("Speed")){

String speedValue = request->getParam("Speed")->value();

Speed = speedValue.toInt();

request->send(SPIFFS, "/index.html", String(), false, processor);

});

// Start server

server.begin();

void loop(){
}

void MotorForward(int Speed)

ledcWrite(pwmChannel1, Speed);

ledcWrite(pwmChannel2, Speed);

Serial.println("Moving Forward");

Serial.println(Speed);

digitalWrite(motor1Pin1, LOW);

digitalWrite(motor1Pin2, HIGH);

digitalWrite(motor2Pin1, HIGH); //swap motor wiring if spin in different direction!

digitalWrite(motor2Pin2, LOW);

delay(200);

void MotorBackward(int Speed)

ledcWrite(pwmChannel1, Speed);

ledcWrite(pwmChannel2, Speed);

Serial.println("Moving Backwards");

Serial.println(Speed);

digitalWrite(motor1Pin1, HIGH);

digitalWrite(motor1Pin2, LOW);

digitalWrite(motor2Pin1, LOW);

digitalWrite(motor2Pin2, HIGH);

delay(200);
}

void MotorLeft(int Speed)

ledcWrite(pwmChannel1, Speed);

ledcWrite(pwmChannel2, Speed);

Serial.println("Moving Left");

Serial.println(Speed);

digitalWrite(motor1Pin1,HIGH);

digitalWrite(motor1Pin2,LOW);

digitalWrite(motor2Pin1,LOW);

digitalWrite(motor2Pin2,LOW);

delay(200);

void MotorRight(int Speed)

ledcWrite(pwmChannel1, Speed);

ledcWrite(pwmChannel2, Speed);

Serial.println("Moving Right");

Serial.println(Speed);

digitalWrite(motor1Pin1,LOW);

digitalWrite(motor1Pin2,LOW);

digitalWrite(motor2Pin1,HIGH);

digitalWrite(motor2Pin2,LOW);

delay(200);

void MotorStop()
{

ledcWrite(pwmChannel1, Speed);

ledcWrite(pwmChannel2, Speed);

Serial.println("Motor stopped");

Serial.println(Speed);

digitalWrite(motor1Pin1, LOW);

digitalWrite(motor1Pin2, LOW);

digitalWrite(motor2Pin1, LOW);

digitalWrite(motor2Pin2, LOW);

delay(200);

void Redled(int Speed)

ledcWrite(pwmChannel3, Speed);

Serial.println("red led : ");

Serial.println(Speed);

digitalWrite(redled, HIGH);

digitalWrite(greenled, LOW);

digitalWrite(yellowled, LOW);

digitalWrite(blueled, LOW);

delay(200);

void Greenled(int Speed)

ledcWrite(pwmChannel4, Speed);

Serial.println("green led : ");

Serial.println(Speed);

digitalWrite(redled, LOW);
digitalWrite(greenled, HIGH);

digitalWrite(yellowled, LOW);

digitalWrite(blueled, LOW);

delay(200);

void Blueled(int Speed)

ledcWrite(pwmChannel5, Speed);

Serial.println("blue led : ");

Serial.println(Speed);

digitalWrite(redled, LOW);

digitalWrite(greenled, LOW);

digitalWrite(yellowled, LOW);

digitalWrite(blueled, HIGH);

delay(200);

void Yellowled(int Speed)

ledcWrite(pwmChannel6, Speed);

Serial.println("yellow led : ");

Serial.println(Speed);

digitalWrite(redled, LOW);

digitalWrite(greenled, LOW);

digitalWrite(yellowled, HIGH);

digitalWrite(blueled, LOW);

delay(200);

void Stopled()

{
ledcWrite(pwmChannel3, Speed);

ledcWrite(pwmChannel4, Speed);

ledcWrite(pwmChannel5, Speed);

ledcWrite(pwmChannel6, Speed);

Serial.println(Speed);

digitalWrite(redled, LOW);

digitalWrite(greenled, LOW);

digitalWrite(yellowled, LOW);

digitalWrite(blueled, LOW);

delay(200);

void playTone(int frequency, int duration) {

digitalWrite(BUZZER_PIN, HIGH); // turn on

delay(200);

You might also like