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}]

2018年11月29日 星期四

Node-Red 寄送 Email 點亮Arduino燈泡

Node-Red 寄送 Email 點亮Arduino燈泡





msg.to = 'alex.6662594@gmail.com';
// msg.cc = 'alex9ufo@gmail.com';
if(msg.payload=='ON'){
    msg.topic = '(NodeRed) 開燈啦';
    msg.payload = '點燈泡開燈啦!' + msg.payload;
}
else if (msg.payload== 'OFF'){
    msg.topic = '(NodeRed) 關燈啦';
    msg.payload = '不知道是誰關燈啦!' + msg.payload;
}
else if (msg.payload== 'FLASH'){
    msg.topic = '(NodeRed) 開關燈-閃爍-啦';
    msg.payload = '不知道是誰開關燈-閃爍-啦!' + msg.payload;
}

return msg;


if (msg.payload === "ON")
  msg.color ="lime";

if (msg.payload === "OFF")
  msg.color ="red";

if (msg.payload === "FLASH")
  msg.color ="blue";
    
return msg;



ESP32 Arduino 程式

/*LED_Breathing.ino Arduining.com  20 AUG 2015
Using NodeMCU Development Kit V1.0
Going beyond Blink sketch to see the blue LED breathing.
A PWM modulation is made in software because GPIO16 can't
be used with analogWrite().
*/

//#define BUILTIN_LED     D0        // Led in NodeMCU at pin GPIO16 (D0).

#define BRIGHT    350     //max led intensity (1-500)
#define INHALE    1250    //Inhalation time in milliseconds.
#define PULSE     INHALE*1000/BRIGHT
#define REST      5000    //Rest Between Inhalations.
String cmd="";

//----- Setup function. ------------------------
void setup() {                
  pinMode(BUILTIN_LED , OUTPUT);   // LED pin as output.   
  Serial.begin(115200);
  Serial.println("ESP32 Build LED ON , Received ON/OFF Message From Com Port!");
  digitalWrite(BUILTIN_LED, HIGH);         // turn the LED off.
}

//----- Loop routine. --------------------------
void loop()
{
    if (Serial.available()) {
        cmd = Serial.readStringUntil('\n');

        if (cmd == "ON") {
            digitalWrite(BUILTIN_LED, HIGH); // on
            Serial.println("aan");
        }
        else if (cmd == "OFF") {
            digitalWrite(BUILTIN_LED, LOW); // off
            Serial.println("uit");
        }
        else {
             for (int i=0;i<=5;i++){
                digitalWrite(BUILTIN_LED, LOW); // off
                delay(500);
                digitalWrite(BUILTIN_LED, HIGH); // off
                delay(500);
              }
              Serial.println("uit2");
     } 
    } // if (Serial.available())
}
//==========================================================
Node-Red Json檔案 (中文會出現亂碼)
[{"id":"d4b0a646.09d0a8","type":"inject","z":"ba373d0d.51bee","name":"","topic":"","payload":"ON","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":1160,"wires":[["ba8491c8.f2cb8","7ba48c78.00e084","35562ef.0e87cd2"]]},{"id":"3dd44f80.9aed4","type":"inject","z":"ba373d0d.51bee","name":"","topic":"","payload":"OFF","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":1200,"wires":[["ba8491c8.f2cb8","7ba48c78.00e084","35562ef.0e87cd2"]]},{"id":"7037ee2a.01b8","type":"inject","z":"ba373d0d.51bee","name":"","topic":"","payload":"FLASH","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":1240,"wires":[["35562ef.0e87cd2","7ba48c78.00e084","ba8491c8.f2cb8"]]},{"id":"35562ef.0e87cd2","type":"function","z":"ba373d0d.51bee","name":"Set Color","func":"if (msg.payload === \"ON\")\n  msg.color =\"lime\";\n\nif (msg.payload === \"OFF\")\n  msg.color =\"red\";\n\nif (msg.payload === \"FLASH\")\n  msg.color =\"blue\";\n    \n\n// msg.color = (msg.payload === \"ON\")?\"lime\":\"red\";\n\n\nreturn msg;","outputs":1,"noerr":0,"x":280,"y":1300,"wires":[["82575e8a.45088","7099f2cc.0d030c"]]},{"id":"82575e8a.45088","type":"ui_text","z":"ba373d0d.51bee","group":"dbdf585d.7c3478","order":3,"width":"4","height":"4","name":"","label":"LED","format":"<font color={{msg.color}} ><i class=\"fa fa-circle\" style=\"font-size:24px;\"></i></font>","layout":"row-center","x":450,"y":1380,"wires":[]},{"id":"7099f2cc.0d030c","type":"function","z":"ba373d0d.51bee","name":"send mail","func":"msg.to = 'alex.6662594@gmail.com';\n// msg.cc = 'alex9ufo@gmail.com';\nif(msg.payload=='ON'){\n    msg.topic = '(NodeRed) ????;\n    msg.payload = '暺?瘜⊿??嚗? + msg.payload;\n}\nelse if (msg.payload== 'OFF'){\n    msg.topic = '(NodeRed) ????;\n    msg.payload = '銝?隤圈??嚗? + msg.payload;\n}\nelse if (msg.payload== 'FLASH'){\n    msg.topic = '(NodeRed) ?????w?-??;\n    msg.payload = '銝?隤圈???-?w?-?佗?' + msg.payload;\n}\n\nreturn msg;\n\n","outputs":1,"noerr":0,"x":440,"y":1300,"wires":[["707355ad.2c4cec","b993a548.3f8f78"]]},{"id":"707355ad.2c4cec","type":"e-mail","z":"ba373d0d.51bee","server":"smtp.gmail.com","port":"465","secure":true,"name":"","dname":"","x":590,"y":1320,"wires":[]},{"id":"b993a548.3f8f78","type":"debug","z":"ba373d0d.51bee","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":610,"y":1280,"wires":[]},{"id":"7ba48c78.00e084","type":"debug","z":"ba373d0d.51bee","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":450,"y":1220,"wires":[]},{"id":"ba8491c8.f2cb8","type":"ui_text","z":"ba373d0d.51bee","group":"dbdf585d.7c3478","order":0,"width":0,"height":0,"name":"LED Message","label":"Arduino Reading","format":"{{msg.payload}}","layout":"col-center","x":460,"y":1160,"wires":[]},{"id":"44b321ce.0f161","type":"debug","z":"ba373d0d.51bee","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":610,"y":1100,"wires":[]},{"id":"e4f03eb9.ff76d","type":"serial out","z":"ba373d0d.51bee","name":"Com4 ESP32","serial":"dff245b0.8b8268","x":620,"y":1020,"wires":[]},{"id":"5a0c336f.ef178c","type":"function","z":"ba373d0d.51bee","name":"ON","func":"msg.payload =\"ON\";\nreturn msg;\n","outputs":1,"noerr":0,"x":370,"y":1020,"wires":[["e4f03eb9.ff76d","44b321ce.0f161"]]},{"id":"982d9cb2.f9e22","type":"function","z":"ba373d0d.51bee","name":"OFF","func":"msg.payload=\"OFF\";\nreturn msg;","outputs":1,"noerr":0,"x":370,"y":1060,"wires":[["e4f03eb9.ff76d","44b321ce.0f161"]]},{"id":"ff5351ae.46d22","type":"function","z":"ba373d0d.51bee","name":"FLASH","func":"msg.payload=\"FLASH\";\nreturn msg;","outputs":1,"noerr":0,"x":380,"y":1100,"wires":[["e4f03eb9.ff76d","44b321ce.0f161"]]},{"id":"27bcb2a3.12006e","type":"switch","z":"ba373d0d.51bee","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"ON","vt":"str"},{"t":"cont","v":"OFF","vt":"str"},{"t":"cont","v":"FLASH","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":230,"y":1060,"wires":[["5a0c336f.ef178c"],["982d9cb2.f9e22"],["ff5351ae.46d22"]]},{"id":"3971be4f.844f92","type":"json","z":"ba373d0d.51bee","name":"","property":"payload","action":"str","pretty":false,"x":230,"y":980,"wires":[["27bcb2a3.12006e"]]},{"id":"dfe55825.b8d058","type":"debug","z":"ba373d0d.51bee","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":250,"y":920,"wires":[]},{"id":"aadb6fe0.6a831","type":"e-mail in","z":"ba373d0d.51bee","name":"","protocol":"IMAP","server":"imap.gmail.com","useSSL":true,"port":"993","box":"INBOX","disposition":"Read","repeat":"20","x":70,"y":960,"wires":[["dfe55825.b8d058","3971be4f.844f92"]]},{"id":"dbdf585d.7c3478","type":"ui_group","z":"","name":"LED Testing","tab":"89e5353f.12fef8","disp":true,"width":"4","collapse":false},{"id":"dff245b0.8b8268","type":"serial-port","z":"","serialport":"COM4","serialbaud":"115200","databits":"8","parity":"none","stopbits":"1","newline":"\\n","bin":"false","out":"char","addchar":false,"responsetimeout":"10000"},{"id":"89e5353f.12fef8","type":"ui_tab","name":"Tab","icon":"dashboard","order":0}]

2018年11月25日 星期日

Interfacing Arduino With Node-RED

Interfacing Arduino With Node-RED


//Arduino 程式

/*LED_Breathing.ino Arduining.com  20 AUG 2015
Using NodeMCU Development Kit V1.0
Going beyond Blink sketch to see the blue LED breathing.
A PWM modulation is made in software because GPIO16 can't
be used with analogWrite().
*/

//#define BUILTIN_LED     D0        // Led in NodeMCU at pin GPIO16 (D0).
#define BRIGHT    350     //max led intensity (1-500)
#define INHALE    1250    //Inhalation time in milliseconds.
#define PULSE     INHALE*1000/BRIGHT
#define REST      5000    //Rest Between Inhalations.

//----- Setup function. ------------------------
void setup() {                
  pinMode(BUILTIN_LED , OUTPUT);   // LED pin as output.   
  Serial.begin(115200);
  Serial.println("Wemos D1 Build LED ON , OFF Message send to Com Port Show to Node-Red!");
  
}

//----- Loop routine. --------------------------
void loop() {
  //ramp increasing intensity, Inhalation: 
  for (int i=1;i<BRIGHT;i++){
    digitalWrite(BUILTIN_LED, LOW);          // turn the LED on.
    delayMicroseconds(i*10);         // wait
    digitalWrite(BUILTIN_LED, HIGH);         // turn the LED off.
    delayMicroseconds(PULSE-i*10);   // wait
    delay(0);                        //to prevent watchdog firing.
  }
  
  Serial.print("ON"); 
  delay(REST);                       //take a rest...
  Serial.println(""); 
  //ramp decreasing intensity, Exhalation (half time):
  for (int i=BRIGHT-1;i>0;i--){
    digitalWrite(BUILTIN_LED, LOW);          // turn the LED on.
    delayMicroseconds(i*10);          // wait
    digitalWrite(BUILTIN_LED, HIGH);         // turn the LED off.
    delayMicroseconds(PULSE-i*10);  // wait
    i--;
    delay(0);                        //to prevent watchdog firing.
  }
  
  Serial.print("OFF");
  delay(REST);                       //take a rest...
  Serial.println(""); 
}

//Node-Red Json File

[{"id":"63b28b77.c39dc4","type":"debug","z":"8e1c2d49.f884e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":330,"y":120,"wires":[]},{"id":"cb08945c.0923d8","type":"ui_text","z":"8e1c2d49.f884e","group":"dbdf585d.7c3478","order":0,"width":0,"height":0,"name":"LED Message","label":"Arduino Reading","format":"{{msg.payload}}","layout":"col-center","x":340,"y":60,"wires":[]},{"id":"dda11bb7.eba0b8","type":"serial in","z":"8e1c2d49.f884e","name":"","serial":"965dbdd7.dd54c","x":70,"y":140,"wires":[["cb08945c.0923d8","63b28b77.c39dc4","67d19b8b.e03474"]]},{"id":"59325d79.183f94","type":"function","z":"8e1c2d49.f884e","name":"Test","func":"if (msg.payload === \"ON\")\n  msg.color =\"lime\";\n\nif (msg.payload === \"OFF\")\n  msg.color =\"red\";\n  \n\n// msg.color = (msg.payload === \"ON\")?\"lime\":\"red\";\n\n\nreturn msg;","outputs":1,"noerr":0,"x":510,"y":280,"wires":[["ae83ae61.feb5d"]]},{"id":"ae83ae61.feb5d","type":"ui_text","z":"8e1c2d49.f884e","group":"dbdf585d.7c3478","order":3,"width":"4","height":"4","name":"","label":"LED","format":"<font color={{msg.color}} ><i class=\"fa fa-circle\" style=\"font-size:24px;\"></i></font>","layout":"row-center","x":650,"y":280,"wires":[]},{"id":"bab93485.9a7a58","type":"switch","z":"8e1c2d49.f884e","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"ON","vt":"str"},{"t":"cont","v":"OFF","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":230,"y":280,"wires":[["b9f8f698.b29508"],["b1a95511.629bf8"]]},{"id":"b9f8f698.b29508","type":"function","z":"8e1c2d49.f884e","name":"ON","func":"msg.payload =\"ON\";\nreturn msg;\n","outputs":1,"noerr":0,"x":370,"y":240,"wires":[["59325d79.183f94"]]},{"id":"b1a95511.629bf8","type":"function","z":"8e1c2d49.f884e","name":"OFF","func":"msg.payload=\"OFF\";\nreturn msg;","outputs":1,"noerr":0,"x":370,"y":320,"wires":[["59325d79.183f94"]]},{"id":"67d19b8b.e03474","type":"json","z":"8e1c2d49.f884e","name":"","property":"payload","action":"str","pretty":false,"x":160,"y":360,"wires":[["bab93485.9a7a58"]]},{"id":"dbdf585d.7c3478","type":"ui_group","z":"","name":"LED Testing","tab":"89e5353f.12fef8","disp":true,"width":"4","collapse":false},{"id":"965dbdd7.dd54c","type":"serial-port","z":"8e1c2d49.f884e","serialport":"COM3","serialbaud":"115200","databits":"8","parity":"none","stopbits":"1","newline":"\\n","bin":"false","out":"char","addchar":false,"responsetimeout":""},{"id":"89e5353f.12fef8","type":"ui_tab","name":"Tab","icon":"dashboard","order":0}]

















2018年11月24日 星期六

display a LED in Node-RED Dashboard

How to display a LED in Node-RED Dashboard

源自於 http://node-red.blogspot.com/2017/12/how-to-display-led-in-node-red-dashboard.html

Node-RED does not provide a user interface node specific to represent a LED in the dashboard.

This is not an issue at all since there are a few different ways to achieve this goal.


1- Using a Text output node from the Dashboard along with an icon from Font Awesome (http://fontawesome.io/icons/)



I have used the most basic representation icon which is the fa-circle.



Simple flow to demonstrate how to configure an icon to represent a LED:


As we want the LED to change colors, normally to green or red we have to add some code to manage the color change.  For this testing, I created a property in the .msg object and assigned to it a color (lime or red).

See the Javascript code inside the function node. It simply adds a color to msg.color property. If msg.payload is "ON" then msg.color will be "lime" (a light green) otherwise (for any other payload) msg.color will be "red"



The text output node should have the following configuration in the field Value format:

<font color={{msg.color}} ><i class="fa fa-circle" style="font-size:24px;"></i></font>





This is the end result of the dashboard.




Flow :

[{"id":"17ca9b9e.d2a564","type":"function","z":"3fc1de78.729e82","name":"Test","func":"msg.color = (msg.payload === \"ON\")?\"lime\":\"red\";\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":200,"wires":[["872b1d71.45076"]]},{"id":"c6ac5bac.e91e38","type":"inject","z":"3fc1de78.729e82","name":"","topic":"","payload":"ON","payloadType":"str","repeat":"","crontab":"","once":false,"x":170,"y":180,"wires":[["17ca9b9e.d2a564"]]},{"id":"872b1d71.45076","type":"ui_text","z":"3fc1de78.729e82","group":"a6e358b.672ffa8","order":3,"width":"3","height":"1","name":"","label":"LED","format":"<font color={{msg.color}} ><i class=\"fa fa-circle\" style=\"font-size:24px;\"></i></font>","layout":"row-left","x":510,"y":200,"wires":[]},{"id":"9d39dfc1.aff32","type":"inject","z":"3fc1de78.729e82","name":"","topic":"","payload":"OFF","payloadType":"str","repeat":"","crontab":"","once":false,"x":170,"y":220,"wires":[["17ca9b9e.d2a564"]]},{"id":"a6e358b.672ffa8","type":"ui_group","z":"","name":"LED Testing","tab":"4e528085.a1bfa","disp":true,"width":"4"},{"id":"4e528085.a1bfa","type":"ui_tab","name":"Tab","icon":"dashboard","order":0}]



2- Using a template node from the Dashboard along with very simple SVG directives




Following code, with SVG directives, must be added to the Template field in the configuration dialog page from the node.


<svg width="100" height="80">
    <circle id="circle1" cx="20" cy="20" r="10"
            style="stroke: none; fill: {{msg.color}};"/>
</svg>


Alternatively, instead of writing the parameters stroke and fill inside the SVG tags it is possible (and many times desirable) to use the CSS way of working. We do that using an additional pair of tags <style></style> and write those parameters as property : value; in each line. The hashtag #circle1 defines the style parameters for the HTML element with id = "circle1".


<style>

#circle1 {
    stroke: none;
    fill: {{msg.color}};
}

</style>

<svg>
    <circle id="circle1" cx="10" cy="20" r="10"/>
</svg>





This is the result we get on the dashboard:





Below a variation on the LED design:


<svg width="100" height="80">
    <circle id="circle1" cx="20" cy="20" r="10" stroke={{msg.color}} fill="transparent" stroke-width="5"/>
</svg>








2018勤益 RFID應用作業練習 2


2018勤益 RFID應用作業練習 2


2018勤益  RFID應用作業練習  < 2 >

期中考第18週前完成

1) 電子檔 mail :   alex9ufo@gmail.com     主旨作業1 RFIDMQTT  實驗  姓名:XXX   學號:XXXXXXXXXXX

2) 紙本  期中考當日繳交

3) 作業格式 





Arduino RFID  MQTT  Node-Red MongoDB 執行步驟







MongoDB+ Studio 3T 安裝與使用

MongoDB + Studio 3T 相關的檔案


https://alex9ufoexploer.blogspot.com/search/label/2018%20%E5%8B%A4%E7%9B%8A%E7%A7%91%E5%A4%A7%20RFID%E6%87%89%E7%94%A8



PM2.5 ( Node-Red 爬蟲 )

https://alex9ufoexploer.blogspot.com/2018/11/pm25-node-red.html


日幣匯率 ( Node-Red 爬蟲 )


https://alex9ufoexploer.blogspot.com/2018/11/node-red_21.html

2018年11月23日 星期五

MongoDB + Studio 3T 相關的檔案

MongoDB + Studio 3T 相關的檔案


下載點(內有mongoDB + Studio 3T)  

下載點 MongoDB 相關資料


學習網站

http://www.runoob.com/mongodb/mongodb-tutorial.html

MongoDB 是一個基於分散式檔存儲的資料庫。由 C++ 語言編寫。旨在為 WEB 應用提供可擴展的高性能資料存儲解決方案。

MongoDB 是一個介於關聯式資料庫和非關聯式資料庫之間的產品,是非關聯式資料庫當中功能最豐富,最像關聯式資料庫的。

Download Studio 3T for MongoDB
Whether you’re new to NoSQL or a MongoDB pro, be more productive with Studio 3T features like rich query autocompletion, the ability to write SQL to query MongoDB, automatic code generation, and more.

2018 勤益科大 協同教學 11/26 11/27日課程資料

2018 勤益科大 協同教學  11/26  11/27日課程資料

低功耗藍芽 BLE

1)  簡報資料

2)  軟體

3)  APP截圖

2018年11月12日 星期一

ESP32 Temperature, Humidity data upload to ThingSpeak

ESP32 Temperature, Humidity data upload to ThingSpeak 



//========================================================

// Robo India Tutorial 
// Simple code upload the tempeature and humidity data using thingspeak.com
// Hardware: ESP32,DHT11

#include "DHT.h"  // Including library for dht
#include <WiFi.h>
String apiKey = "8U1H2OD8L4P9L718";     //  Enter your Write API key from ThingSpeak

const char *ssid =  "alex9ufo";     // replace with your wifi ssid and wpa2 key
const char *pass =  "alex9981";
const char* server = "api.thingspeak.com";

#define DHTPIN 23          //pin where the dht11 is connected
DHT dht(DHTPIN, DHT11);
WiFiClient client;
void setup() 
{
      Serial.begin(115200);
      delay(10);
      dht.begin();
      Serial.println("Connecting to ");
      Serial.println(ssid);
      WiFi.begin(ssid, pass);
      while (WiFi.status() != WL_CONNECTED) 
      {     
            delay(500);
            Serial.print(".");
      }
      Serial.println("");
      Serial.println("WiFi connected");
}
//========================================================
void loop() 
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(h) || isnan(t)) 
    {
      Serial.println("Failed to read from DHT sensor!");
      return;
    }
  if (client.connect(server,80))   //   "184.106.153.149" or api.thingspeak.com
    {  
      String postStr = apiKey;
      postStr +="&field1=";
      postStr += String(t);
      postStr +="&field2=";
      postStr += String(h);
      postStr += "\r\n\r\n";
      client.print("POST /update HTTP/1.1\n");
      client.print("Host: api.thingspeak.com\n");
      client.print("Connection: close\n");
      client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
      client.print("Content-Type: application/x-www-form-urlencoded\n");
      client.print("Content-Length: ");
      client.print(postStr.length());
      client.print("\n\n");
      client.print(postStr);
      Serial.print("Temperature: ");
      Serial.print(t);
      Serial.print(" degrees Celcius, Humidity: ");
      Serial.print(h);
      Serial.println("%. Send to Thingspeak.");
    }
    client.stop();
    Serial.println("Waiting...");
  
    // thingspeak needs minimum 15 sec delay between updates, i've set it to 30 seconds
    delay(10000);
}
//========================================================












APP 

ThingView - ThingSpeak viewer  

SETTING










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

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