0% found this document useful (0 votes)
4 views

2 -ESP8266WiFi

This document contains code for controlling a WiFi-enabled robot using an ESP8266 microcontroller and an L298N motor driver. It includes setup for motor control, WiFi connection, and a web server to receive commands for movement and speed adjustments. The robot can move in various directions, adjust speed, and stop, with feedback provided through a buzzer and LED indicators.

Uploaded by

Pradip baraiya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

2 -ESP8266WiFi

This document contains code for controlling a WiFi-enabled robot using an ESP8266 microcontroller and an L298N motor driver. It includes setup for motor control, WiFi connection, and a web server to receive commands for movement and speed adjustments. The robot can move in various directions, adjust speed, and stop, with feedback provided through a buzzer and LED indicators.

Uploaded by

Pradip baraiya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

ha#include <ESP8266WiFi.

h>
#include <ESP8266WebServer.h>
#include <ArduinoOTA.h>

// Pin definitions
#define ENA 14 // Enable/speed motors Right GPIO14(D5)
#define ENB 12 // Enable/speed motors Left GPIO12(D6)
#define IN_1 15 // L298N in1 motors Right GPIO15(D8)
#define IN_2 13 // L298N in2 motors Right GPIO13(D7)
#define IN_3 2 // L298N in3 motors Left GPIO2(D4)
#define IN_4 0 // L298N in4 motors Left GPIO0(D3)

const int buzPin = D7; // Buzzer pin (active buzzer)


const int ledPin = D8; // LED pin (super bright LED)
const int wifiLedPin = D0; // WiFi indication LED

// Global variables
String command; // String to store app command state.
int speedCar = 800; // Initial speed (400 - 1023).
int speed_Coeff = 3; // Speed coefficient for turn adjustments.

ESP8266WebServer server(80); // Webserver object

const char* ssid = "Electroniclinic"; // WiFi SSID


const char* password = ""; // Password (blank for AP mode)

void setup() {
Serial.begin(115200);
Serial.println("*WiFi Robot Remote Control - L298N*");

// Motor pin configuration


pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN_1, OUTPUT);
pinMode(IN_2, OUTPUT);
pinMode(IN_3, OUTPUT);
pinMode(IN_4, OUTPUT);

// Peripheral pin configuration


pinMode(buzPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(wifiLedPin, OUTPUT);

digitalWrite(buzPin, LOW); // Turn off buzzer initially


digitalWrite(ledPin, LOW); // Turn off LED initially
digitalWrite(wifiLedPin, LOW); // Turn off WiFi LED initially

// Connect to WiFi
WiFi.mode(WIFI_AP); // Set AP mode
WiFi.softAP(ssid, password); // Start AP with SSID and no password
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);

// Indicate WiFi is ready


digitalWrite(wifiLedPin, HIGH);

// OTA Setup
ArduinoOTA.begin();

// Start web server


server.on("/", HTTP_handleRoot);
server.onNotFound(HTTP_handleRoot);
server.begin();
}

void loop() {
ArduinoOTA.handle(); // Handle OTA updates
server.handleClient(); // Handle web server requests

// Parse command
command = server.arg("State");
if (command == "F") goAhead();
else if (command == "B") goBack();
else if (command == "L") goLeft();
else if (command == "R") goRight();
else if (command == "I") goAheadRight();
else if (command == "G") goAheadLeft();
else if (command == "J") goBackRight();
else if (command == "H") goBackLeft();

// Speed adjustment commands


else if (command == "0") speedCar = 400;
else if (command == "1") speedCar = 470;
else if (command == "2") speedCar = 540;
else if (command == "3") speedCar = 610;
else if (command == "4") speedCar = 680;
else if (command == "5") speedCar = 750;
else if (command == "6") speedCar = 820;
else if (command == "7") speedCar = 890;
else if (command == "8") speedCar = 960;
else if (command == "9") speedCar = 1023;

// New speed adjustment levels (additions)


else if (command == "10") speedCar = 1100;
else if (command == "11") speedCar = 1200;
else if (command == "12") speedCar = 1300;
else if (command == "13") speedCar = 1400;
else if (command == "14") speedCar = 1500;
else if (command == "15") speedCar = 1600;

// Stop robot
else if (command == "S") {
stopRobot();
// Flash LED and beep buzzer when stopping
for (int i = 0; i < 3; i++) {
digitalWrite(buzPin, HIGH);
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(buzPin, LOW);
digitalWrite(ledPin, LOW);
delay(200);
}
}
}

// Movement functions
void goAhead() {
digitalWrite(IN_1, LOW);
digitalWrite(IN_2, HIGH);
analogWrite(ENA, speedCar);

digitalWrite(IN_3, LOW);
digitalWrite(IN_4, HIGH);
analogWrite(ENB, speedCar);
}

void goBack() {
digitalWrite(IN_1, HIGH);
digitalWrite(IN_2, LOW);
analogWrite(ENA, speedCar);
digitalWrite(IN_3, HIGH);
digitalWrite(IN_4, LOW);
analogWrite(ENB, speedCar);
}

void goRight() {
digitalWrite(IN_1, HIGH);
digitalWrite(IN_2, LOW);
analogWrite(ENA, speedCar);

digitalWrite(IN_3, LOW);
digitalWrite(IN_4, HIGH);
analogWrite(ENB, speedCar);
}

void goLeft() {
digitalWrite(IN_1, LOW);
digitalWrite(IN_2, HIGH);
analogWrite(ENA, speedCar);

digitalWrite(IN_3, HIGH);
digitalWrite(IN_4, LOW);
analogWrite(ENB, speedCar);
}

void goAheadRight() {
digitalWrite(IN_1, LOW);
digitalWrite(IN_2, HIGH);
analogWrite(ENA, speedCar / speed_Coeff);

digitalWrite(IN_3, LOW);
digitalWrite(IN_4, HIGH);
analogWrite(ENB, speedCar);
}

void goAheadLeft() {
digitalWrite(IN_1, LOW);
digitalWrite(IN_2, HIGH);
analogWrite(ENA, speedCar);

digitalWrite(IN_3, LOW);
digitalWrite(IN_4, HIGH);
analogWrite(ENB, speedCar / speed_Coeff);
}
void goBackRight() {
digitalWrite(IN_1, HIGH);
digitalWrite(IN_2, LOW);
analogWrite(ENA, speedCar / speed_Coeff);

digitalWrite(IN_3, HIGH);
digitalWrite(IN_4, LOW);
analogWrite(ENB, speedCar);
}

void goBackLeft() {
digitalWrite(IN_1, HIGH);
digitalWrite(IN_2, LOW);
analogWrite(ENA, speedCar);

digitalWrite(IN_3, HIGH);
digitalWrite(IN_4, LOW);
analogWrite(ENB, speedCar / speed_Coeff);
}

void stopRobot() {
digitalWrite(IN_1, LOW);
digitalWrite(IN_2, LOW);
analogWrite(ENA, speedCar);

digitalWrite(IN_3, LOW);
digitalWrite(IN_4, LOW);
analogWrite(ENB, speedCar);
}

// HTTP handler
void HTTP_handleRoot() {
if (server.hasArg("State")) {
Serial.println(server.arg("State"));
}
server.send(200, "text/html", "Command received.");
delay(1);
}

You might also like