2018年3月8日 星期四

安裝 Webduino Node-RED 節點



安裝 Webduino Node-RED 節點

Webduino 目前已經開發出了四個 Node-RED 節點,並且也都 OpenSource 在 Github 上面 ( https://github.com/webduinoio/node-red-contrib-webduino ),只要透過 npm 安裝即可,安裝之前記得把原本啟動的 Node-RED 停下來,然後就前往 Node-RED 的目錄,基本上它的目錄是的隱藏的資料夾,名為「.node-red」,從終端機可直接打 cd .node-red 進入 ( Node-RED 安裝在 /Users/帳號/.node-red ),進入後輸入下列指令安裝。
npm install node-red-contrib-webduino
安裝 node-red Arduino 
CMD
cd .node-red
npm install node-red-node-arduino
npm install node-red-node-swerialport

A Node-RED node to talk to an Arduino running firmata
npm install node-red-node-arduino




2018年3月7日 星期三

Node-RED Hello, world! Example, part 2


Node-RED Hello, world! Example, part 2

源自於 http://developer.opto22.com/nodered/general/getting-started/node-red-hello-world-2/

Overview

The Hello, world! example is great, but we can make it more useful and interesting. We’ll add the current time to the message, and execute it every two seconds.

Step 1 - Add a Function node

First off, let’s add the current time to the message. To do that, we’ll use the Function block, which allows you to enter JavaScript code to manipulate the msg object.
  1. Select a Function node and drag it out onto the flow.
  2. Carefully place it over the existing wire between the existing Inject and Debug nodes. Node-RED will insert the new node between the two existing nodes, and rewire the nodes together.
Static Image
 Click the image to start and stop the animation.

Step 2 - Add Code and Run

  1. Double-click on the Function node to open the “Edit function node” view.
  2. Copy and paste the following JavaScript code into the Function field:
    var dateNow = new Date();
    var timeAsString = dateNow.toLocaleTimeString();
        
    msg.payload = msg.payload + 
                  ' The time is ' + 
                  timeAsString + '.';
                      
    return msg;
    
  3. In the Name field, enter “Add time to msg”.
  4. Click Done.
  5. Click Deploy.
  6. Click the Inject node’s button.
  7. In the Debug tab, you should see the new output. For example, “Hello, world! The time is 4:25:29 PM”.
Static Image
 Click the image to start and stop the animation.

Step 3 - Add Two-Second Interval

Now let’s adjust the flow to automatically inject a new message every two seconds. The existing Inject node can already do that, we just need to adjust its settings.
  1. Double-click on the existing Inject node to open the “Edit inject node” view.
  2. For the Repeat field, select interval and enter 2 seconds for the period.
  3. Click Done.
  4. Click Deploy.
  5. In the Debug tab, you should now see your output every two seconds.
Static Image
 Click the image to start and stop the animation.








Node-RED Hello, world! Example

源自於 http://developer.opto22.com/nodered/general/getting-started/node-red-hello-world/#next-step

Overview

While this tutorial covers a very basic example, there are a few key concepts that should be discussed first:
  • Messages are objects containing data, and they flow from node to node. They are the basic mechanism by which Node-RED operates.
  • Nodes either generate a new message or process an incoming message.
  • Messages have properties, which are values attached to the message. Properties are basically a variable and can be numbers, strings, booleans, arrays, or objects.
  • A very common message property is called the payload. Many nodes will use the payload property by default.
  • Inside of a node, the message object is called simply “msg”.
  • Flows are a collection of connected nodes that messages pass through.

Step 1 - Add an Inject node

First, we need a way to start the flow. That’s one of the main purposes of the Inject node—to inject a message into the flow.
  1. If it’s not open already, open Node-RED in your browser at port 1880.
    • If using a GROOV-AR1, then open https://[groov-box-hostname]:1880.
    • If using Node-RED running on your computer, you can use http://127.0.0.1:1880 or whichever address or hostname you’re using.
  2. If you haven’t used Node-RED before, there should be one empty flow named “Flow 1”.
  3. From the node palette on the left side of the Node-RED editor, select an Inject node and drag it onto the flow.
  4. Double-click the node to open the “Edit inject node” view.
  5. For the Payload field, select string and enter Hello, world! in the text field.
  6. Click Done.
Later, with its msg.payload property set to “Hello, world!” this node is going to inject a message into the flow.
Static Image
 Click the image to start and stop the animation.

Step 2 - Add a Debug node

We need a destination for the Inject node’s message. We’ll use the Debug node, which will print out our message to the debug console window.
  1. From the node palette, select a Debug node and drag it onto the flow, and then place it to the right-hand side of the Inject node.
  2. Wire the nodes together. Place the mouse cursor over the Inject node’s output port (a small gray square on the right-hand side of the node), then left-click and drag a wire over to the input port of the Debug node. A gray wire should now be connecting the output of the Inject node to the input of the Debug node.
The Debug node will automatically print the msg.payload property to the console window, which we’ll see in the next step.
Static Image
 Click the image to start and stop the animation.

Step 3 - Deploy and Run

Now that our flow is complete, we need to deploy it to the server and run it.
  1. Click the Deploy button.
  2. Click the Debug tab in the right-hand side of the editor window.
  3. Click the Inject node’s button, which is the blue square coming out from the left-hand side of the Inject node. Clicking the button is what will inject a message into the flow.
  4. A “Hello, world!” message should appear in the debug window.
  5. Click the Inject node again to send another message.
Static Image
 Click the image to start and stop the animation.










安裝Node-RED

安裝Node-RED

參考
https://nodered.org/docs/platforms/windows
https://makerpro.cc/2017/04/use-node-red-for-iot-development/


1. Install Node.js
Download the latest 8.x LTS version of Node.js from the official Node.js home page. It will offer you the best version for your system.
Run the downloaded MSI file. Installing Node.js requires local administrator rights; if you are not a local administrator, you will be prompted for an administrator password on install. Accept the defaults when installing. After installation completes, close any open command prompts and re-open to ensure new environment variables are picked up.
Once installed, open a command prompt and run the following command to ensure Node.js and npm are installed correctly.
Using Powershell: node --version; npm --version
Using cmd: node --version && npm --version
You should receive back output that looks similar to:
v8.9.0
5.5.1

2. Install Node-RED

Installing Node-RED as a global module adds the command node-red to your system path. Execute the following at the command prompt:
npm install -g --unsafe-perm node-red

3. Run Node-RED

Running on Windows

Once installed, the simple way to run Node-RED is to use the node-red command in a command prompt: If you have installed Node-RED as a global npm package, you can use the node-red command:
C:>node-red

進入瀏覽器  http://127.0.0.1:1880/









IBM發行了一套開源視覺化界面開發工具 - Node-RED,嗯?是不是有點眼熟?怎麼跟著名的程式語言Node.js有些相似?不用擔心,即使完全不懂怎麼撰寫Node.js,也能學會使用Node-RED。接下來就讓我們一起看看如何運用Node-RED吧!
大家可能會好奇,Node-RED跟Node.js怎麼名字會這麼像? 是不是有某種關聯存在? 這邊就要先從Node.js開始介紹了。Node.js是一個讀取Javascript的運行環境,可用於開發網頁後端程式,但是Node.js的指令是一行一行寫出來的,為了免去呼叫程式碼麻煩,IBM的工程師開發了Node-RED來更容易地完成工作。
具體來說,每當啟動Node-RED時,背景程式便會創造一個伺服器來處理我們輸入的指令,這時只要使用瀏覽器登入指定的IP即可進入Node-RED的編輯畫面,設定好想要的流程後,背景程式會將流程轉換成程式碼交給Node.js環境運行。
其實在Maker圈,視覺化界面開發工具已經相當流行,特別是MIT開發的Scratch,以及Google開發的Blockly。那麼,Node-RED跟這兩大視覺開發工具相比,又有什麼樣的優勢呢?
Node-REDScratchBlockly
優點各式API、適合用於概念性驗證淺顯易懂、逗趣的圖案用網頁操作、淺顯易懂
特色可用樹梅派開發IOT應用程式邏輯教學、控制Arduino控制Arduino
語言英文中文中文

Node-RED特色

Node-RED起源於2013年IBM’s Emerging Technology Services group的一個計畫的副產物,工程師Nick O’Leary和Dave Conway-Jones想用視覺化界面來概念性驗證一套MQTT應用,讓Node-RED因此孕育而生。
Node-RED很快成為更加全面性的開發工具,並於2013年9月成為Open Source,於2016年10月加入JS Foundation。(以上說明節錄自Node-RED官網)
Node-RED提供各式的API,包括網際網路服務,例如推特與查詢天氣、呼叫使用各式的通訊協定,例如MQTT、TCP、UDP等;在嵌入式系統上,Node-RED提供控制GPIO的功能,並使用MQTT或是HTTP等協定與雲端做溝通,架構IOT產品。
讓Node-RED流行於各大網路社群的理由,在於它擁有以下諸多的優點:
  1. 相較於撰寫程式碼,使用圖形化界面可更快速開發。
  2. 適合作為概念性應用驗證的開發工具。
  3. 淺顯易懂,每個API都有文字說明可供查詢。
  4. 具有高度擴充性,已有大量的Library、Flow、Node供人下載。



2018年3月3日 星期六

Node-RED官方英文文件的內容


https://nodered.org/docs/

Documentation

Node RED Programming Guide


http://noderedguide.com/

Programming the IoT

Home

The node red guide is a series of lectures designed to get you started with Node-RED. Starting from the very basics, it introduces you to how Node-RED works and how to get the most out of its built in nodes. Each lecture consists of a series of hands-on examples that take you from basic to intermediate Node-RED programming in easy to follow steps.
  • Node-RED: Lecture 1 – A brief introduction to Node-RED This lecture will introduce you to some examples of Node-RED, both to give you a sense of its capabilities and to introduce its basic concepts. We’ll also discuss the history of Node-RED and its origins to give you an idea of what Node-RED is good for and of what types of tasks are best left to a more traditional programming language and environments.
  • Node-RED: Lecture 2 – Building your first flows This lecture will introduce you to the Node-RED visual tool and get you started building your first flow. You’ll learn how to create simple flows, use debug nodes to track messages through the flows and how to use the function node to write simple JavaScript code that tailors the node to your specific needs.
  • Node-RED: Lecture 3 – Basic nodes and flows In this lecture you will get to see a few of the more commonly used nodes and build on some of the things you learnt in previous lectures. You’ll start off with a series of examples based around the popular MQTT protocol that shows how to wire together a set of basic but very useful message processing nodes. Then you’ll briefly look at other ways to get messages into and out of your flows using protocols such as TCP, UDP and websockets.
  • Node-RED: Lecture 4 – A tour of the core nodes This lecture will take a look at the core set of nodes that Node-RED installs by default and then show you the extended set of nodes that the cloud-based Node-RED service – FRED – supports.
  • Node-RED: Lecture 5 – The Node-RED programming model In this lecture, you’ll take a more detailed look at Node-RED’s programming model and some of its key concepts and explore the details of the message flow model that underlies Node-RED. Then you’ll dive into how to program your own function nodes using JavaScript and create reusable sub flows to add your own functionality to the set of nodes supplied with Node-RED.
  • Node-RED: Lecture 6 – Intermediate flows This lecture will build on the ideas you saw in lecture 5 and focus on examples that explore some of the key concepts from that lecture. The examples in this lecture are a little more complex than previous examples – mainly in the sense that the function nodes are more complex – but are still kept as clean and simple as possible.
  • Node-RED: Lecture 7 – Dashboards and UI techniques.  In this lecture you will take a look a few techniques to allow you to visualize data passing through flows. We’ll focus on three approaches; the use of a third party dashboard tool – FreeBoard, using a set of UI nodes provided by the contrib.nodered.ui community and a general technique using a standard JavaScript charting library (Morris.JS).
  • Node-RED: Lecture 8 – Node-RED, the cloud and IoT platforms This lecture is not yet available, it will be posted…
  • Node-RED: Lecture 9 – Advanced flows This lecture is not yet available, it will be posted…

About Sense Tecnic: Sense Tecnic Systems Inc have been building IoT applications and services since 2010. We provide these lectures, and FRED, cloud hosted Node-RED as a service to the community. We also offer a commercial version to our customers, as well as professional services. Learn more.

2018年3月1日 星期四

NOMCU 24bit I/O Expander 使用3個74HC165 與3個 74HC595

NOMCU 24bit I/O Expander 
使用3個74HC165 與3個 74HC595

//NodeMCU
//static const uint8_t D0   = 16;
//static const uint8_t D1   = 5;
//static const uint8_t D2   = 4;
//static const uint8_t D3   = 0;
//static const uint8_t D4   = 2;
//static const uint8_t D5   = 14;
//static const uint8_t D6   = 12;
//static const uint8_t D7   = 13;
//static const uint8_t D8   = 15;
//static const uint8_t D9   = 3;
//static const uint8_t D10  = 1;

#define SO  12      // SO-Serial output (Pin 9) of 74165 is connected to Nodemcu D6 = 12.
#define SH_LD  15   // SH/LD (Pin 1) of 74165 is connected to Nodemcu D8 = 15.
#define CLK  14      // CLK (Pin 2) of 74165 is connected to Nodemcu D5 = 14.

#include <ShiftRegister74HC595.h>
// create shift register object (number of shift registers, data pin, clock pin, latch pin)
ShiftRegister74HC595 sr (3, 16, 4, 5);

// variables will change:
int i = 0;         // variable for reading 8-bit data
long int PinState = 0;   //read the state of the SO pin
int Parallel_data = 0;//To store parallel data
int Parallel_data1 = 0;//To store parallel data


int read_shift_data()
{
  Parallel_data = 0;
  digitalWrite(CLK, LOW);
  /*Parallel data that will be entered through D0 - D7 Pins of 74165  **/
  digitalWrite(SH_LD, LOW);
  delay(5);
  /*******************  INHIBIT SECTION START HERE  *****************/
  digitalWrite(SH_LD, HIGH);
  /*******************  INHIBIT SECTION ENDS HERE ******************/
 
  /************** SERIAL SHIFT SECTION START HERE **********/
  //Read 8-bit data from 74HC165
  // Serial.print("Parallel Data1:");
   for(i=0;i<=23;i++)
    {
        PinState = digitalRead(SO);// read the state of the SO:
        digitalWrite(CLK, LOW);
        delay(1);
        digitalWrite(CLK, HIGH);
        delay(1);
        //Serial.print(PinState);
        Parallel_data = (Parallel_data<<1)|PinState; //Store the value in Parallel_data
    }
 return  Parallel_data;

}


void setup()
{
  // initialize serial communication at 9600 bits per second:
  Serial.begin(115200);
  // initialize the SH_LD & CLK pin as an output:
  pinMode(SH_LD, OUTPUT);
  pinMode(CLK, OUTPUT); 
  // initialize the SO-Serial output pin as an input:
  pinMode(SO, INPUT);
 
}

void loop()
{
   Parallel_data1=read_shift_data();
   String thisString1 = String(Parallel_data1, HEX);
   Serial.print("--switchBank1:");
   Serial.println (thisString1);

  // set all pins at once

  byte d1= (MSBFIRST, (Parallel_data1 / 65536));
  byte d2= (MSBFIRST, (Parallel_data1 / 256));
  byte d3= (MSBFIRST, (Parallel_data1 ));
  String thisString4 = String(d1, HEX);
  String thisString5 = String(d2, HEX);
  String thisString6 = String(d3, HEX);
  Serial.print("--switchBank1:");
  Serial.print (thisString4);
  Serial.print("---");
  Serial.print (thisString5);
  Serial.print("---");
  Serial.print (thisString6);
  Serial.println ();

  uint8_t pinValues[] = { d1, d2 , d3 };
  sr.setAll(pinValues);
 
  Serial.println();
  delay(10000);

  }

 
}



  清除項目編譯快取 執行 flutter clean 重新取得套件依賴 執行 flutter pub get 完整重新編譯並執行 flutter run 1. 新增專案套件 (pubspec.yaml) 在 Flutter 專案根目錄下的 pubspec.yaml 中,新增 ...