-
Notifications
You must be signed in to change notification settings - Fork 6
/
Ejercicio02-Comprueba_Entorno_ESP8266.ino
375 lines (315 loc) · 9.01 KB
/
Ejercicio02-Comprueba_Entorno_ESP8266.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
Ejercicio02 - Test del entorno ESP8266 de trabajo para el curso Programación con Arduino
Web: https://www.aprendiendoarduino.com/cursos/programacion-arduino-2019/
Montaje: https://github.com/jecrespo/aprendiendoarduino-Curso_Programacion_Arduino_2019/blob/master/Esquemas%20Montajes%20Pr%C3%A1cticas%20Fritzing/Curso_Programacion-Montaje%20Wemos_bb.png
*/
//Librería Wifi
#include "ESP8266WiFi.h"
//Archivo de contraseñas: añade un fichero secrets.h con este contenido
//const char* ssid = "your-ssid";
//const char* password = "your-password";
#include "secrets.h"
//Librería Pantalla
#include <Wire.h>
#include <Adafruit_SSD1306.h>
//Librería LEDs RGB
#include <Adafruit_NeoPixel.h>
//Define pines
#define PIN_BOTON_B D3
#define PIN_RELE D1
#define PIN_RGB D4
#define LED_NUM 7
//Define pantalla
#define OLED_RESET 0 // GPIO0
//Objeto Pantalla
Adafruit_SSD1306 display(OLED_RESET);
//Objeto Leds
Adafruit_NeoPixel leds = Adafruit_NeoPixel(LED_NUM, PIN_RGB, NEO_GRB + NEO_KHZ800);
// Create an instance of the server
WiFiServer server(80);
boolean led_state = LOW;
void setup() {
Serial.begin(115200);
leds.begin(); // This initializes the NeoPixel library.
//Configure LED BUILTIN
pinMode(LED_BUILTIN, OUTPUT);
//Configure pulsador
pinMode(PIN_BOTON_B, INPUT);
//Pantalla
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 64x48)
Serial.print("Connecting to ");
Serial.println(ssid);
// Set WiFi to station mode and disconnect from an AP if it was previously connected
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
}
void loop() {
muestraMenu();
int respuesta = recogeRespuesta();
switch (respuesta) {
case 1:
muestraIP();
break;
case 2:
pruebaClienteWeb();
break;
case 3:
pruebaServidorWeb();
break;
case 4:
pruebaBoton();
break;
case 5:
pruebaRele();
break;
case 6:
pruebaLedsRGB();
break;
case 7:
pruebaPantalla();
break;
default:
Serial.println(F("Opcion incorrecta.\nVuelve a introducir test a realizar"));
break;
}
}
void muestraMenu() {
Serial.println(F("\n------------------------"));
Serial.println(F("MENU"));
Serial.println(F("Selecciona el test a ejecutar"));
Serial.println(F("1 - Muestra IP Arduino"));
Serial.println(F("2 - Test Cliente Web (comprobar respuesta del servidor)"));
Serial.println(F("3 - Test Servidor Web (comprobar servidor embebido en Arduino)"));
Serial.println(F("4 - Prueba Pulsador (comprobar pulsaciones correctas)"));
Serial.println(F("5 - Prueba Relé Shield"));
Serial.println(F("6 - Prueba Neopixel Shield"));
Serial.println(F("7 - Prueba Pantalla Oled Shield"));
Serial.println(F("------------------------"));
}
int recogeRespuesta() {
int segundos = 0;
String cadena_leida = "";
while (Serial.available() == 0) {
if (segundos > 10) {
segundos = 0;
Serial.println(F("\nEsperando respuesta..."));
muestraMenu();
}
Serial.print(".");
segundos++;
delay(1000);
}
if (Serial.available() > 0) {
do {
char caracter_leido = Serial.read();
cadena_leida += caracter_leido;
delay(5);
} while (Serial.available() > 0);
Serial.println("\nHe Leido la cadena: " + cadena_leida);
int respuesta = cadena_leida.toInt();
return respuesta;
}
}
void muestraIP() {
// Print the IP address
Serial.print("IP asignada: ");
Serial.println(WiFi.localIP());
}
void pruebaClienteWeb() {
const uint16_t port = 80;
const char * host = "www.aprendiendoarduino.com"; // ip or dns
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
if (!client.connect(host, port)) {
Serial.println("connection failed");
Serial.println("wait 5 sec...");
delay(5000);
return;
}
// This will send the request to the server
client.println("GET /servicios/aprendiendoarduino/ HTTP/1.0");
client.println("Host: www.aprendiendoarduino.com");
client.println();
int timeout_flag = 0;
while (client.available() == 0) {
static int count = 0;
Serial.print(".");
delay(250);
if (count > 12) //waiting more than 3 seconds
timeout_flag = 1;
break;
}
Serial.println();
if (timeout_flag == 0) {
Serial.println("Connection Successful. Server Answer:");
//read all lines from server
do {
String line = client.readStringUntil('\r'); //read line by line
Serial.print(line);
} while (client.available() > 0);
Serial.println();
}
else {
Serial.println("Connection error. Server doesn't respond");
}
Serial.println("closing connection");
client.stop();
}
void pruebaServidorWeb() {
Serial.print(F("Test Servidor Web. Para probar conectarse a la IP: "));
Serial.println(WiFi.localIP());
Serial.println(F("Pulsar 'q' para salir."));
while (true) {
//Compruebo si llega una q
if (Serial.available() > 0) {
char caracter_leido = Serial.read();
if (caracter_leido == 'q')
break;
}
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
continue;
}
// Wait until the client sends some data
Serial.println("new client");
while (!client.available()) {
delay(1);
}
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
// Match the request
if (req.indexOf("GET / HTTP/1.1") != -1)
muestraWeb(client);
else if (req.indexOf("POST / HTTP/1.1") != -1) {
Serial.println("------> Cambio estado LED");
led_state = !led_state;
digitalWrite(LED_BUILTIN, led_state); //cambio el estado del led
muestraWeb(client);
}
else {
Serial.println("invalid request");
client.stop();
}
client.flush();
delay(1);
Serial.println("Client disonnected");
}
}
void pruebaBoton() {
Serial.println(F("Prueba Pulsador"));
Serial.println(F("Pulsar 'q' para salir."));
int pulsos_b = 0;
static boolean anterior_b = digitalRead(PIN_BOTON_B);
while (true) {
//Compruebo si llega una q
if (Serial.available() > 0) {
char caracter_leido = Serial.read();
if (caracter_leido == 'q')
break;
}
boolean estado_b = digitalRead(PIN_BOTON_B);
if (anterior_b != estado_b) {
if (estado_b == HIGH) { //flanco ascendente pull-down
Serial.println(F("Pulsado boton B"));
pulsos_b++;
Serial.println("Numero pulsaciones boton B: " + (String)pulsos_b);
}
}
anterior_b = estado_b;
delay(50); //Evitar rebotes
}
}
void pruebaRele() {
pinMode(PIN_RELE, OUTPUT);
Serial.println(F("Prueba Relé"));
Serial.println(F("Pulsar 'q' para salir."));
while (true) {
//Compruebo si llega una q
if (Serial.available() > 0) {
char caracter_leido = Serial.read();
if (caracter_leido == 'q')
break;
}
digitalWrite(PIN_RELE, HIGH);
delay(2000);
digitalWrite(PIN_RELE, LOW);
delay(2000);
}
}
void pruebaLedsRGB() {
Serial.println(F("Prueba Leds RGB"));
Serial.println(F("Pulsar 'q' para salir."));
while (true) {
//Compruebo si llega una q
if (Serial.available() > 0) {
char caracter_leido = Serial.read();
if (caracter_leido == 'q')
break;
}
led_set(10, 0, 0);//red
led_set(0, 0, 0);
led_set(0, 10, 0);//green
led_set(0, 0, 0);
led_set(0, 0, 10);//blue
led_set(0, 0, 0);
}
}
void pruebaPantalla() {
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.setTextColor(WHITE);
display.print("Test de Pantalla!!!");
display.display();
delay(2000);
uint8_t color = 1;
for (int16_t i = 0; i < display.height() / 2; i += 3) {
// alternate colors
display.fillRect(i, i, display.width() - i * 2, display.height() - i * 2, color % 2);
display.display();
delay(1);
color++;
}
delay(2000);
display.clearDisplay();
display.display();
}
void muestraWeb(WiFiClient client) {
client.println("HTTP/1.0 200K");
client.println();
client.println("<!DOCTYPE html>");
client.println("<html>");
client.println("<body>");
if (led_state == HIGH) client.println("<p>LED ENCENDIDO</p>");
else client.println("<p>LED APAGADO</p>");
client.println("<form action=\"\" method=\"post\">");
if (led_state == HIGH) client.println("<input type=\"submit\" value=\"Apaga Led\" />");
else client.println("<input type=\"submit\" value=\"Enciende Led\" />");
client.println("</form>");
client.println("</body>");
client.println("</html>");
client.println();
client.stop();
client.flush();
}
void led_set(uint8 R, uint8 G, uint8 B) {
for (int i = 0; i < LED_NUM; i++) {
leds.setPixelColor(i, leds.Color(R, G, B));
leds.show();
delay(50);
}
}