You are viewing an older revision! See the latest version

mbedServer

Table of Contents

  1. Demo
  2. IoT @ Eclipse

Demo

/media/uploads/marcel1691/deviceservercontext.png

Ein Device Server nutzt:

  • Open-Source-Protokolle wie COAP / HTTP, MQTT, TLS / TCP, DTLS / UDP und OMALWM2M für die Datenkommunikation und Gerätemanagement.
  • Stellt die Interoperabilität zwischen Geräte Protokollen wie MQTT, COAP etc. und REST (über HTTP) her.

Und beinhaltet grundlegende Techniken des „physical mashups“ (wie Sensoren und Aktoren mittels Netzwerk-Technologien ad hoc zu einem funktionsfähigen Ganzen verbunden werden).











IoT @ Eclipse

Das IoT Eclipse Projekt ist einen Sammlung von Internet der Dinge Projekten.

Californium

Dienst zum Erstellen von CoAP Clients und Servern in Java.

leshan - Open Mobile Alliance (OMA) Lightweight M2M (LWM2M) Server

leshan Erweitert Californium um einen Lightweight M2M (LWM2M) Server. D.h. neben der CoAP Funktionalität kommen weitere hinzu, wie:

  • RD - Resource Directory (Verwalten von CoAP Clients)
  • Eine Web Oberfläche
  • REST API
  • Standard Device Management Objects

Standard Device Management Objects

  • 1 - Server
  • 2 - Zugriffsberechtigungen
  • 3 - Gerät
  • 4 - Monitoring
  • 5 - Firmware
  • 6 - GPS Informationen

Installation

wget https://hudson.eclipse.org/leshan/job/leshan/lastSuccessfulBuild/artifact/leshan-standalone.jar
java -jar ./leshan-standalone.jar

Anwahl der Web Oberfläche mittels http://localhost/8080.

Beispiele: REST (leshan Server)

# Alle Clients abfragen (Antwort im JSON Format)
curl -X GET  http://localhost:8080/api/clients
                         
# Wert von LED2 abfragen (Antwort im text/plain Format)                      
curl -X GET  http://localhost:8080/api/clients/mbed-k64f-1234/10/0/2 
 
# Wert für LED2 setzen
curl -X GET -vvv http://localhost:8080/api/clients/mbed-k64f-1234/10/0/2 -H "Content-Type: text/plain" -d "10"

Import programCOAPleshan

CoAP Device Server Client mit leshan Server

Implementierung CoAP Resource:

#include "PwmOutResource.h" 
 
/** Erste verkettete Resource */
PwmOutResource * firstPwmOutResource;
 
/** Default Konstruktor 
 *  @param pin Pin fuer PwmOut 
 */ 
PwmOutResource::PwmOutResource( PinName pin, char *name, char *id ) 
{
    resource = new PwmOut( pin ); 
    this->id = id;
    this->name = name;
    
    // neu verketten - letztes hinzugefuegstes Element = first.
    if  ( firstPwmOutResource != (PwmOutResource *) 0 )
        this->next = firstPwmOutResource;
    firstPwmOutResource = this;
}
 
/** Erstellt die Resource 
 *  @param sn_nsdl_resource_info_s CoAP struct
 *  @return 0 wenn ohne Fehler
 */ 
int PwmOutResource::create( sn_nsdl_resource_info_s *resource_ptr )
{
    nsdl_create_dynamic_resource(resource_ptr, strlen(id), (uint8_t*) id, strlen(name), (uint8_t*) name, 0, &PwmOutResource::callback, (SN_GRS_GET_ALLOWED | SN_GRS_PUT_ALLOWED));
    return  ( 0 );
}
 
/** Callback wenn die Resource gelesen oder geaendert werden soll
 *  @param sn_coap_hdr_s
 *  @param sn_nsdl_addr_s
 *  @param sn_proto_info_s
 *  @return 0 wenn ohne Fehler
 */
uint8_t PwmOutResource::callback( sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s * proto )
{
    sn_coap_hdr_s *coap_res_ptr = 0;
    char buf[8];
    char path[8];
    PwmOutResource* current;
 
    // Path wegkopieren mit \0
    strncpy( path, (const char*) received_coap_ptr->uri_path_ptr, received_coap_ptr->uri_path_len );
    printf("light callback %s\r\n", path );
    
    // Resource laut path suchen
    for ( current = firstPwmOutResource; current != (PwmOutResource *) 0; current = current->next )
        if  ( strcmp( path, current->id ) == 0 )
            break;
    // nichts gefunden - keine weitere Abhandlung
    if  ( current == (PwmOutResource*) 0 )
        return  ( 1 );
 
    // CoAP GET
    if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET)
    {
        coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
 
        sprintf( buf, "%d",  (int) (current->resource->read() * 100) );
        coap_res_ptr->payload_len = strlen( buf );
        coap_res_ptr->payload_ptr = (uint8_t*) buf;
        sn_nsdl_send_coap_message(address, coap_res_ptr);
    }
    // CoAP PUT
    else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT)
    {
        memcpy(buf, (char *)received_coap_ptr->payload_ptr, received_coap_ptr->payload_len);
        buf[received_coap_ptr->payload_len] = '\0';
        current->resource->write( atof( buf ) / 100 );
 
        coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CHANGED);
        sn_nsdl_send_coap_message(address, coap_res_ptr);
    }
 
    sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);    
    return  ( 0 );
}

All wikipages