My IoT Exploration – Part 3 – Sending Data to Cloud

This is my third post about exploring Internet of Things, previous posts can be found here:

To be able to see what my Raspberry Pi is monitoring when I’m not home, I need a hosted service that I can send my data to. I decided to go with Microsoft Azure IoT Hub.

Registering my Raspberry Pi in Azure IoT Hub

I used the Azure Portal to set up the IoT Hub service like this:

Registering Azure IoT Hub in Azure Portal

Once it was created I went back to the console to get a connection string for my Raspberry. This can be done from any computer, by the way.

npm install -g iothub-explorer

I copied the connection string to IoT Hub that can be found in the portal and created a connection string to my device (that I decided to call RaspberrySenseHat) like this.

iothub-explorer [Connection string to IoT Hub that you find in the portal] create RaspberrySenseHAT --connection-string

When this is done I have a connection string to the device, that I will use later.

Creating Raspberry connection string

Creating a data relay in NodeJS

The next step was to create a NodeJS application that should receive data from my Python program, do some optional logging and pass it on to Azure. I hadn’t written a single application in NodeJS before, but I have done my fair share of JavaScript, so I figured I’d better use TypeScript for this to make sure I maximise the learning 🙂 I found Tony Sneed’s Yeoman Generator for TypeScript Projects Using Visual Studio Code, that was a really good starting point. It includes tasks in Gulp for building, running tests etc, so it’s great if you’re using Visual Studio Code (another tool I wanted to get familiar with).

My full implementation can be found in this GitHub project: Receiving environmental data and pushing it to Azure IoT Hub. The readme contains installation instructions, so I’m not going to repeat all of those here. I will however point out a few mistakes I made along the way.

Deployment

One newbie mistake I made early on was to try to move the entire project folder from my Windows machine to the Raspberry Pi. This causes two problems, firstly the node_modules folder tends to be deeper than Windows can handle (moving or copying the folder). See Why does the 260 character path length limit exist in Windows?  Secondly, the Node modules should be built on the target machine. In this case I was even developing on a x64 Windows 10 machine and then running it on a Linux ARM machine, the Raspberry Pi.

"devDependencies": {
  "browser-sync": "^2.11.0",
  "del": "^2.2.0",
  "es6-module-loader": "^0.17.10",
  "event-stream": "^3.3.2",
  "glob": "^6.0.4",
  "gulp": "^3.9.0",  
  "tslint": "^3.2.1",
  "tslint-stylish": "^2.1.0-beta",
  "typescript": "^2.0.3",
  "yargs": "^3.31.0"
},
"dependencies": {
  "azure-iot-device": "^1.0.13",
  "azure-iot-device-amqp": "^1.0.13",
  "azure-iot-device-amqp-ws": "^1.0.14",
  "azure-iot-device-http": "^1.0.14",
  "azure-iot-device-mqtt": "^1.0.13",
  "body-parser": "~1.0.1",
  "express": "^4.14.0"
}

To avoid this and also to avoid having to check everything into source control, pulling it down and building it locally for every single change, I looked more carefully at the dependencies. If you look at the package.json file, there are dependencies and devDependencies. Make sure that only the things needed to run the application is listed in dependencies. Also, set the environment variable NODE_ENV=production in the target machine (the Raspberry Pi in this case) and make sure that package.json is included in the build output folder (dist in my project). Then you can copy the build output folder to the target machine and simply run npm install.

Green light

At this point I’m successfully sending data to Azure and the LED on the Raspberry Pi shows a green light.

Next post: My IoT Exploration – Part 4 – Presenting the Data

No matching posts found. You can use wildcards and search only in titles, e.g. title:iot
Loading search index, please try again in a few seconds.
Index has completed downloading. Please search again.