Code to demonstrate how to map 2 RPC functions to command 2 servo motors via REST HTTP Get request. Pretty useful to control a webcam PAN and TILT position when using Servos in this support: http://www.coolcomponents.co.uk/catalog/product_info.php?products_id=470&osCsid=j90j5kqfegquksdbumahtmuqg5 There is also a servo.html file to test some HTML5 slider RANGE object to set the Servo position and make the Http Get request via AJAX (asynchronous JavaScript call) Note: Demo program to be used on the GeekSessionLab Talk (November 2011). http://devrendezvous.com/?lang=en

Dependencies:   EthernetNetIf mbed HTTPServer Servo

Committer:
botdream
Date:
Thu Oct 27 23:28:15 2011 +0000
Revision:
0:fd56ae0fc8ea

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
botdream 0:fd56ae0fc8ea 1 /* //---------------------------------------------------------------------------------------------
botdream 0:fd56ae0fc8ea 2 // RPC ServoCmd
botdream 0:fd56ae0fc8ea 3 http://192.168.1.100/rpc/servocmd1/run 0
botdream 0:fd56ae0fc8ea 4 http://192.168.1.100/rpc/servocmd1/run 50
botdream 0:fd56ae0fc8ea 5 http://192.168.1.100/rpc/servocmd1/run 100
botdream 0:fd56ae0fc8ea 6
botdream 0:fd56ae0fc8ea 7 http://192.168.1.100/rpc/servocmd2/run 0
botdream 0:fd56ae0fc8ea 8 http://192.168.1.100/rpc/servocmd2/run 50
botdream 0:fd56ae0fc8ea 9 http://192.168.1.100/rpc/servocmd2/run 100
botdream 0:fd56ae0fc8ea 10
botdream 0:fd56ae0fc8ea 11 // HTML5 demo (use chrome or html5 compatible browser)
botdream 0:fd56ae0fc8ea 12 -> copy servo.htm.cpp file to MBED internal flash memory (where the binary code is uploaded)
botdream 0:fd56ae0fc8ea 13 -> open file with an TextEditor and remove comments indicator chars "/*" and "*\/"
botdream 0:fd56ae0fc8ea 14 -> rename servo.htm.cpp to servo.htm
botdream 0:fd56ae0fc8ea 15 -> open browser http://192.168.1.100/servo.htm
botdream 0:fd56ae0fc8ea 16
botdream 0:fd56ae0fc8ea 17 // Hardware
botdream 0:fd56ae0fc8ea 18 -> Connect Servo1 Signal pin to P21 and Servo2 Signal pin to P22
botdream 0:fd56ae0fc8ea 19
botdream 0:fd56ae0fc8ea 20 //---------------------------------------------------------------------------------------------
botdream 0:fd56ae0fc8ea 21 // resources
botdream 0:fd56ae0fc8ea 22 //---------------------------------------------------------------------------------------------
botdream 0:fd56ae0fc8ea 23 http://mbed.org/handbook/C-Data-Types
botdream 0:fd56ae0fc8ea 24 http://mbed.org/cookbook/RPC-Interface-Library
botdream 0:fd56ae0fc8ea 25 http://mbed.org/cookbook/HTTP-Server
botdream 0:fd56ae0fc8ea 26 http://mbed.org/cookbook/Ethernet
botdream 0:fd56ae0fc8ea 27 http://mbed.org/handbook/Ticker
botdream 0:fd56ae0fc8ea 28 //--------------------------------------------------------------------------------------------- */
botdream 0:fd56ae0fc8ea 29 #include "mbed.h"
botdream 0:fd56ae0fc8ea 30 #include "EthernetNetIf.h"
botdream 0:fd56ae0fc8ea 31 #include "HTTPServer.h"
botdream 0:fd56ae0fc8ea 32 #include "RPCFunction.h"
botdream 0:fd56ae0fc8ea 33 #include "Servo.h"
botdream 0:fd56ae0fc8ea 34 //---------------------------------------------------------------------------------------------
botdream 0:fd56ae0fc8ea 35 DigitalOut myled(LED1);
botdream 0:fd56ae0fc8ea 36 Serial pc(USBTX, USBRX); // tx, rx
botdream 0:fd56ae0fc8ea 37 //---------------------------------------------------------------------------------------------
botdream 0:fd56ae0fc8ea 38 //#define internaldebug // send debug messages to USB Serial port (9600,1,N)
botdream 0:fd56ae0fc8ea 39 //#define dhcpenable // auto-setup IP Address from DHCP router
botdream 0:fd56ae0fc8ea 40 //---------------------------------------------------------------------------------------------
botdream 0:fd56ae0fc8ea 41 // Timer Interrupt - NetPool
botdream 0:fd56ae0fc8ea 42 //---------------------------------------------------------------------------------------------
botdream 0:fd56ae0fc8ea 43 Ticker netpool;
botdream 0:fd56ae0fc8ea 44 //---------------------------------------------------------------------------------------------
botdream 0:fd56ae0fc8ea 45 // Ethernet Object Setup
botdream 0:fd56ae0fc8ea 46 //---------------------------------------------------------------------------------------------
botdream 0:fd56ae0fc8ea 47 #ifdef dhcpenable
botdream 0:fd56ae0fc8ea 48 EthernetNetIf eth;
botdream 0:fd56ae0fc8ea 49 #else
botdream 0:fd56ae0fc8ea 50 EthernetNetIf eth(
botdream 0:fd56ae0fc8ea 51 IpAddr(192,168,1,100), //IP Address
botdream 0:fd56ae0fc8ea 52 IpAddr(255,255,255,0), //Network Mask
botdream 0:fd56ae0fc8ea 53 IpAddr(192,168,1,254), //Gateway
botdream 0:fd56ae0fc8ea 54 IpAddr(192,168,1,254) //DNS
botdream 0:fd56ae0fc8ea 55 );
botdream 0:fd56ae0fc8ea 56 #endif
botdream 0:fd56ae0fc8ea 57 //---------------------------------------------------------------------------------------------
botdream 0:fd56ae0fc8ea 58 // HTTP Server
botdream 0:fd56ae0fc8ea 59 //---------------------------------------------------------------------------------------------
botdream 0:fd56ae0fc8ea 60 HTTPServer httpserver;
botdream 0:fd56ae0fc8ea 61 LocalFileSystem fs("webfs");
botdream 0:fd56ae0fc8ea 62
botdream 0:fd56ae0fc8ea 63 Servo servo1(p21);
botdream 0:fd56ae0fc8ea 64 Servo servo2(p22);
botdream 0:fd56ae0fc8ea 65
botdream 0:fd56ae0fc8ea 66 int8_t servo_flag;
botdream 0:fd56ae0fc8ea 67 float servo_cursor1;
botdream 0:fd56ae0fc8ea 68 float servo_cursor2;
botdream 0:fd56ae0fc8ea 69
botdream 0:fd56ae0fc8ea 70 int8_t pool_flag;
botdream 0:fd56ae0fc8ea 71 //---------------------------------------------------------------------------------------------
botdream 0:fd56ae0fc8ea 72
botdream 0:fd56ae0fc8ea 73 //---------------------------------------------------------------------------------------------
botdream 0:fd56ae0fc8ea 74 // ISR -> Pool Ethernet - will be triggered by netpool ticker
botdream 0:fd56ae0fc8ea 75 //---------------------------------------------------------------------------------------------
botdream 0:fd56ae0fc8ea 76 void netpoolupdate()
botdream 0:fd56ae0fc8ea 77 {
botdream 0:fd56ae0fc8ea 78 if(pool_flag !=0)
botdream 0:fd56ae0fc8ea 79 return;
botdream 0:fd56ae0fc8ea 80
botdream 0:fd56ae0fc8ea 81 pool_flag = 1; // start processing ...
botdream 0:fd56ae0fc8ea 82 Net::poll();
botdream 0:fd56ae0fc8ea 83 pool_flag = 0; // end processing ...
botdream 0:fd56ae0fc8ea 84 }
botdream 0:fd56ae0fc8ea 85 //---------------------------------------------------------------------------------------------
botdream 0:fd56ae0fc8ea 86
botdream 0:fd56ae0fc8ea 87 //---------------------------------------------------------------------------------------------
botdream 0:fd56ae0fc8ea 88 // Generic ServoCmd function
botdream 0:fd56ae0fc8ea 89 //---------------------------------------------------------------------------------------------
botdream 0:fd56ae0fc8ea 90 void servocmd(char *input, char *output, int8_t servonbr)
botdream 0:fd56ae0fc8ea 91 {
botdream 0:fd56ae0fc8ea 92 if(servo_flag != 0) // busy ...
botdream 0:fd56ae0fc8ea 93 {
botdream 0:fd56ae0fc8ea 94 #ifdef internaldebug
botdream 0:fd56ae0fc8ea 95 printf("ServoCmd%d is busy. Return from function!\r\n",servonbr);
botdream 0:fd56ae0fc8ea 96 #endif
botdream 0:fd56ae0fc8ea 97
botdream 0:fd56ae0fc8ea 98 return;
botdream 0:fd56ae0fc8ea 99 }
botdream 0:fd56ae0fc8ea 100 servo_flag = 1; // REST request
botdream 0:fd56ae0fc8ea 101
botdream 0:fd56ae0fc8ea 102 int iarg1 = 0;
botdream 0:fd56ae0fc8ea 103 sscanf(input, "%i", &iarg1);
botdream 0:fd56ae0fc8ea 104
botdream 0:fd56ae0fc8ea 105 #ifdef internaldebug
botdream 0:fd56ae0fc8ea 106 printf("Calling RCP Function ServoCmd%d.\r\n",servonbr);
botdream 0:fd56ae0fc8ea 107 //printf("INPUT: %s.\r\n", input);
botdream 0:fd56ae0fc8ea 108 //printf("OUTPUT: %s.\r\n", output);
botdream 0:fd56ae0fc8ea 109 printf("ARG1: %d\r\n", iarg1);
botdream 0:fd56ae0fc8ea 110 #endif
botdream 0:fd56ae0fc8ea 111
botdream 0:fd56ae0fc8ea 112 if(servonbr == 1)
botdream 0:fd56ae0fc8ea 113 {
botdream 0:fd56ae0fc8ea 114 servo_cursor1 = (float)iarg1/(float)100.0;
botdream 0:fd56ae0fc8ea 115 #ifdef internaldebug
botdream 0:fd56ae0fc8ea 116 sprintf(output, "<html><body>RCP ServoCmd1 Completed!<br>PARAM=%d<br>Cursor=%f</body></html>", iarg1, servo_cursor1);
botdream 0:fd56ae0fc8ea 117 #else
botdream 0:fd56ae0fc8ea 118 sprintf(output, "<html><body>OK</body></html>");
botdream 0:fd56ae0fc8ea 119 #endif
botdream 0:fd56ae0fc8ea 120
botdream 0:fd56ae0fc8ea 121 }
botdream 0:fd56ae0fc8ea 122 else if(servonbr == 2)
botdream 0:fd56ae0fc8ea 123 {
botdream 0:fd56ae0fc8ea 124 servo_cursor2 = (float)iarg1/(float)100.0;
botdream 0:fd56ae0fc8ea 125 #ifdef internaldebug
botdream 0:fd56ae0fc8ea 126 sprintf(output, "<html><body>RCP ServoCmd2 Completed!<br>PARAM=%d<br>Cursor=%f</body></html>", iarg1, servo_cursor2);
botdream 0:fd56ae0fc8ea 127 #else
botdream 0:fd56ae0fc8ea 128 sprintf(output, "<html><body>OK</body></html>");
botdream 0:fd56ae0fc8ea 129 #endif
botdream 0:fd56ae0fc8ea 130 }
botdream 0:fd56ae0fc8ea 131 else
botdream 0:fd56ae0fc8ea 132 {
botdream 0:fd56ae0fc8ea 133 #ifdef internaldebug
botdream 0:fd56ae0fc8ea 134 sprintf(output, "<html><body>RCP ServoCmd%d Error<br>PARAM=%d</body></html>", servonbr, iarg1);
botdream 0:fd56ae0fc8ea 135 #else
botdream 0:fd56ae0fc8ea 136 sprintf(output, "<html><body>ERROR</body></html>");
botdream 0:fd56ae0fc8ea 137 #endif
botdream 0:fd56ae0fc8ea 138 }
botdream 0:fd56ae0fc8ea 139
botdream 0:fd56ae0fc8ea 140 servo_flag = 2; // can now update servos
botdream 0:fd56ae0fc8ea 141 }
botdream 0:fd56ae0fc8ea 142 //---------------------------------------------------------------------------------------------
botdream 0:fd56ae0fc8ea 143 // RPC TList Commands (Task/Job List)
botdream 0:fd56ae0fc8ea 144 //---------------------------------------------------------------------------------------------
botdream 0:fd56ae0fc8ea 145 /*
botdream 0:fd56ae0fc8ea 146 void rpc_dummy(char *input, char *output)
botdream 0:fd56ae0fc8ea 147 {
botdream 0:fd56ae0fc8ea 148 // dummy!!!
botdream 0:fd56ae0fc8ea 149 }
botdream 0:fd56ae0fc8ea 150 //--------------------------------------------------------------------------------------------- */
botdream 0:fd56ae0fc8ea 151
botdream 0:fd56ae0fc8ea 152 void rpc_servocmd1(char *input, char *output)
botdream 0:fd56ae0fc8ea 153 {
botdream 0:fd56ae0fc8ea 154 servocmd(input, output, 1);
botdream 0:fd56ae0fc8ea 155 }
botdream 0:fd56ae0fc8ea 156 //---------------------------------------------------------------------------------------------
botdream 0:fd56ae0fc8ea 157
botdream 0:fd56ae0fc8ea 158 void rpc_servocmd2(char *input, char *output)
botdream 0:fd56ae0fc8ea 159 {
botdream 0:fd56ae0fc8ea 160 servocmd(input, output, 2);
botdream 0:fd56ae0fc8ea 161 }
botdream 0:fd56ae0fc8ea 162 //---------------------------------------------------------------------------------------------
botdream 0:fd56ae0fc8ea 163
botdream 0:fd56ae0fc8ea 164 //---------------------------------------------------------------------------------------------
botdream 0:fd56ae0fc8ea 165 // MAIN
botdream 0:fd56ae0fc8ea 166 //---------------------------------------------------------------------------------------------
botdream 0:fd56ae0fc8ea 167 int main()
botdream 0:fd56ae0fc8ea 168 {
botdream 0:fd56ae0fc8ea 169 // Set Serial Port Transfer Rate
botdream 0:fd56ae0fc8ea 170 pc.baud(115200);
botdream 0:fd56ae0fc8ea 171
botdream 0:fd56ae0fc8ea 172 //--------------------------------------------------------
botdream 0:fd56ae0fc8ea 173 servo_flag = 0; // 0-NOP; 1-REST request; 2-update servos;
botdream 0:fd56ae0fc8ea 174 servo_cursor1 = 0.5;
botdream 0:fd56ae0fc8ea 175 servo_cursor2 = 0.5;
botdream 0:fd56ae0fc8ea 176 servo1 = 0.5;
botdream 0:fd56ae0fc8ea 177 servo2 = 0.5;
botdream 0:fd56ae0fc8ea 178
botdream 0:fd56ae0fc8ea 179 pool_flag = 0;
botdream 0:fd56ae0fc8ea 180 //--------------------------------------------------------
botdream 0:fd56ae0fc8ea 181 // Setting Ethernet
botdream 0:fd56ae0fc8ea 182 //--------------------------------------------------------
botdream 0:fd56ae0fc8ea 183 #ifdef internaldebug
botdream 0:fd56ae0fc8ea 184 printf("\r\nSetting up Ethernet interface!\r\n");
botdream 0:fd56ae0fc8ea 185 #endif
botdream 0:fd56ae0fc8ea 186 // Create return object for error check
botdream 0:fd56ae0fc8ea 187 EthernetErr ethErr = eth.setup();
botdream 0:fd56ae0fc8ea 188 if(ethErr)
botdream 0:fd56ae0fc8ea 189 {
botdream 0:fd56ae0fc8ea 190 #ifdef internaldebug
botdream 0:fd56ae0fc8ea 191 printf("\r\nError %d in Ethernet setup.\r\n", ethErr);
botdream 0:fd56ae0fc8ea 192 #endif
botdream 0:fd56ae0fc8ea 193 return -1;
botdream 0:fd56ae0fc8ea 194 }
botdream 0:fd56ae0fc8ea 195 #ifdef internaldebug
botdream 0:fd56ae0fc8ea 196 printf("\r\nEthernet setup completed with success!\r\n");
botdream 0:fd56ae0fc8ea 197 #endif
botdream 0:fd56ae0fc8ea 198 //--------------------------------------------------------
botdream 0:fd56ae0fc8ea 199
botdream 0:fd56ae0fc8ea 200 //--------------------------------------------------------
botdream 0:fd56ae0fc8ea 201 // adding RPC functions
botdream 0:fd56ae0fc8ea 202 //--------------------------------------------------------
botdream 0:fd56ae0fc8ea 203 //RPCFunction RPCdummy(&rpc_dummy, "dummy");
botdream 0:fd56ae0fc8ea 204 RPCFunction RPCservocmd1(&rpc_servocmd1, "servocmd1");
botdream 0:fd56ae0fc8ea 205 RPCFunction RPCservocmd2(&rpc_servocmd2, "servocmd2");
botdream 0:fd56ae0fc8ea 206 //--------------------------------------------------------
botdream 0:fd56ae0fc8ea 207
botdream 0:fd56ae0fc8ea 208 //--------------------------------------------------------
botdream 0:fd56ae0fc8ea 209 // adding Handlers
botdream 0:fd56ae0fc8ea 210 //--------------------------------------------------------
botdream 0:fd56ae0fc8ea 211 FSHandler::mount("/webfs", "/"); //Mount /webfs path on web root path
botdream 0:fd56ae0fc8ea 212
botdream 0:fd56ae0fc8ea 213 httpserver.addHandler<RPCHandler>("/rpc");
botdream 0:fd56ae0fc8ea 214 httpserver.addHandler<FSHandler>("/"); //Default handler
botdream 0:fd56ae0fc8ea 215 //--------------------------------------------------------
botdream 0:fd56ae0fc8ea 216
botdream 0:fd56ae0fc8ea 217 //--------------------------------------------------------
botdream 0:fd56ae0fc8ea 218 // bind http server to port 80 (Listen)
botdream 0:fd56ae0fc8ea 219 //--------------------------------------------------------
botdream 0:fd56ae0fc8ea 220 httpserver.bind(80);
botdream 0:fd56ae0fc8ea 221 #ifdef internaldebug
botdream 0:fd56ae0fc8ea 222 printf("Listening on port 80!\r\n");
botdream 0:fd56ae0fc8ea 223 #endif
botdream 0:fd56ae0fc8ea 224 //--------------------------------------------------------
botdream 0:fd56ae0fc8ea 225
botdream 0:fd56ae0fc8ea 226 //--------------------------------------------------------
botdream 0:fd56ae0fc8ea 227 // ISR -> attach timer interrupt to update Net::Pool();
botdream 0:fd56ae0fc8ea 228 //--------------------------------------------------------
botdream 0:fd56ae0fc8ea 229 //netpool.attach(&netpoolupdate, 0.1);
botdream 0:fd56ae0fc8ea 230 //--------------------------------------------------------
botdream 0:fd56ae0fc8ea 231
botdream 0:fd56ae0fc8ea 232 //--------------------------------------------------------
botdream 0:fd56ae0fc8ea 233 // main loop
botdream 0:fd56ae0fc8ea 234 //--------------------------------------------------------
botdream 0:fd56ae0fc8ea 235 while(1)
botdream 0:fd56ae0fc8ea 236 {
botdream 0:fd56ae0fc8ea 237 /*
botdream 0:fd56ae0fc8ea 238 myled = 1;
botdream 0:fd56ae0fc8ea 239 wait(0.5);
botdream 0:fd56ae0fc8ea 240 myled = 0;
botdream 0:fd56ae0fc8ea 241 wait(0.5);
botdream 0:fd56ae0fc8ea 242 */
botdream 0:fd56ae0fc8ea 243
botdream 0:fd56ae0fc8ea 244 if(servo_flag == 2) // update servos
botdream 0:fd56ae0fc8ea 245 {
botdream 0:fd56ae0fc8ea 246 #ifdef internaldebug
botdream 0:fd56ae0fc8ea 247 printf("Updating Servos!\r\n");
botdream 0:fd56ae0fc8ea 248 #endif
botdream 0:fd56ae0fc8ea 249
botdream 0:fd56ae0fc8ea 250 servo1.write(servo_cursor1);
botdream 0:fd56ae0fc8ea 251 servo2.write(servo_cursor2);
botdream 0:fd56ae0fc8ea 252 servo_flag = 0; // NOP
botdream 0:fd56ae0fc8ea 253 }
botdream 0:fd56ae0fc8ea 254 wait(0.1);
botdream 0:fd56ae0fc8ea 255 netpoolupdate();
botdream 0:fd56ae0fc8ea 256 }
botdream 0:fd56ae0fc8ea 257 }
botdream 0:fd56ae0fc8ea 258 //---------------------------------------------------------------------------------------------