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

MQTT pub/sub library 0.3.4

MQTT library 0.3.4 now available. On this release add Arduino platform, and this client library can use nearly same source code on Particle Photon, Core, severals Arduino Platform(UNO with Ethernet Shield…others).

Particle Photon, Core MQTT is here, and for Arduino branches is here.
BTW, Particle Photon, Core version MQTT library is used over 1,200 apps on the Particle now.

– How to use on Arduino
Particle have Web IDE with community libraries application developer easy to use several latest libraies without notifying. But on Arduino, developer have to add library by your self. here is from github readme.
Download this source code branch zip file and extract to the Arduino libraries directory or checkout MQTT repository. Here is checkout on MacOS X.

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

here is SimplePubSub Arduino source code.

#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <SPI.h>
#include <MQTT.h>

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

void callback(char* topic, byte* payload, unsigned int length);

/**
 * if want to use IP address,
 * byte server[] = { XXX,XXX,XXX,XXX };
 * MQTT client(server, 1883, callback);
 * want to use domain name,
 * MQTT client("www.sample.com", 1883, callback);
 * change "server_name".
 **/
EthernetClient ethclient;
MQTT client("server_name", 1883, callback, ethclient);

// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
  char p[length + 1];
  memcpy(p, payload, length);
  p[length] = NULL;
  String message(p);

  Serial.println(message);
  delay(1000);
}


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

  // Get IP address from DHCP Server
  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();
    
  // connect to the server
  client.connect("mqttclient");

  // publish/subscribe
  if (client.isConnected()) {
    client.publish("/outTopic","hello world");
    client.subscribe("/inTopic");
  }
}

void loop() {
  if (client.isConnected())
    client.loop();
}

Following is Particle Photon, Core same version. Difference points with Arduino are include files and Ethernet connection implementation only.

#include "MQTT/MQTT.h"

void callback(char* topic, byte* payload, unsigned int length);

/**
 * if want to use IP address,
 * byte server[] = { XXX,XXX,XXX,XXX };
 * MQTT client(server, 1883, callback);
 * want to use domain name,
 * MQTT client("www.sample.com", 1883, callback);
 * change "server_name".
 **/
MQTT client("server_name", 1883, callback);

// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
  char p[length + 1];
  memcpy(p, payload, length);
  p[length] = NULL;
  String message(p);

  Serial.println(message);
  delay(1000);
}


void setup() {
  Serial.begin(9600);
    
  // connect to the server
  client.connect("mqttclient");

  // publish/subscribe
  if (client.isConnected()) {
    client.publish("/outTopic","hello world");
    client.subscribe("/inTopic");
  }
}

void loop() {
  if (client.isConnected())
    client.loop();
}

lost Blog data memo

I lost my blog data(my server operation miss…) at 2014/03, but Internet Archive save my lost blog data. Followinig entry for my memo from Internet Archive.

TreasureHunter Robotics equipments

Kinect Skeleton-tracking data search with Solr

Multi Layer Amid Screen

Android 4.2(Jelly Bean) and OpenNI 2.1 beta
Raspberry Pi and OpenNI2

SONY Smart Watch2 and HeartBeat, ball led cube

OpenNI User Tracking on Android

Recognize human robot on OpenNI

How to build OpenNI and Sensor Driver(PrimeSense and Kinect) with Android NDK r7