CoAP library 0.2.0

CoAP simple client/server library 0.2.0 now available. Some bug fixed, add Arduino platform. And now, this library sample source run same sourcode on Particle Photon, Core, severals Arduino Platform(UNO with Ethernet Shield…others).

Particle Photon, Core version.
Arduino Platform version.

– How to use sample
In this exmples run with CoAP server libcoap or microcoap server for check. This is setting the libcoap on Ubuntu Linux. But if there don’t use CoAP server(request/reseponse), following setting don’t be needed.

libcoap server.

git clone https://github.com/obgm/libcoap 
cd libcoap/
./autogen.sh 
./configure --disable-examples 
gcc -o coap-server examples/coap-server.c -I./include/coap/ -I. -L.libs -lcoap-1 -DWITH_POSIX
gcc -o coap-client examples/client.c examples/coap_list.c -I./include/coap/ -I. -I./examples/ -L.libs -lcoap-1 -DWITH_POSIX
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.libs
# run coap-server
./coap-server
# next start Arduino and check the request/response.
# if you need check the coap server, use coap-client.

microcoap

git clone https://github.com/1248/microcoap
cd microcoap
make
# run CoAP server
./coap

On Arduino Platform, download this source code branch zip file and extract to the Arduino libraries directory or checkout repository. Here is checkout on MacOS X.

cd $HOME/Documents/Arduino/libraries/
git clone -b arduino https://github.com/hirotakaster/CoAP CoAP_library
# restart Arduino IDE, you can find CoAP_library examples.

pic1

here is coaptest.ino sample source code for Arduino

#include <SPI.h>
#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <coap.h>

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };

// CoAP client response callback
// This function is called when application send packet to the server and next get response.
void callback_response(CoapPacket &packet, IPAddress ip, int port);

// CoAP server endpoint url callback.
// This function is called when application get request packet from client.
void callback_light(CoapPacket &packet, IPAddress ip, int port);

// UDP and CoAP class
EthernetUDP Udp;
Coap coap(Udp);

// LED STATE
// Change color CaAP request.
bool LEDSTATE;

// CoAP server endpoint URL callback.
void callback_light(CoapPacket &packet, IPAddress ip, int port) {
  Serial.println("[Light] ON/OFF");
  
  // send response
  char p[packet.payloadlen + 1];
  memcpy(p, packet.payload, packet.payloadlen);
  p[packet.payloadlen] = NULL;
  
  String message(p);

  if (message.equals("0"))
    LEDSTATE = false;
  else if(message.equals("1"))
    LEDSTATE = true;
  
  // CoAP server have to response message to the client with same messageid.
  if (LEDSTATE) {
    digitalWrite(9, HIGH) ; 
    coap.sendResponse(ip, port, packet.messageid, "1");
  } else { 
    digitalWrite(9, LOW) ; 
    coap.sendResponse(ip, port, packet.messageid, "0");
  }
}

// CoAP client response callback
void callback_response(CoapPacket &packet, IPAddress ip, int port) {
  Serial.println("[Coap Response got]");
  
  char p[packet.payloadlen + 1];
  memcpy(p, packet.payload, packet.payloadlen);
  p[packet.payloadlen] = NULL;
  
  Serial.println(p);
}

void setup() {
  Serial.begin(9600);

  Ethernet.begin(mac);
  Serial.print("My IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print(".");
  }
  Serial.println();

  // LED State
  pinMode(9, OUTPUT);
  digitalWrite(9, HIGH);
  LEDSTATE = true;
  
  // add server url endpoints.
  // can add multiple endpoint urls.
  // this endpoint url is called by client.
  // exp) coap.server(callback_switch, "switch");
  //      coap.server(callback_env, "env/temp");
  //      coap.server(callback_env, "env/humidity");
  Serial.println("Setup Callback Light");
  coap.server(callback_light, "light");

  // client response callback.
  // this endpoint is single callback.
  Serial.println("Setup Response Callback");
  coap.response(callback_response);

  // start coap server/client
  coap.start();
}

void loop() {
  // send GET or PUT coap request to CoAP server.
  // To test, use libcoap, microcoap server...etc
  // int msgid = coap.put(IPAddress(10, 0, 0, 1), 5683, "light", "1");
  // "time"(libcoap sample server endpoint) is set.
  Serial.println("Send Request");
  int msgid = coap.get(IPAddress(XXX, XXX, XXX, XXX), 5683, "time");

  delay(1000);
  coap.loop();
}
/*
if you change LED, req/res test with coap-client(libcoap), run following.
coap-client -m get coap://(arduino ip addr)/light
coap-client -e "1" -m put coap://(arduino ip addr)/light
coap-client -e "0" -m put coap://(arduino ip addr)/light
*/

next, there is Particle Photon, Core sample sourcode. Difference point with the Arduino is only Udp class, include files and IP address setting only.

#include "coap/coap.h"

void callback_light(CoapPacket &packet, IPAddress ip, int port);
void callback_response(CoapPacket &packet, IPAddress ip, int port);

Coap coap;
bool LEDSTATE;

void callback_light(CoapPacket &packet, IPAddress ip, int port) {
    Serial.println("[Light] ON/OFF");
    
    // send response
    char p[packet.payloadlen + 1];
    memcpy(p, packet.payload, packet.payloadlen);
    p[packet.payloadlen] = NULL;
    
    String message(p);

    if (message.equals("0"))
        LEDSTATE = false;
    else if(message.equals("1"))
        LEDSTATE = true;
        
    if (LEDSTATE) {
        coap.sendResponse(ip, port, packet.messageid, "1");
        RGB.color(255, 255, 255);
    } else { 
        coap.sendResponse(ip, port, packet.messageid, "0");
        RGB.color(0, 0, 0);
    }
}

void callback_response(CoapPacket &packet, IPAddress ip, int port) {
    Serial.println("[Coap Response got]");

    char p[packet.payloadlen + 1];
    memcpy(p, packet.payload, packet.payloadlen);
    p[packet.payloadlen] = NULL;

    Serial.println(p);
}

void setup() {
    Serial.begin(9600);
    
    // LED Controll
    RGB.control(true);
    RGB.color(255, 255, 255);
    LEDSTATE = true;

    Serial.println("Setup Callback Light");
    coap.server(callback_light, "light");

    Serial.println("Setup Response Callback");
    coap.response(callback_response);

    // start coap server/client
    coap.start();
}

void loop() {
    Serial.println("Send Request");
    int msgid = coap.get(IPAddress(XXX, XXX, XXX, XXX), 5683, "time");

    delay(1000);
    coap.loop();
}

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください