Deploy Node-RED on an Amazon EC2 Instance
Before you can deploy Node-RED, you must first create an EC2 instance using your AWS account. Follow the instructions found here on nodered.org to create the EC2 image and setup Node-RED. If you are not sure how to connect to your EC2 instance after it is created, check out the AWS guide for connecting to your instance. Once connected to your EC2 instance, follow the steps to “Setup Node-RED” but do not start Node-RED yet. Before starting Node-Red, run the following commands to install the “node-red-node-aws” and “node-red-contrib-web-worldmap” nodes:
sudo npm install -g node-red-node-aws
sudo npm install -g node-red-contrib-web-worldmap
Now that we have some handy nodes installed, it is time to start Node-RED. Use the following command to start Node-RED:
If everything was successful, you should now be able to open Node-RED in your browser by going to:
Check to make sure the amazon s3 and worldmap nodes are present:
You will come back to Node-RED later in this recipe. For now, let”s move onto the next step and create an Amazon S3 Bucket.
Create an Amazon S3 Bucket, Upload a File with GPS Coordinates and Create and Access Key
Amazon S3 is a place to store files on AWS. You will create a “Bucket” to hold a file with some GPS coordinates which can then be used to gather weather forecast information using the Weather Company service on IBM Bluemix. To create a bucket, go to https://console.aws.amazon.com/s3 and click the Create Bucket button. Give the bucket a name and a region and click the Create button. Make note of the bucket name and region as you will need those later in this recipe. Once the bucket is created, click on the bucket name to open the bucket.
Now click the Upload button and add this file which contains some GPS coordinates.
You now have a bucket that contains a file with GPS locations. Next you need to create an access key so that you can programmatically access the bucket from Node-RED. In AWS, click on your user name in the upper right hand corner and choose Security Credentials.
Expand the section for Access Keys and click the Create New Access Key button. Download the access key file and save it for use later in this recipe.
Let”s now move onto the next step and create an instance of the Weather Company Data on IBM Bluemix.
Create an Instance of the Weather Company Data Service on IBM Bluemix
In this step, you will create an instance of the Weather Company Data on IBM Bluemix. Among many other features, this service provides APIs to gather weather forecasts for locations that you specify. First, navigate to the Bluemix Catalog and login if you are not already. You can quickly find the Weather Data service by typing in “weather” in the search field.
Click on the Weather Company Data service and either use the default names for the service and credentials or provide your own. Then scroll to the bottom and click the Create button.
Once the service is created, click on the Service Credentials tab and make note of your username and password. Mark these down somewhere as you will need them later in this recipe.
You now have an instance of the Weather Company Data service created. Next, we”ll move back to Node-RED and see how we can piece all this together in a Node-RED flow.
Create a Node-RED Flow
In this step, you will import a Node-RED flow and configure some nodes with information from your S3 bucket and Weather Data service. The flow will first access your locations file on AWS S3, parse the GPS locations within that file, call the Weather Data service for each location and then finally plot those locations and their forecasts on a world map.
Navigate back to Node-RED where you ended in step one http://<your-instance-ip>:1880/
Use the Node-RED menu in the upper right hand corner to import from the clipboard:
Copy and paste the text from this file into the clipboard box. The text should not have any new lines in it so be sure to download the file, open it in a text editor, make sure word wrapping is turned off and select all the text and copy it. Finally, click the Import button.
Place the imported flow into the flow editor:
You now need to configure a couple of the nodes with your S3 and Weather Data information. Double click on the “s3 location-bucket” node. Click on the pencil to the right of the AWS field. Enter your access keys found in the file you downloaded in step 2 of this recipe.
Enter the Bucket, Filename and Region to match your information from step two of this recipe and click done.
Next, you will configure the “Get Forecast” node with your credentials for the Weather Data service from step three. Double click the “Get Forecast” node. Check the Use Basic Authentication box. Place in your user name and password for the Weather Company Data service you acquired in step three. Finally click Done.
You are now configured and ready to run your flow.
Deploy and Execute your Node-RED Flow
You will now deploy and execute your Node-RED flow. Click the Deploy button in the upper right hand corner.
We should now be able to navigate to the world map. In a separate web browser tab, go to the following link:
The world map will not show any pins as of yet since we have not executed the flow. The flow is setup to automatically run every five minutes. To execute the flow on demand, click the little blue tab to the left of the “Timestamp” node.
***Be aware that the free version of the Weather Company Data service only allows for 10 API calls per minute. With that Node-RED flow you are making six API calls every time you execute the flow. An API call is made for each GPS coordinate.***
Now examine the world map again. You should now see a number of pins plotted. Clicking a pin will show the forecast.
If it is not working for any reason, try using the Node-RED “Debug” nodes and tab to see if there are any error messages.
You can try updating your locations.txt file in your S3 box with other GPS locations. Just make sure there are no extra new lines at the end of the file.
Understanding the Solution Architecture and Node-RED Flow
This step provides an explanation of the solution architecture and each node in the Node-RED flow.
Explanation of solution architecture:
In the picture below, you can see that Node-RED is running on an EC2 instance on the Amazon Web Services cloud platform. The Node-RED flow grabs GPS coordinates from a file in an S3 bucket in the AWS cloud. Node-RED flow then makes REST API calls using those GPS coordinates to an instance of the Weather Company Data service in the IBM Bluemix cloud. Lastly, Node-RED uses the World Map node to plot the location on a map with weather forecast information.
Explanation of Node-RED flows:
timestamp – Executes the flow every 5 minutes or on demand when the tab is clicked
s3 bucket – Grabs a file from an S3 bucket and returns the contents as a buffer
Buffer to String – Converts the contents of the buffer to a string
Split on new line – Splits the string into multiple messages based on the new line char. Executes the remaining flow for each substring.
Build URLs – Forms the needed URL with the correct geocode in order to call the Weather Company Data REST API
Get Forecast – HTTP GET request using the URL passed in in order to get weather forecast form Bluemix service
Format JSON – converts JSON string to JS object
Add WorldMap Data – Function that composes the message payload to have the name, lat, lon and forecast info.
Complete Forecast Message – Some more parsing to format the message before passing into World Map node.
Send to WorldMap – Create the world map page and plots the point on the map with forecast info.