Get started with Raspberry Pi (Nodejs)
In this tutorial, you will learn the basics of working with Raspberry Pi (that runs the Debian-based Raspbian). You will then learn how to connect your devices to QNAP NAS using QIoT Suite Lite.
Please ensure your Raspberry Pi and NAS are on the same LAN.
Lesson 1: Configure your devices
In this lesson, you configure your Raspberry Pi device with an operating system, set up your development environment, and deploy an application to Raspberry Pi.
1.1 What will you need
- Raspberry Pi
- SD card (an 8GB class 4 SD card is recommended) with NOOBS installed.
- HDMI display.
- USB keyboard and mouse.
- Power Supply: The Pi requires a USB micro power supply with at least 2A at 5V for the Model 3B, or 700mA at 5V for earlier models.
1.2 Set up Raspbian
- Insert the SD card containing extracted NOOBS files into the Raspberry Pi
- Connect the keyboard, mouse, and HDMI display to the Raspberry Pi. Plug in the power adapter. The Raspberry Pi does not have a power switch, and will automatically power on once the adapter is connected.
- A menu will ask what OS to install and what language and keyboard layout to use. Check the box next to Raspbian and click "Install".
- The installation process may take up to 20 minutes, and will restart to the Raspbian desktop when complete.
- Configure Wi-Fi – In the Desktop environment locate the Wireless Network icon in the top-right menu and click the icon to view a list of available Wi-Fi networks.
- To connect to your Raspberry Pi from another device, you must know the Pi’s IP address. Using the terminal (Ctrl + Alt + t to open Terminal), enter hostname -I.
- More Raspberry Pi setup guides can be found at: https://www.raspberrypi.org/learning/software-guide/
1.3 Install Nodejs and required libraries
- Press Ctrl + Alt + t to open Terminal.
- Install the latest nodejs
pi@raspberrypi:~ $ sudo apt update
pi@raspberrypi:~ $ sudo apt install nodejs
- Install MQTT library and Promises library by entering the following commands.
pi@raspberrypi:~ $ npm install mqtt
pi@raspberrypi:~ $ npm install q
Lesson 2: Create your device in QIoT Suite Lite
In this lesson, you will provision your QNAP QIoT Suite Lite software, and create your first device in QIoT Suite Lite.
2.1 Install QIoT Suite Lite
- QIoT Suite Lite can be installed from the App Center.
- Launch and log in QIoT Suite Lite. The default username and password is “ admin”.
2.2 Create a new IoT application
IoT Applications are a combination of Things, Rules, and Dashboards. We recommend that you first create a “Things” in IoT Application. This IoT Application allows you to keep a record of all of the devices that are connected to your NAS.
Rules make it possible to build IoT applications that gather, process, analyze and act on data generated by connected devices. Rules can apply to data from one or many devices, and it can take one or many actions in parallel.
Dashboards turn your data into analytics and reports that provide real-time insights.
All these elements provide user a complete IoT Application environment.
If this is your first time using QIoT Suite Lite, use QIoT Quick Setup to guide you through a quick setup of an IoT application. After reading the introduction, click Next.
In the IoT Application tab, enter a name for your IoT Application. Rule and Dashboard names will be generated automatically based on the IoT Application name. Click Next.
In the Thing tab, click Add. Enter a name for your device and provide the device information (such as serial number and manufacturer) in Add attribute. If your device is already supported by QIoT, choose a pre-defined Thing Type. Click Add to add the device.
Enter the IP address, User Name, and Password of your device and click the Connect icon. QIoT Suite Lite will check the connection with the device. After the connection is verified, click Next.
In the Resource tab, click Add Resource. Enter the name and ID for the sensor in Resource Name and Resource ID. Please note that the Resource ID will be used to create a Topic in the QIoT Broker. This ID should be unique for the device and no duplicates should be allowed for the same device.
Click Next after you add all the resources (sensors) on your device.
If you want to embed QIoT dashboard widgets into web pages or access QIoT APIs from third-party applications, go to the API Keys tab and click Generate New API Key. Click Done.
Your IoT application will now be created. Node.js sample codes and related files (certificate, resource information) will be uploaded to the specified path on your device.
A sample dashboard will be created.
Go to the Rule tab to define the flow or rules on how to process the data sent from the device, and how to present in a dashboard.
Lesson 3: Connect your device to QIoT Suite Lite using MQTTS
In this lesson, you will generate a certificate from QIoT Suite Lite, download the SDK, and connect Raspberry Pi to QIoT Suite Lite.
3.1 Generate certificate and Resource information Inside QIoT Suite.
- On your Raspberry Pi, open the Terminal.
- Go to the directory where QIoT Suite Lite uploaded sample codes to.
- Run the sample application.
pi@raspberrypi:~ $ cd bundle/nodejs/device/raspberry/examples/DHT11
pi@raspberrypi:~ $ node qiot-DHT11-example.js
Lesson 4: Integrate Power BI
4.1 Get your first Power BI account
- Sign up for a free PowerBI account at https://powerbi.microsoft.com/en-us/
- After registering, you will see the following page:
4.2 Setup your streaming dataset API
Go to “Datasets -> Streaming datasets” on the left menu, and click “+ Add streaming dataset” in the top-right corner.
Select “API” as your source of data, and click “Next”.
Define your values from stream, and you will get a JSON result in the textbox. We will use this JSON code to push data to the IoT application. Click “Create” to finish.
Once you create your data stream, you get a REST API URL which IoT applications can call using POST requests to push your live data to the streaming data dataset you created.
4.3 Configure Node-RED’s nodes in IoT application
Create an IoT application in QIoT Suite. The following is your first Node-RED flow, and then you can start creating your own IoT flow. You can learn more about Node-RED at https://nodered.org/
Before you start pushing live data to Power BI. We need a “function” node to convert IoT data to a streaming data dataset. Here you can replace msg.payload to your JSON dataset.
We need an “http request” node to help us to push live data to Power BI. Just drag and drop the “http request” node and connect to the tail of the “function“ node.
Copy and paste the REST API URL that you got from the Power BI console, and set http method to POST. Click “Deploy” to save changes.
Your Node-RED flow will look like below.
4.4 Add tiles to display real-time data
Go to “Dashboards +” to create your first dashboard, then click “+ Add tile” to configure a widget.
Select “CUSTOM STREAMING DATA” and click “Next”.
Select datasets and click “Next”.
You will have a streaming dataset to work with, and you can get a real time gauge that looks as following.