To get started easily with Azure Sphere, the Grove Shield is a great tool. This provides easy access to sensors and interfaces which are not (yet) natively available on the Sphere development board. In this article I will show how to setup the temperature and humidity sensor.

Software setup required

  • Visual studio version 15.7 and up, free community edition is supported.
  • Azure Sphere SDK, download here.
  • Actual Azure Sphere dev board and Grove Shield ;)
  • Azure account, a free account can be used.

Hardware setup

Simply connect the Grove Shield to the Sphere board, then connect the sensor to the I2C port on the board. It should look something like this:

Connected Azure Sphere Grove Shield with Temperature and Humidity sensor

Setup dev board for development (you can skip this if your board is already dev enabled)

Before you get started with your Azure Sphere board, you have to enroll this into an Azure tenant and set it up for sideloading of apps, here is a complete guide or follow steps below.
With the installation of the Azure Sphere SDK you have also installed the Azure Sphere Developer Prompt which we will use for the commands below.

  1. Boot the Azure Sphere Developer Prompt app and make sure the Sphere is connected via USB.
  2. Login to Azure with azsphere login, follow the commands to log in.
  3. Update the Azure Sphere with azsphere recover.
  4. Claim your device into your tenant with azsphere claim WARNING: the tenant cannot be changed after claiming!
  5. Add WIFI to the device with azsphere device wifi add --ssid <yourSSID> --key <yourNetworkKey>. To check if this worked, check the WIFI status with azsphere device wifi show-status.
  6. Enable dev capabilities with azsphere device prep-debug.

Create your application

If you made it here you have done the hardest part already and the fun can begin. Create a new blank Azure Sphere Project in Visual Studio like below.

Add the Grove shield library to the application

Let's add the Grove Shield library to our solution, this can be downloaded or cloned from this git. You can download this anywhere, however for a better overview I save the library inside the main project folder.

  1. Download or clone the library into your main project folder form here
  2. To add this library right click in the solutions explorer, add -> existing project -> add the 'MT3620_Grove_Shield_Library.vcxproj' file in the 'MT3620_Grove_Shield_Library' folder.
Adding additional projects to Visual Studio Solution
  1. Add a reference to the library in your blank example project. Expand your project and right click on 'Add Reference', then select the Grove Shield Library. The project should look like this:
  1. Lastly, you have to add the reference for the library to your own project. To do this, right click on your solution -> Properties -> General -> c/c++. Here, click the drop down button -> "Edit" and add the "MT3620_Grove_Shield_Library" folder. This should look something like this:
Add additional references to a c project in Visual Studio

Add the code

Finally, let's add the code to read the sensors. In this case, we will read the humidity and temperature sensor.

  1. Firstly, add the proper pins to the application manifest so the shield can be used. Open the "appmanifest.json" and add the following capabilities to the respective entities and keep the rest the same:
    {
        "Gpio": [ 8, 9, 10, 15, 16, 17, 18, 19, 20, 12, 13, 0, 1, 4, 5, 57, 58, 11, 14, 48 ],
        "Uart": [ "ISU0", "ISU3" ],
    }
    
  2. Open the "main.c" file, after #include "mt3620_rdb.h", add the code below. This is the library for the shield and the library for the humidity and temperature sensor.
    #include "Grove.h"
    #include "Sensors/GroveTempHumiSHT31.h"
    
  3. Now that we have all the needed groundwork in place, we can add the code to read and display the sensor data. In the main function, add the code below. This will initialize the Grove shield and the attached temperature and humidity sensor.
    int i2cFd;
    GroveShield_Initialize(&i2cFd, 115200);
    void* sht31 = GroveTempHumiSHT31_Open(i2cFd);
    
  4. Lastly, let's add the code to read and print the sensor data inside the while (!terminationRequired) {} block within the main function.
    GroveTempHumiSHT31_Read(sht31);
    float temp = GroveTempHumiSHT31_GetTemperature(sht31);
    float humi = GroveTempHumiSHT31_GetHumidity(sht31);
    Log_Debug("Temperature: %.1fC\n", temp);
    Log_Debug("Humidity: %.1f\%c\n", humi, 0x25);
    
  5. Run the code, the output should be something like this:
Temperature and Humidity output

Wrap up

Awesome, you made it!
The complete tutorial can be fond here.
This sample just shows how to read values locally without sending anything to the cloud. In my next post I will show how to add the IoT Hub capabilities to the program.
If you have a question, let it know in the comments below.