Getting technology to work can be like playing "Pin the tail on the Donkey". Putting a tail on a paper donkey seems straight forward and obvious but once that blind fold comes on and your mates spin you a few times it turns out you have no idea what your up to. Before you know it you have pinned the tail to the cake and everyones laughing at you and your starting to feel a little sheepish.
Well this tutorial is going to be the cheat to getting data to a Ubidots account from a Raspberry Pi using NodeRED. No longer need you fear the ridicule of your peers. Instead enjoy watching their brains melt out their ears as they try to comprehend how you managed this task with such ease.
My intent is that this tutorial makes this goal very achievable through the use of MQTT and the afore mentioned materials. Have a read through the following material and give it a go. If you have any problems post a comment and with some luck we can address the issue.
BackgroundSending data to the cloud through the use of a Raspberry Pi and NodeRED is relatively straight forward. Where I got into some trouble was how to use the MQTT components within the NodeRED interface. As luck would have it though I somehow managed to find an article by Agustin Pelaez of Ubidots that showed me exactly how to make use of them.
So huge shout out to Agustin for his awesome tutorial.
Honestly his article is excellent and will provide most of what you need to know however this tutorial is going to be specifically about how to send data to Ubidots from a Raspberry Pi with NodeRED.
This is crucial as there is a good chance you are going to be collecting data from a sensor and that sensor will be controlled by a Pi. Getting that data where you really need it is going to be the jam in your dough-nut or the nuts in your vegan breakfast depending how you roll.
Assumptions- You have a Raspberry Pi with an updated version of NodeRED installed and running. If not please go to this link to read my tutorial on how to setup and run NodeRED on your Pi.
- That you are interfacing with your Pi through your PC. Again see my tutorial on how to achieve this.
- You have a Ubidots account. If not go and sign up for a free educational one or 30 day free trial industrial account.
- This is a recommendation more than anything but its a good idea to read Agustin's tutorial as its on point and may explain some steps that I may accidentally/will gloss over.
This tutorial will not deal with an actual sensor which will simplify the setup and instead we will create some data to send from NodeRED to your Ubidots account.
The flow content from this tutorial is mostly copied from Agustin's tutorial as there is no point in skinning this particular cat twice though I may change some of the names.
Start pointYou have NodeRED up and running on your Pi and your ready to develop a flow.
Quick note on flows. As far as I can tell the term simply refers to a series of nodes connected together. Flow coming from the fact that information tends to flow from left to right.
Now I'm a fan of MQTT so we are going to start with that. There could be scope in the future to throw up a HTTP example.
This is the screen you want to start with…
Nice and clean and ready to have a flow put together on it. This is the view from my PC.
There are only two nodes that are required in order to send data to Ubidots through MQTT and those are the “Inject” input node and the “MQTT” output node.
The following image shows these two nodes in the workspace
I simply selected them from the side bar and dragged and dropped them onto the workspace. Clicking on the small Grey squarish circle on the right of the blue node will allow you to drag a line between the two nodes.
Note the blue dots. Every time you change some aspect of a node, including placement, a blue dot will appear basically showing you that it has changed since the last Deploy. Hitting deploy again will remove these dots assuming everything is in order.
So ignoring the writing that you can see in the above nodes you should have two nodes sitting there eager to start up a seriously one sided chat with Ubidots cloud.
Inject NodeThe inject node does not require a great deal of data. You essentially give it some data that it inserts into the stream whenever you click on the grey box on the left hand side of the node. What data it spits out is entirely up to you. However through some trial and error I found that if you don’t put in numbers for the output it wont work. You can put stuff in quotation marks and it will create the device in the cloud however it will not post the data in the quotation marks. This is something I figured out through a little trial and error and may help you out.
The name "RandomData" in brackets is the name given to the variable. This is assigned by the Ubidots platform when it receives the data. If the variable does not exist it will be created
The following image will show everything that I put into the node.
This window can be achieved by double clicking on the node on the screen.
The only data that I added to this window was the line “{"RandomData":456}”. What this does is it creates a variable called “RandomData” under the device “node-red” within your Ubidots platform. Within this variable, the numerical value of 456 is stored every time you click on the left hand grey box.
Later I will show you the messages the program should show you every time you hit Deploy or the grey button.
So you can put any device name you like and any numerical goods that tickle your fancy. Remember we are simply creating something to send. Without anything to send it becomes difficult to check if the MQTT is functioning as desired.
Note that I selected the string option in terms of input type for this node. There are others which I encourage you to explore however I have not looked into them extensively yet.
MQTT NodeSo we have two nodes joined together with a message to send to the cloud. However its now time to put the rice into your friend rice and bulk out this bad boy with all the goods we are really hanging out for.
The following three images will show you what sort of data should go where.
This section to me is the most crucial. Putting in the data is not that hard. However there are a ton of fields that could be filled in and if you anything like me you were not certain of what needed to go in where.
For example you do not need to fill in the password field. However my question following that would be why the hell is that even an option then if you don’t need it. At least mention that it might not be required. Theres obviously a reason for it but it confused the hell out of me.
However I digress which I am heavily prone to doing so beware.
All of these images come from clicking on the MQTT node and subsequent options within the resulting window.
For educational users of Ubidots please use following number 1 when asked for the server name and for industrial users such as myself please use number 2.
- 1. things.ubidots.com
- 2. industrial.api.ubidots.com
Both use port 1883. If you don’t get this right you will struggle to see the desired result pop up on your Ubidots Devices page.
Honestly I feel like this is pretty straight forward in terms of what to do. Looking at the third image you can see where to put your Ubidots token. If your not sure what or where your Token is please click here and read this article.
The topic is the only thing left that really needs to be discussed here. Its in the first window and you should be able to read it as “/v1.6/devices/{Device_Label}”.
You can choose the name of the device if you want to use an existing one or just put in any random garbage and the Cloud will create the device for you. After you have run the program a new device with your chosen name should pop up on your platform.
The details of what that line means is material for another tutorial however its essentially the topic you are posting to. There is a topic you are adding information too and thats its address. Not dissimilar to sending an envelope full of info to a certain house in the suburbs.
But for the purposes of this straight forward wee gem just copy and paste that in and get into it.
Running what you have gotOk so we are set to go. Time to run this bad boy and reap the fruits of our labour and hopefully send some fairly succulent numerical gems to your Ubidots account.
In order to start this all running you simply have to push the red Deploy button top left of your work space.
This image is showing three key things:
- The Deploy button has gone grey as it has deployed. It will only go red again if you change something and it then presents the option to redeploy.
- A green bordered notification has popped up at the top to say that the program has deployed correctly. Now this means it has deployed and not that everything is miraculously working as expected. It can deploy and give you no output at all. So just check what you have got in your Ubidots account before you crack out the fine China and the Champagne.
- I pushed the grey button twice on the Inject node and hence two notifications popped up saying that injection was successful. This means that if everything is going to plan then you should have the value 456 occur twice under the variable name "RandomData" within the Device "node-red".
The aforementioned flags will disappear quite quickly so keep an eye out for them.
ResultThe result we are after is shown in the following images. What we are looking at is my Ubidots account with a "node-red" device in there and within that device a variable named "RandomData" that has the value 456 occurring multiple times.
Every time you hit the inject button another value of 456 with a different time stamp should occur.
As is clearly visible I got a little trigger happy on the inject button but its so satisfying when you get it to work if you have had some troubles which I definitely did on my journey to bringing you this tutorial.
RecapSo what we have achieved here is the ability to send data to Ubidots from your Raspberry Pi through the use of NodeRED and MQTT.
Once you are in the program creating the flow you tend to forget about the fact that you are on a Raspberry Pi however the Pi factor is significant.
With your Pi you can gather information from all sorts of different sensors in thousands of different situations and now you can send it all to the cloud. This is incredibly powerful and later tutorials will work with and expand on what we have achieved here today.
But for now, assuming you have got this to work, enjoy your success and play around with it. Personally I am not a fan of playing around with stuff once I have it working however theres no doubt that it helps understanding.
Cheers for reading my tutorial. If this tutorial has helped out one person then it was worth it to me. If it helped out more then thats really going to put a smile on my face: )
Comments
Please log in or sign up to comment.