mbed RPC Server - Eclipse SmartHome Variante

Dependencies:   EthernetInterface HttpServer Motor Servo mbed-rtos mbed StepperMotorUni TMP175

Fork of RPCHTTPServerSimple by smd.iotkit2.ch

Das ist der mbed Teil zu Eclipse SmartHome (openHAB2). Compiliert das Programm und lädt es auf das Board.

Installation Eclipse SmartHome

Lädt eine vorbereitete Version des openHAB2 Runtimes von http://images.workshoptage.ch/images/ws4/ herunter und entpackt es auf Eurem PC. Alternativ kann die Entwicklungsumgebung von openHAB2 inkl. Eclipse wie hier beschrieben, installiert werden.

Zusätzlich ist das Addon (Eclipse Plug-In - ch.iotkit.smarthome.binding.mbedRPC*), ebenfalls von http://images.workshoptage.ch/images/ws4/ downzuladen und ins Verzeichnis addons zu kopieren.

Danach kann das openHAB2 Runtime mittels des start Datei gestartet werden und das UI mittels http://localhost:8080 aufrufen werden.

Der Sourcecode zum Eclipse Plug-In befindet sich auf GitHub

Konfiguration

Vorgehen:

  • Konfiguriert die Ethernet Bridge mit der IP Adresse des IoTKit SMD Shield
  • Fügt die Sensoren, Aktoren, LED's etc. vom IoTKit SMD Shield hinzu, aufbauend auf der Bridge

Unterstützte Geräte

  • Sensoren (auf Shield)
    • Poti (Pin A0)
    • Helligkeits Sensor (Pin A1)
    • Hall Sensor (Pin A2)
    • Temperatur Sensor (mittels I2C Bus)
  • Aktoren
    • Motor (Pin D3, D2, D4), verbinden mit DCMOT D2-D7 oben
    • Servo (Pin D9), verbinden mit Servo2 Stecker
    • Stepper (K64F Pins), verbinden mit STEPPER3, rotes Kabel nach unten
  • LED's (auf Shield)
    • LED's rot, gelb, grün, blau (Pin D10 - D13)
  • LED Strip 12 Volt (Pin D5 - D7), verbinden mit FET D5-D7, 12V oben

Der Motor und der LED Strip benötigen ein externes 12 Volt Netzteil.

cURL

Die Funktionen können mittels cURL oder Browser wie folgt getestet werden:

# RGB LED Strip (weiss 0xFFFFFF, rot 0xFF00, grün 0xFF0000, blau, 0xFF)
http://192.168.178.32/rpc/ledstrip/write+16777215
http://192.168.178.32/rpc/ledstrip/write+‭16711680‬
http://192.168.178.32/rpc/ledstrip/write+65280
http://192.168.178.32/rpc/ledstrip/write+255
 
# Motor Up, Down, Stop (Simulation Rollladen)
http://192.168.178.32/rpc/motor1/up
http://192.168.178.32/rpc/motor1/down
http://192.168.178.32/rpc/motor1/stop
 
# Schrittmotor                               
http://192.168.178.32/rpc/stepper1/up
http://192.168.178.32/rpc/stepper1/down
 
# Servo (Positionieren 0 - 1.0 und Position lesen)
http://192.168.178.32/rpc/servo1/write+0.5
http://192.168.178.32/rpc/servo1/read
 
# Temperatur Sensor abfragen                        
http://192.168.178.32/rpc/temp/read

IP-Adresse entsprechend dem Board anpassen.

mbed-rpc/rpc.h

Committer:
marcel1691
Date:
2015-08-28
Revision:
16:d0a5bb230d94
Parent:
11:43e28c85fd75

File content as of revision 16:d0a5bb230d94:

/* mbed Microcontroller Library
 * Copyright (c) 2006-2013 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef RPC_H
#define RPC_H

#include "mbed.h"
#include "Arguments.h"

namespace mbed {

#define RPC_MAX_STRING      128

struct rpc_function {
    const char *name;
    void (*function_caller)(Arguments*, Reply*);
};

struct rpc_class {
    const char *name;
    const rpc_function *static_functions;
    struct rpc_class *next;
};

/* Class RPC
 *  The RPC class for most things
 */
class RPC {

public:

    RPC(const char *name = NULL);

    virtual ~RPC();

    /* Function get_rpc_methods
     *  Returns a pointer to an array describing the rpc methods
     *  supported by this object, terminated by either
     *  RPC_METHOD_END or RPC_METHOD_SUPER(Superclass).
     *
     * Example
     * > class Example : public RPC {
     * >   int foo(int a, int b) { return a + b; }
     * >   virtual const struct rpc_method *get_rpc_methods() {
     * >     static const rpc_method rpc_methods[] = {
     * >       { "foo", generic_caller<int, Example, int, int, &Example::foo> },
     * >       RPC_METHOD_SUPER(RPC)
     * >     };
     * >     return rpc_methods;
     * >   }
     * > };
     */
    virtual const struct rpc_method *get_rpc_methods();

    static bool call(const char *buf, char *result);

    /* Function lookup
     *  Lookup and return the object that has the given name.
     *
     * Variables
     *  name - the name to lookup.
     */
    static RPC *lookup(const char *name);

protected:
    static RPC *_head;
    RPC *_next;
    char *_name;
    bool _from_construct;

private:
    static rpc_class *_classes;

    static const rpc_function _RPC_funcs[];
    static rpc_class _RPC_class;

    void delete_self();
    static void list_objs(Arguments *args, Reply *result);
    static void clear(Arguments *args, Reply *result);

public:
    /* Function add_rpc_class
     *  Add the class to the list of classes which can have static
     *  methods called via rpc (the static methods which can be called
     *  are defined by that class' get_rpc_class() static method).
     */
    template<class C>
    static void add_rpc_class() {
        rpc_class *c = C::get_rpc_class();
        c->next = _classes;
        _classes = c;
    }

    template<class C>
    static const char *construct() {
        RPC *p = new C();
        p->_from_construct = true;
        return p->_name;
    }

    template<class C, typename A1>
    static const char *construct(A1 arg1) {
        RPC *p = new C(arg1);
        p->_from_construct = true;
        return p->_name;
    }

    template<class C, typename A1, typename A2>
    static const char *construct(A1 arg1, A2 arg2) {
        RPC *p = new C(arg1, arg2);
        p->_from_construct = true;
        return p->_name;
    }

    template<class C, typename A1, typename A2, typename A3>
    static const char *construct(A1 arg1, A2 arg2, A3 arg3) {
        RPC *p = new C(arg1, arg2, arg3);
        p->_from_construct = true;
        return p->_name;
    }

    template<class C, typename A1, typename A2, typename A3, typename A4>
    static const char *construct(A1 arg1, A2 arg2, A3 arg3, A4 arg4) {
        RPC *p = new C(arg1, arg2, arg3, arg4);
        p->_from_construct = true;
        return p->_name;
    }
};

/* Macro MBED_OBJECT_NAME_MAX
 *  The maximum size of object name (including terminating null byte)
 *  that will be recognised when using fopen to open a FileLike
 *  object, or when using the rpc function.
 */
#define MBED_OBJECT_NAME_MAX 32

/* Macro MBED_METHOD_NAME_MAX
 *  The maximum size of rpc method name (including terminating null
 *  byte) that will be recognised by the rpc function (in rpc.h).
 */
#define MBED_METHOD_NAME_MAX 32

/* Function rpc_method_caller
 */
template<class T, void(T::*member)(const char *, char *)>
void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) {
    (static_cast<T*>(this_ptr)->*member)(arguments, result);
}

/* Function rpc_method_caller
 */
template<class T, void(T::*member)()>
void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) {
    (static_cast<T*>(this_ptr)->*member)();
}

/* Function rpc_method_caller
 */
template<class T, typename A1, void(T::*member)(A1)>
void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) {
    A1 arg1 = arguments->getArg<A1>();

    (static_cast<T*>(this_ptr)->*member)(arg1);
}

/* Function rpc_method_caller
 */
template<class T, typename A1, typename A2, void(T::*member)(A1, A2)>
void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) {
    A1 arg1 = arguments->getArg<A1>();
    A2 arg2 = arguments->getArg<A2>();

    (static_cast<T*>(this_ptr)->*member)(arg1, arg2);
}

/* Function rpc_method_caller
 */
template<class T, typename A1, typename A2, typename A3, void(T::*member)(A1, A2, A3)>
void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) {
    A1 arg1 = arguments->getArg<A1>();
    A2 arg2 = arguments->getArg<A2>();
    A3 arg3 = arguments->getArg<A3>();

    (static_cast<T*>(this_ptr)->*member)(arg1, arg2, arg3);
}

/* Function rpc_method_caller
 */
template<typename R, class T, R(T::*member)()>
void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) {
    R res = (static_cast<T*>(this_ptr)->*member)();
    result->putData<R>(res);
}

/* Function rpc_method_caller
 */
template<typename R, class T, typename A1, R(T::*member)(A1)>
void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) {
    A1 arg1 = arguments->getArg<A1>();

    R res = (static_cast<T*>(this_ptr)->*member)(arg1);
    result->putData<R>(res);
}

/* Function rpc_method_caller
 */
template<typename R, class T, typename A1, typename A2, R(T::*member)(A1, A2)>
void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) {
    A1 arg1 = arguments->getArg<A1>();
    A2 arg2 = arguments->getArg<A2>();

    R res = (static_cast<T*>(this_ptr)->*member)(arg1, arg2);
    result->putData<R>(res);
}

/* Function rpc_method_caller
 */
template<typename R, class T, typename A1, typename A2, typename A3, R(T::*member)(A1, A2, A3)>
void rpc_method_caller(RPC *this_ptr, Arguments *arguments, Reply *result) {
    A1 arg1 = arguments->getArg<A1>();
    A2 arg2 = arguments->getArg<A2>();
    A3 arg3 = arguments->getArg<A3>();

    R res = (static_cast<T*>(this_ptr)->*member)(arg1, arg2, arg3);
    result->putData<R>(res);
}

/* Function rpc_function caller
 */
template<typename R, R(*func)()>
void rpc_function_caller(Arguments *arguments, Reply *result) {
    R res = (*func)();
    result->putData<R>(res);
}

/* Function rpc_function caller
 */
template<typename R, typename A1, R(*func)(A1)>
void rpc_function_caller(Arguments *arguments, Reply *result) {
    A1 arg1 = arguments->getArg<A1>();
    R res = (*func)(arg1);
    result->putData<R>(res);
}

/* Function rpc_function caller
 */
template<typename R, typename A1, typename A2, R(*func)(A1, A2)>
void rpc_function_caller(Arguments *arguments, Reply *result) {
    A1 arg1 = arguments->getArg<A1>();
    A2 arg2 = arguments->getArg<A2>();

    R res = (*func)(arg1, arg2);
    result->putData<R>(res);
}

/* Function rpc_function caller
 */
template<typename R, typename A1, typename A2, typename A3, R(*func)(A1, A2, A3)>
void rpc_function_caller(Arguments *arguments, Reply *result) {
    A1 arg1 = arguments->getArg<A1>();
    A2 arg2 = arguments->getArg<A2>();
    A3 arg3 = arguments->getArg<A3>();

    R res = (*func)(arg1, arg2, arg3);
    result->putData<R>(res);
}

/* Function rpc_function caller
 */
template<typename R, typename A1, typename A2, typename A3, typename A4, R(*func)(A1, A2, A3, A4)>
void rpc_function_caller(Arguments *arguments, Reply *result) {
    A1 arg1 = arguments->getArg<A1>();
    A2 arg2 = arguments->getArg<A2>();
    A3 arg3 = arguments->getArg<A3>();
    A4 arg4 = arguments->getArg<A4>();

    R res = (*func)(arg1, arg2, arg3, arg4);
    result->putData<R>(res);
}

struct rpc_method {
    const char *name;
    typedef void (*method_caller_t)(RPC*, Arguments*, Reply*);
    typedef const struct rpc_method *(*super_t)(RPC*);
    union {
        method_caller_t method_caller;
        super_t super;
    };
};

template<class C>
const struct rpc_method *rpc_super(RPC *this_ptr) {
    return static_cast<C*>(this_ptr)->C::get_rpc_methods();
}

#define RPC_METHOD_END      { NULL, NULL }
#define RPC_METHOD_SUPER(C) { NULL, (rpc_method::method_caller_t)rpc_super<C> }

} // namespace mbed

#endif