Connecting Things to Internet (Part 4) – Configuring and Connecting a Gateway Device to IoT Hub

This is the fourth installment of 5 part series Connecting Things to Internet – a step-by-step guide on how to get things connected to the Internet using low cost devices. In this fourth article, I will focus on getting Windows Azure IoT SDK installed on on your gateway device, The Rasbberry Pi 2, and configure the device in IoT hub that was create in the second article. As mentioned in the earlier article, I will be using Node implementation of Azure IoT SDK in this series.

Installing Azure IoT Node SDK

It is assumed that you already have Node(0.12.x or later) and serialport package installed in your RPi. If not done so, please refer to the previous article and have the gateway device ready before proceeding with the steps in this article. There are 2 ways of installing Azure IoT in your RPi.

Option 1: Installing IoT suite by compiling the source code.

git-url

  • Connect to your RPi using Bitvise SSH Client and clone the project to your working directory

git-clone

 

  • Now you should have the project cloned in your working directory

project-dir

  • Navigate to the node folder in your local copy of this repository (azure-iot-sdks). Run the build/dev-setup.sh script to prepare your development environment. it will take a while to complete the build. so, please be patient.

dev-setup

  • Then run the build/build.sh script to verify your installation. if the build is completed successfully you should not see any errors.

build

 

Option 2: Installing IoT suite by using npm

  • Go to your working directory and execute the following commands in the given order

     

Configuring Remote Device

After installing Windows IoT Azure SDK on your RPi, the next step of the process is to configure the device that you added in the  second article. Follow the steps to have the device configured in the IoT hub.

  • Copy the following javascript code in to your text editor and save the file, configure-device.js, in the working directory of your RPi. make sure that you replace IoT hub name, device id and the device key in the code with the values recorded from the device creation step. you may add dummy metadata from line 28 to 40. however, make sure to have a valid DeviceID at line 27.
  • Execute the following command
  • Log in to the IoT hub, select the newly added device and verify the device metadata defined in the above code.

This concludes the fourth article in the series. In the fifth and last article, I will explain how to send the sensor data captured from Arduino to the Iot Hub.

Connecting Things to Internet (Part 3) – Device Setup

This is the third article of 5 part series on how to get things connected to Internet. The first article sets the stage for the series by explaining the objectives and the background while the second article focuses on creating the heart of the IoT project, The IoT Hub. In this third article, I will go more hands-on with setting up the physical remote sensing device that will be collecting and pushing telemetry to the IoT hub. Devices generally fall into two categories;

  • Devices that are directly connected to the Internet which has more computing power to support secure IP connections like, Intel Galileo, Particle Core and Windows Phone.
  • The second type is the simple devices requiring a gateway to connect to the Internet. In this article, I will configure Raspberry Pi to act as the gateway for Arduino UNO micro-controller.

Arduino UNO + DHTXX Setup

Before I go into much details, I assume that you have some basic electronics knowledge, basic understanding of Arduino and how to download a sketch code to the controller. if not, please check out the article Getting Started with Arduino. As I mentioned in my first article, a low-cost DHT11 humidity and temperature sensor is used for the project. These sensors are very basic and slow, but are great for hobbyists who want to do some basic data collection and logging. There is also a very basic chip inside that sensor that converts analog temperature and humidity readings to digital and spits out a digital signal.

  • DHTXX has four pins;
    dht22-pinout

 

 

 

 

 

 

  • Connect the sensor to Arduino board as per the below sketch

sketch

  • Connect the board to your computer via USB cable

arduino-pc

  • Make sure Arduino IDE is configured correctly with your Arduino board by following two steps;
    • Confirm IDE is connected to the correct board. in my case it’s Arduino Uno.select-board
    • Confirm the COM port connection. based on your USB connection you will have different COM port number.select-port

 

  • Compile and burn the below Arduino code into the board. make sure that you include the DHT library in to your Arduino libraries folder before you start compiling the code. this link provides more information how to import external libraries in to your development environment.

NOTE: Certain bloggers convert raw data into a format that is compatible with the IoT hub application before it’s written to the serial port. In my opinion, micro-controllers are not optimized to carry out heavy string operations and has limited memory capacity. But, they are optimized for read/write operations. Therefore, I will write only the raw data to the serial port and pass the responsibility of string formatting to more powerful gateway device.

  • Open serial monitor(Tools–>Serial Monitor in Arduino IDE) and note the raw data being written to the serial port.

serial-output

Once you start getting raw data readings on serial port, you are ready for the next step, setting up the gateway device – the Raspberry Pi.

Raspberry Pi 2 B Setup

The next step of the device setup project is the configuration of the Internet gateway. This article assumes that you have fair bit of understanding of Raspberry Pi and it’s models, have a Raspberry Pi up and running which is connected to your wireless network (or wired), and able to connect to it using a remote client, such as Putty or Bitvise SSH Client. I am a fan of Bitvise and will be using it to remotely connect to RPi 2 B. If you don’t have the RPi setup yet, please download the OS and follow the installation guide.

For this article, I will use Node.js implementation of Azure IoT SDK. Please follow the provided steps below in the given order to get the RPi setup with Node SDK.

Installing Node on RPi

  • Update the existing OS by executing the following commands. it will take longer to update your OS if you have not done this for a while.

NOTE 1: If you wish to install the latest Node.js on your RPi, then make sure you have the latest OS, Raspbian Jessie, installed on your device. the latest Node installation will not work with older versions of the OS.

  • Download the latest Node package from Node-ARM site and install the package
  • NOTE 2: You also can install older version of Node.js if you have Raspbian Wheezy OS

  • Install older version of Node

    DISCLAIMER: My advice in this case is to install the older version of Node as Windows Azure IoT SDK does not require you to have latest Node and you always can upgrade to the latest if required. At the time of writing, serialport (Node package that is used to communicate with serial port) is compatible with Node 0.10.xx but, not 1.12.xx. If you are required to install latest Node for other projects, please make sure serialport package is compatibly with your Node version.
  • Verify the installation by typing the following command. it should give you the Node version on your RPi

 node-version

  • Node Package Manager(npm) is used in the next step to install serialport package. therefore, verify npm instillation with the following command and install npm if not installed already.

npm-version

Installing serialport package on RPi

  • In addition to setting up Node on your RPi, you also require to install serialport package that enables communication with the micro-controller serial port. please run the following command to have the serialport installed.
  • After installing serialport, run the following command to verify and validate the installation.If serialport is installed successfully, you will see the following output.

verify-serial

Configuring RPi with Arduino

Configuring RPi with Arduino to read serial data involves few steps that you have to follow in the order. First and foremost you have to detect the serial port which Arduino is connected via.

  • Connect Arduino to RPi over any available USB ports.

rpi-arduino-connection

 

  • Save the following code into get-serial-ports.js and ftp the file to /home/pi/development/iot (or any working directory of your choice) in your RPi.

     
  • Execute get-serial-ports.js file

     

run-get-serial-ports

This code will scan the active serial ports in your RPi and return the list of port names to the terminal. Based on the configuration of the device, you will see different serial port names in the format /dev/ttyXXXX and you can choose the appropriate port that is connected to Arduino. In my configuration, /dev/ttyACM0 is connected to Arduino and /dev/ttyAMA0 is connected to wireless network card. Write down the serial port name that will be used in the next step.

  • Save below code into read-serial.js, upload the file to your working directory and run the node file.

    run-read-serial-ports

If all the steps are followed in the given order you will have your very first IoT device setup that is capable of connecting to the Internet and you should now see RPi reading serial data sent from Arduino scrolling down the command window. As I mentioned earlier, I prefer not to process the data in Arduino, but send the raw data to the RPi that will take care of processing. Therefore, you will see comma separated temperature and humidity readings.

This concludes setting up Raspberry Pi and micro-controller which will be used for telemetry. In my next article, I will explain how to install, configure Windows Azure IoT SDK on your RPi and start sending data to Azure IoT hub that was created in my previous article.