-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal
218 lines (176 loc) · 4.92 KB
/
final
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*************************************************************************************
* Robot Control with Android App and NodeMCU
*
* Differential Robot using H-Bridget L293D
*
* Voice activation and response
*
* Developed by Marcelo Rovai on 31March17
* Visit my blog: https://MJRoBot.org
*
************************************************************************************/
#include <ESP8266WiFi.h>
WiFiClient client;
WiFiServer server(80);
const char* ssid = "hello nodemcu";
const char* password = "nodemcu4";
String command =""; // Command received from Android device
// Set Motor Control Pins
int rightMotor2 = 12; // D7 - right Motor -
int rightMotor1 = 13; // D8 - right Motor +
int leftMotor2 = 0; // D3 - left Motor -
int leftMotor1 = 2; // D4 - left Motor +
int sensorValue;
float voltage;
int serialCount = 0;
void setup()
{
Serial.begin(115200);
pinMode(leftMotor1, OUTPUT);
pinMode(leftMotor2, OUTPUT);
pinMode(rightMotor1, OUTPUT);
pinMode(rightMotor2, OUTPUT);
digitalWrite(leftMotor1,LOW);
digitalWrite(leftMotor2,LOW);
digitalWrite(rightMotor1,LOW);
digitalWrite(rightMotor2,LOW);
pinMode(14, OUTPUT);
connectWiFi();
server.begin();
}
void loop()
{
if(serialCount == 10000) {
sensorValue = analogRead(A0);
voltage = sensorValue * (5.0/1023.0);
Serial.println(voltage);
serialCount = 0;
}
else {
serialCount ++;
}
digitalWrite(14, HIGH);
if(voltage < 1.5) {
stopMotor();
return;
}
client = server.available();
if (!client) return;
command = checkClient();
if(voltage > 1.5) {
if (command == "forward" || command == "go%20forward"){
forwardMotor();
}
else if (command == "reverse" || command == "backward" || command == "go%20back"){
reverseMotor();
}
else if (command == "left" || command == "turn%20left"){
leftMotor();
}
else if (command == "right" || command == "turn%20right") {
rightMotor();
}
else if (command == "stop") {
stopMotor();
}
sendBackEcho(command); // send command echo back to android device
command = "";
}
}
/* command motor forward */
void forwardMotor(void)
{
analogWrite(leftMotor1, 750);
analogWrite(rightMotor1, 600);
analogWrite(leftMotor2, 0);
analogWrite(rightMotor2, 0);
digitalWrite(leftMotor1,HIGH);
digitalWrite(leftMotor2,LOW);
digitalWrite(rightMotor1,HIGH);
digitalWrite(rightMotor2,LOW);
}
/* command motor backward */
void reverseMotor(void)
{
analogWrite(leftMotor1, 0);
analogWrite(rightMotor1, 0);
analogWrite(leftMotor2, 1023);
analogWrite(rightMotor2, 1023);
digitalWrite(leftMotor1,LOW);
digitalWrite(leftMotor2,HIGH);
digitalWrite(rightMotor1,LOW);
digitalWrite(rightMotor2,HIGH);
}
/* command motor turn left */
void leftMotor(void)
{
analogWrite(leftMotor1, 0);
analogWrite(rightMotor1, 500);
analogWrite(leftMotor2, 500);
analogWrite(rightMotor2, 0);
digitalWrite(leftMotor1,LOW);
digitalWrite(leftMotor2,HIGH);
digitalWrite(rightMotor1,HIGH);
digitalWrite(rightMotor2,LOW);
}
/* command motor turn right */
void rightMotor(void)
{
analogWrite(leftMotor1, 500);
analogWrite(rightMotor1, 0);
analogWrite(leftMotor2, 0);
analogWrite(rightMotor2, 500);
digitalWrite(leftMotor1,HIGH);
digitalWrite(leftMotor2,LOW);
digitalWrite(rightMotor1,LOW);
digitalWrite(rightMotor2,HIGH);
}
/* command motor stop */
void stopMotor(void)
{
analogWrite(leftMotor1, 0);
analogWrite(rightMotor1, 0);
analogWrite(leftMotor2, 0);
analogWrite(rightMotor2, 0);
digitalWrite(leftMotor1,LOW);
digitalWrite(leftMotor2,LOW);
digitalWrite(rightMotor1,LOW);
digitalWrite(rightMotor2,LOW);
}
/* connecting WiFi */
void connectWiFi()
{
Serial.println("Connecting to WIFI");
WiFi.begin(ssid, password);
while ((!(WiFi.status() == WL_CONNECTED)))
{
delay(300);
Serial.print("..");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("NodeMCU Local IP is : ");
Serial.print((WiFi.localIP()));
}
/* check command received from Android Device */
String checkClient (void)
{
while(!client.available()) delay(1);
String request = client.readStringUntil('\r');
request.remove(0, 5);
request.remove(request.length()-9,9);
return request;
}
/* send command echo back to android device */
void sendBackEcho(String echo)
{
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println(echo);
client.println("</html>");
client.stop();
delay(1);
}