ICP_7 - akkipant/IoT-Fall-2019 GitHub Wiki
This lab introduced us to the custom node creation in Node-Red. Creating a Node in Node-Red requires knowledge of JavaScript, HTML and creating package using .json. The JavaScript file describes what functionality the node has, HTML file describes how the node looks and it's properties while the .json file packages it together as a single node i.e. an npm module. This lab also discusses the use of Arduino Node in Node-Red and it's configuration.
There were 4 objectives for the ICP.
- Creating a lower-case node in Node-red
- Creating a Celsius to Fahrenheit converter node in Node-Red
- Installing Arduino node in Node-red and toggling a LED connected to Arduino.
- Traffic Light Controller using Node-Red and Arduino.
Our approach was to execute each objective step by step. We first learned to create custom nodes in Node-Red for upper-case to lower-case and Celsius to Fahrenheit conversion. Secondly, we moved towards interfacing Arduino with Node-Red and implemented the traffic light controller.
- Create 3 files in a folder for JavaScript, HTML, and package.json.
- Write a module in JavaScript for converting into lower-case and HTML code for the node.
- Start command prompt window in the folder where these 3 files are placed.
- Type " npm init" command and it will walk through the process for creating package.json file.
- After creating the file with the name "node-red-contrib--lower-case", edit it and write a code for Node-Red. Fere the source code in ICP7 folder for this file.
- Save the file. In the same command prompt window type "npm link"
- Open another command prompt window and enter the .node-red directory. Type "npm link node-red-contrib--lower-case". This will link the package with the local node-red modules.
- Type "npm node-red" to start the node-red. Type the "http://localhost:1880/" to start creating the flow.
- Use inject node, lower-case node (newly added to functions) and debug node.
- Configure the inject to string mode and type an uppercase word. Deploy the flow and check output in the debug window. It will show the lower-case converted word.
- Follow the same procedure for the celsius to Fahrenheit conversion node. Filename "node-red-contrib-icp7-c2f"
- For creating the flow use the openweathermap node and provide the API keys. Connect it to the C_to_F node and then to the debug node. A function node can be added to append "degree F" to the converted temperature. Deploy and activate the timestamp and check output in the debug window.
- First, we have to install the Arduino node in Node-Red. Go in command prompt and in .node-red folder. Type following commands. These will install Arduino node. Start Node-Red and connect to localhost. cd .node-red npm install node-red-node-arduino
- In Arduino IDE, we have to use Standard Firmata. Open IDE, go in File--> Examples--> Firmata--> Standard Firmata.
- Open it and upload the sketch into Arduino.
- IDE and Node-Red use the same serial port hence, they will not work simultaneously.
- In the Node-Red window, pull inject node, function node, and output node of Arduino.
- Configure the output node to set the serial port, Pin type as digital and pin number where LED is connected.
- Write a JavaScript code in function node to toggle the LED.
- Connect a LED at the pin 13 or use the led on the Arduino board to verify the flow.
- Second part is to implement a traffic light controller with Node-Red.
- Pull an inject node, function node and 3 Arduino output nodes.
- In function node, set the number of outputs to 3.
- Write a code to implement the sequential flashing of Red, Green, and Yellow.
- We can use the same Firmata from the previous task and directly connect Arduino with Node-Red.
- Verify the output on hardware.
- Creating Lower-case node in Node-Red
- Creating Celsius to Fahrenheit conversion node in Node-Red
- Connecting Arduino with Node-Red for blinking of LED
- Traffic Light Controller using Node-Red with Arduino
The parameters are as per the flow diagram and video.
-
Connecting Arduino with Node-red
Creating modules in JavaScript, writing an HTML code, and compiling it was a little difficult at the beginning. For me, the "Install" command was not working to create a custom node. So, I have used the "Link" command. "Install" command worked for the other 2 teammates. Also, the Arduino sometimes takes time to connect through serial port to Arduino.
Through this ICP we learned to create nodes in Node-Red. We must have knowledge of JavaScript and HTML for the same. Connecting Arduino with Node-Red is simple and requires the installation of Arduino node. Writing functions for Arduino in JavaScript is difficult compared to writing a code in IDE. Overall, we learned methods to add more functionality to Node-Red.