-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathESP32 code
More file actions
213 lines (193 loc) · 5.47 KB
/
Copy pathESP32 code
File metadata and controls
213 lines (193 loc) · 5.47 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
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
// Load Wi-Fi library
#include <WiFi.h>
#define RXD2 16
#define TXD2 17
// Replace with your network credentials
const char* ssid = "";
const char* password = "";
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String response, ip_address;
// Auxiliar variables to store the current output state
String output26State = "off";
// Assign output variables to GPIO pins
const int output26 = 26;
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;
int wait30 = 30000; // time to reconnect when connection is lost.
// This is your Static IP
IPAddress local_IP(192, 168, 43, 107);
// Gateway IP address
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 0, 0);
IPAddress primaryDNS(8, 8, 8, 8);
IPAddress secondaryDNS(8, 8, 4, 4);
void setup() {
Serial.begin(115200);
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
// Initialize the output variables as outputs
pinMode(output26, OUTPUT);
// Set outputs to LOW
digitalWrite(output26, LOW);
//Configure Static IP
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS))
{
Serial.println("Static IP failed to configure");
}
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
ip_address = WiFi.localIP().toString();
Serial.println(ip_address);
server.begin();
}
void loop() {
// If disconnected, try to reconnect every 30 seconds.
if ((WiFi.status() != WL_CONNECTED) && (millis() > wait30)) {
Serial.println("Trying to reconnect WiFi...");
WiFi.disconnect();
WiFi.begin(ssid, password);
wait30 = millis() + 30000;
}
// Check if a client has connected..
WiFiClient client = server.available();
if (!client) {
return;
}
Serial.print("New client: ");
Serial.println(client.remoteIP());
// Espera hasta que el cliente envíe datos.
// while(!client.available()){ delay(1); }
/////////////////////////////////////////////////////
// Read the information sent by the client.
String req = client.readStringUntil('\r');
Serial.println(req);
// Make the client's request.
if (req.indexOf("status") != -1)
{
digitalWrite(output26, HIGH);
response = "WiFi Connected: " + ip_address;
Serial2.write(0x11);
}
if (req.indexOf("onRed") != -1)
{
digitalWrite(output26, HIGH);
response = "RED LED ON";
Serial2.write(0x31);
}
if (req.indexOf("offRed") != -1)
{
digitalWrite(output26, LOW);
response = "RED LED OFF";
Serial2.write(0x30);
}
if (req.indexOf("onGreen") != -1)
{
digitalWrite(output26, HIGH);
response = "GREEN LED ON";
Serial2.write(0x33);
}
if (req.indexOf("offGreen") != -1)
{
digitalWrite(output26, LOW);
response = "GREEN LED OFF";
Serial2.write(0x32);
}
if (req.indexOf("onBlue") != -1)
{
digitalWrite(output26, HIGH);
response = "BLUE LED ON";
Serial2.write(0x35);
}
if (req.indexOf("offBlue") != -1)
{
digitalWrite(output26, LOW);
response = "BLUE LED OFF";
Serial2.write(0x34);
}
if (req.indexOf("forwardSlightly") != -1)
{
digitalWrite(output26, HIGH);
response = "MOVED FORWARD";
Serial2.write(0x21);
}
if (req.indexOf("forwardHold") != -1)
{
digitalWrite(output26, HIGH);
response = "FORWARD";
Serial2.write(0x22);
}
if (req.indexOf("backwardSlightly") != -1)
{
digitalWrite(output26, HIGH);
response = "MOVED BACKWARD";
Serial2.write(0x23);
}
if (req.indexOf("backwardHold") != -1)
{
digitalWrite(output26, HIGH);
response = "BACKWARD";
Serial2.write(0x24);
}
if (req.indexOf("rightSlightly") != -1)
{
digitalWrite(output26, HIGH);
response = "TURNED RIGHT";
Serial2.write(0x25);
}
if (req.indexOf("rightHold") != -1)
{
digitalWrite(output26, HIGH);
response = "TURNING RIGHT";
Serial2.write(0x26);
}
if (req.indexOf("leftSlightly") != -1)
{
digitalWrite(output26, HIGH);
response = "TURNED LEFT";
Serial2.write(0x27);
}
if (req.indexOf("leftHold") != -1)
{
digitalWrite(output26, HIGH);
response = "TURNING LEFT";
Serial2.write(0x28);
}
if (req.indexOf("autoDrive") != -1)
{
digitalWrite(output26, HIGH);
response = "START AUTO DRIVE";
Serial2.write(0x51);
}
/*
if (req.indexOf("on12") != -1) {digitalWrite(LED12, HIGH); estado = "LED12 ON";}
if (req.indexOf("off12") != -1){digitalWrite(LED12, LOW); estado = "LED12 OFF";}
if (req.indexOf("on14") != -1) {digitalWrite(LED14, HIGH); estado = "LED14 ON";}
if (req.indexOf("off14") != -1){digitalWrite(LED14, LOW); estado = "LED14 OFF";}
if (req.indexOf("consulta") != -1){
estado ="";
if (digitalRead(LED12) == HIGH) {estado = "LED12 ON,";} else {estado = "LED12 OFF,";}
if (digitalRead(LED14) == HIGH) {estado = estado + "LED14 ON";} else {estado = estado + "LED14 OFF";}
}*/
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println(response); // Return status.
client.flush();
client.stop();
Serial.println("Client disconnected.");
}