Está en la página 1de 12

INSTITUTO TECNOLÓGICO INDUSTRIAL COMERCIAL

“PUERTO MEJILLONES”
CARRERA DE ELECTRÓNICA 500 M
MICROCONTROLADORES II

PRACTICA_7 - NODEMCU -
SERVIDOR_WEB_AP
DOCENTE: ING. ERICK BENITEZ GARECA

ESTUDIANTES: ADRIAN MAURICIO AVILA OLAGUIVEL


JUAN DANIEL BELTRAN CONDORI
JHON KEVIN MOYA CONDORI

FECHA: 14 – Octubre – 2023

COCHABAMBA – BOLIVIA
2023
PRACTICA NRO: 7

PRACTICA_7 – NODEMCU SERVIDOR WEB AP

Unidad Temática: PUERTOS DE COMUNICACIÓN DE LOS


MICRONONTROLADORES ATMEL

1. Competencia a Desarrollar:
• Configuración de un Servidor Web en un NodeMCU.
• Creación de una red Wi-Fi Access Point (AP) utilizando un NodeMCU.
• Implementación de páginas web dinámicas para interactuar con el
NodeMCU.
2. Objetivos de la Práctica:
• Configuración del NodeMCU: Aprender a configurar un NodeMCU como un
servidor web y como punto de acceso Wi-Fi. Esto incluye la instalación del
firmware necesario en el NodeMCU.
• Creación de una Red Wi-Fi AP: Configurar el NodeMCU para funcionar
como un punto de acceso Wi-Fi, lo que permitirá a otros dispositivos
conectarse a él.
• Desarrollo de Páginas Web Dinámicas: Desarrollar habilidades para crear
páginas web dinámicas que puedan ser servidas por el NodeMCU. Estas
páginas pueden contener controles interactivos, como botones y
formularios, que permitan a los usuarios interactuar con el NodeMCU.
• Control y Monitoreo del NodeMCU: Aprender a programar el NodeMCU
para controlar dispositivos o sensores conectados a él, y para mostrar
información o estados en las páginas web.
3. Contenido Temático:
• Microcontrolador ESP8266
• Aplicaciones IOT
• Redes WIFI
• Tarjeta de desarrollo NODEMCU
4. Materiales:
• Computadora Personal
• Arduino IDE
• Tarjeta de Desarrollo NODEMCU
• 10 leds
• 10 resistencias
5. Metodología:
✓ Implementación
6. Desarrollo Experimental:
Desarrollar un sistema que a partir de una página web puedan controlar 10 leds
que estén conectados a una tarjeta de desarrollo NodeMCU.
➢ El servidor WEB debe correr en el NodeMCU.
➢ El NodeMCU debe de estar configurado como AP
➢ Considerar como SSID: NODE (iniciales de los nombres de los integrantes
del grupo).
➢ El puerto de escucha debe ser el 797

7. Resultados Obtenidos:

#include <ESP8266WiFi.h>
#define L1 16 //GPIO16 D0
#define L2 5 //GPIO5 D1
#define L3 4 //GPIO4 D2
#define L4 0 //GPIO0 D3
#define L5 2 //GPIO2 D4
#define L6 14 //GPIO14 D5
#define L7 12 //GPIO12 D6
#define L8 13 //GPIO13 D7
#define L9 15 //GPIO15 D8
#define L10 3 //GPIO3 RX

// Replace with your network credentials


const char* ssid = "NODEMCUEBG_AP BELTRAN";
const char* password = "123456788";

WiFiServer server(797);

String header;
String LED1 = "off";
String LED2 = "off";
String LED3 = "off";
String LED4 = "off";
String LED5 = "off";
String LED6 = "off";
String LED7 = "off";
String LED8 = "off";
String LED9 = "off";
String LED10 = "off";

void setup()
{
Serial.begin(115200);
// Initialize the output variables as outputs
pinMode(L1,OUTPUT);
pinMode(L2,OUTPUT);
pinMode(L3,OUTPUT);
pinMode(L4,OUTPUT);
pinMode(L5,OUTPUT);
pinMode(L6,OUTPUT);
pinMode(L7,OUTPUT);
pinMode(L8,OUTPUT);
pinMode(L9,OUTPUT);
pinMode(L10,OUTPUT);
// Set outputs to LOW
digitalWrite(L1,LOW);
digitalWrite(L2,LOW);
digitalWrite(L3,LOW);
digitalWrite(L4,LOW);
digitalWrite(L5,LOW);
digitalWrite(L6,LOW);
digitalWrite(L7,LOW);
digitalWrite(L8,LOW);
digitalWrite(L9,LOW);
digitalWrite(L10,LOW);

// Connect to Wi-Fi network with SSID and password


delay(500);
Serial.println("Configurando AP (Access Point) . . . ");
// Remove the password parameter, if you want the AP (Access Point) to be
open
WiFi.softAP(ssid,password);

IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address:= ");
Serial.println(IP);

server.begin();
}

void loop()
{
WiFiClient client = server.available(); // Listen for incoming clients

if (client)
{ // If a new client connects,
Serial.println("Nuevo cliente"); // print a message out in the
serial port
String currentLine = ""; // make a String to hold
incoming data from the client
while (client.connected())
{ // loop while the client's connected
if (client.available())
{ // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial
monitor
header += c;
if (c == '\n')
{ // if the byte is a newline character
// if the current line is blank, you got two newline characters in
a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0)
{
// HTTP headers always start with a response code (e.g. HTTP/1.1
200 OK)
// and a content-type so the client knows what's coming, then a
blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();

// turns the GPIOs on and off


if (header.indexOf("GET /L1/on") >= 0)
{
Serial.println("LED1 on");
LED1 = "on";
digitalWrite(L1,HIGH);
}
else if (header.indexOf("GET /L1/off") >= 0)
{
Serial.println("LED1 off");
LED1 = "off";
digitalWrite(L1, LOW);
}
else if (header.indexOf("GET /L2/on") >= 0)
{
Serial.println("LED2 on");
LED2 = "on";
digitalWrite(L2, HIGH);
}
else if (header.indexOf("GET /L2/off") >= 0) {
Serial.println("LED2 off");
LED2 = "off";
digitalWrite(L2,LOW);
}
else if (header.indexOf("GET /L3/on") >= 0)
{
Serial.println("LED3 on");
LED3 = "on";
digitalWrite(L3, HIGH);
}
else if (header.indexOf("GET /L3/off") >= 0) {
Serial.println("LED3 off");
LED3 = "off";
digitalWrite(L3,LOW);
}
else if (header.indexOf("GET /L4/on") >= 0)
{
Serial.println("LED4 on");
LED4 = "on";
digitalWrite(L4, HIGH);
}
else if (header.indexOf("GET /L4/off") >= 0) {
Serial.println("LED4 off");
LED4 = "off";
digitalWrite(L4,LOW);
}
else if (header.indexOf("GET /L5/on") >= 0)
{
Serial.println("LED5 on");
LED5 = "on";
digitalWrite(L5, HIGH);
}
else if (header.indexOf("GET /L5/off") >= 0) {
Serial.println("LED5 off");
LED5 = "off";
digitalWrite(L5,LOW);
}
else if (header.indexOf("GET /L6/on") >= 0)
{
Serial.println("LED6 on");
LED6 = "on";
digitalWrite(L6, HIGH);
}
else if (header.indexOf("GET /L6/off") >= 0) {
Serial.println("LED6 off");
LED6 = "off";
digitalWrite(L6,LOW);
}
else if (header.indexOf("GET /L7/on") >= 0)
{
Serial.println("LED7 on");
LED7 = "on";
digitalWrite(L7, HIGH);
}
else if (header.indexOf("GET /L7/off") >= 0) {
Serial.println("LED7 off");
LED7 = "off";
digitalWrite(L7,LOW);
}
else if (header.indexOf("GET /L8/on") >= 0)
{
Serial.println("LED8 on");
LED8 = "on";
digitalWrite(L8, HIGH);
}
else if (header.indexOf("GET /L8/off") >= 0) {
Serial.println("LED8 off");
LED8 = "off";
digitalWrite(L8,LOW);
}
else if (header.indexOf("GET /L9/on") >= 0)
{
Serial.println("LED9 on");
LED9 = "on";
digitalWrite(L9, HIGH);
}
else if (header.indexOf("GET /L9/off") >= 0) {
Serial.println("LED9 off");
LED9 = "off";
digitalWrite(L9,LOW);
}
else if (header.indexOf("GET /L10/on") >= 0)
{
Serial.println("LED10 on");
LED10 = "on";
digitalWrite(L10, HIGH);
}
else if (header.indexOf("GET /L10/off") >= 0) {
Serial.println("LED10 off");
LED10 = "off";
digitalWrite(L10,LOW);
}

// Display the HTML web page


client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\"
content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the on/off buttons
// Feel free to change the background-color and font-size
attributes to fit your preferences
client.println("<style>html { font-family: Helvetica; display:
inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #4CAF50; border:
none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin:
2px; cursor: pointer;}");
client.println(".button2 {background-color:
#555555;}</style></head>");

// Web Page Heading


client.println("<body><h1>Servidor Web NODEMCU MIC 500 -
CONTROL</h1>");

client.println("<p>Luces - Estado " + LED1 + "</p>");


if (LED1=="off")
{
client.println("<p><a href=\"/L1/on\"><button
class=\"button\">ON</button></a></p>");
}
else
{
client.println("<p><a href=\"/L1/off\"><button class=\"button
button2\">OFF</button></a></p>");
}

client.println("<p>Pasillo - Estado " + LED2 + "</p>");


if (LED2=="off") {
client.println("<p><a href=\"/L2/on\"><button
class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/L2/off\"><button class=\"button
button2\">OFF</button></a></p>");
}

client.println("<p>Cocina - Estado " + LED3 + "</p>");


if (LED3=="off") {
client.println("<p><a href=\"/L3/on\"><button
class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/L3/off\"><button class=\"button
button2\">OFF</button></a></p>");
}
client.println("<p>Cocina - Estado " + LED4 + "</p>");
if (LED4=="off") {
client.println("<p><a href=\"/L4/on\"><button
class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/L4/off\"><button class=\"button
button2\">OFF</button></a></p>");
}

client.println("<p>Cocina - Estado " + LED5 + "</p>");


if (LED5=="off") {
client.println("<p><a href=\"/L5/on\"><button
class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/L5/off\"><button class=\"button
button2\">OFF</button></a></p>");
}

client.println("<p>Cocina - Estado " + LED6 + "</p>");


if (LED6=="off") {
client.println("<p><a href=\"/L6/on\"><button
class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/L6/off\"><button class=\"button
button2\">OFF</button></a></p>");
}

client.println("<p>Cocina - Estado " + LED7 + "</p>");


if (LED7=="off") {
client.println("<p><a href=\"/L7/on\"><button
class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/L7/off\"><button class=\"button
button2\">OFF</button></a></p>");
}

client.println("<p>Cocina - Estado " + LED8 + "</p>");


if (LED8=="off") {
client.println("<p><a href=\"/L8/on\"><button
class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/L8/off\"><button class=\"button
button2\">OFF</button></a></p>");
}

client.println("<p>Cocina - Estado " + LED9 + "</p>");


if (LED9=="off") {
client.println("<p><a href=\"/L9/on\"><button
class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/L9/off\"><button class=\"button
button2\">OFF</button></a></p>");
}

client.println("<p>Cocina - Estado " + LED10 + "</p>");


if (LED10=="off") {
client.println("<p><a href=\"/L10/on\"><button
class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/L10/off\"><button class=\"button
button2\">OFF</button></a></p>");
}

client.println("</body></html>");

// The HTTP response ends with another blank line


client.println();
// Break out of the while loop
break;
} else
{ // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r')
{ // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}

8. Conclusiones
la práctica logró cumplir con los objetivos establecidos al darnos conocimientos
necesarios para configurar un NodeMCU como:
• Configuración y Red Wi-Fi: Se logró la configuración del NodeMCU como
servidor web y punto de acceso Wi-Fi.
• Páginas Web Interactivas: Se desarrollaron páginas web interactivas.
• Control y Monitoreo: El NodeMCU se programó para controlar dispositivos
y mostrar información.
Además, se tomó en cuenta temas como IoT y redes Wi-Fi.

También podría gustarte