-
Notifications
You must be signed in to change notification settings - Fork 0
/
FullCodeForAmbhibian.ino
114 lines (96 loc) · 3.5 KB
/
FullCodeForAmbhibian.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
/* include library */
#include <ESP8266WiFi.h>
/* define port */
WiFiClient client;
WiFiServer server(80);
/* WIFI settings */
const char* ssid = "sagar";
const char* password = "sagar1234";
/* data received from application */
String data ="";
/* define L298N or L293D motor control pins */
int leftMotorForward = 2; /* GPIO2(D4) -> IN3 d4*/
int rightMotorForward = 15; /* GPIO15(D8) -> IN1 d8 */
int leftMotorBackward = 0; /* GPIO0(D3) -> IN4 d3*/
int rightMotorBackward = 13; /* GPIO13(D7) -> IN2 d7*/
/* define L298N or L293D enable pins */
int rightMotorENB = 14; /* GPIO14(D5) -> Motor-A Enable d5 */
int leftMotorENB = 12; /* GPIO12(D6) -> Motor-B Enable d6*/
void setup()
{
/* initialize motor control pins as output */
pinMode(leftMotorForward, OUTPUT);
pinMode(rightMotorForward, OUTPUT);
pinMode(leftMotorBackward, OUTPUT);
pinMode(rightMotorBackward, OUTPUT);
/* start server communication IP*/
server.begin();
Serial.begin(115200);
}
void loop()
{
/* If the server available, run the "checkClient" function */
client = server.available();
if (!client) return;
data = checkClient ();
Serial.println(data);
/************************ Run function according to incoming data from application *************************/
/* If the incoming data is "forward", run the "MotorForward" function */
if (data == "forward") MotorForward();
/* If the incoming data is "backward", run the "MotorBackward" function */
else if (data == "backward") MotorBackward();
/* If the incoming data is "left", run the "TurnLeft" function */
else if (data == "left") TurnLeft();
/* If the incoming data is "right", run the "TurnRight" function */
else if (data == "right") TurnRight();
/* If the incoming data is "stop", run the "MotorStop" function */
else if (data == "stop") MotorStop();
}
/********************************************* FORWARD *****************************************************/
void MotorForward(void)
{
digitalWrite(leftMotorForward,HIGH);
digitalWrite(rightMotorForward,HIGH);
digitalWrite(leftMotorBackward,LOW);
digitalWrite(rightMotorBackward,LOW);
}
/********************************************* BACKWARD *****************************************************/
void MotorBackward(void)
{
digitalWrite(leftMotorBackward,HIGH);
digitalWrite(rightMotorBackward,HIGH);
digitalWrite(leftMotorForward,LOW);
digitalWrite(rightMotorForward,LOW);
}
/********************************************* TURN LEFT *****************************************************/
void TurnLeft(void)
{
digitalWrite(leftMotorForward,LOW);
digitalWrite(rightMotorForward,HIGH);
digitalWrite(rightMotorBackward,LOW);
digitalWrite(leftMotorBackward,HIGH);
}
/********************************************* TURN RIGHT *****************************************************/
void TurnRight(void)
{
digitalWrite(leftMotorForward,HIGH);
digitalWrite(rightMotorForward,LOW);
digitalWrite(rightMotorBackward,HIGH);
digitalWrite(leftMotorBackward,LOW);
}
/********************************************* STOP *****************************************************/
void MotorStop(void)
{
digitalWrite(leftMotorForward,LOW);
digitalWrite(leftMotorBackward,LOW);
digitalWrite(rightMotorForward,LOW);
digitalWrite(rightMotorBackward,LOW);
}
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;
}