Simple code to demonstrate how to manually use RPC functionality to easily access the microcontroller hardware via WebInterface. Note: Demo program to be used on the GeekSessionLab Talk (November 2011). http://devrendezvous.com/?lang=en

Dependencies:   EthernetNetIf mbed HTTPServer

Committer:
botdream
Date:
Thu Oct 27 23:01:40 2011 +0000
Revision:
0:c308e6d6f56b

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
botdream 0:c308e6d6f56b 1 /* //---------------------------------------------------------------------------------------------
botdream 0:c308e6d6f56b 2 http://192.168.1.100/rpc/DigitalOut/new%20LED1%20myled1
botdream 0:c308e6d6f56b 3
botdream 0:c308e6d6f56b 4 http://192.168.1.100/rpc/DigitalOut/new LED1 myled1
botdream 0:c308e6d6f56b 5 http://192.168.1.100/rpc/DigitalOut/new LED2 myled2
botdream 0:c308e6d6f56b 6 http://192.168.1.100/rpc/DigitalOut/new LED3 myled3
botdream 0:c308e6d6f56b 7 http://192.168.1.100/rpc/DigitalOut/new LED4 myled4
botdream 0:c308e6d6f56b 8
botdream 0:c308e6d6f56b 9 http://192.168.1.100/rpc/
botdream 0:c308e6d6f56b 10
botdream 0:c308e6d6f56b 11 http://192.168.1.100/rpc/myled1/read
botdream 0:c308e6d6f56b 12 http://192.168.1.100/rpc/myled1/write 1
botdream 0:c308e6d6f56b 13
botdream 0:c308e6d6f56b 14 http://192.168.1.100/rpc/myled2/write 2
botdream 0:c308e6d6f56b 15 http://192.168.1.100/rpc/myled3/write 3
botdream 0:c308e6d6f56b 16 http://192.168.1.100/rpc/myled4/write 4
botdream 0:c308e6d6f56b 17
botdream 0:c308e6d6f56b 18 //---------------------------------------------------------------------------------------------
botdream 0:c308e6d6f56b 19 // resources
botdream 0:c308e6d6f56b 20 //---------------------------------------------------------------------------------------------
botdream 0:c308e6d6f56b 21 http://mbed.org/handbook/C-Data-Types
botdream 0:c308e6d6f56b 22 http://mbed.org/cookbook/RPC-Interface-Library
botdream 0:c308e6d6f56b 23 http://mbed.org/cookbook/HTTP-Server
botdream 0:c308e6d6f56b 24 http://mbed.org/cookbook/Ethernet
botdream 0:c308e6d6f56b 25 http://mbed.org/handbook/Ticker
botdream 0:c308e6d6f56b 26 //--------------------------------------------------------------------------------------------- */
botdream 0:c308e6d6f56b 27 #include "mbed.h"
botdream 0:c308e6d6f56b 28 #include "EthernetNetIf.h"
botdream 0:c308e6d6f56b 29 #include "HTTPServer.h"
botdream 0:c308e6d6f56b 30 #include "RPCVariable.h"
botdream 0:c308e6d6f56b 31 #include "RPCFunction.h"
botdream 0:c308e6d6f56b 32 //---------------------------------------------------------------------------------------------
botdream 0:c308e6d6f56b 33 DigitalOut myled1(LED1);
botdream 0:c308e6d6f56b 34 DigitalOut myled2(LED2);
botdream 0:c308e6d6f56b 35 DigitalOut myled3(LED3);
botdream 0:c308e6d6f56b 36 DigitalOut myled4(LED4);
botdream 0:c308e6d6f56b 37 Serial pc(USBTX, USBRX); // tx, rx
botdream 0:c308e6d6f56b 38 //---------------------------------------------------------------------------------------------
botdream 0:c308e6d6f56b 39 #define internaldebug // send debug messages to USB Serial port (9600,1,N)
botdream 0:c308e6d6f56b 40 //#define dhcpenable // auto-setup IP Address from DHCP router
botdream 0:c308e6d6f56b 41 //---------------------------------------------------------------------------------------------
botdream 0:c308e6d6f56b 42 // Timer Interrupt - NetPool
botdream 0:c308e6d6f56b 43 //---------------------------------------------------------------------------------------------
botdream 0:c308e6d6f56b 44 Ticker netpool;
botdream 0:c308e6d6f56b 45 //---------------------------------------------------------------------------------------------
botdream 0:c308e6d6f56b 46 // Ethernet Object Setup
botdream 0:c308e6d6f56b 47 //---------------------------------------------------------------------------------------------
botdream 0:c308e6d6f56b 48 #ifdef dhcpenable
botdream 0:c308e6d6f56b 49 EthernetNetIf eth;
botdream 0:c308e6d6f56b 50 #else
botdream 0:c308e6d6f56b 51 EthernetNetIf eth(
botdream 0:c308e6d6f56b 52 IpAddr(192,168,1,100), //IP Address
botdream 0:c308e6d6f56b 53 IpAddr(255,255,255,0), //Network Mask
botdream 0:c308e6d6f56b 54 IpAddr(192,168,1,254), //Gateway
botdream 0:c308e6d6f56b 55 IpAddr(192,168,1,254) //DNS
botdream 0:c308e6d6f56b 56 );
botdream 0:c308e6d6f56b 57 #endif
botdream 0:c308e6d6f56b 58 //---------------------------------------------------------------------------------------------
botdream 0:c308e6d6f56b 59 // HTTP Server
botdream 0:c308e6d6f56b 60 //---------------------------------------------------------------------------------------------
botdream 0:c308e6d6f56b 61 HTTPServer httpserver;
botdream 0:c308e6d6f56b 62 LocalFileSystem fs("webfs");
botdream 0:c308e6d6f56b 63 //---------------------------------------------------------------------------------------------
botdream 0:c308e6d6f56b 64
botdream 0:c308e6d6f56b 65 //---------------------------------------------------------------------------------------------
botdream 0:c308e6d6f56b 66 // ISR -> Pool Ethernet - will be triggered by netpool ticker
botdream 0:c308e6d6f56b 67 //---------------------------------------------------------------------------------------------
botdream 0:c308e6d6f56b 68 void netpoolupdate()
botdream 0:c308e6d6f56b 69 {
botdream 0:c308e6d6f56b 70 Net::poll();
botdream 0:c308e6d6f56b 71 }
botdream 0:c308e6d6f56b 72 //---------------------------------------------------------------------------------------------
botdream 0:c308e6d6f56b 73
botdream 0:c308e6d6f56b 74 //---------------------------------------------------------------------------------------------
botdream 0:c308e6d6f56b 75 // MAIN
botdream 0:c308e6d6f56b 76 //---------------------------------------------------------------------------------------------
botdream 0:c308e6d6f56b 77 int main()
botdream 0:c308e6d6f56b 78 {
botdream 0:c308e6d6f56b 79 // Set Serial Port Transfer Rate
botdream 0:c308e6d6f56b 80 pc.baud(115200);
botdream 0:c308e6d6f56b 81
botdream 0:c308e6d6f56b 82 //--------------------------------------------------------
botdream 0:c308e6d6f56b 83 // Setting RPC
botdream 0:c308e6d6f56b 84 //--------------------------------------------------------
botdream 0:c308e6d6f56b 85 Base::add_rpc_class<AnalogIn>();
botdream 0:c308e6d6f56b 86 Base::add_rpc_class<AnalogOut>();
botdream 0:c308e6d6f56b 87 Base::add_rpc_class<DigitalIn>();
botdream 0:c308e6d6f56b 88 Base::add_rpc_class<DigitalOut>();
botdream 0:c308e6d6f56b 89 Base::add_rpc_class<DigitalInOut>();
botdream 0:c308e6d6f56b 90 Base::add_rpc_class<PwmOut>();
botdream 0:c308e6d6f56b 91 Base::add_rpc_class<Timer>();
botdream 0:c308e6d6f56b 92 Base::add_rpc_class<SPI>();
botdream 0:c308e6d6f56b 93 Base::add_rpc_class<BusOut>();
botdream 0:c308e6d6f56b 94 Base::add_rpc_class<BusIn>();
botdream 0:c308e6d6f56b 95 Base::add_rpc_class<BusInOut>();
botdream 0:c308e6d6f56b 96 Base::add_rpc_class<Serial>();
botdream 0:c308e6d6f56b 97 //--------------------------------------------------------
botdream 0:c308e6d6f56b 98
botdream 0:c308e6d6f56b 99 //--------------------------------------------------------
botdream 0:c308e6d6f56b 100 // Setting Ethernet
botdream 0:c308e6d6f56b 101 //--------------------------------------------------------
botdream 0:c308e6d6f56b 102 #ifdef internaldebug
botdream 0:c308e6d6f56b 103 printf("\r\nSetting up Ethernet interface!\r\n");
botdream 0:c308e6d6f56b 104 #endif
botdream 0:c308e6d6f56b 105 // Create return object for error check
botdream 0:c308e6d6f56b 106 EthernetErr ethErr = eth.setup();
botdream 0:c308e6d6f56b 107 if(ethErr)
botdream 0:c308e6d6f56b 108 {
botdream 0:c308e6d6f56b 109 #ifdef internaldebug
botdream 0:c308e6d6f56b 110 printf("\r\nError %d in Ethernet setup.\r\n", ethErr);
botdream 0:c308e6d6f56b 111 #endif
botdream 0:c308e6d6f56b 112 return -1;
botdream 0:c308e6d6f56b 113 }
botdream 0:c308e6d6f56b 114 #ifdef internaldebug
botdream 0:c308e6d6f56b 115 printf("\r\nEthernet setup completed with success!\r\n");
botdream 0:c308e6d6f56b 116 #endif
botdream 0:c308e6d6f56b 117 //--------------------------------------------------------
botdream 0:c308e6d6f56b 118
botdream 0:c308e6d6f56b 119 //--------------------------------------------------------
botdream 0:c308e6d6f56b 120 // adding Handlers
botdream 0:c308e6d6f56b 121 //--------------------------------------------------------
botdream 0:c308e6d6f56b 122 FSHandler::mount("/webfs", "/"); //Mount /webfs path on web root path
botdream 0:c308e6d6f56b 123
botdream 0:c308e6d6f56b 124 httpserver.addHandler<RPCHandler>("/rpc");
botdream 0:c308e6d6f56b 125 httpserver.addHandler<FSHandler>("/"); //Default handler
botdream 0:c308e6d6f56b 126 //--------------------------------------------------------
botdream 0:c308e6d6f56b 127
botdream 0:c308e6d6f56b 128 //--------------------------------------------------------
botdream 0:c308e6d6f56b 129 // bind http server to port 80 (Listen)
botdream 0:c308e6d6f56b 130 //--------------------------------------------------------
botdream 0:c308e6d6f56b 131 httpserver.bind(80);
botdream 0:c308e6d6f56b 132 #ifdef internaldebug
botdream 0:c308e6d6f56b 133 printf("Listening on port 80!\r\n");
botdream 0:c308e6d6f56b 134 #endif
botdream 0:c308e6d6f56b 135 //--------------------------------------------------------
botdream 0:c308e6d6f56b 136
botdream 0:c308e6d6f56b 137 //--------------------------------------------------------
botdream 0:c308e6d6f56b 138 // ISR -> attach timer interrupt to update Net::Pool();
botdream 0:c308e6d6f56b 139 //--------------------------------------------------------
botdream 0:c308e6d6f56b 140 netpool.attach(&netpoolupdate, 0.1);
botdream 0:c308e6d6f56b 141 //--------------------------------------------------------
botdream 0:c308e6d6f56b 142
botdream 0:c308e6d6f56b 143 //--------------------------------------------------------
botdream 0:c308e6d6f56b 144 // main loop
botdream 0:c308e6d6f56b 145 //--------------------------------------------------------
botdream 0:c308e6d6f56b 146 // main loop
botdream 0:c308e6d6f56b 147 while(1)
botdream 0:c308e6d6f56b 148 wait(0.5);
botdream 0:c308e6d6f56b 149 }
botdream 0:c308e6d6f56b 150 //---------------------------------------------------------------------------------------------