Today we will connect the MQTT bus that is core part of IoT Cloud to the MongoDB using Node-RED flow.
The goal is to have the values – that are sent by our simulated device – stored in our database to be able to display them to an user.
We need to add MongoDB to our Bluemix application.
From the Bluemix Dashboard click IoT application, Add a Service and find MongoLab and click Create.
Find the new MongoLab service on the Dashboard and click Show credentials.
Copy the „uri“ line somewhere. My MongoDB URI looks similar to this:
"uri": "mongodb://IbmCloud_tobljl5v_c71bl94o_o7es2oe4:nPRhVWj38GJFvyHWwkfzUDYlJ8_DKkpK@ds059029.mongolab.com:59029/IbmCloud_tobljl5v_c71bl94o"
Return back to the Node-RED user interface (or open it from Bluemix dashboard if not opened) and add Mongodb node and connect its to the Event listener node:
Now parametrize the Mongodb node by splitting the uri obtained above into pieces:
Example using my values. Yours will be different.
Host: ds059029.mongolab.com Port: 59029 Name: IoT:Mongolab (name of Bluemix service) Username: IbmCloud_tobljl5v_c71bl94o_o7es2oe4 Password: nPRhVWj38GJFvyHWwkfzUDYlJ8_DKkPk
Deploy the flow.
Now send some events using simulated device. Check that you are receiving the event by looking at the debug window of Node-RED.
Now we can check weather are the sent values written into database:
Click the MongoLab service on the dashboard and then Lunch MongoLab Dashboard.
Click the database name and collection name sensor-data.
You should see something like this:
If not then you have most probably not set the MongoDB credentials in the Node-RED flow correctly.
Otherwise voilà – the last step remains – to show the values somehow to the user…