traffic light controller with RPC over http interrupter
Dependencies: EthernetInterface HTTPClient HTTPServer mbed-rpc mbed-rtos mbed
main.cpp@2:bf0da8f925e0, 2018-08-03 (annotated)
- Committer:
- daklowprofile
- Date:
- Fri Aug 03 08:07:51 2018 +0000
- Revision:
- 2:bf0da8f925e0
- Parent:
- 1:2c577df21639
RPC over http + RPC function + traffic light controller ; (need improvements for http post and get on time, need faster response maybe)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
daklowprofile | 0:36fbb19fb8fb | 1 | #include "mbed.h" |
daklowprofile | 0:36fbb19fb8fb | 2 | #include "EthernetInterface.h" |
daklowprofile | 0:36fbb19fb8fb | 3 | #include "HTTPServer.h" |
daklowprofile | 0:36fbb19fb8fb | 4 | #include "HTTPClient.h" |
daklowprofile | 0:36fbb19fb8fb | 5 | #include "mbed_rpc.h" |
daklowprofile | 0:36fbb19fb8fb | 6 | |
daklowprofile | 0:36fbb19fb8fb | 7 | Timer timer_tf; |
daklowprofile | 0:36fbb19fb8fb | 8 | |
daklowprofile | 0:36fbb19fb8fb | 9 | Serial pc(USBTX, USBRX); |
daklowprofile | 0:36fbb19fb8fb | 10 | |
daklowprofile | 0:36fbb19fb8fb | 11 | DigitalOut led1(LED1); |
daklowprofile | 0:36fbb19fb8fb | 12 | DigitalOut led2(LED2); |
daklowprofile | 0:36fbb19fb8fb | 13 | DigitalOut led3(LED3); |
daklowprofile | 0:36fbb19fb8fb | 14 | |
daklowprofile | 0:36fbb19fb8fb | 15 | //T1 |
daklowprofile | 0:36fbb19fb8fb | 16 | DigitalOut t1red(p5); |
daklowprofile | 0:36fbb19fb8fb | 17 | DigitalOut t1yellow(p6); |
daklowprofile | 0:36fbb19fb8fb | 18 | DigitalOut t1green(p7); |
daklowprofile | 0:36fbb19fb8fb | 19 | //T2 |
daklowprofile | 0:36fbb19fb8fb | 20 | DigitalOut t2red(p11); |
daklowprofile | 0:36fbb19fb8fb | 21 | DigitalOut t2yellow(p12); |
daklowprofile | 0:36fbb19fb8fb | 22 | DigitalOut t2green(p13); |
daklowprofile | 0:36fbb19fb8fb | 23 | //T3 |
daklowprofile | 0:36fbb19fb8fb | 24 | DigitalOut t3red(p18); |
daklowprofile | 0:36fbb19fb8fb | 25 | DigitalOut t3yellow(p19); |
daklowprofile | 0:36fbb19fb8fb | 26 | DigitalOut t3green(p17); |
daklowprofile | 0:36fbb19fb8fb | 27 | //pin 20 cannot use for led |
daklowprofile | 0:36fbb19fb8fb | 28 | |
daklowprofile | 0:36fbb19fb8fb | 29 | void t1light(char a){ |
daklowprofile | 0:36fbb19fb8fb | 30 | t1red = 0; |
daklowprofile | 0:36fbb19fb8fb | 31 | t1yellow = 0; |
daklowprofile | 0:36fbb19fb8fb | 32 | t1green = 0; |
daklowprofile | 0:36fbb19fb8fb | 33 | if (a == 'R'){ |
daklowprofile | 0:36fbb19fb8fb | 34 | t1red = 1; |
daklowprofile | 0:36fbb19fb8fb | 35 | t1yellow = 0; |
daklowprofile | 0:36fbb19fb8fb | 36 | t1green = 0; |
daklowprofile | 0:36fbb19fb8fb | 37 | }else if(a == 'Y'){ |
daklowprofile | 0:36fbb19fb8fb | 38 | t1red = 0; |
daklowprofile | 0:36fbb19fb8fb | 39 | t1yellow = 1; |
daklowprofile | 0:36fbb19fb8fb | 40 | t1green = 0; |
daklowprofile | 0:36fbb19fb8fb | 41 | }else if(a == 'G'){ |
daklowprofile | 0:36fbb19fb8fb | 42 | t1red = 0; |
daklowprofile | 0:36fbb19fb8fb | 43 | t1yellow = 0; |
daklowprofile | 0:36fbb19fb8fb | 44 | t1green = 1; |
daklowprofile | 0:36fbb19fb8fb | 45 | }else{ |
daklowprofile | 0:36fbb19fb8fb | 46 | t1red = 0; |
daklowprofile | 0:36fbb19fb8fb | 47 | t1yellow = 0; |
daklowprofile | 0:36fbb19fb8fb | 48 | t1green = 0; |
daklowprofile | 0:36fbb19fb8fb | 49 | } |
daklowprofile | 0:36fbb19fb8fb | 50 | } |
daklowprofile | 0:36fbb19fb8fb | 51 | |
daklowprofile | 0:36fbb19fb8fb | 52 | void t2light(char a){ |
daklowprofile | 0:36fbb19fb8fb | 53 | t2red = 0; |
daklowprofile | 0:36fbb19fb8fb | 54 | t2yellow = 0; |
daklowprofile | 0:36fbb19fb8fb | 55 | t2green = 0; |
daklowprofile | 0:36fbb19fb8fb | 56 | if (a == 'R'){ |
daklowprofile | 0:36fbb19fb8fb | 57 | t2red = 1; |
daklowprofile | 0:36fbb19fb8fb | 58 | t2yellow = 0; |
daklowprofile | 0:36fbb19fb8fb | 59 | t2green = 0; |
daklowprofile | 0:36fbb19fb8fb | 60 | }else if(a == 'Y'){ |
daklowprofile | 0:36fbb19fb8fb | 61 | t2red = 0; |
daklowprofile | 0:36fbb19fb8fb | 62 | t2yellow = 1; |
daklowprofile | 0:36fbb19fb8fb | 63 | t2green = 0; |
daklowprofile | 0:36fbb19fb8fb | 64 | }else if(a == 'G'){ |
daklowprofile | 0:36fbb19fb8fb | 65 | t2red = 0; |
daklowprofile | 0:36fbb19fb8fb | 66 | t2yellow = 0; |
daklowprofile | 0:36fbb19fb8fb | 67 | t2green = 1; |
daklowprofile | 0:36fbb19fb8fb | 68 | }else{ |
daklowprofile | 0:36fbb19fb8fb | 69 | t2red = 0; |
daklowprofile | 0:36fbb19fb8fb | 70 | t2yellow = 0; |
daklowprofile | 0:36fbb19fb8fb | 71 | t2green = 0; |
daklowprofile | 0:36fbb19fb8fb | 72 | } |
daklowprofile | 0:36fbb19fb8fb | 73 | } |
daklowprofile | 0:36fbb19fb8fb | 74 | |
daklowprofile | 0:36fbb19fb8fb | 75 | void t3light(char a){ |
daklowprofile | 0:36fbb19fb8fb | 76 | t3red = 0; |
daklowprofile | 0:36fbb19fb8fb | 77 | t3yellow = 0; |
daklowprofile | 0:36fbb19fb8fb | 78 | t3green = 0; |
daklowprofile | 0:36fbb19fb8fb | 79 | if (a == 'R'){ |
daklowprofile | 0:36fbb19fb8fb | 80 | t3red = 1; |
daklowprofile | 0:36fbb19fb8fb | 81 | t3yellow = 0; |
daklowprofile | 0:36fbb19fb8fb | 82 | t3green = 0; |
daklowprofile | 0:36fbb19fb8fb | 83 | }else if(a == 'Y'){ |
daklowprofile | 0:36fbb19fb8fb | 84 | t3red = 0; |
daklowprofile | 0:36fbb19fb8fb | 85 | t3yellow = 1; |
daklowprofile | 0:36fbb19fb8fb | 86 | t3green = 0; |
daklowprofile | 0:36fbb19fb8fb | 87 | }else if(a == 'G'){ |
daklowprofile | 0:36fbb19fb8fb | 88 | t3red = 0; |
daklowprofile | 0:36fbb19fb8fb | 89 | t3yellow = 0; |
daklowprofile | 0:36fbb19fb8fb | 90 | t3green = 1; |
daklowprofile | 0:36fbb19fb8fb | 91 | }else{ |
daklowprofile | 0:36fbb19fb8fb | 92 | t3red = 0; |
daklowprofile | 0:36fbb19fb8fb | 93 | t3yellow = 0; |
daklowprofile | 0:36fbb19fb8fb | 94 | t3green = 0; |
daklowprofile | 0:36fbb19fb8fb | 95 | } |
daklowprofile | 0:36fbb19fb8fb | 96 | } |
daklowprofile | 0:36fbb19fb8fb | 97 | |
daklowprofile | 0:36fbb19fb8fb | 98 | void traffic_timer (char t1, char t2, char t3){ |
daklowprofile | 0:36fbb19fb8fb | 99 | t1light(t1); |
daklowprofile | 0:36fbb19fb8fb | 100 | t2light(t2); |
daklowprofile | 0:36fbb19fb8fb | 101 | t3light(t3); |
daklowprofile | 0:36fbb19fb8fb | 102 | } |
daklowprofile | 0:36fbb19fb8fb | 103 | |
daklowprofile | 0:36fbb19fb8fb | 104 | |
daklowprofile | 0:36fbb19fb8fb | 105 | void http_get(){ |
daklowprofile | 2:bf0da8f925e0 | 106 | char* Server = "http://192.168.1.103/PDRM/api/api_update.php"; |
daklowprofile | 0:36fbb19fb8fb | 107 | char buffer[256]; |
daklowprofile | 0:36fbb19fb8fb | 108 | HTTPClient http; |
daklowprofile | 1:2c577df21639 | 109 | |
daklowprofile | 0:36fbb19fb8fb | 110 | buffer[0] = 0; |
daklowprofile | 0:36fbb19fb8fb | 111 | sprintf(buffer,"%s?t1=%d,%d,%d&t2=%d,%d,%d&t3=%d,%d,%d",Server,t1red.read(),t1yellow.read(),t1green.read(),t2red.read(),t2yellow.read(),t2green.read(),t3red.read(),t3yellow.read(),t3green.read()); |
daklowprofile | 0:36fbb19fb8fb | 112 | printf("Send to %s\r\n", buffer); |
daklowprofile | 0:36fbb19fb8fb | 113 | http.get(buffer, buffer , 10); // Execute the URL of urlBuffer |
daklowprofile | 0:36fbb19fb8fb | 114 | } |
daklowprofile | 0:36fbb19fb8fb | 115 | |
daklowprofile | 0:36fbb19fb8fb | 116 | void traffic_interrupt (char t1, char t2, char t3, int time){ |
daklowprofile | 0:36fbb19fb8fb | 117 | t1light(t1); |
daklowprofile | 0:36fbb19fb8fb | 118 | t2light(t2); |
daklowprofile | 0:36fbb19fb8fb | 119 | t3light(t3); |
daklowprofile | 0:36fbb19fb8fb | 120 | http_get(); |
daklowprofile | 0:36fbb19fb8fb | 121 | wait(time); |
daklowprofile | 0:36fbb19fb8fb | 122 | } |
daklowprofile | 0:36fbb19fb8fb | 123 | |
daklowprofile | 2:bf0da8f925e0 | 124 | void interrupt(Arguments *in, Reply *out); |
daklowprofile | 2:bf0da8f925e0 | 125 | RPCFunction rpcMove(&interrupt, "Interrupt"); |
daklowprofile | 2:bf0da8f925e0 | 126 | int duration,Id; |
daklowprofile | 2:bf0da8f925e0 | 127 | |
daklowprofile | 2:bf0da8f925e0 | 128 | |
daklowprofile | 2:bf0da8f925e0 | 129 | void interrupt(Arguments *in, Reply *out){ |
daklowprofile | 2:bf0da8f925e0 | 130 | Id = in->getArg<int>(); |
daklowprofile | 2:bf0da8f925e0 | 131 | duration = in->getArg<int>(); |
daklowprofile | 2:bf0da8f925e0 | 132 | pc.printf("Traffic Light %d will turn to green for %d seconds\r\n", Id, duration); |
daklowprofile | 2:bf0da8f925e0 | 133 | |
daklowprofile | 2:bf0da8f925e0 | 134 | switch(Id){ |
daklowprofile | 0:36fbb19fb8fb | 135 | case 1 : |
daklowprofile | 2:bf0da8f925e0 | 136 | led1 = 1; |
daklowprofile | 0:36fbb19fb8fb | 137 | timer_tf.stop(); |
daklowprofile | 0:36fbb19fb8fb | 138 | if(timer_tf.read() >= 0 && timer_tf.read() <= 51){ |
daklowprofile | 2:bf0da8f925e0 | 139 | wait(duration); |
daklowprofile | 0:36fbb19fb8fb | 140 | }else if(timer_tf.read() >= 52 && timer_tf.read() <= 90){ |
daklowprofile | 0:36fbb19fb8fb | 141 | traffic_interrupt('R','Y','R',3); |
daklowprofile | 2:bf0da8f925e0 | 142 | traffic_interrupt('G','R','R',duration); |
daklowprofile | 0:36fbb19fb8fb | 143 | traffic_interrupt('Y','R','R',3); |
daklowprofile | 0:36fbb19fb8fb | 144 | }else if(timer_tf.read() >= 91 && timer_tf.read() <= 94){ |
daklowprofile | 0:36fbb19fb8fb | 145 | wait(3); |
daklowprofile | 2:bf0da8f925e0 | 146 | traffic_interrupt('G','R','R',duration); |
daklowprofile | 0:36fbb19fb8fb | 147 | traffic_interrupt('Y','R','R',3); |
daklowprofile | 0:36fbb19fb8fb | 148 | }else if(timer_tf.read() >= 95 && timer_tf.read() <= 165){ |
daklowprofile | 0:36fbb19fb8fb | 149 | traffic_interrupt('R','R','Y',3); |
daklowprofile | 2:bf0da8f925e0 | 150 | traffic_interrupt('G','R','R',duration); |
daklowprofile | 0:36fbb19fb8fb | 151 | traffic_interrupt('Y','R','R',3); |
daklowprofile | 0:36fbb19fb8fb | 152 | }else if(timer_tf.read() >= 166 && timer_tf.read() <= 169){ |
daklowprofile | 0:36fbb19fb8fb | 153 | wait(3); |
daklowprofile | 2:bf0da8f925e0 | 154 | traffic_interrupt('G','R','R',duration); |
daklowprofile | 0:36fbb19fb8fb | 155 | traffic_interrupt('Y','R','R',3); |
daklowprofile | 0:36fbb19fb8fb | 156 | } |
daklowprofile | 2:bf0da8f925e0 | 157 | led1 = 0; |
daklowprofile | 0:36fbb19fb8fb | 158 | timer_tf.start(); |
daklowprofile | 0:36fbb19fb8fb | 159 | break; |
daklowprofile | 0:36fbb19fb8fb | 160 | |
daklowprofile | 0:36fbb19fb8fb | 161 | case 2 : |
daklowprofile | 2:bf0da8f925e0 | 162 | led2 = 1; |
daklowprofile | 0:36fbb19fb8fb | 163 | timer_tf.stop(); |
daklowprofile | 0:36fbb19fb8fb | 164 | if(timer_tf.read() >= 0 && timer_tf.read() <= 47){ |
daklowprofile | 0:36fbb19fb8fb | 165 | traffic_interrupt('Y','R','R',3); |
daklowprofile | 2:bf0da8f925e0 | 166 | traffic_interrupt('R','G','R',duration); |
daklowprofile | 0:36fbb19fb8fb | 167 | traffic_interrupt('R','Y','R',3); |
daklowprofile | 0:36fbb19fb8fb | 168 | }else if(timer_tf.read() >= 48 && timer_tf.read() <= 51){ |
daklowprofile | 0:36fbb19fb8fb | 169 | wait(3); |
daklowprofile | 2:bf0da8f925e0 | 170 | traffic_interrupt('R','G','R',duration); |
daklowprofile | 0:36fbb19fb8fb | 171 | traffic_interrupt('R','Y','R',3); |
daklowprofile | 0:36fbb19fb8fb | 172 | }else if(timer_tf.read() >= 52 && timer_tf.read() <= 94){ |
daklowprofile | 2:bf0da8f925e0 | 173 | wait(duration); |
daklowprofile | 0:36fbb19fb8fb | 174 | }else if(timer_tf.read() >= 95 && timer_tf.read() <= 165){ |
daklowprofile | 0:36fbb19fb8fb | 175 | traffic_interrupt('R','R','Y',3); |
daklowprofile | 2:bf0da8f925e0 | 176 | traffic_interrupt('R','G','R',duration); |
daklowprofile | 0:36fbb19fb8fb | 177 | traffic_interrupt('R','Y','R',3); |
daklowprofile | 0:36fbb19fb8fb | 178 | }else if(timer_tf.read() >= 166 && timer_tf.read() <= 169){ |
daklowprofile | 0:36fbb19fb8fb | 179 | wait(3); |
daklowprofile | 2:bf0da8f925e0 | 180 | traffic_interrupt('R','G','R',duration); |
daklowprofile | 0:36fbb19fb8fb | 181 | traffic_interrupt('R','Y','R',3); |
daklowprofile | 2:bf0da8f925e0 | 182 | } |
daklowprofile | 2:bf0da8f925e0 | 183 | led2 = 0; |
daklowprofile | 2:bf0da8f925e0 | 184 | timer_tf.start(); |
daklowprofile | 0:36fbb19fb8fb | 185 | break; |
daklowprofile | 0:36fbb19fb8fb | 186 | |
daklowprofile | 0:36fbb19fb8fb | 187 | case 3 : |
daklowprofile | 2:bf0da8f925e0 | 188 | led3 = 1; |
daklowprofile | 0:36fbb19fb8fb | 189 | timer_tf.stop(); |
daklowprofile | 0:36fbb19fb8fb | 190 | if(timer_tf.read() >= 0 && timer_tf.read() <= 47){ |
daklowprofile | 0:36fbb19fb8fb | 191 | traffic_interrupt('Y','R','R',3); |
daklowprofile | 2:bf0da8f925e0 | 192 | traffic_interrupt('R','R','G',duration); |
daklowprofile | 0:36fbb19fb8fb | 193 | traffic_interrupt('R','R','Y',3); |
daklowprofile | 0:36fbb19fb8fb | 194 | }else if(timer_tf.read() >= 48 && timer_tf.read() <= 51){ |
daklowprofile | 0:36fbb19fb8fb | 195 | wait(3); |
daklowprofile | 2:bf0da8f925e0 | 196 | traffic_interrupt('R','R','G',duration); |
daklowprofile | 0:36fbb19fb8fb | 197 | traffic_interrupt('R','R','Y',3); |
daklowprofile | 0:36fbb19fb8fb | 198 | }else if(timer_tf.read() >= 52 && timer_tf.read() <= 90){ |
daklowprofile | 0:36fbb19fb8fb | 199 | traffic_interrupt('R','Y','R',3); |
daklowprofile | 2:bf0da8f925e0 | 200 | traffic_interrupt('R','R','G',duration); |
daklowprofile | 0:36fbb19fb8fb | 201 | traffic_interrupt('R','R','Y',3); |
daklowprofile | 0:36fbb19fb8fb | 202 | }else if(timer_tf.read() >= 91 && timer_tf.read() <= 94){ |
daklowprofile | 0:36fbb19fb8fb | 203 | wait(3); |
daklowprofile | 2:bf0da8f925e0 | 204 | traffic_interrupt('R','R','G',duration); |
daklowprofile | 0:36fbb19fb8fb | 205 | traffic_interrupt('R','R','Y',3); |
daklowprofile | 0:36fbb19fb8fb | 206 | }else if(timer_tf.read() >= 95 && timer_tf.read() <= 169){ |
daklowprofile | 2:bf0da8f925e0 | 207 | wait(duration); |
daklowprofile | 0:36fbb19fb8fb | 208 | } |
daklowprofile | 2:bf0da8f925e0 | 209 | led3 = 0; |
daklowprofile | 0:36fbb19fb8fb | 210 | timer_tf.start(); |
daklowprofile | 0:36fbb19fb8fb | 211 | break; |
daklowprofile | 0:36fbb19fb8fb | 212 | } |
daklowprofile | 0:36fbb19fb8fb | 213 | |
daklowprofile | 0:36fbb19fb8fb | 214 | } |
daklowprofile | 0:36fbb19fb8fb | 215 | |
daklowprofile | 0:36fbb19fb8fb | 216 | //post when changed |
daklowprofile | 0:36fbb19fb8fb | 217 | int t1red_o,t1yellow_o,t1green_o,t2red_o,t2yellow_o,t2green_o,t3red_o,t3yellow_o,t3green_o = 0; |
daklowprofile | 0:36fbb19fb8fb | 218 | void change(int t1red_n, int t1yellow_n,int t1green_n, int t2red_n, int t2yellow_n, int t2green_n, int t3red_n, int t3yellow_n, int t3green_n){ |
daklowprofile | 0:36fbb19fb8fb | 219 | |
daklowprofile | 0:36fbb19fb8fb | 220 | if(t1red_o != t1red_n || t1yellow_o != t1yellow_n || t1green_o != t1green_n || t2red_o != t2red_n || t2yellow_o != t2yellow_n || t2green_o != t2green_n || t3red_o != t3red_n || t3yellow_o != t3yellow_n || t3green_o != t3green_n){ |
daklowprofile | 0:36fbb19fb8fb | 221 | http_get(); |
daklowprofile | 0:36fbb19fb8fb | 222 | t1red_o = t1red_n; |
daklowprofile | 0:36fbb19fb8fb | 223 | t1yellow_o = t1yellow_n; |
daklowprofile | 0:36fbb19fb8fb | 224 | t1green_o = t1green_n; |
daklowprofile | 0:36fbb19fb8fb | 225 | t2red_o = t2red_n; |
daklowprofile | 0:36fbb19fb8fb | 226 | t2yellow_o = t2yellow_n; |
daklowprofile | 0:36fbb19fb8fb | 227 | t2green_o = t2green_n; |
daklowprofile | 0:36fbb19fb8fb | 228 | t3red_o = t3red_n; |
daklowprofile | 0:36fbb19fb8fb | 229 | t3yellow_o = t3yellow_n; |
daklowprofile | 0:36fbb19fb8fb | 230 | t3green_o = t3green_n; |
daklowprofile | 0:36fbb19fb8fb | 231 | }else{} |
daklowprofile | 0:36fbb19fb8fb | 232 | } |
daklowprofile | 0:36fbb19fb8fb | 233 | |
daklowprofile | 0:36fbb19fb8fb | 234 | void normal_traffic(){ |
daklowprofile | 2:bf0da8f925e0 | 235 | change(t1red.read(),t1yellow.read(),t1green.read(),t2red.read(),t2yellow.read(),t2green.read(),t3red.read(),t3yellow.read(),t3green.read()); |
daklowprofile | 0:36fbb19fb8fb | 236 | if(timer_tf.read() >= 0 && timer_tf.read() <= 47){ |
daklowprofile | 0:36fbb19fb8fb | 237 | traffic_timer('G','R','R'); |
daklowprofile | 0:36fbb19fb8fb | 238 | }else if(timer_tf.read() >= 48 && timer_tf.read() <= 51){ |
daklowprofile | 0:36fbb19fb8fb | 239 | traffic_timer('Y','R','R'); |
daklowprofile | 0:36fbb19fb8fb | 240 | }else if(timer_tf.read() >= 52 && timer_tf.read() <= 90){ |
daklowprofile | 0:36fbb19fb8fb | 241 | traffic_timer('R','G','R'); |
daklowprofile | 0:36fbb19fb8fb | 242 | }else if(timer_tf.read() >= 91 && timer_tf.read() <= 94){ |
daklowprofile | 0:36fbb19fb8fb | 243 | traffic_timer('R','Y','R'); |
daklowprofile | 0:36fbb19fb8fb | 244 | }else if(timer_tf.read() >= 95 && timer_tf.read() <= 165){ |
daklowprofile | 0:36fbb19fb8fb | 245 | traffic_timer('R','R','G'); |
daklowprofile | 0:36fbb19fb8fb | 246 | }else if(timer_tf.read() >= 166 && timer_tf.read() <= 169){ |
daklowprofile | 0:36fbb19fb8fb | 247 | traffic_timer('R','R','Y'); |
daklowprofile | 0:36fbb19fb8fb | 248 | }else if(timer_tf.read() >= 170){ |
daklowprofile | 0:36fbb19fb8fb | 249 | timer_tf.reset(); |
daklowprofile | 0:36fbb19fb8fb | 250 | } |
daklowprofile | 2:bf0da8f925e0 | 251 | } |
daklowprofile | 0:36fbb19fb8fb | 252 | |
daklowprofile | 0:36fbb19fb8fb | 253 | |
daklowprofile | 0:36fbb19fb8fb | 254 | |
daklowprofile | 0:36fbb19fb8fb | 255 | int main() { |
daklowprofile | 0:36fbb19fb8fb | 256 | EthernetInterface eth; |
daklowprofile | 0:36fbb19fb8fb | 257 | HTTPServer svr; |
daklowprofile | 0:36fbb19fb8fb | 258 | |
daklowprofile | 0:36fbb19fb8fb | 259 | printf("Setting up...\r\n"); |
daklowprofile | 0:36fbb19fb8fb | 260 | eth.init(); |
daklowprofile | 0:36fbb19fb8fb | 261 | int ethErr = eth.connect(); |
daklowprofile | 0:36fbb19fb8fb | 262 | if(ethErr < 0) |
daklowprofile | 0:36fbb19fb8fb | 263 | { |
daklowprofile | 0:36fbb19fb8fb | 264 | printf("Error %d in setup.\r\n", ethErr); |
daklowprofile | 0:36fbb19fb8fb | 265 | return -1; |
daklowprofile | 0:36fbb19fb8fb | 266 | } |
daklowprofile | 0:36fbb19fb8fb | 267 | |
daklowprofile | 0:36fbb19fb8fb | 268 | svr.addHandler<HTTPRpcRequestHandler>("/rpc"); |
daklowprofile | 0:36fbb19fb8fb | 269 | |
daklowprofile | 0:36fbb19fb8fb | 270 | //attach server to port 80 |
daklowprofile | 0:36fbb19fb8fb | 271 | svr.start(80, ð); |
daklowprofile | 0:36fbb19fb8fb | 272 | |
daklowprofile | 0:36fbb19fb8fb | 273 | printf("Listening...\n"); |
daklowprofile | 0:36fbb19fb8fb | 274 | |
daklowprofile | 0:36fbb19fb8fb | 275 | //Listen indefinitely |
daklowprofile | 0:36fbb19fb8fb | 276 | Timer tm; |
daklowprofile | 0:36fbb19fb8fb | 277 | tm.start(); |
daklowprofile | 0:36fbb19fb8fb | 278 | timer_tf.start(); |
daklowprofile | 0:36fbb19fb8fb | 279 | |
daklowprofile | 0:36fbb19fb8fb | 280 | while(1) |
daklowprofile | 0:36fbb19fb8fb | 281 | { |
daklowprofile | 0:36fbb19fb8fb | 282 | normal_traffic(); |
daklowprofile | 0:36fbb19fb8fb | 283 | svr.poll(); |
daklowprofile | 0:36fbb19fb8fb | 284 | if(tm.read()>.5) |
daklowprofile | 0:36fbb19fb8fb | 285 | { |
daklowprofile | 0:36fbb19fb8fb | 286 | tm.start(); |
daklowprofile | 0:36fbb19fb8fb | 287 | } |
daklowprofile | 2:bf0da8f925e0 | 288 | } |
daklowprofile | 0:36fbb19fb8fb | 289 | } |