2018年11月30日 星期五

ESP32 Web Server Control 2 LED & Show state in Node-red









ESP32 2LED Web_Server
/*********
  Rui Santos
  Complete project details at http://randomnerdtutorials.com  
*********/

// Load Wi-Fi library
#include <WiFi.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 output26State = "off";
String output27State = "off";

// Assign output variables to GPIO pins
const int output26 = 26;
const int output27 = 27;

void setup() {
  Serial.begin(115200);
  // Initialize the output variables as outputs
  pinMode(output26, OUTPUT);
  pinMode(output27, OUTPUT);
  // Set outputs to LOW
  digitalWrite(output26, LOW);
  digitalWrite(output27, 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 /26/on") >= 0) {
              Serial.println("GPIO 26 on");
              output26State = "on";
              digitalWrite(output26, HIGH);
            } else if (header.indexOf("GET /26/off") >= 0) {
              Serial.println("GPIO 26 off");
              output26State = "off";
              digitalWrite(output26, LOW);
            } else if (header.indexOf("GET /27/on") >= 0) {
              Serial.println("GPIO 27 on");
              output27State = "on";
              digitalWrite(output27, HIGH);
            } else if (header.indexOf("GET /27/off") >= 0) {
              Serial.println("GPIO 27 off");
              output27State = "off";
              digitalWrite(output27, 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>ESP32 Web Server</h1>");
            
            // Display current state, and ON/OFF buttons for GPIO 26  
            client.println("<p>GPIO 26 - State " + output26State + "</p>");
            // If the output26State is off, it displays the ON button       
            if (output26State=="off") {
              client.println("<p><a href=\"/26/on\"><button class=\"button\">ON</button></a></p>");
            } else {
              client.println("<p><a href=\"/26/off\"><button class=\"button button2\">OFF</button></a></p>");
            } 
               
            // Display current state, and ON/OFF buttons for GPIO 27  
            client.println("<p>GPIO 27 - State " + output27State + "</p>");
            // If the output27State is off, it displays the ON button       
            if (output27State=="off") {
              client.println("<p><a href=\"/27/on\"><button class=\"button\">ON</button></a></p>");
            } else {
              client.println("<p><a href=\"/27/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("");
  }

========================================
Node-red json File
========================================
[{"id":"87829af0.b5a588","type":"http request","z":"cf6a2f8.eda6dd","name":"","method":"GET","ret":"txt","url":"http://192.168.43.86/","tls":"","x":130,"y":100,"wires":[["4ccf532f.7c98ac"]]},{"id":"79ce4d9a.2bcb84","type":"inject","z":"cf6a2f8.eda6dd","name":"","topic":"","payload":"","payloadType":"date","repeat":"2.5","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":360,"wires":[["87829af0.b5a588"]]},{"id":"4ccf532f.7c98ac","type":"switch","z":"cf6a2f8.eda6dd","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"/26/on","vt":"str"},{"t":"cont","v":"/26/off","vt":"str"},{"t":"cont","v":"/27/on","vt":"str"},{"t":"cont","v":"/27/off","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":270,"y":100,"wires":[["f0990323.29f23"],["177b98cc.5db7f7"],["5491eae1.d47c94"],["25fc368.80e7dca"]]},{"id":"f973b427.7b9228","type":"debug","z":"cf6a2f8.eda6dd","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":730,"y":100,"wires":[]},{"id":"f0990323.29f23","type":"function","z":"cf6a2f8.eda6dd","name":"P26 ON","func":"msg.payload =\"P26 ON\";\nreturn msg;\n","outputs":1,"noerr":0,"x":420,"y":40,"wires":[["f973b427.7b9228","3dec28ca.f97b98"]]},{"id":"177b98cc.5db7f7","type":"function","z":"cf6a2f8.eda6dd","name":"P26 OFF","func":"msg.payload =\"P26 OFF\";\nreturn msg;\n","outputs":1,"noerr":0,"x":420,"y":80,"wires":[["f973b427.7b9228","3dec28ca.f97b98"]]},{"id":"5491eae1.d47c94","type":"function","z":"cf6a2f8.eda6dd","name":"P27 ON","func":"msg.payload =\"P27 ON\";\nreturn msg;\n","outputs":1,"noerr":0,"x":420,"y":120,"wires":[["f973b427.7b9228","c297311a.d7d05"]]},{"id":"25fc368.80e7dca","type":"function","z":"cf6a2f8.eda6dd","name":"P27 OFF","func":"msg.payload =\"P27 OFF\";\nreturn msg;\n","outputs":1,"noerr":0,"x":420,"y":160,"wires":[["f973b427.7b9228","c297311a.d7d05"]]},{"id":"3dec28ca.f97b98","type":"function","z":"cf6a2f8.eda6dd","name":"Set Color","func":"if (msg.payload === \"P26 ON\")\n  msg.color =\"lime\";\nelse\n// if (msg.payload === \"P26 OFF\")\n  msg.color =\"red\";\n\n\n\nreturn msg;","outputs":1,"noerr":0,"x":660,"y":180,"wires":[["fe9fe612.ac62a8","2001aacc.9a8c26"]]},{"id":"fe9fe612.ac62a8","type":"ui_text","z":"cf6a2f8.eda6dd","group":"cfe942ae.4006","order":3,"width":"2","height":"2","name":"","label":"ESP32 LED-1","format":"<font color={{msg.color}} ><i class=\"fa fa-circle\" style=\"font-size:24px;\"></i></font> ","layout":"row-center","x":840,"y":180,"wires":[]},{"id":"c297311a.d7d05","type":"function","z":"cf6a2f8.eda6dd","name":"Set Color","func":"\nif (msg.payload === \"P27 ON\")\n  msg.color =\"blue\";\nelse    \n//if (msg.payload === \"P27 OFF\")\n  msg.color =\"yallow\";\n\nreturn msg;","outputs":1,"noerr":0,"x":660,"y":280,"wires":[["7f67e119.cfdab","3d43382c.3fd738"]]},{"id":"7f67e119.cfdab","type":"ui_text","z":"cf6a2f8.eda6dd","group":"cfe942ae.4006","order":3,"width":"2","height":"2","name":"","label":"ESP32 LED-2","format":"<font color={{msg.color}} ><i class=\"fa fa-circle\" style=\"font-size:24px;\"></i></font> ","layout":"row-center","x":840,"y":280,"wires":[]},{"id":"2001aacc.9a8c26","type":"ui_text","z":"cf6a2f8.eda6dd","group":"cfe942ae.4006","order":0,"width":"3","height":"3","name":"LED Message","label":"Arduino Reading","format":"{{msg.payload}}","layout":"col-center","x":840,"y":220,"wires":[]},{"id":"3d43382c.3fd738","type":"ui_text","z":"cf6a2f8.eda6dd","group":"cfe942ae.4006","order":0,"width":"3","height":"3","name":"LED Message","label":"Arduino Reading","format":"{{msg.payload}}","layout":"col-center","x":840,"y":320,"wires":[]},{"id":"cfe942ae.4006","type":"ui_group","z":"","name":"ESP32 2LED","tab":"ba052423.3729f8","disp":true,"width":"6","collapse":false},{"id":"ba052423.3729f8","type":"ui_tab","z":"","name":"alex9ufoBMP280","icon":"dashboard","order":1}]

沒有留言:

張貼留言

113 學年度第 1 學期 RFID應用課程 Arduino程式

113 學年度第 1 學期 RFID應用課程 Arduino程式 https://www.mediafire.com/file/zr0h0p3iosq12jw/MFRC522+(2).7z/file 內含修改過後的 MFRC522 程式庫 (原程式有錯誤) //定義MFRC522...