Water is an important resource. We use water for our day to day needs. It is quite apparent that water needs to be managed properly to avoid water scarcity. Many places around the globe do not have batter management or planning to save water. As a result there is water scarcity many times. The examples these situation is city like the Johannesburg in south Africa. One of the common cause of water waste inside house is that people often forget to turn off the motor once the overhead tank is full. These tasks are still manual at many places where residents have to turn on and turn off the motor themselves as needed. Many times they forgot to turn off the motor which results in water flowing to drainage. This waste of water can be avoided either through automation of this task or using some other similar but less costly and complex technology. The sensor based automation of this task is still quite expensive and not every household can afford it. Hence, it is required to have some low cost system that can monitor the water actively and alert the user when required and how much the overhead tank is full.
Blues Notecards can provide cost effective solution to this problem. In addition cellular Notecard (Which I am using for this project) they have WiFi, LoRa and Satellite communication based Notecards. This will make it possible to switch to different technology with minimum changes in the project.
Required hardwareNotecard is a device that can communicate to Notehub.io which is a cloud platform form Blues using LTE-M or NB-IoT communication protocols. It can do so using JSON
based request and response commands. These JSON based commands can talk with Notecard over either serial
or I2C
communication. The host can be either some micro controller development board or the user PC. After host sends JSON
requests, if there is a successful communication between your Notecard and cellular tower, you will have your data on the Notehub.io.
For this project, I am going to use Blues Notecard Cellular global. This device can operate almost anywhere in the world. Along with it I will use Notecarrier-A that is used to attached the Notecard and contains an antenna for cellular and GPS, SIM card slot, GPIO connections and other required hardware such as connectors for solar and battery.
For this project, I am using nRF52840-DK from Nordic Semiconductor to send JSON commands over I2C interface. I am using waterproof Ultrasonic distance sensor to measure the water level of the tank. This sensor communicates over UART to the board. Along with that, I have also attached some air quality and weather monitoring sensors such as SGP40, SHTC3, PMOD-ALS etc. I am also using external GPS to connect to the Notecard to get location updates more easily.
My final goal is to create a dashboard that can display values for water level inside tank and other parameters related to water and air quality. See the example.
Introduction to Apache-Mynewt and using note-c API for the NotecardApache Mynewt is a real-time operating system. It is designed for low-power wireless IoT devices such as those with Bluetooth low energy support. It has a real-time kernel along with other features for designing a product that has predictable timing behaviour. In Mynewt there is a support for most common hardware interfaces such as I2C, UART, SPI, etc. For that reason, I had decided to port blues note-c API to work with Mynewt applications. note-c provides the functionality to communicate with Notecard using microcontroller host.
Before we can do that, we need to install the Apache Mynewt to our PC. The getting started section of the documentation explains how to install Apache Mynewt to Linux system. Mynewt uses newt, a build and package management tool. It is used to build applications and manage various Packages and Repositories (Repositories are versioned collections of Packages).
After the newt tool is installed, the first step is to create a project workspace with $ newt new myprj
. This will download basic project structure. After the project is created, newt can download the required packages and repositories with user specified versions. This repository and versions are specified in project.yml
file. The download process is done automatically with $ newt upgrade
. But before doing that let's specify the version we need.
The core repository to download for Mynewt is apache-mynewt-core. It specifies other repositories with their version numbers inside a repository.yml
file. The master branch of apache-mynewt-core includes the package definition file (pkg.yml
inside /net/cellular/blues
) to download the sources for Blues note-c directly from github. It will also downloads simple tracking application inside /apps
folder. That is why we specify version master (0.0.0) for apache-mynewt-core in project.yml
file above and then simple run
$ newt upgrade
As we see in the above screenshot the newt upgrade
command downloads the note-c library if master version (tag 0.0.0) of apache-mynewt-core is specified. You can check the folder named /blues-note-c
inside the /repos
folder of the project workspace.
Before we can build and flash application to talk to Notecard, we need to look at some Mynewt terminologies and settings. The newt can create and build different targets (using $ newt target create
) and configure the following things for them. For our application we need to do the same.
- Appications - Application to build build for specific target e.g.
newt target set nrf52_boot app
=
@mcuboot/boot/mynewt
- Build profile - build profile for selected target (e.g. optimised, debug)
newt target set nrf52_boot build_profile
=
optimized
- BSP - Specific board supported by the Mynewt to build the application for for specific target (e.g. nRF52-DK, STM32, etc)
newt target set nrf52_boot bsp=@apache-mynewt-core/hw/bsp/nordic_pca10040
- syscfg - different settings such as peripherals, stack size, pin mapping and others to create the build for specific target. Defined in file
syscfg.yml
Apache Mynewt has Newtmgr
tool that can be used to update the flashed image over the air with some transport such as BLE or WiFi. Hence, in the future one can update or add new functions to the existing project from remote place. As I may want to update the hardware and flash new image in the future, I can use this feature of Apache Mynewt.
As discussed earlier Blues cloud platform is Notehub.io. Data can be routed from Notecard to Notehub.io and visa-versa using JSON based request and response. This data can then further be routed to user preferred cloud applications such as Ubidots, Thinger, datacake, etc. As I have used Thinger for my previous project (Smart Helmet) using Blues, I am going to use it again to create the dashboard for visualisation of the data. The reason for using Thinger is that it is completely open-source and has possibility to expand the existing dashboard widgets using custom widget creation option. This will help me create custom widgets for my project.
To send data to the preferred cloud platform it is required to create a project
and routes
inside Nothub.io as explained in the Smart Helmet project. While creating a project it is required to note the ProjectUUID.
This will be needed for us to send the data to the cloud application directly form our device. It is also used to associate a particular Notecard to the particular project and helps Notehub.io to uniquely identify the data from specific Notecard. After that a Route
must be defined. As the name states Route
helps to route data coming from the Notecard to cloud platform or any other third party service such as SMS alert etc. Thinger supports data coming from various kinds of devices such as Arduino, RaspberryPi, MQTT, SIGFOX or generic HTTP devices as shown in the following image.
As we want to route data from Notehub.io we need to create Generic HTTP Device
on Thinger.io. After that, we need to go to Devices
tab and go to the Callback
section and get the Target URL
and permanent Authorization token.
These two things then can be used to create a URL in the format given below. Also, tick on the Write Bucket
to write and save data to the Bucket.
You can create Bucket on Thinger.io if they haven't already and select From API Request
in the Data Source
field of the Bucket
. This will save any incoming data to the Bucket
with table like format and we can use these data on the dashboard widgets to visualise them.
REST API
+ ?authorization=
+ Token
Now, we can go back to the Notehub.io and paste the URL that we created in the specific Route's
URL section. Then we can define other fields as required. In the Filters
section we can chose which files that we want to send to the Thinger.io platform. In the Data
section we can transform the data to particular format such as JSONata
that eventually will only send selected fields of the data that we have in JSON format. Last thing is to save and enable the Route
. With these settings in effect once the device sends the data, it will be saved in the bucket on the Thinger.io.
On Thinger.io different dashboards can be created with different kinds of Widgets. The dashboard documentation mentions them all. For us important widgets are Google Maps, Text Widget and Gauge Widget. These are enough to visualise the data that we have from environment sensors, from GPS module of Notecard and external GPS and also for water level indication.
After the above mentioned settings are done on Notehub.io and Thinger.io, we can move to Apache Mynewt part to create and flash Mynewt project to the nRF52840-dk so that it can send data to Thinger.io dashboard via Notehub.io.
For Mynewt after installation has finished as mentioned in previous sections, it is required to create
target and application
with appropriate settings in syscfg.yml
files. I have already mentioned in the previous section how to create target
, set application
, set bsp
and build_profile
. The following command helps to create syscfg
values for specific target. Or you can use text editor to change the settings.
newt target set target syscfg=I2C0=1:CONFIG_NEWTMGR=0
For
my project's target
I have following settings for target
named blues
that I have created. The command newt target show
will list them all. As you could see that I have enabled some peripherals such as I2C1
or SPI_0_MASTER
for this project that are not enabled by default in Mynewt. These default settings are defined inside the Mynewt/hw/bsp
folder of the core repository. we can overwrite this default settings by specifying target specific syscfg.yml.
If there is an error or conflicting peripheral settings in syscfg.yml
or if some value such as pin number must be defined explicitly or is missing, newt
will through an error when building the project with newt build target
command.
targets/blues
app=apps/ble
bsp=@apache-mynewt-core/hw/bsp/nordic_pca10056
build_profile=debug
syscfg=BLE_ROLE_BROADCASTER=1:CONSOLE_IMPLEMENTATION=full:CONSOLE_RTT=0:CONSOLE_UART=1:I2C_0=1:I2C_0_FREQ_KHZ=400:I2C_1=1:I2C_1_PIN_SCL=11:I2C_1_PIN_SDA=12:SHELL_PROMPT_MODULE=1:SHELL_TASK=1:SPI_0_MASTER=1:UART_0=1:UART_1=1:UART_1_PIN_RX=34:UART_1_PIN_TX=33
targets/my_blinky_sim
app=apps/blinky
bsp=@apache-mynewt-core/hw/bsp/native
build_profile=debug
targets/nrf52_blinky
app=apps/blinky
bsp=@apache-mynewt-core/hw/bsp/nordic_pca10040
build_profile=debug
targets/nrf52_boot
app=@mcuboot/boot/mynewt
bsp=@apache-mynewt-core/hw/bsp/nordic_pca10056
build_profile=optimized
After the target
is built successfully we can create an image for it using command such as newt create-image blues 1.0.0
. This will create the binary to upload to the board and sign it with appropriate keys. Finally, newt load blues
will upload the binary to the device.
For this project, I have created four tasks
that runs in parallel to each other with different priorities. These tasks are for reading the sensor values from peripherals such as I2C, SPI, UART
and sending these data to the Notehub.io. These is what main.c
file has.
The hardest part for this project was the installation of the sensor and the device. As the sensor is waterproof there is no risk of damage if I keep it inside the tank cover for prolonged time. The humidity inside the tank does not damage it.
So I installed the sensor as shown in above image and kept the device running on battery for hours. I monitored the data coming from the device on Thinger.io dashboard. For this project, new data from the device is uploaded every ~300 seconds (5 minutes). I can observe this behaviour in the device data bucket.
- When downloading repositories after creating a project with
newt upgrade
, make sure to specifymaster
branch ofapache-mynewt-core
repository. - While placing order for the hardware part also order appropriate connectors such as
JST PH
,grove
, etc. This will save time on wiring and hardware related issues. - One can try using Notecard WiFi instead of cellular to create this project.
Working on this project was really interesting. It took me quite some time to figure out everything and make this project to a state where I can say "yes I have it
". One thing that I wanted to do for this project is use multi threaded environment such as RTOS. I am satisfied that I was able to port the note-c
API to the Apache Mynewt and create a project that works perfectly. I have added battery and solar charging option as the Notecard - AL already have this in it. It will also help installing this device at a place where electricity connection is not available at ease.
This project could be expanded in many ways in the future. One of the feature that I would like to add is over the air image update for the application. I could have added more sensors to monitor the environment or water quality. This eventually can be combined with SMS
based alert or visual/audio alert. But there is always time to create something new and in the future I will add new features to this project and update this project here for everyone to look.
Comments