Sending sensor board data from Raspberry Pi to Intelligent Systems Service (ISS)

I have been mucking around with the Xtrinsic-sense board which is an add-on to my Raspberry Pi. In my previous post, I had executed the Python scripts that came with the sensor library that I cloned out of this github. There is a good article that explains about how to enable Xtrinsic-sense board in Raspbian.

However my aim is to get the Intelligent Systems Service (ISS) sample C application to invoke the sensor board shared library to retrieve the altimeter and temperature values, assign that to a string property within the data model, then send it to ISS. The sensor board library comes in the form of a shared library called sensor.so.

Hence I started on a journey to try to invoke the functions within the sensor.so out of mirroring the Python code, but in the sample C application. Now I must admit that Python coding skills is non-existent, and my C coding skills were really rusty. The last time I wrote a C/C++ application was in 2000/2001! What’s surprising is that I haven’t completed forgotten C, it’s like riding a bike, suddenly remnants of makefile, linking with dynamic/static libraries, include files, and function pointers in C slowly came back to memory.

But I’m still hopeless in Python, I gave up because I didn’t understand what the following code does except that it does some byte shifting, but for what reason, it beats me.

def getTemp(self):
    t = self.readTemp()
    t_m = (t >> 8) & 0xff;
    t_l = t & 0xff;

    if (t_l > 99):
        t_l = t_l / 1000.0
    else:
        t_l = t_l / 100.0
    return (t_m + t_l)

I knew there has to be an easier way to invoke the sensor board library in C, but where’s the sample code? After much searching, I found a reverse-engineered sensor.so with C source code! How amazing, thanks to this bloke named Lars Christensen. It was just hot off the oven, to be specific just last Saturday 13 Sept. To get started, all I had to do was to clone his source from github. In my raspberrywifai SSH session, I issued the following command (provided you have already installed git too).

git clone https://github.com/larsch/rpi-mems-sensor.git

There was a slight problem. The mpl3115a2.h header file was empty. No worries, just go to the github repository at https://github.com/larsch/rpi-mems-sensor, click on mpl3115a2.h, copy the content, and paste it in the file in your working directory on RaspPi.

You also need to download and extract the bcm2835 library. Then just follow the steps on how to make it. This creates a static library called libbcm2835.a. Next, make the reverse-engineered sensor board library so that you create a new sensor.so, and libmemssensor.a. I was only interested in the static libraries of libbcm2835.a and libmemssensor.a so that I can compile test.c. There wasn’t a proper make statement for test so I just issued my own gcc command.

gcc -Wall -I /home/pi/rpi-mems-sensor  -L. -lmemssensor -lbcm2835 -static test.c -o test libmemssensor.a libbcm2835.a

Do not just run ./test because initializing bcm2835 requires elevated permission. If you do that you will get the following error:

pi@raspberryfai ~/rpi-mems-sensor $ ./test
bcm2835_init: Unable to open /dev/mem: Permission denied

So do this instead:

sudo ./test

And voila! I get the altimeter and temperature reading.

raw alt = 11968
alt: 46.750000, temp: 23.812500
raw alt = 11968
alt: 46.750000, temp: 23.812500
raw alt = 11968
alt: 46.750000, temp: 23.812500
raw alt = 11968

The Python code which I just couldn’t comprehend is the equivalent of the following C code:

 

double getTemp() {
    int t = MPL3115A2_Read_Temp();
    int t_m = (t >> 8) & 0xFF;
    int t_l = t & 0xFF;
    if (t_m > 0x7f) t_m = t_m - 256;
    return t_m + t_l / 256.0;
}

It still beats me what it does, but that’s alright, I just wanted this code to work within the ISS sample C application. Next I embedded this code within the DATAPROVIDER_RESULT DATA_PROVIDER_INIT function. I didn’t want to create a new data model so I reused the sample data model, and chose to assign the altimeter and temperature reading to the stringProperty, as shown in the following code:

// setting StringProperty which contains the alt and temperature
bcm2835_init();
MPL3115A2_Init_Alt();
MPL3115A2_Active();
sleep(1);
char tempinfo[1024];
snprintf(tempinfo, sizeof(tempinfo), "alt: %f, temp: %f\n", getAlt($
stringProperty = tempinfo;

Compile (also includes modifying the makefile to ensure the sensor board header files and static libraries are correctly linked), and then run the ISSAgent app, and wait for it……..

Info: Microsoft Intelligent Systems Agent Console
Info: Using device endpoint: getfityall.device.intelligentsystems.azure.net
Info: Registering device: Name=raspberrywifai, FriendlyName=raspberrywifai, ModelName=Contoso.Device, Description=raspberrywifai
Info: x-ms-activity-id: 557374f8-2342-4bfb-bb7b-ce19199fa77e
Info: Getting endpoints for device raspberrywifai
Info: x-ms-activity-id: 229227bf-483b-4914-a613-e447bb7caca5
Info: Got ingress queue namespace=uswepm01c02x0sb20-0.servicebus.windows.net, name=getfityall/raspberrywifai
Info: Got commands queue namespace=uswepm01c02x0sb20-0.servicebus.windows.net, topic path=DeviceBank1, subscription name=raspberrywifai
Info: Got per device token=[secret]
raw alt = 11152
Info: Uploading message 2e5f2634-bee2-4e88-b1c3-3ed991f25f41 for device raspberrywifai (payload={"@iot.devicename":"raspberrywifai", "value":[ {"@odata.context":"Contoso\/$metadata#Device\/$entity", "@odata.id":"Device('raspberrywifai')", "@Microsoft.IntelligentSystems.Vocabulary.V1.Timestamp":"2014-09-17T00:34:41Z", "structProperty":{"simpleField":17, "structField":{"int32Field":10, "int64Field":34, "doubleField":40.000000000000000, "stringField":"Wed Sep 17 10:34:40 2014\u000A", "guidField":"0F1E2D3C-4B5A-6978-8796-A5B4C3D2E1F0", "binaryField":"AAAA", "dateTimeOffsetField":"2014-09-17T10:34:40Z", "booleanField":true}}, "stringProperty":"alt: 43.562500, temp: 22.812500\u000A", "dateTimeOffsetProperty":"2014-09-17T10:34:40Z"} ]}) ...Info: Success

Or more specifically:

"stringProperty":"alt: 43.562500, temp: 22.812500\u000A"

Woo hoo! I got it. Now moving on to the next steps. This opens up other possibilities such as sending temperature as an event on a pre-determined interval, and also set up alarm when temperature falls below or goes above a certain value. The fun begins! 🙂

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.