Giter VIP home page Giter VIP logo

coap-shell's Introduction

CoAP Shell

Download

CoAP is a RESTful web transfer protocol specialized for use with constrained nodes and constrained networks in the Internet of Things (IoT).

The CoAP Shell provides an interactive, command line interface for interacting with CoAP enabled servers. It supports the coap: and coaps: schemas (e.g. UDP and DTLS). It also can manage your IKEA TRÅDFRI smart lights set ;)

The CoAP Shell is build on top of the Spring Shell, Californium (Cf) and Scandium (Sc) projects. It is a SpringBoot application, that builds into a single, self-executable jar and runs on any Java8+ environment.

CoAP Shell Demo

Features

  • Plain coap: and secured coaps: endpoints (e.g. UDP and DTLS transports).

  • CoAP GET, PUT, POST and DELETE methods.

  • CoAP Resource Observing.

  • CoAP Resource Discovery. Filters by href, ct, rt, obs …​

  • Synchronous and Asynchronous (--async argument) message exchanges.

  • Confirmable and Non-Confirmable message exchange.

  • TAB auto-completion for commands and arguments.

  • Extensive commands help (type help).

  • Plugable key/trust stores and credentials.

  • SpringBoot, self-executable jar, running in any Java 8+ environment.

  • Basic support for IKEA Tradfri Gateway.

The CoAP Shell Video highlights some of the features:

CoAP Shell video

Quick Start

  • Get a pre-build coap-shell.jar or build one yourself following the instructions further down.

  • Then start the shell:

java -jar ./coap-shell-1.1.1.jar
  _____     ___   ___     ______       ____
 / ___/__  / _ | / _ \   / __/ /  ___ / / /
/ /__/ _ \/ __ |/ ___/  _\ \/ _ \/ -_) / /
\___/\___/_/ |_/_/     /___/_//_/\__/_/_/
CoAP Shell (v1.1.1)
For assistance hit TAB or type "help".

server-unknown:>
Caution
If you see an exception like: Caused by: java.lang.NumberFormatException: For input string: then you have to remove the existing spring-shell.log file in the same directory.
  • Connect to a CoAP server (such as coap://californium.eclipseprojects.io:5683/ or coap://coap.me)

server-unknown:>connect coap://californium.eclipseprojects.io:5683/
available
coap://californium.eclipseprojects.io:5683/:>
  • Discover the available CoAP resources:

coap://californium.eclipseprojects.io:5683/:>discover --query href=/*

┌─────────────────┬──────────────────┬────────────────────────────────────┬──────────────┬─────────┬────────────────┐
│Path [href]      │Resource Type [rt]│Content Type [ct]                   │Interface [if]│Size [sz]│Observable [obs]│
├─────────────────┼──────────────────┼────────────────────────────────────┼──────────────┼─────────┼────────────────┤
│/.well-known/core│                  │                                    │              │         │                │
│/large           │block             │                                    │              │1280     │                │
│/link1           │Type1, Type2      │                                    │If1           │         │                │
│/multi-format    │                  │text/plain (0), application/xml (41)│              │         │                │
│/obs             │observe           │text/plain (0)                      │              │         │observable      │
                                                        .....
  • Get resource data

coap://californium.eclipseprojects.io:5683/:>get /multi-format --accept application/xml

----------------------------------- Response -----------------------------------
GET coap://californium.eclipse.org/multi-format
MID: 31291, Type: ACK, Token: [0a10eaafaf3d024f], RTT: 123ms
Options: {"Content-Format":"application/xml"}
Status : 205-Reset Content, Payload: 63B
----------------------------------- Payload ------------------------------------
<msg type="CON" code="GET" mid=31291 accept="application/xml"/>
--------------------------------------------------------------------------------
  • Use help to the available commands and how are they used.

  • Use TAB for command and argument auto-completion.

Caution
If you see org.eclipse.californium.elements.EndpointMismatchException error message then your DTLS session has expired. You will have to re-connect again.

IKEA TRÅDFRI Gateway Support

  • Generate Gateway pre-share key Use the ikea gateway key to register a new account (e.g. identity + secret) to your IKEA Gateway:

server-unknown:>ikea gateway key --ip 192.168.178.150 --identity myIkeaGatewayIdentity --security-code <Gateway Code Label>

-------------------------------- CoAP Response ---------------------------------
 MID    : 58318
 Token  : [60d1dcf80d8eb84f]
 Type   : ACK
 Status : 201-Created
 Options: {}
 RTT    : 371 ms
 Payload: 45 Bytes
............................... Body Payload ...................................
{"9091":"X5xyYM41qFS7vNa9","9029":"1.3.0014"}
--------------------------------------------------------------------------------
IDENTITY: myIkeaGatewayIdentity , PRE_SHARED_KEY: X5xyYM41qFS7vN10
  • You can find the security-code value at the back of your Gateway box.

  • The 192.168.178.150 is the IP address of the gateway in your network. You should find the IP of your IKEA gateway box and use it instead!

  • The myIkeaGatewayIdentity is the new identity to be registered with the gateway. the <Gateway Code Label> is printed on the back side of the Gateway box.

The response IDENTITY: myIkeaGatewayIdentity , PRE_SHARED_KEY: X5xyYM41qFS7vNa9 contains the new credential created for you. Store the generated identity and secret so you can use them to interact with your IKEA gateway.

  • Use the generated credentials to connect to the gateway

server-unknown:>connect coaps://192.168.178.150:5684 --identity myIkeaGatewayIdentity --secret X5xyYM41qFS7vN10
available
coaps://192.168.178.150:5684:>

(Again substitute with the IP of your gateway. Use the identity and secret created in the previous step)

  • List all devices registered with the gateway

coaps://192.168.178.150:5684:>ikea device list

┌────────┬─────────┬──────┬───────────────────────────────┬────────┬───────────┬──────┐
│Instance│Name     │Type  │Model                          │Firmware│Battery [%]│ON/OFF│
├────────┼─────────┼──────┼───────────────────────────────┼────────┼───────────┼──────┤
│65537   │E27 LR4  │LIGHT │TRADFRI bulb E27 CWS opal 600lm│1.3.002 │-          │OFF   │
│65539   │GU10 WC  │LIGHT │TRADFRI bulb GU10 W 400lm      │1.2.214 │-          │OFF   │
│65536   │Remote LR│SWITCH│TRADFRI remote control         │1.2.214 │87         │-     │
│65542   │GU10 LR3 │LIGHT │TRADFRI bulb GU10 WS 400lm     │1.2.217 │-          │ON    │
│65540   │GU10 LR1 │LIGHT │TRADFRI bulb GU10 WS 400lm     │1.2.217 │-          │ON    │
│65541   │GU10 LR2 │LIGHT │TRADFRI bulb GU10 WS 400lm     │1.2.217 │-          │ON    │
│65538   │Sensor WC│SENSOR│TRADFRI motion sensor          │1.2.214 │100        │-     │
└────────┴─────────┴──────┴───────────────────────────────┴────────┴───────────┴──────┘
  • Turn a lamp on/off

coaps://192.168.178.150:5684:>ikea turn on --instance 65539
OK

coaps://192.168.178.150:5684:>ikea device list
┌────────┬─────────┬──────┬───────────────────────────────┬────────┬───────────┬──────┐
│Instance│Name     │Type  │Model                          │Firmware│Battery [%]│ON/OFF│
├────────┼─────────┼──────┼───────────────────────────────┼────────┼───────────┼──────┤
│65539   │GU10 WC  │LIGHT │TRADFRI bulb GU10 W 400lm      │1.2.214 │-          │ON    │


coaps://192.168.178.150:5684:>ikea turn off --instance 65539
OK

coaps://192.168.178.150:5684:>ikea device list
┌────────┬─────────┬──────┬───────────────────────────────┬────────┬───────────┬──────┐
│Instance│Name     │Type  │Model                          │Firmware│Battery [%]│ON/OFF│
├────────┼─────────┼──────┼───────────────────────────────┼────────┼───────────┼──────┤
│65539   │GU10 WC  │LIGHT │TRADFRI bulb GU10 W 400lm      │1.2.214 │-          │OFF   │
  • Use the CoAP’s GET check the raw message response

coaps://192.168.178.150:5684:>get //15001/65539

----------------------------------- Response -----------------------------------
GET coaps://192.168.178.150:5684//15001/65539
MID: 30881, Type: ACK, Token: [260128b68be34371], RTT: 5ms
Options: {"Content-Format":"application/json", "Max-Age":604800}
Status : 205-Reset Content, Payload: 220B
----------------------------------- Payload ------------------------------------
{
  "3311" : [ {
    "5850" : 0,
    "5851" : 203,
    "9003" : 0
  } ],
  "9001" : "GU10 WC",
  "9002" : 1528124737,
  "9020" : 1528447038,
  "9003" : 65539,
  "9054" : 0,
  "5750" : 2,
  "9019" : 0,
  "3" : {
    "0" : "IKEA of Sweden",
    "1" : "TRADFRI bulb GU10 W 400lm",
    "2" : "",
    "3" : "1.2.214",
    "6" : 1
  }
}
--------------------------------------------------------------------------------
Note
The CoAP Gateway follows (partially!) some of the OpenMobileAlliance (formerly IPSO) Smart Object specs. For example "3311" block corresponds to the Light Control object and the 5850 is an mandatory On/Off attribute within this object. Similarly the LWM2M_Device section provides a Device manufacturer object spec. The gateway is not OpenMobileAlliance/IPSO compliant though as some compulsory attributes are missing. The range of 9xxx codes are proprietary. Some description of those codes can be found here.
  • Next you can use CoAP’s PUT method with JSON payload to turn a light On or Off.

Set 5850 to 1 for turning the lamp ON:

coaps://192.168.178.150:5684:>put //15001/65539 --payload '{"3311":[{"5850":1}]}'

or 0 for turning it OFF:

coaps://192.168.178.150:5684:>put //15001/65539 --payload '{"3311":[{"5850":0}]}'

Use the //15001/<DEVICE ID> template to address the device you want.

Caution
If you see org.eclipse.californium.elements.EndpointMismatchException message that means that your DTLS session has expired due to IDLE timeout. You have to re-connect again.

How to Build

Clone the project from GitHub and build with Maven.

git clone https://github.com/tzolov/coap-shell.git
cd ./coap-shell
./mvnw clean install

Then run the self-executable jar in the target folder.

Debugging

Start the shell with --logging.level=DEBUG to enable debug log level for the entire applicationor --logging.level.org.eclipse.californium=DEBUG to debug only californium and scandium. Later is useful to debug the CoAP request message and DTLS interactions.

For example:

java -jar ./target/coap-shell-1.1.1-SNAPSHOT.jar --logging.level.org.eclipse.californium=DEBUG

coap-shell's People

Contributors

tzolov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

coap-shell's Issues

Unable to "connect" to libcoap-based server due to invalid ping

This issue is caused by invalid Empty Message in Californium "ping" implementation.

Hence when the ping "fails", the connection is regarded as invalid, even though the server is perfectly fine.

Unable to "connect", makes it impossible to use other commands (such as GET), as it relies on connect successful to send other requests.

Update maven dependecies

  • Update Spring Boot to latest 2.2.0.RELEASE
  • Update Spring Shell to latest 2.0.1.RELEASE
  • Update Eclipse californium to latest 2.0.0-M18

Binary download links no longer work

The bintray links in the readme file no longer work due to bintray being shut down.

Perhaps the binaries could be uploaded as releases on Github instead.

BAD_CERTIFICATE error when trying to connect to Californium ExampleDTLSServer

I'm running the open source californium project locally, with the ExampleDTLSServer. I am trying to connect through CoAP shell to this example server through the following command:
connect --uri coaps://localhost:5684 --identity Client_identity --secret secretPSK

The identity and secret are default and used in the code. However, I get the following error in my DLTS Server:

org.eclipse.californium.scandium.dtls.HandshakeException: Received 'fatal alert/BAD_CERTIFICATE'
	at org.eclipse.californium.scandium.DTLSConnector.processAlertRecord(DTLSConnector.java:1668)
	at org.eclipse.californium.scandium.DTLSConnector.processRecord(DTLSConnector.java:1414)
	at org.eclipse.californium.scandium.DTLSConnector$12.run(DTLSConnector.java:1276)
	at org.eclipse.californium.elements.util.SerialExecutor$1.run(SerialExecutor.java:289)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
	at java.base/java.lang.Thread.run(Thread.java:832)
11:50:29.816 [DTLS-Worker-/[0:0:0:0:0:0:0:0]:5684#5] DEBUG org.eclipse.californium.scandium.dtls.Connection - Handshake with [/127.0.0.1:50823] has failed

The following error appears in my CoAP shell:

Initial Discover Filed!
unavailable

Any thoughts?

javax.xml.bind dependency error when using JAVA v11

As javax.xml.bind has been deprecated from Java v11, running './mvnw clean install' will cause an error.

To solve it, you need to add an javax.xml.bind dependency to the pom.xml file.

<dependency>
  <groupId>javax.xml.bind</groupId>
  <artifactId>jaxb-api</artifactId>
  <version>2.3.0</version>
</dependency>

ask about APIs

Hi, just wonder is there any APIs can be used to build a customized android application to control the Ikea light?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.