PROBLEMA Domotica: Arduino. ESP8266

abemus

Nuovo Utente
14
0
Salve a tutti ragazzi. Ho comprato da poco la scheda nodeMCU, con modulo ESP8266 già installato sopra.
Il programma ha 4 stanze, di cui deve semplicemente controllare l'accensione e lo spegnimento delle luci delle rispettive stanze.
Ho 2 problemi:
Il primo è che una volta entrato via web riesco ad accendere le luci, ma non più a spegnerle. E non riesco a capire come mai. Però ho notato che manualmente, mediante analizza elemento, modificando la stringa:
Codice:
<a href="/5/on">
in:
Codice:
<a href="/5/off">
il pulsante funziona tranquillamente.
Il secondo problema è che all'avvio della scheda, ed entrati nella pagina web, trovo o la luce del bagno o quella del soggiorno (maggiormente quest'ultima) già accesa. Penso sia un problema di GPIO, in quanto il led del modulo wi-fi è già acceso quando la avvio.

Vi ringrazio in anticipo, e scusate se sono molto inesperto a riguardo.

Ecco il codice
Codice:
// Load Wi-Fi library
#include <ESP8266WiFi.h>

// Replace with your network credentials
const char* ssid     = "Vodafone";
const char* password = "ciaociao";

// 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 output5State = "off";
String output4State = "off";
String output0State = "off";
String output2State = "off";

// Assign output variables to GPIO pins
const int output5 = 5;
const int output4 = 4;
const int output0 = 0;
const int output2 = 2;

void setup() {
  Serial.begin(115200);
  // Initialize the output variables as outputs
  pinMode(output5, OUTPUT);
  pinMode(output4, OUTPUT);
  pinMode(output0, OUTPUT);
  pinMode(output2, OUTPUT);
  // Set outputs to LOW
  digitalWrite(output5, LOW);
  digitalWrite(output4, LOW);
  digitalWrite(output0, LOW);
  digitalWrite(output2, LOW);

  // Connect to Wi-Fi network with SSID and password
  Serial.print("Connecting to ");
  Serial.println(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("WiFi connected.");
  Serial.println("IP address: ");
  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("New Client.");          // 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 /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 /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 /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);
            }

            // Display the HTML web page
            client.println("<!DOCTYPE html><html>");
            client.println("<h1 style=\"text-align: center;\"><span style=\"color: #ff0000;\">Casa domotica</span></h1>");
            client.println("<p style=\"text-align: center;\"><span style=\"color: #000000;\"><strong><em>Controllo domotico di una casa mediante modulo Wi-Fi ESP8266 su scheda NodeMCU.</em></strong></span></p>");
            client.println("<p style=\"text-align: center;\">&nbsp;</p>");
            client.println("<p style=\"text-align: center;\"><span style=\"color: #3366ff;\"><strong><em>Luci:</em></strong></span></p>");
            client.println("<table style=\"height: 229px; border-color: red; margin-left: auto; margin-right: auto;\" border=\"1\" width=\"373;id=\">");
            client.println("<tbody>");
            client.println("<tr>");
            client.println("<th style=\"width: 117px; text-align: center;\"><strong><span style=\"color: #339966;\">&nbsp;Luogo:</span></strong></th>");
            client.println("<th style=\"width: 117px; text-align: center;\"><span style=\"color: #339966;\"><strong>&nbsp;Stato:</strong></span></th>");
            client.println("<th style=\"width: 117px; text-align: center;\"><span style=\"color: #339966;\"><strong>&nbsp;Controllo:</strong></span></th>");
            client.println("</tr>");
            client.println("<tr>");
            client.println("<td style=\"width: 117px;\">&nbsp;Soggiorno</td>");

          
            // Display current state, and ON/OFF buttons for GPIO 5
            client.println("<td style=\"width: 117px; text-align: center;\">&nbsp;" + output5State + "</span></td>");
            // If the output5State is off, it displays the ON button
            if (output5State == "off") {
              client.println("<td style=\"width: 117px;\">&nbsp;<a href=\"/5/on\"><button style=\"height: 40px; width: 110px;\">On</button></a></td>");
            } else {
              client.println("<td style=\"width: 117px;\">&nbsp;<a href=\"/5/on\"><button style=\"height: 40px; width: 110px;\">Off</button></a></td>");
            }

            client.println("</tr>");
            client.println("<tr>");
            client.println("<td style=\"width: 117px;\">&nbsp;Cucina</td>");

            // Display current state, and ON/OFF buttons for GPIO 4
            client.println("<td style=\"width: 117px; text-align: center;\">&nbsp;" + output4State + "</span></td>");
            // If the output4State is off, it displays the ON button
            if (output4State == "off") {
              client.println("<td style=\"width: 117px;\">&nbsp;<a href=\"/4/on\"><button style=\"height: 40px; width: 110px;\">On</button></a></td>");
            } else {
              client.println("<td style=\"width: 117px;\">&nbsp;<a href=\"/4/on\"><button style=\"height: 40px; width: 110px;\">Off</button></a></td>");
            }

            client.println("</tr>");
            client.println("<tr>");
            client.println("<td style=\"width: 117px;\">&nbsp;Camera</td>");

            // Display current state, and ON/OFF buttons for GPIO 0
            client.println("<td style=\"width: 117px; text-align: center;\">&nbsp;" + output0State + "</span></td>");
            // If the output0State is off, it displays the ON button
            if (output0State == "off") {
              client.println("<td style=\"width: 117px;\">&nbsp;<a href=\"/0/on\"><button style=\"height: 40px; width: 110px;\">On</button></a></td>");
            } else {
              client.println("<td style=\"width: 117px;\">&nbsp;<a href=\"/0/on\"><button style=\"height: 40px; width: 110px;\">Off</button></a></td>");
            }

            client.println("</tr>");
            client.println("<tr>");
            client.println("<td style=\"width: 117px;\">&nbsp;Bagno</td>");

            // Display current state, and ON/OFF buttons for GPIO 2
            client.println("<td style=\"width: 117px; text-align: center;\">&nbsp;" + output2State + "</span></td>");
            // If the output2State is off, it displays the ON button
            if (output2State == "off") {
              client.println("<td style=\"width: 117px;\">&nbsp;<a href=\"/2/on\"><button style=\"height: 40px; width: 110px;\">On</button></a></td>");
            } else {
              client.println("<td style=\"width: 117px;\">&nbsp;<a href=\"/2/on\"><button style=\"height: 40px; width: 110px;\">Off</button></a></td>");
            }
            client.println("</tr>");
            client.println("</tbody>");
            client.println("</table>");
            client.println("</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("");
  }
}

img1.PNG
 
Ultima modifica:

enricovela

Utente Attivo
443
124
CPU
Ryzen 3600
Dissipatore
Thermaltake Water 3.0 240
Scheda Madre
MSI Gaming plus max x470
HDD
970 evo plus;Crucial MX500 1TB; GIGABYTE M.2 PCIe SSD; p300
RAM
16 GB @3000 MHz
GPU
quadro fx 3800
Audio
Audioengine D1
Monitor
LG 32QK500
PSU
Straight power 11 650 W
Case
Cooler Master MB510L
Net
FTTH Tim
OS
Pop!_OS
Secondo me ti conviene affidare alla schedina solo l'accensione e lo spegnimento dei pin. Facendo girare il fronted web su una macchina vera e propria. Io subito dopo l'estate avevo cominciato a scrivere qualcosa, poi per mancanza di tempo, e per le mie limitate conoscenze di html e css, mi sono fermato.
 

Entra

oppure Accedi utilizzando
Discord Ufficiale Entra ora!

Discussioni Simili