BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

webSocketServer

nonblynkrelated

#1

hola buenas noches, este proyecto es de un control de luces y sensor de temperatura, he buscado mucho y no he logrado incorporar el sistema webSocketsServer a este proyecto, y el otro problema que me ocurre es que en el monitor serial no aparece el ip ni nombre de la red esp8266, les pido humildemente su ayuda, porque yo no se mucho de programacion y menos de WebSocketServer, muchas gracias y si les sirve el codigo es para quien lo use, muchas gracias a ustedes.

#include <DHT.h>
#include <ESP8266WiFi.h>

// 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 header;

// Auxiliar variables to store the current output state
String output16State = "off";
String output5State = "off";
String output4State = "off";
String output3State = "off";
String output2State = "off";
String output1State = "off";
String output0State = "off";

// Assign output variables to GPIO pins
const int output16 = 16;

const int SENSOR = 14;
int temp, humedad;

const int output5 = 15;
const int output4 = 13;
const int output3 = 12;
const int output2 = 2;
const int output1 = 1;
const int output0 = 0;

DHT dht (SENSOR, DHT11);

void setup() {
  Serial.begin(115200);
  // Initialize the output variables as outputs
  pinMode(output16, OUTPUT);
  pinMode(SENSOR, OUTPUT);
  pinMode(output5, OUTPUT);
  pinMode(output4, OUTPUT);
  pinMode(output3, OUTPUT);
  pinMode(output2, OUTPUT);
  pinMode(output1, OUTPUT);
  pinMode(output0, OUTPUT);

  // Set outputs to LOW
  digitalWrite(output16, LOW);
  digitalWrite(SENSOR, LOW);
  digitalWrite(output5, LOW);
  digitalWrite(output4, LOW);
  digitalWrite(output3, LOW);
  digitalWrite(output2, LOW);
  digitalWrite(output1, LOW);
  digitalWrite(output0, LOW);

  // Connect to Wi-Fi network with SSID and password
  dht.begin();  
  Serial.printf("Conectando: %\n, 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(ssid);
  Serial.println("WiFi conectado.");
  Serial.println("Direccion IP: ");
  Serial.println(WiFi.localIP());
  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");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();
            
            // cambia los GPIOs on y off
            if (header.indexOf("GET /5/on") >= 0) {
              Serial.println("GPIO 5 on");
              output5State = "on";
              digitalWrite(output5, HIGH);
            } 
            else if (header.indexOf("GET /5/off") >= 0) {
              Serial.println("GPIO 5 off");
              output5State = "off";
              digitalWrite(output5, LOW);
            }
                        
            else if (header.indexOf("GET /4/on") >= 0) {
              Serial.println("GPIO 4 on");
              output4State = "on";
              digitalWrite(output4, HIGH);
            }
            else if (header.indexOf("GET /4/off") >= 0) {
              Serial.println("GPIO 4 off");
              output4State = "off";
              digitalWrite(output4, LOW);
            }
            
            else if (header.indexOf("GET /3/on") >= 0) {
              Serial.println("GPIO 3 on");
              output3State = "on";
              digitalWrite(output3, HIGH);
            }
            else if (header.indexOf("GET /3/off") >= 0) {
              Serial.println("GPIO 3 off");
              output3State = "off";
              digitalWrite(output3, LOW);
            }
            
            else if (header.indexOf("GET /2/on") >= 0) {
              Serial.println("GPIO 2 on");
              output2State = "on";
              digitalWrite(output2, HIGH);
            }
            else if (header.indexOf("GET /2/off") >= 0) {
              Serial.println("GPIO 2 off");
              output2State = "off";
              digitalWrite(output2, LOW);
            }
            
            else if (header.indexOf("GET /1/on") >= 0) {
              Serial.println("GPIO 1 on");
              output1State = "on";
              digitalWrite(output1, HIGH);
            }
            else if (header.indexOf("GET /1/off") >= 0) {
              Serial.println("GPIO 1 off");
              output1State = "off";
              digitalWrite(output1, LOW);
            }
            
            else if (header.indexOf("GET /0/on") >= 0) {
              Serial.println("GPIO 0 on");
              output0State = "on";
              digitalWrite(output0, HIGH);
            }
            else if (header.indexOf("GET /0/off") >= 0) {
              Serial.println("GPIO 0 off");
              output0State = "off";
              digitalWrite(output0, LOW);
            }
            else if (header.indexOf("GET /16/on") >= 0) {
              Serial.println("GPIO 16 on");
              output16State = "on";
              digitalWrite(output16, HIGH);
            }
            else if (header.indexOf("GET /16/off") >= 0) {
              Serial.println("GPIO 16 off");
              output16State = "off";
              digitalWrite(output16, LOW);
            }
            
            // Muestra el contenido de la pagina web
            client.println("<!DOCTYPE html><html>");
 
            client.println("<html lang='es'>");
            client.println("<head><title>ByR Hogar</title><meta charset='utf-8'/><meta name='viewport' content='user-scalable=no, width=device-width, initial-scale=1'><link rel='stylesheet' href='estilos.css' /><meta><link rel='shortcut icon' href='/favicon.ico' /></head>");
            client.println("<link rel=\'icon\' href=\'data:,\'></head>");

            // estilo CSS de los botones on/off  
            client.println("<style>body {font-family: Helvetica; display: block; margin: 0px auto; text-align: center;}");
            client.println("body { text-align: center; background-color: #DAF7A6;}");
            client.println("header{ background-color: #B3CA89; height: 50px; color: blue; margin-top: -20px;margin-bottom: -20px}");
            client.println("h1{ padding-top: 8px;} hr{ margin-top: 23px;}");
            client.println("h2{margin-top: 10px;}");
            client.println(" body h3{ margin-top: 20px; margin-bottom: 13px; font-size: 16px; color: brown;}");
            client.println(" section { width: 98%; height: 100%px; margin: auto; display: flex; flex-wrap: wrap; justify-content: space-around; margin-top: -22px;}");
            client.println(" section p{ color: #FF0000;margin-bottom:6px;}");
            client.println(".comedor{ width: 30%; border: 1px;}");
            client.println(".dormit{ width: 30%; border: 1px;}");
            client.println(".baño{width: 30%; border: 1px;}");
            client.println(".button{ background: red; Z-index:1; color: aliceblue; margin-top: -10px; font-size: 21px; text-decoration: none; border-radius: 35px; width: 90%; height: 55px; cursor: pointer;}");
            client.println(".button2 {background-color: #47FB05; Z-index:100; color:#0C5107}");
            client.println(" div{ width: 30%; margin: -10px; border: 2px;}");
            client.println(".temp{ position: relative; margin-top: 4px; width: 100%; display: flex; flex-wrap: nowrap; justify-content: space-around;}");
            client.println(".clima{margin-top: 20px; width: 100%; display: flex; flex-wrap: nowrap; justify-content: space-around; padding:0px;}");
            client.println(".temp div{ margin-top: -35px;}");
            client.println(".temp button{ height: 55px; margin-top: -30px; font-size: 18px;}");
            client.println(".temp p{font-size: 27px;}");
            client.println(".temp span{font-size: 20px; color: brown;}");
            client.println(".final{width: 100%;position:relative;}");
            client.println(".final p{width:100%; font-size:13px;color:#000;}");
            client.println(".final footer p{width:100%; margin-bottom: 5px; font-size:15;color: brown;}");
            client.println("</style>");
            
            //muestra la página en pantalla
            client.println("<body>");
            client.println("<header><h1>ByR Hogar Inteligente</h1></header>");
            client.println("<hr><h2>Control por Habitación</h2>");
            client.println("<section><div class='comedor'>");
            // Si el output5State es off, se muestra el boton ON
            client.println("<h3>Comedor " + output5State + "</h3>");
            if (output5State=="off") {
            client.println("<p><a href=\"/5/on\"><button class='button'>OFF</button></a></p>");
            } else {
              client.println("<p><a href=\"/5/off\"><button class=\"button button2\">ON</button></a></p>");
            }
            client.println("<h3>Alacena " + output4State + "</h3>");
            if (output4State=="off") { 
            client.println("<p><a href=\"/4/on\"><button class='button'>OFF</button></a></p>");
            } else {
              client.println("<p><a href=\"/4/off\"><button class=\"button button2\">ON</button></a></p>");
            }
            client.println("</div>");
            
            client.println("<div class='dormit'>");
            client.println("<h3>Dormitorio " + output3State + "</h3>");
            if (output3State=="off") { 
            client.println("<p><a href=\"/3/on\"><button class='button'>OFF</button></a></p>");
            } else {
              client.println("<p><a href=\"/3/off\"><button class=\"button button2\">ON</button></a></p>");
            }
            client.println("<h3>Lampara " + output2State + "</h3>");
            if (output2State=="off") { 
            client.println("<p><a href=\"/2/on\"><button class='button'>OFF</button></a></p>");
            } else {
              client.println("<p><a href=\"/2/off\"><button class=\"button button2\">ON</button></a></p>");
            }          
            client.println("</div>");
            
            client.println("<div class='baño'>");
            client.println("<h3>Baño " + output1State + "</h3>");
            if (output1State=="off") { 
            client.println("<p><a href=\"/1/on\"><button class='button'>OFF</button></a></p>");
            } else {
              client.println("<p><a href=\"/1/off\"><button class=\"button button2\">ON</button></a></p>");
            }
            client.println("<h3>Tocador " + output0State + "</h3>");
            if (output0State=="off") { 
            client.println("<p><a href=\"/0/on\"><button class='button'>OFF</button></a></p>");
            } else {
              client.println("<p><a href=\"/0/off\"><button class=\"button button2\">ON</button></a></p>");
            }
            client.println("</div>");
            client.println("</section>");
            
            client.println("<section class='clima'><div class='temp'>");
            client.println("<div><p>");
            temp = dht.readTemperature();
            delay(500);
            client.println("<span>Temperatura</span>");
            client.print(temp);
            client.println("°C");
            client.println("</p></div>");
            client.println("<div><h3>Puerta " + output16State + "</h3>");      
            if (output16State=="off") { 
            client.println("<p><a href=\"/16/on\"><button class='button'>CERRADA</button></a></p>");
            } else {
              client.println("<p><a href=\"/16/off\"><button class=\"button button2\">ABIERTA</button></a></p>");
            }
            client.println("</div>");          
            client.println("<div><p>");
            humedad = dht.readHumidity();
            delay(500);
            client.println("<span>Humedad</span>");
            client.print(humedad);
            client.println("%");
            client.println("</p></div>");
            client.println("</div></section>");
            client.println("<hr><section class='final'>");
            client.println("<aside><h3>Tecnologia Y hogar</h3><p>Por medio de la tecnologia e internet podemos usar cada artefacto eléctrico y electrónico que tengamos en nuestro hogar</p></aside>");
            client.println("<footer><p>Diseñado por Michel Bernales &copy; 2018</p></footer>");
            client.println("</section></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 = "";
    // Cierra la coneccion
    client.stop();
    Serial.println("Cliente desconectado.");
    Serial.println("");
  }
}

#2

Hello. I had to format your post to properly see the code…

And now that I see it…

I don’t even know where to begin :astonished:

This does not appear to be a Blynk sketch at all…