Using Microsoft Azure IoT With Resin.io
Resin.io brings container-based development to IoT. See how to connect it, via Microsoft Azure, to your device - in this case, a Raspberry Pi.
Join the DZone community and get the full member experience.Join For Free
Microsoft has published an open-source SDK for their Azure IoT Gateway that enables gateway-connected devices to communicate with Azure IoT Hub. It is also really simple to wrap this SDK in a resin.io project so that remote devices using the Azure IoT service can be managed safely and easily. Resin.io is a platform that brings modern container-based development and deployment to the Internet of Things. In this example, we will show a simple scenario in which resin.io updates modules in the Azure IoT Gateway SDK.
I started by creating a resin.io application. In my case, I used a Raspberry Pi, but this could be any supported device. Information on creating a Resin application is available in our docs.
Once I created my application, I added a few components: a Dockerfile template to build and run the "hello world" application and a couple of scripts to run the application and display its output. (In my case I also added a configuration file,
hello_world_lin.json, to control where the output gets logged, but not including it and using the default path will work just as well.)
The Dockerfile downloads the SDK from Microsoft's GitHub repository and builds it before deploying it to a device:
FROM resin/%%RESIN_MACHINE_NAME%%-debian RUN apt-get update \ && apt-get install -y git build-essential cmake pkg-config uuid-dev libglib2.0-dev \ curl libcurl4-openssl-dev libssl-dev valgrind expect RUN git clone --recursive https://github.com/Azure/azure-iot-gateway-sdk /app RUN mkdir /app/build WORKDIR /app/build RUN cmake /app RUN make -j $(nproc) COPY . ./ CMD /app/build/start.sh
This means that I don't even need to have a copy of the SDK locally to build the app as the Resin build system will get it for me.
Once this is done, I can do a git push resin master and start the build process. This takes some time as the SDK is quite large.
Once it is built it's automatically deployed on the device. You'll see that the output from the default "hello world" application is displayed in the Resin dashboard:
Now that the application is built and running on my Raspberry Pi, let's see what the process of modifying it looks like. In this case, I simply want to change the message being output from the "hello world" application to indicate that I am using resin.io.
To do this, I took advantage of the fact that the Resin build server has cached my earlier build image so that I don't have to rebuild the entire SDK. I added a few lines to my Dockerfile:
FROM resin/%%RESIN_MACHINE_NAME%%-debian RUN apt-get update \ && apt-get install -y git build-essential cmake pkg-config uuid-dev libglib2.0-dev \ curl libcurl4-openssl-dev libssl-dev valgrind expect RUN git clone --recursive https://github.com/Azure/azure-iot-gateway-sdk /app RUN mkdir /app/build WORKDIR /app/build RUN cmake /app RUN make -j $(nproc) COPY . ./ # New lines: RUN sed -i 's/from Azure IoT Gateway SDK simple sample/from Azure IoT Gateway SDK updated by resin.io/' /app/modules/hello_world/src/hello_world.c RUN cmake /app RUN make -j $(nproc) CMD /app/build/start.sh
The new lines simply change the string displayed in the "hello world" application and rebuild the SDK. Because most of the SDK is already built, this time it goes much faster:
And looking at the Resin dashboard, I see that the changes have been deployed to my Raspberry Pi!
This scenario is quite simple, though we have customers working with resin.io to actively manage highly intelligent gateway applications for edge analytics, data filtering, building automation workflows and more.
That's all there is to it! Now you can combine the management and update functionality of resin.io with the data collection and intelligence of the Azure IoT platform. Go out and make cool things!
Published at DZone with permission of Gergely Imreh, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.