domingo, 2 de diciembre de 2018

Servidor http con ESP32 (8 bits)

Falta depurar el código, limpiar comentarios y activar las correspondientes salidas, pero ya funciona.
No olvidar poner el nombre de la red y el password.

Código:


#include <WiFi.h>

// Replace with your network credentials
const char* ssid = "ssid";
const char* password = "password";//he habilitado el acceso a la red de área local
//ya funciona con inbita2

WiFiServer server(80);

const int led = 2; // the number of the LED pin

// Client variables
char linebuf[80];
int charcount = 0;

int estado=0;//byte con información de las 8 salidas

void setup()
{
  estado=0;
  // initialize the LED as an output:
  //pinMode(led, OUTPUT);
  //Initialize serial and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
  }

  // We start by connecting to a WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  // attempt to connect to Wifi network:
  while (WiFi.status() != WL_CONNECTED)
  {
    // Connect to WPA/WPA2 network.
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();
  Serial.print("Estado: ");
  Serial.println(WiFi.status());
}

void loop()
{
  delay(1000);
  // listen for incoming clients
  WiFiClient client = server.available();
  if (client)
  {
    Serial.println("New client");
    memset(linebuf, 0, sizeof(linebuf));
    charcount = 0;
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected())
    {
      if (client.available())
      {
        char c = client.read();
        Serial.write(c);
        //read char by char HTTP request
        linebuf[charcount] = c;
        if (charcount < sizeof(linebuf) - 1) charcount++;

        if (c == '\n' && currentLineIsBlank)
        {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close"); // the connection will be closed after completion of the response
          client.println();
          client.println("<!DOCTYPE HTML><html><head>");
          client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"></head>");
          client.println("<h1>ESP32 - Web Server example</h1>");
          client.println("<p>LED <a href=\"01\"><button>ON</button></a>&nbsp;<a href=\"00\"><button>OFF</button></a></p>");
          client.println("<p>LED <a href=\"11\"><button>ON</button></a>&nbsp;<a href=\"10\"><button>OFF</button></a></p>");
          client.println("<p>LED <a href=\"21\"><button>ON</button></a>&nbsp;<a href=\"20\"><button>OFF</button></a></p>");
          client.println("<p>LED <a href=\"31\"><button>ON</button></a>&nbsp;<a href=\"30\"><button>OFF</button></a></p>");
          client.println("<p>LED <a href=\"41\"><button>ON</button></a>&nbsp;<a href=\"40\"><button>OFF</button></a></p>");
          client.println("<p>LED <a href=\"51\"><button>ON</button></a>&nbsp;<a href=\"50\"><button>OFF</button></a></p>");
          client.println("<p>LED <a href=\"61\"><button>ON</button></a>&nbsp;<a href=\"60\"><button>OFF</button></a></p>");
          client.println("<p>LED <a href=\"71\"><button>ON</button></a>&nbsp;<a href=\"70\"><button>OFF</button></a></p>");
          client.println("<table>");
          client.println("<tr>");
          client.println("<td  align=\"center\">0</td>");
          client.println("<td  align=\"center\">1</td>");
          client.println("<td  align=\"center\">2</td>");
          client.println("<td  align=\"center\">3</td>");
          client.println("<td  align=\"center\">4</td>");
          client.println("<td  align=\"center\">5</td>");
          client.println("<td  align=\"center\">6</td>");
          client.println("<td  align=\"center\">7</td>");
          client.println("</tr>");
          client.println("<tr>");
          client.print("<td  align=\"center\">");
          if (estado & 0x80) {
            client.print("<div style=\"width: 100px; height: 100px; background: #00ff00;\"></div>");
          }
          else {
            client.print("<div style=\"width: 100px; height: 100px; background: #ff0000;\"></div>");
          }
          client.print("</td>");
          client.print("<td  align=\"center\">");
          if (estado & 0x40) {
            client.print("<div style=\"width: 100px; height: 100px; background: #00ff00;\"></div>");
          }
          else {
            client.print("<div style=\"width: 100px; height: 100px; background: #ff0000;\"></div>");
          }
          client.print("</td>");
          client.print("<td  align=\"center\">");
          if (estado & 0x20) {
            client.print("<div style=\"width: 100px; height: 100px; background: #00ff00;\"></div>");
          }
          else {
            client.print("<div style=\"width: 100px; height: 100px; background: #ff0000;\"></div>");
          }
          client.print("</td>");
          client.print("<td  align=\"center\">");
          if (estado & 0x10) {
            client.print("<div style=\"width: 100px; height: 100px; background: #00ff00;\"></div>");
          }
          else {
            client.print("<div style=\"width: 100px; height: 100px; background: #ff0000;\"></div>");
          }
          client.print("</td>");
          client.print("<td  align=\"center\">");
          if (estado & 0x08) {
            client.print("<div style=\"width: 100px; height: 100px; background: #00ff00;\"></div>");
          }
          else {
            client.print("<div style=\"width: 100px; height: 100px; background: #ff0000;\"></div>");
          }
          client.print("</td>");
          client.print("<td  align=\"center\">");
          if (estado & 0x04) {
            client.print("<div style=\"width: 100px; height: 100px; background: #00ff00;\"></div>");
          }
          else {
            client.print("<div style=\"width: 100px; height: 100px; background: #ff0000;\"></div>");
          }
          client.print("</td>");
          client.print("<td  align=\"center\">");
          if (estado & 0x02) {
            client.print("<div style=\"width: 100px; height: 100px; background: #00ff00;\"></div>");
          }
          else {
            client.print("<div style=\"width: 100px; height: 100px; background: #ff0000;\"></div>");
          }
          client.print("</td>");
          client.print("<td  align=\"center\">");
          if (estado & 0x01) {
            client.print("<div style=\"width: 100px; height: 100px; background: #00ff00;\"></div>");
          }
          else {
            client.print("<div style=\"width: 100px; height: 100px; background: #ff0000;\"></div>");
          }
          client.print("</td>");


         
          client.print("</tr>");
          client.print("</table>");
         

         
          client.println("</html>");
          break;
        }
        if (c == '\n')
        {
          // you're starting a new line
          currentLineIsBlank = true;
          if (strstr(linebuf, "GET /00") > 0)
          {
            //Serial.println("LED ON");
            Serial.println("LED 0 ON");
            estado &= 0x7F; //AND CON 0111 1111
            //digitalWrite(led, HIGH);
          }
          else if (strstr(linebuf, "GET /01") > 0)
          {
            //Serial.println("LED OFF");
            Serial.println("LED 0 OFF");
            estado |= 0x80; //OR CON 1000 0000
            //digitalWrite(led, LOW);
          }


          if (strstr(linebuf, "GET /10") > 0)
          {
            //Serial.println("LED ON");
            Serial.println("LED 1 ON");
            estado &= 0xBF; //AND CON 1011 1111
            //digitalWrite(led, HIGH);
          }
          else if (strstr(linebuf, "GET /11") > 0)
          {
            //Serial.println("LED OFF");
            Serial.println("LED 1 OFF");
            estado |= 0x40; //OR CON 0100 0000
            //digitalWrite(led, LOW);
          }


          if (strstr(linebuf, "GET /20") > 0)
          {
            //Serial.println("LED ON");
            Serial.println("LED 2 ON");
            estado &= 0xDF; //AND CON 1101 1111
            //digitalWrite(led, HIGH);
          }
          else if (strstr(linebuf, "GET /21") > 0)
          {
            //Serial.println("LED OFF");
            Serial.println("LED 2 OFF");
            estado |= 0x20; //OR CON 0010 0000
            //digitalWrite(led, LOW);
          }


          if (strstr(linebuf, "GET /30") > 0)
          {
            //Serial.println("LED ON");
            Serial.println("LED 3 ON");
            estado &= 0xEF; //AND CON 1110 1111
            //digitalWrite(led, HIGH);
          }
          else if (strstr(linebuf, "GET /31") > 0)
          {
            //Serial.println("LED OFF");
            Serial.println("LED 3 OFF");
            estado |= 0x10; //OR CON 0001 0000
            //digitalWrite(led, LOW);
          }


          if (strstr(linebuf, "GET /40") > 0)
          {
            //Serial.println("LED ON");
            Serial.println("LED 4 ON");
            estado &= 0xF7; //AND CON 1111 0111
            //digitalWrite(led, HIGH);
          }
          else if (strstr(linebuf, "GET /41") > 0)
          {
            //Serial.println("LED OFF");
            Serial.println("LED 4 OFF");
            estado |= 0x08; //OR CON 0000 1000
            //digitalWrite(led, LOW);
          }


          if (strstr(linebuf, "GET /50") > 0)
          {
            //Serial.println("LED ON");
            Serial.println("LED 5 ON");
            estado &= 0xFB; //AND CON 1111 1011
            //digitalWrite(led, HIGH);
          }
          else if (strstr(linebuf, "GET /51") > 0)
          {
            //Serial.println("LED OFF");
            Serial.println("LED 5 OFF");
            estado |= 0x04; //OR CON 0000 0100
            //digitalWrite(led, LOW);
          }


          if (strstr(linebuf, "GET /60") > 0)
          {
            //Serial.println("LED ON");
            Serial.println("LED 6 ON");
            estado &= 0xFD; //AND CON 1111 1101
            //digitalWrite(led, HIGH);
          }
          else if (strstr(linebuf, "GET /61") > 0)
          {
            //Serial.println("LED OFF");
            Serial.println("LED 6 OFF");
            estado |= 0x02; //OR CON 0000 0010;
            //digitalWrite(led, LOW);
          }


          if (strstr(linebuf, "GET /70") > 0)
          {
            //Serial.println("LED ON");
            Serial.println("LED 7 ON");
            estado &= 0xFE; //AND CON 1111 1110;
            //digitalWrite(led, HIGH);
          }
          else if (strstr(linebuf, "GET /71") > 0)
          {
            //Serial.println("LED OFF");
            Serial.println("LED 7 OFF");
            estado |= 0x01; //OR CON 0000 0001
            //digitalWrite(led, LOW);
          }



          // you're starting a new line
          currentLineIsBlank = true;
          memset(linebuf, 0, sizeof(linebuf));
          charcount = 0;
        }
        else if (c != '\r')
        {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }



          Serial.print("Estado de las salidas: ");
          Serial.println(estado, HEX);
    // give the web browser time to receive the data
    delay(1);

    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}

No hay comentarios:

Publicar un comentario