CoAP using NodeJS | Californium CoAP Client

CoAP using NodeJS | Californium CoAP Client

This post shows the Constrained Application Protocol (CoAP) demonstration using Node.js

Prerequisites:
1. Node.js Software
3. A Coap Client (we are planning to use Californium, which is based on JavaFX)
4. Oracle 11 JDK is needed
5. Java FX 17 version, which is to be unzipped in any folder.

Step 1: The Source code is named coap.js

Listen to the following video for an explanation of the source code:



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();
    }
}

Step 2: Node.js

Install the Node.js software in Windows 11. Download it from https://nodejs.org/en/download
Open a command prompt and check whether Node.js is installed

C:/> node

C:/> npm install coap


Step 3 : Californium Browser

Install Oracle JDK 11 or later in your Windows 11 OS.
Install JavaFX 17 and unzip the contents to a folder, preferably D:/

I have installed it in C:\Users\tspra\Downloads\javafx-sdk-17.0.16\lib

And follow the instructions

Open two command prompts

First prompt, run the following command (run the coap.js file)

Prompt> node coap.js 

You will get the following output
CoAP

 
Second prompt, run the following command:

Go to the folder where the  cf-browser-3.6.0.jar is

Prompt> java --module-path C:\Users\tspra\Downloads\javafx-sdk-17.0.16\lib --add-modules javafx.controls,javafx.fxml -jar cf-browser-3.6.0.jar


CoAP
CF browser


In the above browser, since our code is dealing with the MIME as application/JSON, enable that in CoAP Details -> Accept -> application/json 

Then in the URL location, give the following to check the Response Window

coap://localhost:5683/co2
coap://localhost:5683/humidity
coap://localhost:5683/temperature


CoAP
CF Browser



Post a Comment

0 Comments

Featured Post

5G Network Simulation in NS3 using mmWave | NS3 Tutorial 2024