|
| 1 | +// By Coding Moves |
| 2 | +// WiFi Controlled Car using NodeMCU (ESP8266) |
| 3 | +// Project: Coding Moves RC+ |
| 4 | + |
| 5 | +#include <ESP8266WiFi.h> |
| 6 | +#include <WiFiClient.h> |
| 7 | +#include <ESP8266WebServer.h> |
| 8 | + |
| 9 | +// Motor driver pin definitions |
| 10 | +#define ENA 14 // Enable/speed motors Right (D5) |
| 11 | +#define ENB 12 // Enable/speed motors Left (D6) |
| 12 | +#define IN_1 15 // Motor IN1 (Right) (D8) |
| 13 | +#define IN_2 13 // Motor IN2 (Right) (D7) |
| 14 | +#define IN_3 2 // Motor IN3 (Left) (D4) |
| 15 | +#define IN_4 0 // Motor IN4 (Left) (D3) |
| 16 | + |
| 17 | +// WiFi settings |
| 18 | +const char* ssid = "Coding Moves RC+"; |
| 19 | +const char* password = "codingmoves_123"; |
| 20 | + |
| 21 | +ESP8266WebServer server(80); |
| 22 | + |
| 23 | +String command; // Command received from mobile app |
| 24 | +int speedCar = 800; // Default speed |
| 25 | +int speed_Coeff = 3; // Coefficient for turning curves |
| 26 | + |
| 27 | +void setup() { |
| 28 | + // Motor pin setup |
| 29 | + pinMode(ENA, OUTPUT); |
| 30 | + pinMode(ENB, OUTPUT); |
| 31 | + pinMode(IN_1, OUTPUT); |
| 32 | + pinMode(IN_2, OUTPUT); |
| 33 | + pinMode(IN_3, OUTPUT); |
| 34 | + pinMode(IN_4, OUTPUT); |
| 35 | + |
| 36 | + Serial.begin(115200); |
| 37 | + |
| 38 | + // Start WiFi access point |
| 39 | + WiFi.mode(WIFI_AP); |
| 40 | + WiFi.softAP(ssid, password); |
| 41 | + |
| 42 | + IPAddress myIP = WiFi.softAPIP(); |
| 43 | + Serial.print("AP IP address: "); |
| 44 | + Serial.println(myIP); |
| 45 | + |
| 46 | + // Route handling |
| 47 | + server.on("/", HTTP_handleRoot); |
| 48 | + server.onNotFound(HTTP_handleRoot); |
| 49 | + server.begin(); |
| 50 | +} |
| 51 | + |
| 52 | +// Movement functions |
| 53 | +void goAhead() { |
| 54 | + digitalWrite(IN_1, LOW); digitalWrite(IN_2, HIGH); analogWrite(ENA, speedCar); |
| 55 | + digitalWrite(IN_3, LOW); digitalWrite(IN_4, HIGH); analogWrite(ENB, speedCar); |
| 56 | +} |
| 57 | + |
| 58 | +void goBack() { |
| 59 | + digitalWrite(IN_1, HIGH); digitalWrite(IN_2, LOW); analogWrite(ENA, speedCar); |
| 60 | + digitalWrite(IN_3, HIGH); digitalWrite(IN_4, LOW); analogWrite(ENB, speedCar); |
| 61 | +} |
| 62 | + |
| 63 | +void goRight() { |
| 64 | + digitalWrite(IN_1, LOW); digitalWrite(IN_2, HIGH); analogWrite(ENA, speedCar); |
| 65 | + digitalWrite(IN_3, HIGH); digitalWrite(IN_4, LOW); analogWrite(ENB, speedCar); |
| 66 | +} |
| 67 | + |
| 68 | +void goLeft() { |
| 69 | + digitalWrite(IN_1, HIGH); digitalWrite(IN_2, LOW); analogWrite(ENA, speedCar); |
| 70 | + digitalWrite(IN_3, LOW); digitalWrite(IN_4, HIGH); analogWrite(ENB, speedCar); |
| 71 | +} |
| 72 | + |
| 73 | +void goAheadRight() { |
| 74 | + digitalWrite(IN_1, LOW); digitalWrite(IN_2, HIGH); analogWrite(ENA, speedCar / speed_Coeff); |
| 75 | + digitalWrite(IN_3, LOW); digitalWrite(IN_4, HIGH); analogWrite(ENB, speedCar); |
| 76 | +} |
| 77 | + |
| 78 | +void goAheadLeft() { |
| 79 | + digitalWrite(IN_1, LOW); digitalWrite(IN_2, HIGH); analogWrite(ENA, speedCar); |
| 80 | + digitalWrite(IN_3, LOW); digitalWrite(IN_4, HIGH); analogWrite(ENB, speedCar / speed_Coeff); |
| 81 | +} |
| 82 | + |
| 83 | +void goBackRight() { |
| 84 | + digitalWrite(IN_1, HIGH); digitalWrite(IN_2, LOW); analogWrite(ENA, speedCar / speed_Coeff); |
| 85 | + digitalWrite(IN_3, HIGH); digitalWrite(IN_4, LOW); analogWrite(ENB, speedCar); |
| 86 | +} |
| 87 | + |
| 88 | +void goBackLeft() { |
| 89 | + digitalWrite(IN_1, HIGH); digitalWrite(IN_2, LOW); analogWrite(ENA, speedCar); |
| 90 | + digitalWrite(IN_3, HIGH); digitalWrite(IN_4, LOW); analogWrite(ENB, speedCar / speed_Coeff); |
| 91 | +} |
| 92 | + |
| 93 | +void stopRobot() { |
| 94 | + digitalWrite(IN_1, LOW); digitalWrite(IN_2, LOW); analogWrite(ENA, 0); |
| 95 | + digitalWrite(IN_3, LOW); digitalWrite(IN_4, LOW); analogWrite(ENB, 0); |
| 96 | +} |
| 97 | + |
| 98 | +// Handle incoming commands |
| 99 | +void loop() { |
| 100 | + server.handleClient(); |
| 101 | + command = server.arg("State"); |
| 102 | + |
| 103 | + if (command == "F") goAhead(); |
| 104 | + else if (command == "B") goBack(); |
| 105 | + else if (command == "L") goLeft(); |
| 106 | + else if (command == "R") goRight(); |
| 107 | + else if (command == "I") goAheadRight(); |
| 108 | + else if (command == "G") goAheadLeft(); |
| 109 | + else if (command == "J") goBackRight(); |
| 110 | + else if (command == "H") goBackLeft(); |
| 111 | + else if (command == "S") stopRobot(); |
| 112 | + |
| 113 | + // Speed levels 0–9 |
| 114 | + else if (command == "0") speedCar = 400; |
| 115 | + else if (command == "1") speedCar = 470; |
| 116 | + else if (command == "2") speedCar = 540; |
| 117 | + else if (command == "3") speedCar = 610; |
| 118 | + else if (command == "4") speedCar = 680; |
| 119 | + else if (command == "5") speedCar = 750; |
| 120 | + else if (command == "6") speedCar = 820; |
| 121 | + else if (command == "7") speedCar = 890; |
| 122 | + else if (command == "8") speedCar = 960; |
| 123 | + else if (command == "9") speedCar = 1023; |
| 124 | +} |
| 125 | + |
| 126 | +// Web server route |
| 127 | +void HTTP_handleRoot() { |
| 128 | + if (server.hasArg("State")) { |
| 129 | + Serial.println(server.arg("State")); |
| 130 | + } |
| 131 | + server.send(200, "text/html", ""); |
| 132 | + delay(1); |
| 133 | +} |
0 commit comments