Smart Farms: Arduino, Sigfox and Oracle IoT
OK, I need an explanation. “Tina” is the nickname I have given to my basil plant (Tina is short for piantina, the Italian word): my way to try to follow the myth of the zero-km cooking. It is not a trivial step: fresh basil is essential in the kitchen.
“Tina” gives me many satisfactions but, like all beautiful things, needs care. It must be watered with a certain regularity and sometimes I forget it. To prevent it from dying, I have chosen the technological path. I have decided to put in place a small system that periodically checks the soil moisture, the temperature and warns me when … it needs water.
In the article I will cover many aspects of the overall solution.
In the first part, I will describe the module that takes care of reading the data from the sensors and sending them to the “network”. The module uses a Low-Power wireless technology: Sigfox. I will explain how to make the configuration of the Arduino board used and how to write the code to be installed in the card itself.
In the second part I will explain how Sigfox can be integrated with our backend system.
In the third part, I will illustrate an example of implementing the backend service, which receives data from Sigfox, and how that service integrates with Oracle IoT Cloud Service or with a generic MQTT broker.
In a subsequent article I will describe the sensor component, to acquire soil humidity and other “vital” parameters.
The first module.
The first module of the system is the module that will monitor the environmental conditions and soil moisture.
But here the usual Arduino-compatible board is not enough. The plant is on the balcony, outdoors, at a point where I cannot access electricity and WIFI.
So, two important requirements:
- The module must be “Low Power”, in order to work with a battery to be changed only after (several) months
- I have to use wireless technology, an alternative to WIFI
To these two requirements I add a third: communication must be “Long Range”. In my case it would not be necessary, but if we wanted to develop a solution for a Smart Farm it would probably be.
The three requirements greatly restrict the field of possible solutions. Today I know only three of them:
- NB IoT
I have decided to experiment with the third one (Sigfox) also because I easily managed to buy a MKRFOX 1200 board from Arduino, with a limited cost. But, I plan to try the NB IoT card that Arduino is developing in collaboration with Vodafone, as soon as it is available.
Sigfox is a LPWA (Low Power Wide Area) network.
Sigfox provides wireless connectivity in many countries through a series of agreements with local operators. In Italy the operator is Nettrotter, who claims to have already set up a network with over 1000 base stations and which covers over 40 cities and 80% of the population. The coverage looks good, the map can be consulted on their site.
Sigfox has a strong limit on the size (max 12 bytes of data per msg) and number of messages per day (140). But for my purposes it is OK: the conditions I want to monitor change quite slowly over time.
MKRFOX 1200 unboxing.
The first operation is, obviously, to unpack the board, extract the antenna from its package and connect it to the board. The antenna connects via an UFL micro connector, located on the right. It is really micro, the latter operation is not really simple, but with a good amount of patience you will succeed.
The next steps to be taken are those typical of the Arduino board programming. I assume that the reader is familiar with these operations, otherwise a useful place for more information is the section Getting Started, from Arduino site
After launching the Arduino IDE, in summary the first steps to be taken are:
- install the MKRFOX 1200 library
- install the RTCZero library
- install the Arduino Low Power library
The last library is necessary to use code that minimizes energy consumption and send in deep-sleep the card in the time interval (long enough) between one data transmission and the next.
The card connects to the Mac via a micro USB cable and care must be taken in the IDE to select the “Arduino MKRFOX 1200” card.
Registration on Sigfox network.
In order to send data to one of our applications (which we will run on Oracle Cloud) the route is the following:
- data from the Arduino board travel on the Sigfox network
- data are received from gateways and sent from network devices to Sigfox’s backend servers (Sigfox Cloud)
- they are then sent to one of our servers, via a callback to be defined
The first concrete operation to do is to register the card on the Sigfox Cloud. To do this you need to know two specific information on the card (ID and PAC) that can be read by running the following program on the card:
The information (ID, PAC) is written on the Arduino Serial Monitor.
After having found these data, you can proceed on the Sigfox backend to register and activate the card.
If you do not already have an account, you must create it, providing your personal data and email, along with a password to be used on the portal.
In the activation, you must specify:
- As provider kit: Arduino
- your Country and, therefore, the network provider (in Italy: Nettrotter)
After completing these operations, the card is registered, but still not able to transmit. We have to load a second sketch on the card, which must deal with:
- initialize the card
and, in loop:
- read the data
- send data to the Sigfox network
- put the card in deep-sleep
For now, I have not dealt with the acquisition of data from the sensors, nothing really new, the code obviously depends on the model of the sensors. But I have made a prototype that transmits (simulated) data. The sketch is as follows:
In the sketch a message is sent every 60 sec. OK to speed up the tests, but, in going “in production”, we must remember to reduce the time frequency, to adapt to the limits imposed (140 msgs per day).
It should be noted that in the loop() function, the usual delay() function is not used to pause, but instead the sleep() function of the LowPower library is used.
Data on Sigfox Portal
At this point, we can see our board activated on the Sigfox portal:
and we can see the messages received from the Sigfox network. In this way we can verify that the transmission is successful.
It should be noted that the string constituting the message is sent encoded in hexadecimal and thus is displayed.
If we click on the icon in the “Location” column, a map opens, showing, with a certain level of approximation, the geographical location of our device. This is another feature of Sigfox technology: we can geo-locate a device without having to use GPS. Obviously, we must take into account the fact that the accuracy is of the order of several kilometers.
Sigfox allows, for each type of device, to define a callback that can be invoked when specific events occur.
In our case we want to define a callback that is invoked whenever a message is received by the Sigfox Cloud and we want the content of the message to be sent to the callback.
In short, it is a question of defining a URL to which the message, together with other data (device ID, RSSI, …) is sent via POST or GET.
The message body format can be defined, as well as the content-type. In my case I made the following choices:
- I chose to use HTTP and not HTTPS
- I chose POST
- Content-type: application / json
- I deleted some information
The original message is encapsulated in a JSON message. The field “data“ contains, always in hexadecimal, the message that our device has sent.
Integration with Oracle Cloud.
Synthetically, I have defined in Sigfox a callback that invokes the URL of an HTTP server distributed on the Oracle Cloud. The Sigfox message is encapsulated in a JSON that also contains the device name (as recorded in Sigfox).
The server was implemented in NodeJS, using the Express framework.
The initial skeleton of the server code is in the following gist and the code is limited to printing the received message.
The function exa2a has the task of transforming the encoding from hexadecimal to ASCII.
The (very) last mile.
The last step is to integrate with Oracle IoT Cloud.
We can not directly invoke the Oracle IoT Cloud endpoint, because in Sigfox we can not directly use the Oracle IoT SDK. But the solution I have chosen, which we could define as based on a Cloud Gateway, is a commonly adopted Design Pattern.
The complete code is contained in the latter gist:
If we want to integrate with a generic MQTT broker, we have to insert the calls to the MQTT client functions in the NodeJS server code. In this case I would undoubtedly direct my choice towards the Paho-MQTT library.
In this article I have showed how it is possible to build a solution for a Smart Farm, based on the technologies: Arduino, Sigfox and Oracle IoT Cloud.
The solution allows you to remotely control the environmental and soil conditions and to identify if they are suitable for the type of crop.
The solution is based on a Low Power communication technology, Sigfox, which can be easily adopted in the field.
Finally, I explained to you how I hope to be able to adopt it to create the optimal environment for my “Tina”.
One last thing.
After writing the article, I discovered that Sigfox has published another interesting article on Medium, always linked to the theme: listen to the small plants. Enjoy.
Sigfox technical details.
The limits we talked about (12 bytes per message, 140 messages / day) may seem very restrictive.
On the one hand, a technology that wants to be “very Low Power” cannot aspire to the typical data rates of the 4G world.
But it is useful to know that, in reality, these limits also derive from the (narrow) spectrum band available, from the need to make it available to many potential users. The limits are defined in Europe by the European Telecommunication Standards Institute (ETSI). This is what Sigfox reports on his site:
ETSI (European) regulations say that devices can emit 1% of the time on the public band (868MHz) over the course of 1 hour. It translates into 6 Sigfox messages of 12 bytes per hour. In other countries, the regulation could allow more messages but we kept our commercial offering the same worldwide.
The transmission of a message requires up to a maximum of 6 sec. From this it derives, in compliance with the duty rate of 1%, the maximum number of 6 messages/hour.