-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifi_controlled_1.ino
182 lines (142 loc) · 3.61 KB
/
wifi_controlled_1.ino
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
#include <ESP8266WiFi.h>
char ssid[] = "k27";
char password[] = "987654321";
int motorR1= D0;
int motorR2=D1;
int motorL1=D2;
int motorL2=D3;
int EN1=D5;
int EN2=D6;
int value=0;
WiFiServer server(80);
void setup()
{
Serial.begin(115200);
delay(10);
pinMode(EN1,OUTPUT);
pinMode(EN2,OUTPUT);
pinMode(motorL1,OUTPUT);
pinMode(motorL2,OUTPUT);
pinMode(motorR1,OUTPUT);
pinMode(motorR2,OUTPUT);
digitalWrite(EN1,HIGH);
digitalWrite(EN2,HIGH);
// Connect to WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(300);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}
void loop()
{
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
// Match the request
if (request.indexOf("/F=ON") != -1)
{
forward();
Serial.println("Your bot is gonna move forward");
value = 1;
}
else if (request.indexOf("/B=ON") != -1)
{
backward() ;
Serial.println("Your bot is gonna move bakwards");
value = 2;
}
else if (request.indexOf("/L=ON") != -1)
{
left();
Serial.println("Your bot is gonna move left");
value = 3;
}
else if (request.indexOf("/R=ON") != -1)
{
Serial.println("Your bot is gonna move right");
right();
value = 4;
}
// Set ledPin according to the request
//digitalWrite(ledPin, value);
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.print("Bot is now moving : ");
if(value == 1) {
client.print("Forward");
}
if ( value==2)
{
client.print("Backward");
}
if(value == 3) {
client.print("Left");
}
if(value==4) {
client.print("Right");
}
client.println("<br><br>");
client.println("<a href=\"/F=ON\"\"> <button>Move Forward </button></a>");
client.println("<a href=\"/B=ON\"\"><button>Move Backward </button></a><br />");
client.println("<a href=\"/L=ON\"\"><button>Move Left</button></a>");
client.println("<a href=\"/R=ON\"\"><button>Move Right</button></a><br />");
client.println("</html>");
}
void forward()
{
digitalWrite(motorR[0],HIGH);
digitalWrite(motorR[1],LOW);
digitalWrite(motorL[0],HIGH);
digitalWrite(motorL[1],LOW);
}
void backward()
{
digitalWrite(motorR[0],LOW);
digitalWrite(motorR[1],HIGH);
digitalWrite(motorL[0],LOW);
digitalWrite(motorL[1],HIGH);
}
void left()
{
digitalWrite(motorR[0],HIGH);
digitalWrite(motorR[1],LOW);
digitalWrite(motorL[0],LOW);
digitalWrite(motorL[1],HIGH);
}
void right()
{
digitalWrite(motorR[0],LOW);
digitalWrite(motorR[1],HIGH);
digitalWrite(motorL[0],HIGH);
digitalWrite(motorL[1],LOW);
}