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.
- Connect the sensor to Arduino board as per the below sketch
- Connect the board to your computer via USB cable
- Make sure Arduino IDE is configured correctly with your Arduino board by following two steps;
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include "DHT.h" #define DHTPIN 2 // what digital pin we're connected to #define DHTTYPE DHT11 //DHT chip type DHT dht(DHTPIN, DHTTYPE); String strNL, strOut, strDelim; void setup(void) { Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } dht.begin(); strDelim = String(";"); strNL = String("\r\n"); strOut = String(); delay(2000);//Wait before accessing Sensor } void loop(void) { float temperature = dht.readTemperature(true); float humidity = dht.readHumidity(); if (isnan(humidity) ) { humidity = 0.00; } if (isnan(temperature)) { temperature = 0.00; } // print string for temperature, separated by line for ease of reading // sent as one Serial.Print to reduce risk of serial errors strOut = temperature + strDelim + humidity + strNL; Serial.print(strOut); //Serial.println(temperature); //Serial.println(humidity); delay(3000); //slow down the output so it is easier to read } |
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.
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.
12sudo apt-get update -ysudo apt-get upgrade -y
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
-
12wget http://node-arm.herokuapp.com/node_latest_armhf.debsudo dpkg -i node_latest_armhf.deb
NOTE 2: You also can install older version of Node.js if you have Raspbian Wheezy OS
- Install older version of Node
12wget https://node-arm.herokuapp.com/node_archive_armhf.debsudo dpkg -i node_archive_armhf.deb
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
1node -v
- 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.
1npm -v
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.
1npm install serialport - After installing serialport, run the following command to verify and validate the installation.If serialport is installed successfully, you will see the following output.
1node -e "console.log(require('serialport'));"
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.
- 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.
1234567891011'use strict';var sp = require("serialport");var port;sp.list(function (err, ports) {ports.forEach(function(port) {console.log(port.comName);console.log(port.manufacturer);console.log(port.pnpId);});});
- Execute get-serial-ports.js file
1node get-serial-ports.js
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.
1234567891011121314151617var sp = require("serialport");var SerialPort = sp.SerialPort;var serialport = new SerialPort("/dev/ttyACM0", { //serial port name identified in the previous stepbaudrate: 9600,parity:'none',stopbits:1,bytesize:8,parser:sp.parsers.readline("\r\n")});serialport.on('open', function(){console.log('Serial Port Opend');serialport.on('data', function(data){console.log(data.toString());});});
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.
2 thoughts on “Connecting Things to Internet (Part 3) – Device Setup”