Milli Developer Kit v1.0 Add your own sensor

and an observable sensor

Get the Milli Developer Kit Temperature Sensor Reference Application on GitHub

This page describes how to add your own sensor to the Temperature Sensor Reference Application. It also covers how to make your sensor observable.
 

Introduction

This page describes how to add your own sensor to the Milli Dev Kit example application described here.  The picture to the right shows how the example application hardware looks like when it is set-up.

On Developer Portal, you can purchase a Milli Dev Kit that comes with or without an Arduino M0 Pro using the following links:

The Milli Dev Kit consists of an Arduino shield (aka MilliShield), an antenna, a micro USB cable and a USB/TTL Smart Cable.  Below is a photo of the Milli Dev Kit v1.0 with an Arduino M0 Pro:

 

The sensor measurements are handled by a CoAP server running on the Arduino's microprocessor. Communication between the Arduino board and the shield is done via HDLC over the UART.  The below drawing shows the communication path from Sensor to Client:

 

 

Unified Resource Identifier - URI

The way the User Application (or a CoAP Client) interacts with a CoAP Server is via commands such as GET, PUT, DELETE and a special case of GET called Observe. Similar to how an URL is specified in HTML using a string of characters, an Unified Resource Identifier, URI, is also using a string of characters.  In the example application referenced on this page, we use /sensor/arduino/ as the URI followed by a particular CoAP resource e.g. /sensor/arduino/temp for the termperature sensor.

The objective of this page is to describe how to add your own sensor e.g. a humidity sensor.  If the URI of your humidity sensor is humi, the full identifier would be /sensor/arduino/humi before the query.

 

C++ Sensor Template

 

How to add your own Sensor

  1. To open the IDE, locate and click on the file mshield.ino in the folder mshield
  2. Search for the text MY_SENSOR
  3. Comment out the code at that location and you should have the following in mshield.ino:

            if (!coap_opt_strcmp( o, MY_SENSOR ))
            {
                /* Replace mysensor below with a name for your sensor               */
                /* The function 'crmysensor' is implemented in a new C++ file       */
                return crmysensor( req, rsp, it );
            }

     

  4. In the above code, replace MY_SENSOR with an uppercase name for your new sensor e.g. HUMIDITY_SENSOR
    NOTE: HUMIDITY_SENSOR is the definition of a URI string, which later will be defined in the file mshield.h
  5. In the above code, replace mysensor in crmysensor above with a lowercase name for your new sensor e.g. crhumidity
    NOTE: 'cr' is short for CoAP Resource and in the above example, crhumidity is the name of the new C++ function that will handle the incoming CoAP Request to your new sensor
  6. Open the file mshield.h by clicking on the black triangle on the upper right-hand side of the IDE, see red line below. Scroll among the files and select mshield.h.


     

  7. In the file mshield.h, search for MY_SENSOR and locate the string "a_sensor"
  8. Replace MY_SENSOR with whatever name you picked in Step 4 e.g. HUMIDITY_SENSOR
  9. Replace the string "a_sensor" with an URI string of your choice e.g. "humi" using max 22 characters
    When you are done, it should look something like this:


     

  10. In the folder mshield, locate the C++ file TT_resource.cpp and the associated header file TT_resource.h seen below:


     

  11. Rename the C++ file TT_resource.cpp and the header file TT_resource.h to something related to your new sensor e.g. humi_sensor.cpp and humi_sensor.h
  12. In the Arduino IDE, go Sketch/Add file and and pick the new header file from Step 11.  In Step 11, we renamed the header file to humi_sensor.h.


     

  13. In the file humi_sensor.h, locate the C++ function prototype crmysensor and replace it with whatever C++ function name that you picked in Step 5, e.g. crhumidity
  14. In the file humi_sensor.h, replace all instances of the string TT with whatever name you would like e.g. humi.   For an example, look at the before and after images below:

    Before:

    After:


     

  15. To save your changes, press Ctrl-S
  16. In the Arduino IDE, go Sketch/Add file and and pick the new C++ file from Step 7.  In Step 7, we renamed the C++ file to humi_sensor.cpp.
  17. In the C++ file humi_sensor.cpp, locate the C++ function crmysensor and replace it with whatever C++ function name that you picked in Step 5, e.g. crhumidity

 

Make your Sensor Observable

Do the following steps to make your sensor observable, that is, it will send measurements at a certain rate e.g. once a minute:

  1. To open the IDE, locate and click on the file mshield.ino in the folder mshield
  2. Open the file mshield.h by clicking on the black triangle on the upper right-hand side of the IDE, see red line below. Scroll among the files and select mshield.h.

  3. In the file mshield.h, search for the text OBS_SENSOR_NAME. This is where we specify what sensor that we want to make observable. Pick one from the list of defined sensors above in mshield.h, e.g. HUMIDITY_SENSOR.
  4. In the file mshield.h, search for the text OBS_FUNC_PTR. This is where we specify what C++ function to use when reading your sensor e.g. arduino_get_humi located in sensor_humi.cpp.
    When done, you should see something like this:


     

  5. In the Arduino IDE, press Ctrl-R, to build your observable sensor application
  6. In the Arduino IDE, press Ctrl-U, to upload your observable sensor application
  7. In your User Application (or CoAP Client), if you issue a GET with the Observe Option and an URI /sensor/arduino/humi?sens, you should be receiving CoAP Observe response messages with sensor data at regular intervals.
    NOTE: sens in the above URI, is the Option after the query that you designed when you implemented your own sensor as described in the section "How to add your own Sensor" above.