0% found this document useful (0 votes)
47 views3 pages

Codigo Esp32 para Comunicacion Internet

This document defines code for an ESP32 WiFi module to serve as a web server. It includes functions for WiFi initialization, handling client requests, and generating an HTML webpage. The webpage displays the state of an LED connected to pin 2 and includes buttons to turn the LED on and off by making requests to the server. The server listens for client connections on port 80 and processes GET requests to change the LED state and return the webpage.

Uploaded by

Yulian Hernandez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views3 pages

Codigo Esp32 para Comunicacion Internet

This document defines code for an ESP32 WiFi module to serve as a web server. It includes functions for WiFi initialization, handling client requests, and generating an HTML webpage. The webpage displays the state of an LED connected to pin 2 and includes buttons to turn the LED on and off by making requests to the server. The server listens for client connections on port 80 and processes GET requests to change the LED state and return the webpage.

Uploaded by

Yulian Hernandez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

//Libraries

#include
<WiFi.h>//https://www.arduino.cc/en/Reference/WiFi
//Constants
#define LED 2
//Parameters
String request ;
char* ssid  = "********";
char* password  = "********";
String nom  = "ESP32";
//Objects
WiFiServer server(80);
WiFiClient client;
void setup(){
//Init Serial USB
Serial.begin(9600);
Serial.println(F("Initialize System"));
//Init ESP32Wifi
 Serial.print("Connecting to ");Serial.println(ssid);
WiFi.begin(ssid, password);
 // Connect to Wifi network.
while (WiFi.status() != WL_CONNECTED)
{
 delay(500);Serial.print(F("."));
}
 server.begin();
 Serial.println();
 Serial.println(F("ESP32Wifi initialized"));
 Serial.print(F("IP Address: "));
 Serial.println(WiFi.localIP());
pinMode(LED,OUTPUT);
}
void loop(){
WiFiClient client = server.available();
 if (client) {
   while(client.connected()){
     if (client.available()) {
       String request = client.readStringUntil('\r');
       Serial.println(request);
       handleRequest(request);
     }
     webpage(client);//Return webpage
     break;
   }
 }
   }
void handleRequest(String request){/* function
handleRequest */
////Handle web client request
String pwmCmd;
//Digital Ouputs
if (request.indexOf("/dig0on") > 0){
               digitalWrite(LED, HIGH);
         }
         if (request.indexOf("/dig0off") >0) {
               digitalWrite(LED, LOW);
         }
}
void webpage(WiFiClient client){/* function webpage */
////Send webpage to client
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head>");
client.println("<title> AranaCorp </title>");
client.println("<meta http-equiv='content-type'
content='text/html; charset=UTF-8'>");
client.println("<meta name='apple-mobile-web-app-
capable' content='yes' />");
client.println("<meta name='apple-mobile-web-app-
status-bar-style' content='black-translucent' />");
client.println("<meta http-equiv='refresh'
content='5'>");
client.println("</head>");
client.println("<body bgcolor = '#70706F'> ");
client.println("<hr/><hr>");
client.println("<h1 style='color : #3AAA35;'><center>
AranaCorp - "+nom+" Web Controller </center></h1>");
client.println("<hr/><hr>");
client.println("<br><br>");
client.println("<br><br><h2> Analog Inputs/Ouputs
</h2><center>");
client.println("  Pin G0");
client.println("  <input value="+String(analogRead(0))
+" readonly></input>");
client.println("  </center><h2> Digital Pin states
</h2><center><center>");
client.println("  LED Pin");
client.println("  <input
value="+String(digitalRead(LED))+"
readonly></input>");
client.println("  <a href='/dig0on'><button>Turn On
</button></a>");
client.println("  <a href='/dig0off'><button>Turn Off
</button></a><br /> ");
client.println("  </center><center>");
client.println("<table>");
client.println("<tr>");
if (digitalRead(LED)){
client.println("   <td>LED pin is HIGH</td>");
                }else{
client.println("<td>LED pin is LOW</td>    ");
               }
client.println("</tr>");
client.println("</table></center></body></html>");
delay(1);
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy