Arka Manna
Published © LGPL

WEB- server Car

Car that can be controlled buy a web interface

IntermediateShowcase (no instructions)2 hours158
WEB- server Car

Things used in this project

Hardware components

DC motor (generic)
×4
Dual H-Bridge motor drivers L298
SparkFun Dual H-Bridge motor drivers L298
×1
esp 8266
×1
Rechargeable Battery, 7.2 V
Rechargeable Battery, 7.2 V
×1

Story

Read more

Code

main code

Arduino
/*<!--***********************************************************************************************************************
******                                    Home Automation System with Theme Switching                                   *****
*****                                     Author: ARKA MANNA                                                            *****
*****                                                                                                                   *****
*****                                     Licensed under the Apache License, Version 2.0 (the "License") ;              *****
*****                                     you may not use this file except in compliance with the License.              *****
*****                                     You may obtain a copy of the License at                                       *****
*****                                                                                                                   *****
*****                                     http://www.apache.org/licenses/LICENSE-2.0                                    *****
*****                                                                                                                   *****
*****                                     Unless required by applicable law or agreed to in writing, software           *****
*****                                     distributed under the License is distributed on an "AS IS" BASIS,             *****
*****                                     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.      *****
*****                                     See the License for the specific language governing permissions and           *****
*****                                     limitations under the License.                                                *****
*****************************************************************************************************************************

*/
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

ESP8266WebServer server(80);

// Motor pins
const int motor1Pin1 = 5; // D1
const int motor1Pin2 = 4; // D2
const int motor2Pin1 = 0; // D3
const int motor2Pin2 = 2; // D4

// Light and horn pins
const int lightPin = 12; // D6
const int hornPin = 13; // D7

void setup() {
  Serial.begin(115200);

  // Set up ESP8266 as an Access Point
  WiFi.softAP("ESP8266_RC_Car", "12345678"); // SSID and Password
  Serial.println("Access Point started");
  Serial.print("IP Address: ");
  Serial.println(WiFi.softAPIP());

  // Initialize motor pins
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(motor2Pin1, OUTPUT);
  pinMode(motor2Pin2, OUTPUT);

  // Initialize light and horn pins
  pinMode(lightPin, OUTPUT);
  pinMode(hornPin, OUTPUT);

  // Ensure car starts in the stopped state
  stopCar();

  // Set up web server
  server.on("/", handleRoot);
  server.on("/forward", handleForward);
  server.on("/backward", handleBackward);
  server.on("/left", handleLeft);
  server.on("/right", handleRight);
  server.on("/stop", handleStop);
  server.on("/lighton", handleLightOn);
  server.on("/lightoff", handleLightOff);
  server.on("/hornon", handleHornOn);
  server.on("/hornoff", handleHornOff);
  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
  server.handleClient();
}

void handleRoot() {
  String html = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<title>ESP8266 Robot</title>
<style>
  body { background-color: #121212; font-family: Arial, Helvetica, Sans-Serif; color: #ffffff; }
  #JD { text-align: center; font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif; font-size: 24px; }
  .foot { text-align: center; font-family: "Comic Sans MS", cursive; font-size: 30px; color: #FF5722; }
  .button { border: none; color: white; padding: 17px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; border-radius: 12px; width: 100%; }
  .red {background-color: #D32F2F;}
  .green {background-color: #388E3C;}
  .yellow {background-color: #FBC02D;}
  .blue {background-color: #1976D2;}
</style>
<script>
  function sendRequest(path) {
    fetch(path).then(response => response.text()).then(text => console.log(text));
  }
  function handleButton(event, path, holdPath) {
    event.preventDefault();
    if (holdPath) {
      sendRequest(path);
      document.addEventListener('mouseup', () => sendRequest(holdPath), {once: true});
    } else {
      sendRequest(path);
    }
  }
</script>
</head>
<body>
  <table width="100%" border="1">
    <tr>
      <td bgcolor="#212121" id="JD">Robot Controller</td>
    </tr>
  </table>
  <table width="100" height="249" border="0" align="center">
    <tr>
      <td>&nbsp;</td>
      <td align="center" valign="middle">
        <button class="button green" onmousedown="handleButton(event, '/forward', '/stop')">Forward</button>
      </td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td align="center" valign="middle">
        <button class="button green" onmousedown="handleButton(event, '/left', '/stop')">Turn Left</button>
      </td>
      <td align="center" valign="middle">
        <button class="button red" onclick="handleButton(event, '/stop')">Stop</button>
      </td>
      <td align="center" valign="middle">
        <button class="button green" onmousedown="handleButton(event, '/right', '/stop')">Turn Right</button>
      </td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td align="center" valign="middle">
        <button class="button green" onmousedown="handleButton(event, '/backward', '/stop')">Backward</button>
      </td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td align="center" valign="middle">
        <button class="button blue" onclick="handleButton(event, '/lighton')">Light On</button>
      </td>
      <td align="center" valign="middle">
        <button class="button blue" onclick="handleButton(event, '/lightoff')">Light Off</button>
      </td>
      <td align="center" valign="middle">
        <button class="button yellow" onclick="handleButton(event, '/hornon')">Horn On</button>
      </td>
      <td align="center" valign="middle">
        <button class="button yellow" onclick="handleButton(event, '/hornoff')">Horn Off</button>
      </td>
    </tr>
  </table>
  <p class="foot">Arkenstine LAB</p>
</body>
</html>
)rawliteral";
  server.send(200, "text/html", html);
  Serial.println("Served root page");
}

void handleForward() {
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin1, HIGH);
  digitalWrite(motor2Pin2, LOW);
  server.send(200, "text/plain", "Moving forward");
  Serial.println("Moving forward");
}

void handleBackward() {
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor2Pin1, LOW);
  digitalWrite(motor2Pin2, HIGH);
  server.send(200, "text/plain", "Moving backward");
  Serial.println("Moving backward");
}

void handleLeft() {
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, HIGH);
  digitalWrite(motor2Pin1, HIGH);
  digitalWrite(motor2Pin2, LOW);
  server.send(200, "text/plain", "Turning left");
  Serial.println("Turning left");
}

void handleRight() {
  digitalWrite(motor1Pin1, HIGH);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin1, LOW);
  digitalWrite(motor2Pin2, HIGH);
  server.send(200, "text/plain", "Turning right");
  Serial.println("Turning right");
}

void handleStop() {
  stopCar();
  server.send(200, "text/plain", "Stopped");
  Serial.println("Stopped");
}

void handleLightOn() {
  digitalWrite(lightPin, HIGH);
  server.send(200, "text/plain", "Light On");
  Serial.println("Light On");
}

void handleLightOff() {
  digitalWrite(lightPin, LOW);
  server.send(200, "text/plain", "Light Off");
  Serial.println("Light Off");
}

void handleHornOn() {
  digitalWrite(hornPin, HIGH);
  server.send(200, "text/plain", "Horn On");
  Serial.println("Horn On");
}

void handleHornOff() {
  digitalWrite(hornPin, LOW);
  server.send(200, "text/plain", "Horn Off");
  Serial.println("Horn Off");
}

void stopCar() {
  digitalWrite(motor1Pin1, LOW);
  digitalWrite(motor1Pin2, LOW);
  digitalWrite(motor2Pin1, LOW);
  digitalWrite(motor2Pin2, LOW);
}

Credits

Arka Manna
4 projects • 1 follower
Contact

Comments

Please log in or sign up to comment.