WoT Project is a Web of Things middleware platform that turns a Raspberry-Pi to a WoT server. The main target of this project is to run and test WoT architecture on a simulated smart home scenario. This sample code tries to implement some basic functionalities of Smart Home application including climate, lighting and security controlling and monitoring.
At first step, you need to prepare some kind of requirements to run this code. Requirements of this project are divided into Hardware and Software as below.
- Raspberry-Pi 3 Model B or B+
- DHT11/22 sensor
- PIR motion sensor
- Some LEDs in colors of Red, Green, Blue
Since this code should run on Raspberry-Pi, the following software have to install on Raspberry. It is strongly recommended to use Raspbian as main OS on Raspberry-Pi.
When preparing requirements is done, you can clone this repository on your Raspberry-Pi and setup hardware and software.
To setup your hardware on Raspberry-Pi, follow this instruction according to schematic plan:
-
Open a Raspberry-Pi pinout to guide you that which pin has what number. Also the pinout of DHT11 and PIR motion sensor would be helpful.
-
Connect 5v VCC and GND pins of Raspberry-Pi to power rails of breadboard.
-
Set the DHT11 on the breadboard and connect its VCC and GND pins to power rails of breadbaord. Then Connect DHT11 pin-2 to GPIO-14 of Raspberry-Pi.
Notice: Do not forget that connect a pull-up resistor to pin 2 of DHT11. Otherwise, your sensor doesn’t work properly.
-
Connect GPIOs 25, 8, 7 to positive leg of your LEDs, respectively. Then connect negative LEDs legs by resistor to GND of breadboard power rails.
-
In the same way, connect alert LED to GPIO 20.
-
Connect VCC and GND pins of PIR sensor to power rails. Then according to schematic plan, connect positive leg of PIR LED with a resistor to power rails and then connect negative leg to PIR Data pin and GPIO 21. Note that PIR LED should be sited between the Data pin of PIR sensor and Raspberry-Pi GPIO.
-
Congratulation, your hardware project is ready! 👏 💃
-
Clone this repository into your Raspberry-Pi.
-
Open terminal in direction of this repository.
-
Install dependencies by running this script:
npm install --save
-
Run the code by:
sudo npm run dev
orsudo node main.js
Notice: This code must run in administrator mode. So, don't forget using
sudo
. -
If the application works well and perfect, you should see some kind of result on terminal screen.
-
Now open your browser, on the Raspberry- Pi or on the other device on the same network, and test the program by entering this API:
http://Raspberry_IP:8080/
.
Your first WoT application is running on the Raspberry-Pi. Please feel free to change the code and hardware to make your favorite WoT project.
To configure hardware or software, you should modify resources.json
file in src/resources/
. This file enables you to modify port number, sensors, and actuators information.
The below table lists all APIs that you can use to interact with WoT Middleware Platform. We recommend to use Postman or any similar options like curl
, axios
or etc. to use these APIs to create requests.
HTTP Verb | API | Request Body Value | Description |
---|---|---|---|
GET | / | {} | Index |
GET | /pi | {} | Get Raspberry-Pi status |
GET | /weather | {} | Get weather resource status |
GET | /weather/temperature | {} | Get temperature status |
GET | /weather/temperature/critical | {} | Get critical temperature value |
PUT | /weather/temperature/critical | {"value": #number} | Update critical temperature value |
GET | /weather/humidity | {} | Get humidity status |
GET | /lighting | {} | Get lighting resources status |
GET | /lighting/id | {} | Get LED value by id |
PUT | /lighting/id | {"value" = true} | Update LED value by id |
GET | /security | {} | Get security resources status |
GET | /security/motion_detector | {} | Get motion detector status |
GET | /security/motion_detector/lock | {} | Get motion security lock |
PUT | /security/motion_detector/lock | {"value": true} | Update motion security lock |
GET | /security/motion_detector/log | {} | Download motion sensor recorded log |
The main architecture of this system is based on Layered Architecture Style that is a suitable architecture for this type of IoT program. However, we leverages Node.js module structure to constructe the scripts and codes.
As depicted on system architecture, include three major layers as Application, Middleware, and Things layer that each of them is responsible to support some system functionalities. In following, we detail each layer.
In application layer, vendor developers can use APIs of this system to add, some or entire, system functionalities into their applications.
The main goal of Middleware, in such IoT architecture, is to act like a intermediate layer between Application and Things layers. Middleware layer is responsible to make functionalities of devices accessible on applications. in summary, Middleware layer get the raw data from devices by using hardware or software connector (GPIOs or network connection) and turn them into some high level (business) services. Technically, there are many types of middlewares that could be used in the IoT architecture such as, event-based, VM-based, or etc. However, we leverage Web of Things to create our Middleware layer. The architecture of Web of Things uses fundamental and technical Web technologies to develop such IoT applications that would be used in many scenarios. Therefore, we use Node.js by Express framework to develop our WoT Middleware Platform on Raspberry-Pi.
Things layer is such devices or sensors that we used in our work. In this system, Raspberry-Pi as main server, is enable us to connect these sensors (DHT11 and PIR) and actuators (LEDs) directly by using some GPIOs.
In the following we describe functionalities and responsibility of each module separately.
-
Main.js
: This module is defined to run program by starting all sub modules. -
src/server
: this module contains all servers of program including:-
HTTP.js: HTTP server is responsible for provide a web server to process requests and responses. This module is using Express.js framework.
-
Websocket.js: Websocket module creates a two way communication between server and user's browser (push server) to send value of temperature and humidity in real-time manner.
-
-
src/routes
: All functionalities of entire system are turning into some services and representing through some APIs. Route module as a dependency of HTTP.js module is responsible to create some REST APIs. So, this module separates into sub modules for representing specific services as follows: -
src/model
: This module is provide a data model for program. Of course, we using a JSON file to create our resources, however, it can be replaced by any type of data modeling. You can use a SQLight database to create and store your models. -
src/plugin
: Every piece of hardware like sensors or LEDs that we used in our system needs a script code to initialize and run in our program. So, this module responsible for containing these scripts. Each script, depending on supporting hardware, needs a specific Library or SDK in order to provide communication ability with certain hardware.
src/
plugins/
DHT11.js
leds.js
pir.js
resources/
model.js
resources.json
routes/
lighting/
LED.js
pi/
index.js
security/
MotionPir.js
weather/
temperature.js
routes.js
server/
http.js
websocket.js
security_LOG.txt
-
You can learn more about Web of Things in W3C or Web of Things official Website.
-
Of cource, if you want to learn how is Internet of Things and what its works, you can learn a lot in IBM official blog
-
If you have not any clues of how you programming with Node.js and Raspberry-Pi, W3Schools has some helpful tuterials.