Showing posts with label Internet of Things. Show all posts
Showing posts with label Internet of Things. Show all posts
, ,

Constrained Application Protocol (CoAP) using Node JS

Constrained Application Protocol using Node JS

Constrained application protocol is shortly called as CoAP and its based on Request Response Model where a browser or application will be requesting for a resource from the server. The resource would be a sensor reading like temperature, humidity, heartbeat, etc. 

For complete explanation of the source code and the demo please go through the video:


CoAP can be developed with many programming or scripting languages like:

  1. Python
  2. Node JS
  3. Contiki OS

In this article, I will be writing or explaining the source code of CoAP using Node JS and this can be demonstrated with a plugin name called Cu Plugin for Chrome Browser. There are many CoAP client available like coap, libcoap, etc in Linux OS and Cu Plugin being a easier and common approach for a client. 

You can refer the complete

This program first starts the CoAP Server and accept only the JSON format headers, else it will throw the error number '4.06'. 

Based on the request URL (req.url), the incoming requests will be handled by the server and responded with json based sensor values namely: for example, for "temperature", the server respond with {'temperature':988}. The values generated her is baaed on a random integer, in case of real sensor attached to the sensor, it can send those information as well. 

Here is the source code of the CoAP program.  The name of the file is coap.js 

var coap = require('coap');

function randomInt(min,max) {

return (Math.floor(Math.random()*(max-min) + min));
}

var portNumber=5683;
coap.createServer(function (req,res) {
console.info('CoAP device got a request from %s', req.url);
if(req.headers['Accept'] != 'application/json') {
res.code='4.06';
return res.end();
}
switch(req.url) {
case "/co2":
displayOutput(res, {'Co2':randomInt(0,1000)});
break;
case "/temperature":
displayOutput(res, {'Temperature':randomInt(-10,50)});
break;
case "/humidity":
displayOutput(res, {'Humidity':randomInt(0,100)});
break;
default:
displayOutput(res);
}
}).listen(portNumber);
console.log('CoAP Server is started at port Number 5683');

function displayOutput (res,content) {
if(content) {
res.setOption('Content-Format','application/json');
res.code='2.05';
res.end (JSON.stringify(content));
} else {
res.code='4.04';
res.end();
}
}
//End of Program
To install the packages in Linux, here is the command
$ sudo apt update
$ sudo apt install nodejs npm
$ npm install coap
The above program can be run using the command 
$ node coap.js 
The request are  
coap://localhost:5683/co2
coap://localhost:5683/temperature
coap://localhost:5683/humidity

The following will be the output
{'Co2':899}
{'Temperature':45}
{'Humidity':67}
This will start the server and the client can browse the server through the plugin called Cu plugin in Google Chrome Browser. There is a small work around to do the customisation. Please follow the video for enabling the Cu plugin in Google Chrome browser. 

Here is the output screen shot of the above request.
CoAP
CoAP Client (Cu) plugin


, , ,

Virtual Sensor implementation in Contiki NG OS

Native Temperature, Humidity and Pressure Sensor in Contiki NG 

Implementation of Three sensors namely temperature, humidity and pressure sensor in contiki NG OS.

We need four files namely

Step 1: Create a Folder called temperature/ in the folder contiki-ng/examples/
and then copy paste the following four files to the above folder.

// mytemp.h
#ifndef MYTEMP_H
#define MYTEMP_H
struct Sensor {
 char name[15];
 float value;
};
struct Sensor read_temperature();
struct Sensor read_humidity();
struct Sensor read_pressure();
#endif


// mytemp.c
#include "mytemp.h"
#include <string.h>
#include <stdlib.h>

float random_value(float min, float max)
{
 float scale = rand() / (float) RAND_MAX;
 return min + scale * (max - min);
}

struct Sensor read_temperature()
{
 struct Sensor temp;
 strncpy(temp.name, "Temperature", 15);
 temp.value = random_value(0, 35);
 return temp;
}
struct Sensor read_humidity()
{
 struct Sensor humdidty;
 strncpy(humdidty.name, "Humidity", 15);
 humdidty.value = random_value(40, 80);
 return humdidty;
}

struct Sensor read_pressure()
{
 struct Sensor pressure;
 strncpy(pressure.name, "Pressure", 15);
 pressure.value = random_value(30, 78);
 return pressure;
}


// demo.c
#include "contiki.h"
#include "sys/etimer.h" //etimer Event Timer
#include "mytemp.h"
#include <stdio.h>

PROCESS(sensor_process, "Sensor process");
AUTOSTART_PROCESSES(&sensor_process);

static struct etimer timer;

PROCESS_THREAD(sensor_process, ev, data)
{
 PROCESS_BEGIN();
 printf("Native Sensor\n");
 
while(1) {
 etimer_set(&timer, CLOCK_SECOND*2);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&timer));

 struct Sensor temp = read_temperature();
 printf("%s: %.2f\n", temp.name, (double)temp.value);

 struct Sensor hum = read_humidity();
 printf("%s: %.2f\n", hum.name, (double)hum.value);

struct Sensor pressure = read_pressure();
 printf("%s: %.2f\n", pressure.name, (double)pressure.value);
  
printf("------------\n");
 }
 PROCESS_END();
}



#Makefile
CONTIKI_PROJECT = demo
all: $(CONTIKI_PROJECT)

PROJECT_SOURCEFILES += mytemp.c

CONTIKI = ../../
include $(CONTIKI)/Makefile.include

To run this File

Open a Terminal  and go to contiki-ng/examples/temperature/

$] cd contiki-ng/examples/temperature
$] make TARGET=native 
$] ./demo.native

Please see the screenshot below:
Sensors
Sensors in Contiki NG

Sensors in Contiki NG
Sensors in Contiki NG




, , ,

Download VM Image of NS3 and Contiki NG

Dear Learners,

Good day. I have created a Virtual Machine Image with all the software modules in built in this Ubuntu 18.04 OS. You just need to do the following steps. 

Software Included in this Virtual Machine

  • network Simulator 3
  • Gnuplot
  • Wireshark
  • Contiki NG
  • Java Compiler
  • Tracemetrics
  • COOJA Simulator for IoT

Assume You have a Windows Machine. 

Step 1: 

Download the VMWare Workstation Player from this link

https://www.vmware.com/go/getplayer-win

Install the Software in your Machine.

Step 2: 

Download the VM Image from the following link, the total size of the file is around 4.6GB.

http://www.mediafire.com/file/3itf14zxb7gclp4/Ubuntu1804.rar

Step 3: 

Extract the above rar file into your D drive or E Drive Preferrably (As the total size may exceed 15GB). So do not extract it in the C Drive.

Step 4: 

Open your VMWare Workstation player and Click "Open a Virtual Machine" as shown in the Figure given below.

VMware


And then Go to the place where you extracted the VM Image. Only One file will be visible as indicated below


Open the file and you will get a screen like this

Vmware


Just Click the "Play Virtual Machine", It may ask whether you Moved It or Copied it. Select "I Copied It"

And then it will boot to the Ubuntu OS. Username is pradeepkumar and Password is a1b2c3d4

Thats It !!!!

For any queries, Comment below. 

, , , , ,

Ns2 and Ns3 Video Tutorials with Free Source Code

This post shows you the way to download, learn and watch source  codes and their working in ns3 and ns2.
Everyday thousands of visitor visits this blog and search for source code and explanation. Since 2011, the blog is delivering information across the globe.

Now this era is on Videos and online tutorials. nsnam is happily created a Video Channel called "Engineering Clinic" in Youtube and deliver video lectures with free source codes for all applications.

Engineering Clinic
Engineering Clinic
Subscribers of this blog are requested to subscribe to the video channel given below

https://www.youtube.com/tspradeepkumar 

Here are some Playlists that are important to the viewers of this blog.

NS2 Tutorials - https://www.youtube.com/watch?v=jRS0-hNk-Gw&list=PLX6MKaDw0naZBi0R-6_IAuYA9Yk_yuyXp

NS3 Tutorials - https://www.youtube.com/watch?v=LPOl81VIDG4&list=PLX6MKaDw0naZILVYjo8_quA4JI8Jz-idL

IoT Tutorials - https://www.youtube.com/watch?v=MRTJdC4ye9A&list=PLX6MKaDw0nab-VNTAfQkFAtl2zSv-N08N

MOODLE Tutorials - https://www.youtube.com/watch?v=ch5zs6oW1cM&list=PLX6MKaDw0naYBT-eabfOYYnO4xOXmqeJh


T S Pradeep Kumar
,

Routing Protocol for Lossy Networks (RPL)

RPL protocol for Internet of Things is called as Routing Protocol for Low Power Lossy Networks. The following video depicts the use of RPL in Contiki OS with COOJA Framework.

The simulation was carried out using sky motes.  These are the modules that are used as firmware.

~contiki-2.7/examples/ipv6/rpl-border-router/border-router.c (This firmware is used for one of the sky mote)

~contiki-2.7/examples/skywebsense/skywebsense.c (this is programmed with other 5 nodes)

Please refer the video for more details.


T S Pradeep Kumar
, ,

Contik OS - A Complete Video

Contiki OS is an operating system for wireless sensor networks. You can use this OS to generate object code for supported sensors.

Please look at this following video that tells

  • How to use Contiki OS
  • How to deploy the OS in to motes
  • How to create our own application using Protothreads
  • How to create a target code for a given Sensor node and etc...
Please comment below for any queries related to Contiki OS


T S Pradeep Kumar
,

Internet of Things and 6LoWPAN

This post tells you about the concepts behind
  1. Internet of Things
  2. Why we need IoT
  3. What Demands IoT
  4. What are the features of 6LoWPAN
  5. 6LoWPAN characteristics 
  6. Impact of 6LoWPAN
  7. Open Network for IoT
  8. 6LoWPAN supporting Operating Systems
  9. Contiki OS, RIOT OS and Tiny OS 
  10. Comparison of various OS for IoT
  11. A simple demo of CoAP Protocol 

Please see this video 
2016-11-23 15.00 Internet of Things and 6LoWPAN from Techgig on Vimeo.


See the Video on Constrained Application Protocol (CoAP) using Contiki OS


T S Pradeep Kumar
,

RIOT OS (An Operating System for IoT)


Introduction

The Internet of Things (IoT) is used with heterogeneous devices. They range from 8 bit to 32 bit microcontrollers from different manufacturers. Traditional Operating system (OS) or the conventional embedded OS may not suit the requirements that these tiny devices demand (low power, small in size and low memory foot print). RIOT Operating system is a free and open source arachtiecture which is friendly to IoT applications and proved to be a perfect OS for IoT Systems.
It started in the year 2008 as an Operating system for Wireless Sensor nodes, later the OS is fine tuned for IOT systems.

Features of RIOT

The OS is actively developed and maintained.
  • There are no new programming environments, C or C++ can be used directly with existing tools like gcc, gdb, etc.
  • Less hardware dependent code
  • Supports 8,16 ad 32 bit microcontroller platforms
  • Energy efficieny is maintained
  • less interrupt latency, so real time capability is ensured
  • multi threading is enabled
  • Supports the entire Network Stack of IoT (802.15.4 Zigbee, 6LoWPAN, ICMP6, Ipv6,RPL, CoAP, etc)
  • Both static and dynamic memory allocation
  • POSIX complaint (Partial)
  • All output can be seen the in terminal if hardware is not available, however there is a visualization tool called RIOT-TV is provided 
  • MSP430 
  • ARM7 
  • Cortex-M0, M3 ,M4
  • x86, etc
  • Radio receivers
  • Environmental sensors like humidity, temperature, pressure, alcohol gas sensors
  • accelerometer
  • gyroscopes
  • ultrasonic sensors, light and servo motor

Features
Contiki OS
Tiny OS
RIOT OS
Minimum RAM and ROM needed
<2kb,
<30kb
<1kb
<4kb
~1.5kb
`5kb
C and C++ Support
Partial
No support (nesc)
Full support
Multi threading
No (protothreads are used here)
No
Yes
Real Time
No
No
Yes
  • msp 430 GCC compiler 
  • msp 430 libraries
  • avr utils,
  • arm based gcc 
  • gcc multilibrary
  • etc

Architecture, Board and Driver Support

It supports various architecture like
and it supports the native port, where one can simulate the output within the OS it was running. So RIOT is supported in Linux as well as OS X.
There are in built drivers for the following sensors (without a need of hardware, these sensors can be modeled in native mode also)
Most of the sensor boards have support from this OS like TelosB, ST Microcontroller sensor boards, Zolertia, MSP 430 boards,Aruduino sensors, Atmel, and the list is so huge.
It also support virtualization that the code and application can run as a simple unix process. It also uses wireshark for packet sniffing.

Comparison with Contiki OS and Tiny OS

There are other two OS called Contiki OS (Already featured in OSFY) and Tiny OS which are suitable for IoT applications. But RIOT takes an upper hand in terms of memory usage and support.
The following table shows the comparision between these OS
Table 1: Comparison of various OS for IoT
Figure 1 and Figure 2 represents the block and Network stack diagram of IoT. Except for RPL, all the modules have the support from RIOT OS (for RPL alone, there is a partial support from RIOT)
Network Stack for IoT
Network Stack for IoT

Component of IoT
Component of IoT

Installation of RIOT in LinuxOS Used for installing : Linux Mint 17.2 and Ubuntu 14.04.2Architecture used: 64 bit OSInstalling RIOT needs lot of packages that are prerequisites like
Assume if you have installed just now, follow the steps to install

pradeepkumar $] sudo apt-get update
pradeepkumar $] sudo apt-get install libc6-dev-i386 build-essential binutils-msp430 gcc-msp430 msp430-libc libncurses5-dev openjdk-7-jre openjdk-7-jdk avrdude avr-libc gcc-avr gdb-avr binutils-avr mspdebug msp430mcu autoconf automake libxmu-dev gcc-arm-none-eabi openocd git bridge-utils gcc-multilib socket
The OS can be downloaded as a zip file from github (https://github.com/RIOT-OS/RIOT/archive/master.zip) , else you can clone it using the following command
pradeepkumar $] git clone https://github.com/RIOT-OS/RIOT.git

The folder RIOT/ or RIOT-Master/ contains various subfolders
->boards  (This folder has all the drivers for the boards that are supported by the OS)
->cpu (this folder contains the architecture supported by RIOT)
  ->cores  (Core of the OS like kernel, modules, etc)
->doc (Documentation)
->examples (some examples like hello word, border router, Ipv6 formation, etc)
->drivers (drivers for all the sensors)
etc
Simple Example
These type of Operating systems can be learnt only through the examples given within the source code of the application. This OS has some examples in the examples/ folder and one can go through the source code and run it using the following method. There are more examples available in the github of RIOT (https://github.com/RIOT-OS)
If sufficient board or hardware is not available, then the code can be run in the terminal itself (native mode)
To run the hello-world example from the examples folder

pradeepkumar $] cd RIOT-Master/examples/hello-world
pradeepkumar $] make flash
pradeepkumar $] make term 
Running a hello-world example
Running a hello-world example

hello world application run in the terminal
hello world application run in the terminal

-----OUTPUT-----
/home/pradeepkumar/RIOT-master/examples/hello-world/bin/native/hello-world.elf
RIOT native interrupts/signals initialized.
LED_GREEN_OFF
LED_RED_ON
RIOT native board initialized.
RIOT native hardware initialization complete.
main(): This is RIOT! (Version: UNKNOWN (builddir: /home/pradeepkumar/RIOT-master))
Hello World!You are running RIOT on a(n) native board.
This board features a(n) native MCU.
Press Ctl+C to quit the application
--------------------------CODE-----------------------------------
make flash will compile and show the text and data size occupied by the memory. Since this application is run under native mode, the output can be viewed using make term command.
Networking Applcation in RIOT
There are few number of applications avaiable in RIOT. One such application is a socket application which works with the POSIX API with UDP as the protocol. All the relevant source codes are avaialble in the directory itself
----------------------------------CODE--------------------------------------
pradeepkumar $ ] cd RIOT-Master/examples/posix-socket/
pradeepkumar $ ] make flash
pradeepkumar $ ] sudo make term
output of the above command
/home/pradeepkumar/RIOT-master/examples/posix_sockets/bin/native/posix_sockets.elf tap0
RIOT native interrupts/signals initialized.
LED_GREEN_OFF
LED_RED_ON
RIOT native board initialized.
RIOT native hardware initialization complete.
main(): This is RIOT! (Version: UNKNOWN (builddir: /home/pradeepkumar/RIOT-master))
RIOT socket example application
All up, running the shell now
> help

posix in riot
POSIX Application

Command
---------------------------------------
udp                  send data over UDP and listen on UDP ports
reboot              Reboot the node
ps                   Prints information about running threads.
random_init          initializes the PRNG
random_get          returns 32 bit of pseudo randomness;
ifconfig             Configure network interfaces
txtsnd               send raw data
ncache              manage neighbor cache by hand
routers              IPv6 default router list
Figure 5 shows the above configuration: Routers can be configured, txt messages can be sent, running processes details can be found out, etc can be done using the application
--------------------------CODE----------------------------

Conclusion
There are only limited number of Operating systems available for the IoT sensors and among these OS, RIOT really has a well documented API with huge support for most of the boards, architecture and sensors. The main advantage being, RIOT is actively developed and maintained, there is a incremental version every month.  Students and electronic hobby enthusiasts are using arduino and Raspberry Pi for their IoT applications these days, RIOT really help these boards and sensors to achieve low memory footprint and low energy efficiency without any compromise on the performance of the IoT Systems.

NB: This article is published in the "Open Source for You Magazine" February 2016 Edition.
,

Compiling Contiki OS from Source - an IOT Perspective

Installation and usage of Instant Contiki OS is already featured in my blog post. 

This post tells you how to compile contiki OS from source and make use of the maximum benefit of IOT.

Internet of Things is a new word in the technology and every corporate to healthcare talks about this. Being a researcher or a learner, the first step towards the learning of IOT starts with implementing an application. In this context, Contiki OS with cooja is a framework that can be installed in Linux to program sensor motes for any IOT application.

OS Used for testing: Ubuntu 14.04.3

Assuming you installed Ubuntu, here are the commands you can try now

$] sudo apt-get update
$] sudo apt-get install build-essential binutils-msp430 gcc-msp430 msp430-libc libncurses5-dev openjdk-7-jre openjdk-7-jdk avrdude avr-libc gcc-avr gdb-avr binutils-avr mspdebug msp430mcu autoconf automake libxmu-dev





contiki
Contiki OS

Once installed, download the contiki OS from Github.
https://github.com/contiki-os/contiki/releases
There are various releases, the latest one being contiki-3.0, but if you have sensor motes of more than 2 or 3 years old, then you may prefer contiki-2.7

Once downloaded, unzip the contents of contiki

$] tar zxvf contiki-2.7.tar.gz 
$] cd contiki-2.7/tools/cooja
$] ant run 
This will open the cooja window (Which is based on eclipse editor) and you can start working on the project. See the screenshot below

cooja
Cooja
If you are planning to use contiki of older version and newest version, there is not need to download any additional dependencies. You can simply download the tool and use the ant run command to open Contiki.
T S Pradeep Kumar