Giter Club home page Giter Club logo

qtcoap's Introduction

QtCoap

Constrained Application Protocol (CoAP) library written in Qt.

The QtCoap lib allowes you communicate with a CoAP server according to the RFC7252 standard.

=====================================================================

Lib features

=====================================================================

The coap-cli tool

$ coap-cli --help

Usage: ./coap-cli [options] [method] url

coap-cli is a command line tool which allowes to interact with a CoAP server.
Version: 1.0.0
Copyright © 2016 Simon Stürz <[email protected]>
Released under the GNU GENERAL PUBLIC LICENSE Version 3.

Options:
  -h, --help               Displays this help.
  -p, --payload <payload>  The payload data you want to send.
  -o, --observe            Observe the given resource.
  -d, --discover           Discover the resources of the server.
  -v, --verbose            Print the whole communication process.

Arguments:
  method                   Method you want to use. Possible values are:
                              get (default)
                              put
                              post
                              delete
  url                      URL of the CoAP resource.

Examples

  • Discover a resource:

      $ coap-cli -d coap://vs0.inf.ethz.ch
    
  • GET

      $ coap-cli get coap://vs0.inf.ethz.ch
    

    Alternative: $ coap-cli coap://vs0.inf.ethz.ch

  • POST

      $ coap-cli post coap://vs0.inf.ethz.ch/test -p "Hello world!"
    
  • PUT

      $ coap-cli put coap://vs0.inf.ethz.ch/test -p "Hello world!"
    
  • DELETE

      $ coap-cli delete coap://vs0.inf.ethz.ch/test
    
  • Observe

      $ coap-cli --observe coap://vs0.inf.ethz.ch/obs
    

=====================================================================

Use QtCoap in your project

Using the libqtcoap

You can checkout the coapclient.pro file or the coaptests.pro to see how the lib will be included into your project:

    LIBS += -L/path/to/the/lib/libqtcoap/ -lqtcoap
    INCLUDEPATH += /path/to/this/repository/coap

Using the coap.pri (like coap-cli)

You can copy the coap folder into your Qt project structure and include the coap.pri file in your project file:

    include(coap/coap.pri)

=====================================================================

Example

This example shows you the basic usage of the QtCoap lib:

#include "coapnetworkaccessmanager.h"

MyClass::MyClass(QObject *parent) : 
  QObject(parent)
{
  CoapNetworkAccessManager *coap = new CoapNetworkAccessManager(this);
  connect(m_coap, SIGNAL(replyFinished(CoapReply*)), this, SLOT(onReplyFinished(CoapReply*)));

  CoapRequest request(QUrl("coap://coap.me/hello"));
  coap->get(request);
}
void MyClass::onReplyFinished(CoapReply *reply)
{
  if (reply->error() != CoapReply::NoError) {
    qWarning() << "Reply finished with error" << reply->errorString();
  }
  
  qDebug() << "Reply finished" << reply;
  reply->deleteLater();
}

For more examples please checkout the coaptests and the coap-cli source code. There is a test case for almost every resource on coap://coap.me/ and observing on coap://vs0.inf.ethz.ch/.

=====================================================================

License

QtCoap is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License.

You should have received a copy of the GNU General Public License along with QtCoap. If not, see http://www.gnu.org/licenses/.

qtcoap's People

Contributors

t-mon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

qtcoap's Issues

Error: "The server did not respond after 4 retransmissions."

After the discover request from a local californium server (as well as the test server coap://vs0.inf.ethz.ch/)t , the reply is treated as an error and the program exists after giving the error.
The error is Error: "The server did not respond after 4 retransmissions
What may be the cause of the error?

   void Core::onReplyFinished(CoapReply *reply)
    {
       if (reply->error() != CoapReply::NoError) {  // this is where the error comes
          qDebug() << "Error:" << reply->errorString(); // this line gives the error: Error: "The server did not respond after 4 retransmissions."
           reply->deleteLater();
           exit(-1);
       }

       if (reply == m_discoverReply) { 
           qDebug() << "Discovery finished";
           qDebug() << "------------------------------------------" << endl;
           CoreLinkParser linkParser(reply->payload());
           foreach (const CoreLink &link, linkParser.links()) {
               qDebug() << link;
               qDebug() << link.path();
               dataList.append(new dataobject(link.title(), link.path()));
           }
           qDebug() << "Data change emitted";
       } ...
   ......
   }
```

Error: Unknown reply received: Please report a bug if you get this message

The Core::onReplyFinished(CoapReply *reply) finished method does not recognize one of the replies of a get request and instead executes the last else of the method. What might be the problem here?

} else if (reply == m_reply) { // the get request should execute this 
    //qDebug() << "------------------------------------------" << endl << reply;

    QString path = reply->request().url().path();

    QString payload = reply->payload();

} else { //but instead executes part

    qWarning() << "Unknown reply received: Please report a bug if you get this message";

}

CoAP server example code

Hello t-mon,

First of all, I'd like to thanks so much for sharing the source code.
Would you please show how to build a simple CoAP server by using this library?

Thanks and Best Regards

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.