![](/media/cache/group/Download_mxxYCWN.jpg.50x50_q85.jpg)
Minimalistischer RCP HTTP Server Erweiterte Variante mit RCPVariable und Servo
Dependencies: EthernetInterface HttpServer Servo mbed-rpc mbed-rtos mbed
Fork of RPCHTTPServerSimple by
main.cpp@8:3e6bfb96a451, 2015-03-07 (annotated)
- Committer:
- stefan1691
- Date:
- Sat Mar 07 12:57:40 2015 +0000
- Revision:
- 8:3e6bfb96a451
- Parent:
- 7:8a319a112fba
- Child:
- 9:66ff9ae5572e
neue Libraries version
Who changed what in which revision?
User | Revision | Line number | New 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 |
stefan1691 | 8:3e6bfb96a451 | 102 | FSHandler::mount("/local", "/"); |
stefan1691 | 8:3e6bfb96a451 | 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 | } |