-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTorque.txt
More file actions
128 lines (114 loc) · 3.23 KB
/
Torque.txt
File metadata and controls
128 lines (114 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <ESP8266WiFi.h> //wifi library for node mcu
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <String.h>
#define motor1 5 //motor connected to GPIO5. Basically pin number
#define motor2 4
#define motor3 0
#define motor4 2
//WiFi
char* ssid = "placeholder";
char* password = "jioairtel";
WiFiClient client; //wificlient defining
WiFiServer server(80); //port assighned to 80
void forward();
void backward();
void right();
void left();
void stop();
void turnright();
void turnleft();
void setup() {
//In the line Serial.begin(115200) u have to pass the baud rate which should be the same as in the rightmost top corner of the serial monitor written as 115200 baud. If u want to check whether it is connected or not, u may remove the usb cable and after uploading the code and then reconnect it then check in the serial monitor.
Serial.begin(115200);
// Set motor pin as output
pinMode(motor1, OUTPUT);
pinMode(motor2, OUTPUT);
pinMode(motor3, OUTPUT);
pinMode(motor4, OUTPUT);
//turning on access point
WiFi.softAP(ssid, password);
//output for checking wifi and finding ip
Serial.println("NodeMCU is connected");
Serial.print("IP address: ");
Serial.println(WiFi.softAPIP());
//starts server for client requests
server.begin();
}
void loop() {
//client reads any device connected to network
client = server.available();
//if client is connected
if (client){
//read request and string management
String request = client.readStringUntil('\n');
Serial.println(request);
request.trim();
//motor commands
if (request.indexOf("GET /?dir=forward") >= 0) {
forward();
Serial.println("FORWARD");
}
else if (request.indexOf("GET /?dir=backward") >= 0) {
backward();
Serial.println("BACK");
}
else if (request.indexOf("GET /?dir=stop") >= 0) {
stop();
Serial.println("STOP");
}
else if (request.indexOf("GET /?dir=right") >= 0) {
right();
Serial.println("RIGHT");
}
else if (request.indexOf("GET /?dir=left") >= 0) {
left();
Serial.println("LEFT");
}
else if (request.indexOf("GET /?dir=R") >= 0) {
Serial.println("ROTATE LEFT");
turnleft();
}
else if (request.indexOf("GET /?dir=L") >= 0) {
Serial.println("ROTATE RIGHT");
turnright();
}
}
}
void forward() {
digitalWrite(motor2, LOW);
digitalWrite(motor1, HIGH);
digitalWrite(motor4, HIGH);
digitalWrite(motor3, LOW);
}
void backward() {
digitalWrite(motor2, HIGH);
digitalWrite(motor1, LOW);
digitalWrite(motor4, LOW);
digitalWrite(motor3, HIGH);
}
void right() {
digitalWrite(motor2, LOW);
digitalWrite(motor1, HIGH);
digitalWrite(motor4, LOW);
digitalWrite(motor3, HIGH);
}
void left() {
digitalWrite(motor2, HIGH);
digitalWrite(motor1, LOW);
digitalWrite(motor4, HIGH);
digitalWrite(motor3, LOW);
}
void stop() {
digitalWrite(motor1, LOW);
digitalWrite(motor2, LOW);
digitalWrite(motor3, LOW);
digitalWrite(motor4, LOW);
}
void turnright() {
right();
delay(500);
void turnleft() {
left();
delay(500);
}