Same as mallet... but distance
Dependencies: EthernetInterface NetworkAPI mbed-rtos mbed
Fork of MalletFirmware by
main.cpp@16:c3f922f61b8f, 2014-11-14 (annotated)
- Committer:
- timmey9
- Date:
- Fri Nov 14 18:26:39 2014 +0000
- Revision:
- 16:c3f922f61b8f
- Parent:
- 11:59dcefdda506
- Child:
- 17:2f978f823020
This is what I wrote before I added the NetworkAPI library.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
timmey9 | 16:c3f922f61b8f | 1 | /* |
timmey9 | 16:c3f922f61b8f | 2 | #include "mbed.h" |
timmey9 | 16:c3f922f61b8f | 3 | #include "EthernetInterface.h" |
timmey9 | 16:c3f922f61b8f | 4 | |
timmey9 | 16:c3f922f61b8f | 5 | #define IP "127.0.0.5" |
timmey9 | 16:c3f922f61b8f | 6 | #define MASK "255.255.255.0" |
timmey9 | 16:c3f922f61b8f | 7 | #define GATEWAY "10.1.1.1" |
timmey9 | 16:c3f922f61b8f | 8 | #define DES_ADDR "127.0.0.6" |
timmey9 | 16:c3f922f61b8f | 9 | |
timmey9 | 16:c3f922f61b8f | 10 | #define PORT 80 |
timmey9 | 16:c3f922f61b8f | 11 | |
timmey9 | 16:c3f922f61b8f | 12 | Serial pc(USBTX, USBRX); |
timmey9 | 16:c3f922f61b8f | 13 | EthernetInterface eth; |
timmey9 | 16:c3f922f61b8f | 14 | UDPSocket point1; |
timmey9 | 16:c3f922f61b8f | 15 | |
timmey9 | 16:c3f922f61b8f | 16 | |
timmey9 | 16:c3f922f61b8f | 17 | int main () { |
timmey9 | 16:c3f922f61b8f | 18 | wait_ms(500); |
timmey9 | 16:c3f922f61b8f | 19 | // intitialize Serial connection |
timmey9 | 16:c3f922f61b8f | 20 | pc.baud(230400); |
timmey9 | 16:c3f922f61b8f | 21 | |
timmey9 | 16:c3f922f61b8f | 22 | // initialize IP connection |
timmey9 | 16:c3f922f61b8f | 23 | eth.init(IP, MASK, GATEWAY); |
timmey9 | 16:c3f922f61b8f | 24 | wait_ms(100); |
timmey9 | 16:c3f922f61b8f | 25 | |
timmey9 | 16:c3f922f61b8f | 26 | // setup UDP socket |
timmey9 | 16:c3f922f61b8f | 27 | point1.init(); |
timmey9 | 16:c3f922f61b8f | 28 | wait_ms(1); |
timmey9 | 16:c3f922f61b8f | 29 | point1.bind(PORT); |
timmey9 | 16:c3f922f61b8f | 30 | |
timmey9 | 16:c3f922f61b8f | 31 | |
timmey9 | 16:c3f922f61b8f | 32 | } |
timmey9 | 16:c3f922f61b8f | 33 | */ |
timmey9 | 16:c3f922f61b8f | 34 | |
donatien | 0:bb128f0e952f | 35 | #include "mbed.h" |
donatien | 0:bb128f0e952f | 36 | #include "EthernetInterface.h" |
donatien | 0:bb128f0e952f | 37 | |
timmey9 | 16:c3f922f61b8f | 38 | |
timmey9 | 16:c3f922f61b8f | 39 | #define SWITCHER 0 |
timmey9 | 16:c3f922f61b8f | 40 | #if SWITCHER == 1 |
timmey9 | 16:c3f922f61b8f | 41 | |
timmey9 | 16:c3f922f61b8f | 42 | #define IP "127.0.0.5" |
timmey9 | 16:c3f922f61b8f | 43 | #define MASK "255.255.255.0" |
timmey9 | 16:c3f922f61b8f | 44 | #define GATEWAY "10.1.1.1" |
timmey9 | 16:c3f922f61b8f | 45 | #define DES_ADDR "127.0.0.6" |
timmey9 | 16:c3f922f61b8f | 46 | //#define DES_ADDR "128.187.113.146" //"utcnist.colorado.edu" |
timmey9 | 16:c3f922f61b8f | 47 | #define PORT 80 |
timmey9 | 16:c3f922f61b8f | 48 | |
timmey9 | 16:c3f922f61b8f | 49 | DigitalOut led_blue(LED_BLUE); |
timmey9 | 16:c3f922f61b8f | 50 | EthernetInterface eth; |
timmey9 | 16:c3f922f61b8f | 51 | Serial pc(USBTX, USBRX); |
timmey9 | 16:c3f922f61b8f | 52 | Timer timer; |
timmey9 | 16:c3f922f61b8f | 53 | |
emilmont | 7:65188f4a8c25 | 54 | int main() { |
timmey9 | 16:c3f922f61b8f | 55 | led_blue = 1; |
timmey9 | 16:c3f922f61b8f | 56 | pc.baud(230400); |
donatien | 0:bb128f0e952f | 57 | |
timmey9 | 16:c3f922f61b8f | 58 | timer.stop(); |
timmey9 | 16:c3f922f61b8f | 59 | pc.printf("\r\nStarting program1...\r\n"); |
donatien | 0:bb128f0e952f | 60 | |
timmey9 | 16:c3f922f61b8f | 61 | eth.init(IP, MASK, GATEWAY); //Use DHCP |
timmey9 | 16:c3f922f61b8f | 62 | eth.connect(); |
timmey9 | 16:c3f922f61b8f | 63 | |
timmey9 | 16:c3f922f61b8f | 64 | pc.printf("IP Address is %s\r\n", eth.getIPAddress()); |
emilmont | 7:65188f4a8c25 | 65 | |
timmey9 | 16:c3f922f61b8f | 66 | if(1) |
timmey9 | 16:c3f922f61b8f | 67 | { |
timmey9 | 16:c3f922f61b8f | 68 | UDPSocket sock; |
timmey9 | 16:c3f922f61b8f | 69 | sock.init(); |
timmey9 | 16:c3f922f61b8f | 70 | |
timmey9 | 16:c3f922f61b8f | 71 | Endpoint nist; |
timmey9 | 16:c3f922f61b8f | 72 | nist.set_address(DES_ADDR, PORT)? pc.printf("Endpoint address NOT set\r\n") : pc.printf("Endpoint address set\r\n"); |
timmey9 | 16:c3f922f61b8f | 73 | led_blue = 0; |
timmey9 | 16:c3f922f61b8f | 74 | while(!pc.getc()); |
timmey9 | 16:c3f922f61b8f | 75 | led_blue = 1; |
timmey9 | 16:c3f922f61b8f | 76 | char out_buffer[] = "password"; // Does not matter |
timmey9 | 16:c3f922f61b8f | 77 | int returnVar = sock.sendTo(nist, out_buffer, sizeof(out_buffer)); |
timmey9 | 16:c3f922f61b8f | 78 | led_blue = 0; |
timmey9 | 16:c3f922f61b8f | 79 | switch(returnVar) { |
timmey9 | 16:c3f922f61b8f | 80 | case -1: |
timmey9 | 16:c3f922f61b8f | 81 | pc.printf("Password send failure\r\n"); |
emilmont | 7:65188f4a8c25 | 82 | break; |
timmey9 | 16:c3f922f61b8f | 83 | default: |
timmey9 | 16:c3f922f61b8f | 84 | pc.printf("Password sent %i chars\r\n", returnVar); |
timmey9 | 16:c3f922f61b8f | 85 | } |
timmey9 | 16:c3f922f61b8f | 86 | wait(1); |
timmey9 | 16:c3f922f61b8f | 87 | led_blue = 1; |
timmey9 | 16:c3f922f61b8f | 88 | char in_buffer[400]; |
timmey9 | 16:c3f922f61b8f | 89 | int n = sock.receiveFrom(nist, in_buffer, sizeof(in_buffer)); |
timmey9 | 16:c3f922f61b8f | 90 | |
timmey9 | 16:c3f922f61b8f | 91 | unsigned int timeRes = ntohl( *((unsigned int*)in_buffer)); |
timmey9 | 16:c3f922f61b8f | 92 | pc.printf("Received %d bytes from server %s on port %d: %u seconds since 1/01/1900 00:00 GMT\n", n, nist.get_address(), nist.get_port(), timeRes); |
timmey9 | 16:c3f922f61b8f | 93 | sock.close(); |
emilmont | 7:65188f4a8c25 | 94 | } |
timmey9 | 16:c3f922f61b8f | 95 | else |
timmey9 | 16:c3f922f61b8f | 96 | { |
timmey9 | 16:c3f922f61b8f | 97 | TCPSocketConnection sock; |
timmey9 | 16:c3f922f61b8f | 98 | //sock.connect("mbed.org", 80); |
timmey9 | 16:c3f922f61b8f | 99 | sock.connect(DES_ADDR, 80); |
timmey9 | 16:c3f922f61b8f | 100 | |
timmey9 | 16:c3f922f61b8f | 101 | char http_cmd[] = "GET htdocs/index.html HTTP/1.1"; |
timmey9 | 16:c3f922f61b8f | 102 | sock.send_all(http_cmd, sizeof(http_cmd)-1); |
timmey9 | 16:c3f922f61b8f | 103 | |
timmey9 | 16:c3f922f61b8f | 104 | char buffer[30000]; |
timmey9 | 16:c3f922f61b8f | 105 | int ret; |
timmey9 | 16:c3f922f61b8f | 106 | while (true) { |
timmey9 | 16:c3f922f61b8f | 107 | timer.reset(); |
timmey9 | 16:c3f922f61b8f | 108 | timer.start(); |
timmey9 | 16:c3f922f61b8f | 109 | ret = sock.receive(buffer, sizeof(buffer)-1); |
timmey9 | 16:c3f922f61b8f | 110 | timer.stop(); |
timmey9 | 16:c3f922f61b8f | 111 | pc.printf("Timer: %i\r\n", timer.read()); |
timmey9 | 16:c3f922f61b8f | 112 | if (ret <= 0) |
timmey9 | 16:c3f922f61b8f | 113 | break; |
timmey9 | 16:c3f922f61b8f | 114 | buffer[ret] = '\0'; |
timmey9 | 16:c3f922f61b8f | 115 | pc.printf("Received %d chars from server:\n%s\n", ret, buffer); |
timmey9 | 16:c3f922f61b8f | 116 | } |
timmey9 | 16:c3f922f61b8f | 117 | sock.close(); |
timmey9 | 16:c3f922f61b8f | 118 | } |
donatien | 0:bb128f0e952f | 119 | |
emilmont | 7:65188f4a8c25 | 120 | eth.disconnect(); |
timmey9 | 16:c3f922f61b8f | 121 | pc.printf("\r\nEnding program\r\n"); |
emilmont | 9:4757a976148d | 122 | while(1) {} |
timmey9 | 16:c3f922f61b8f | 123 | |
donatien | 0:bb128f0e952f | 124 | } |
timmey9 | 16:c3f922f61b8f | 125 | |
timmey9 | 16:c3f922f61b8f | 126 | |
timmey9 | 16:c3f922f61b8f | 127 | |
timmey9 | 16:c3f922f61b8f | 128 | |
timmey9 | 16:c3f922f61b8f | 129 | |
timmey9 | 16:c3f922f61b8f | 130 | |
timmey9 | 16:c3f922f61b8f | 131 | |
timmey9 | 16:c3f922f61b8f | 132 | |
timmey9 | 16:c3f922f61b8f | 133 | |
timmey9 | 16:c3f922f61b8f | 134 | |
timmey9 | 16:c3f922f61b8f | 135 | |
timmey9 | 16:c3f922f61b8f | 136 | |
timmey9 | 16:c3f922f61b8f | 137 | #else |
timmey9 | 16:c3f922f61b8f | 138 | |
timmey9 | 16:c3f922f61b8f | 139 | #define IP "127.0.0.6" |
timmey9 | 16:c3f922f61b8f | 140 | #define MASK "255.255.255.0" |
timmey9 | 16:c3f922f61b8f | 141 | #define GATEWAY "10.1.1.1" |
timmey9 | 16:c3f922f61b8f | 142 | #define DES_ADDR "127.0.0.5" |
timmey9 | 16:c3f922f61b8f | 143 | //#define DES_ADDR "128.187.113.146" //"utcnist.colorado.edu" |
timmey9 | 16:c3f922f61b8f | 144 | #define PORT 80 |
timmey9 | 16:c3f922f61b8f | 145 | |
timmey9 | 16:c3f922f61b8f | 146 | DigitalOut led_blue(LED_BLUE); |
timmey9 | 16:c3f922f61b8f | 147 | EthernetInterface eth; |
timmey9 | 16:c3f922f61b8f | 148 | Serial pc(USBTX, USBRX); |
timmey9 | 16:c3f922f61b8f | 149 | Timer timer; |
timmey9 | 16:c3f922f61b8f | 150 | |
timmey9 | 16:c3f922f61b8f | 151 | int main() { |
timmey9 | 16:c3f922f61b8f | 152 | led_blue = 1; |
timmey9 | 16:c3f922f61b8f | 153 | pc.baud(230400); |
timmey9 | 16:c3f922f61b8f | 154 | |
timmey9 | 16:c3f922f61b8f | 155 | timer.stop(); |
timmey9 | 16:c3f922f61b8f | 156 | pc.printf("\r\nStarting program2...\r\n"); |
timmey9 | 16:c3f922f61b8f | 157 | |
timmey9 | 16:c3f922f61b8f | 158 | eth.init(IP, MASK, GATEWAY); //Use DHCP |
timmey9 | 16:c3f922f61b8f | 159 | eth.connect(); |
timmey9 | 16:c3f922f61b8f | 160 | |
timmey9 | 16:c3f922f61b8f | 161 | pc.printf("IP Address is %s\r\n", eth.getIPAddress()); |
timmey9 | 16:c3f922f61b8f | 162 | |
timmey9 | 16:c3f922f61b8f | 163 | if(1) |
timmey9 | 16:c3f922f61b8f | 164 | { |
timmey9 | 16:c3f922f61b8f | 165 | UDPSocket sock; |
timmey9 | 16:c3f922f61b8f | 166 | sock.init(); |
timmey9 | 16:c3f922f61b8f | 167 | |
timmey9 | 16:c3f922f61b8f | 168 | Endpoint nist; |
timmey9 | 16:c3f922f61b8f | 169 | nist.set_address(DES_ADDR, PORT)? pc.printf("Endpoint address NOT set\r\n") : pc.printf("Endpoint address set\r\n"); |
timmey9 | 16:c3f922f61b8f | 170 | led_blue = 0; |
timmey9 | 16:c3f922f61b8f | 171 | while(!pc.getc()); |
timmey9 | 16:c3f922f61b8f | 172 | led_blue = 1; |
timmey9 | 16:c3f922f61b8f | 173 | char out_buffer[] = "blah"; // Does not matter |
timmey9 | 16:c3f922f61b8f | 174 | int returnVar = sock.sendTo(nist, out_buffer, sizeof(out_buffer)); |
timmey9 | 16:c3f922f61b8f | 175 | led_blue = 0; |
timmey9 | 16:c3f922f61b8f | 176 | switch(returnVar) { |
timmey9 | 16:c3f922f61b8f | 177 | case -1: |
timmey9 | 16:c3f922f61b8f | 178 | pc.printf("Password send failure\r\n"); |
timmey9 | 16:c3f922f61b8f | 179 | break; |
timmey9 | 16:c3f922f61b8f | 180 | default: |
timmey9 | 16:c3f922f61b8f | 181 | pc.printf("Password sent %i chars\r\n", returnVar); |
timmey9 | 16:c3f922f61b8f | 182 | } |
timmey9 | 16:c3f922f61b8f | 183 | wait(1); |
timmey9 | 16:c3f922f61b8f | 184 | led_blue = 1; |
timmey9 | 16:c3f922f61b8f | 185 | |
timmey9 | 16:c3f922f61b8f | 186 | char in_buffer[400]; |
timmey9 | 16:c3f922f61b8f | 187 | int n = sock.receiveFrom(nist, in_buffer, sizeof(in_buffer)); |
timmey9 | 16:c3f922f61b8f | 188 | |
timmey9 | 16:c3f922f61b8f | 189 | unsigned int timeRes = ntohl( *((unsigned int*)in_buffer)); |
timmey9 | 16:c3f922f61b8f | 190 | pc.printf("Received %d bytes from server %s on port %d: %u seconds since 1/01/1900 00:00 GMT\n", n, nist.get_address(), nist.get_port(), timeRes); |
timmey9 | 16:c3f922f61b8f | 191 | sock.close(); |
timmey9 | 16:c3f922f61b8f | 192 | } |
timmey9 | 16:c3f922f61b8f | 193 | else |
timmey9 | 16:c3f922f61b8f | 194 | { |
timmey9 | 16:c3f922f61b8f | 195 | TCPSocketConnection sock; |
timmey9 | 16:c3f922f61b8f | 196 | //sock.connect("mbed.org", 80); |
timmey9 | 16:c3f922f61b8f | 197 | sock.connect(DES_ADDR, 80); |
timmey9 | 16:c3f922f61b8f | 198 | |
timmey9 | 16:c3f922f61b8f | 199 | char http_cmd[] = "GET htdocs/index.html HTTP/1.1"; |
timmey9 | 16:c3f922f61b8f | 200 | sock.send_all(http_cmd, sizeof(http_cmd)-1); |
timmey9 | 16:c3f922f61b8f | 201 | |
timmey9 | 16:c3f922f61b8f | 202 | char buffer[30000]; |
timmey9 | 16:c3f922f61b8f | 203 | int ret; |
timmey9 | 16:c3f922f61b8f | 204 | while (true) { |
timmey9 | 16:c3f922f61b8f | 205 | timer.reset(); |
timmey9 | 16:c3f922f61b8f | 206 | timer.start(); |
timmey9 | 16:c3f922f61b8f | 207 | ret = sock.receive(buffer, sizeof(buffer)-1); |
timmey9 | 16:c3f922f61b8f | 208 | timer.stop(); |
timmey9 | 16:c3f922f61b8f | 209 | pc.printf("Timer: %i\r\n", timer.read()); |
timmey9 | 16:c3f922f61b8f | 210 | if (ret <= 0) |
timmey9 | 16:c3f922f61b8f | 211 | break; |
timmey9 | 16:c3f922f61b8f | 212 | buffer[ret] = '\0'; |
timmey9 | 16:c3f922f61b8f | 213 | pc.printf("Received %d chars from server:\n%s\n", ret, buffer); |
timmey9 | 16:c3f922f61b8f | 214 | } |
timmey9 | 16:c3f922f61b8f | 215 | sock.close(); |
timmey9 | 16:c3f922f61b8f | 216 | } |
timmey9 | 16:c3f922f61b8f | 217 | |
timmey9 | 16:c3f922f61b8f | 218 | eth.disconnect(); |
timmey9 | 16:c3f922f61b8f | 219 | pc.printf("\r\nEnding program\r\n"); |
timmey9 | 16:c3f922f61b8f | 220 | while(1) {} |
timmey9 | 16:c3f922f61b8f | 221 | |
timmey9 | 16:c3f922f61b8f | 222 | } |
timmey9 | 16:c3f922f61b8f | 223 | #endif |