As I mentioned in Part II of this series after setting up the platform, we would be addressing the write-up of some code to retrieve sensor information via NodeJS.
Here is what we’re going to do:
- Install SensorTag library by Sandeep
- Bake some code to read accelerometer data
- Store the data for later review (simple edition)
- Modify Sensor Tag firmware for practical purposes
In the first post, we installed Node and verified that worked. Assuming that you haven’t already messed things :-) the next step is to install a library that will help us to retrieve data from the sensor. Enter the directory Andromeda or whatever you called the folder that will hold the project and type
npm install sensortag
I found necessary to run the following command otherwise I would have been facing permission issues while executing the code.
sudo setup cap_net_raw+eip $(eval readlink -f `which node`)
That grants the node binary cap_net_raw privileges, so it can start/stop BLE advertising. If you followed my previous instructions cap_net_raw is already installed and current otherwise here you find what to do.
Let’s download the code to the local folder
G-Spot of the movements
Now that the library is installed and the code has been downloaded, turn on the Tag and let’s give it a spin using
You should see something like the following
Which prints in console timestamp, X, Y, Z in G units. I might modify the output format in a later revision, to add other sensors data like temperature.
There are 2+ ways of storing data. One is pretty straightforward which is redirect the console output to a file
node accelog.js > data.csv
And a more sophisticated way (reliable too) of queuing up the data and storing them into a database for retrieve them later via some small web server kindly hosted on your RPi. There are many other options, but I wanted to follow this path as it would build foundational pieces that I can use in other projects as well.
I will post my findings after I have gone through that route to benefit whoever else runs in the same scenario.
Sensor Tag Firmware
By default, the Sensor Tag firmware stops advertising after a few minutes that is not very useful if you want to collect data for a long period. The source code to modify the firmware is available by TI however unless you have the right setup and equipment, it is not a walk in the park (I have done it, and it’s not pain-free).
Fortunately, some smart folks out there have made the change for us. On this page, you find the details and the instructions on how to update the firmware using iTunes and a dedicated app from Ti that you can download from the Apple Store.
If you are not brave enough to adventure yourself in the writing of firmware code for the Sensor Tag however, you want to feel the breeze and understand the insights of programming a Bluetooth Low Energy device, read this blog post.
I am confident you will learn some and it will inspire you to experiment more on the subject. Even without writing code just playing with the Gatt tool that I documented in part II. For now it is outside of the realm of this project. Share away if you do, though.
Please, note that that firmware is for the v1 of the Sensor Tag, I have not looked yet into the new and cooler version 2. I placed on other of a few as I am writing this post. If I discover anything of good use, I will share in a later post.
In part IV, we will be addressing the task of creating a queue so the data can be stored into a database and support inherently multiple sensors. If the post doesn’t too long we will also explore how to control the data collection from remote via a web server, otherwise it will be covered later on. At this time in order to kick off the data collection you need to be connected via secure shell or have access directly to the RPi. There’s a lot ahead of us and it looks exciting :-)
Since this week in the United States is Thanksgiving I want to take the opportunity to thank ALL my readers for the support, help and encouragements. This exercise of having this open live notes taking book is proving quite a successful experiment. Networking, good practice for the mind and a lot of fun!