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.

Committer:
marcel1691
Date:
Sun Jan 18 14:39:15 2015 +0000
Revision:
7:8a319a112fba
Parent:
6:c9c7ffa0594e
Child:
8:3e6bfb96a451
AnalogInput eingebaut

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yueee_yt 0:050a5d4ffd55 1 //#define DNS_SERVER_ADDRESS(ipaddr) (ip4_addr_set_u32(ipaddr, ipaddr_addr("208.67.222.222"))) /* resolver1.opendns.com */
marcel1691 6:c9c7ffa0594e 2
yueee_yt 0:050a5d4ffd55 3 #include "mbed.h"
yueee_yt 0:050a5d4ffd55 4 #include "rtos.h"
yueee_yt 0:050a5d4ffd55 5 #include "EthernetInterface.h"
yueee_yt 0:050a5d4ffd55 6 #include "HTTPServer.h"
yueee_yt 3:5758cfefe980 7 #include "mbed_rpc.h"
marcel1691 6:c9c7ffa0594e 8 #include "SDFileSystem.h"
yueee_yt 5:bfa9878aa274 9
yueee_yt 0:050a5d4ffd55 10 EthernetInterface eth;
yueee_yt 3:5758cfefe980 11
yueee_yt 3:5758cfefe980 12 DigitalOut led4(LED4);
yueee_yt 5:bfa9878aa274 13
marcel1691 6:c9c7ffa0594e 14 //LocalFileSystem local("local");
marcel1691 6:c9c7ffa0594e 15 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "local");
yueee_yt 0:050a5d4ffd55 16
marcel1691 7:8a319a112fba 17 static float value = 20;
marcel1691 7:8a319a112fba 18
marcel1691 7:8a319a112fba 19 class AnalogInHandler : public SimpleHandler
marcel1691 7:8a319a112fba 20 {
marcel1691 7:8a319a112fba 21 public:
marcel1691 7:8a319a112fba 22 AnalogInHandler(const char* rootPath, const char* path, TCPSocketConnection* pTCPSocketConnection) : SimpleHandler(rootPath, path, pTCPSocketConnection) { }
marcel1691 7:8a319a112fba 23 virtual ~AnalogInHandler() {}
marcel1691 7:8a319a112fba 24
marcel1691 7:8a319a112fba 25 //protected:
marcel1691 7:8a319a112fba 26 static inline HTTPRequestHandler* inst(const char* rootPath, const char* path, TCPSocketConnection* pTCPSocketConnection) { return new AnalogInHandler(rootPath, path, pTCPSocketConnection); } //if we ever could do static virtual functions, this would be one
marcel1691 7:8a319a112fba 27
marcel1691 7:8a319a112fba 28 virtual void doGet()
marcel1691 7:8a319a112fba 29 {
marcel1691 7:8a319a112fba 30 char buf[20];
marcel1691 7:8a319a112fba 31 sprintf( buf, "%f", value );
marcel1691 7:8a319a112fba 32 value += 0.01;
marcel1691 7:8a319a112fba 33 setContentLen( strlen(buf) );
marcel1691 7:8a319a112fba 34 respHeaders()["Connection"] = "close";
marcel1691 7:8a319a112fba 35 writeData(buf, strlen(buf));
marcel1691 7:8a319a112fba 36 }
marcel1691 7:8a319a112fba 37
marcel1691 7:8a319a112fba 38 };
marcel1691 7:8a319a112fba 39
yueee_yt 5:bfa9878aa274 40 void aliveState(void const *args)
yueee_yt 5:bfa9878aa274 41 {
marcel1691 6:c9c7ffa0594e 42 while (true)
marcel1691 6:c9c7ffa0594e 43 {
yueee_yt 3:5758cfefe980 44 led4 = !led4;
marcel1691 6:c9c7ffa0594e 45 Thread::wait(2000);
yueee_yt 0:050a5d4ffd55 46 }
yueee_yt 0:050a5d4ffd55 47 }
yueee_yt 0:050a5d4ffd55 48
marcel1691 6:c9c7ffa0594e 49 uint32_t do_list(const char *fsrc)
marcel1691 6:c9c7ffa0594e 50 {
marcel1691 6:c9c7ffa0594e 51 DIR *d = opendir(fsrc);
marcel1691 6:c9c7ffa0594e 52 struct dirent *p;
marcel1691 6:c9c7ffa0594e 53 uint32_t counter = 0;
marcel1691 6:c9c7ffa0594e 54
marcel1691 6:c9c7ffa0594e 55 while ((p = readdir(d)) != NULL) {
marcel1691 6:c9c7ffa0594e 56 counter++;
marcel1691 6:c9c7ffa0594e 57 printf("%s\n", p->d_name);
marcel1691 6:c9c7ffa0594e 58 }
marcel1691 6:c9c7ffa0594e 59 closedir(d);
marcel1691 6:c9c7ffa0594e 60 return counter;
marcel1691 6:c9c7ffa0594e 61 }
marcel1691 6:c9c7ffa0594e 62
yueee_yt 0:050a5d4ffd55 63 int main()
yueee_yt 0:050a5d4ffd55 64 {
yueee_yt 0:050a5d4ffd55 65 printf("********* PROGRAM START ***********\r\n");
yueee_yt 0:050a5d4ffd55 66 Thread thread(aliveState);
marcel1691 7:8a319a112fba 67
marcel1691 7:8a319a112fba 68 // LED's
yueee_yt 3:5758cfefe980 69 RPC::add_rpc_class<RpcDigitalOut>();
marcel1691 6:c9c7ffa0594e 70 RPC::construct<RpcDigitalOut, PinName, const char*>(PTC3, "led1");
marcel1691 6:c9c7ffa0594e 71 RPC::construct<RpcDigitalOut, PinName, const char*>(PTC2, "led2");
marcel1691 6:c9c7ffa0594e 72 RPC::construct<RpcDigitalOut, PinName, const char*>(PTA2, "led3");
marcel1691 7:8a319a112fba 73
marcel1691 7:8a319a112fba 74 // Sensoren bringt Linkfehler
marcel1691 7:8a319a112fba 75 //RPC::add_rpc_class<RpcAnalogIn>();
marcel1691 7:8a319a112fba 76 //RPC::construct<RpcAnalogIn, PinName, const char*>(PTB3, "s1");
marcel1691 6:c9c7ffa0594e 77 //RPCFunction rpcFunc(LcdWrite, "LcdWrite"); //ADD Here!!
marcel1691 7:8a319a112fba 78
yueee_yt 3:5758cfefe980 79
yueee_yt 0:050a5d4ffd55 80 printf("EthernetInterface Setting up...\r\n");
marcel1691 6:c9c7ffa0594e 81 if(eth.init()!=0)
marcel1691 6:c9c7ffa0594e 82 { //for DHCP Server
yueee_yt 5:bfa9878aa274 83 //if(eth.init(IPAddress,NetMasks,Gateway)!=0) { //for Static IP Address
yueee_yt 0:050a5d4ffd55 84 printf("EthernetInterface Initialize Error \r\n");
yueee_yt 0:050a5d4ffd55 85 return -1;
yueee_yt 0:050a5d4ffd55 86 }
marcel1691 6:c9c7ffa0594e 87 if(eth.connect()!=0)
marcel1691 6:c9c7ffa0594e 88 {
yueee_yt 0:050a5d4ffd55 89 printf("EthernetInterface Connect Error \r\n");
yueee_yt 0:050a5d4ffd55 90 return -1;
yueee_yt 0:050a5d4ffd55 91 }
yueee_yt 0:050a5d4ffd55 92 printf("IP Address is %s\r\n", eth.getIPAddress());
yueee_yt 0:050a5d4ffd55 93 printf("NetMask is %s\r\n", eth.getNetworkMask());
yueee_yt 0:050a5d4ffd55 94 printf("Gateway Address is %s\r\n", eth.getGateway());
yueee_yt 0:050a5d4ffd55 95 printf("Ethernet Setup OK\r\n");
yueee_yt 0:050a5d4ffd55 96
marcel1691 6:c9c7ffa0594e 97 printf("\nList all directories/files /local.\n");
marcel1691 6:c9c7ffa0594e 98 do_list("/local");
marcel1691 6:c9c7ffa0594e 99
marcel1691 7:8a319a112fba 100 //HTTPServerAddHandler<SimpleHandler>("/hello"); //Default handler
marcel1691 7:8a319a112fba 101 HTTPServerAddHandler<AnalogInHandler>("/analogIn"); //Default handler
marcel1691 7:8a319a112fba 102 //FSHandler::mount("/local", "/");
marcel1691 7:8a319a112fba 103 //HTTPServerAddHandler<FSHandler>("/");
yueee_yt 3:5758cfefe980 104 HTTPServerAddHandler<RPCHandler>("/rpc");
yueee_yt 0:050a5d4ffd55 105 HTTPServerStart(80);
yueee_yt 0:050a5d4ffd55 106 }
yueee_yt 0:050a5d4ffd55 107
yueee_yt 4:155c6ff99458 108 void LcdWrite(Arguments* arg, Reply* r) //ADD Here!!
yueee_yt 4:155c6ff99458 109 {
marcel1691 6:c9c7ffa0594e 110 printf("%s",arg->argv[0]);
yueee_yt 4:155c6ff99458 111 }