A WiFiDipCortex based robot. Control is via sockets over WiFi. See also: https://github.com/mfurseman/robo-android
Dependencies: Motordriver USBDevice cc3000_hostdriver_mbedsocket_hacked mbed
main.cpp@2:50c151183047, 2014-10-25 (annotated)
- Committer:
- mfurseman
- Date:
- Sat Oct 25 17:34:45 2014 +0000
- Revision:
- 2:50c151183047
- Parent:
- 1:b66a2d756c8a
- Child:
- 3:ba11f6207550
First working TCP Socket connection (just sending hello world)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfurseman | 0:993d6b65e255 | 1 | #include "mbed.h" |
mfurseman | 0:993d6b65e255 | 2 | #include "cc3000.h" |
mfurseman | 2:50c151183047 | 3 | #include "TCPSocketConnection.h" |
mfurseman | 2:50c151183047 | 4 | #include "TCPSocketServer.h" |
mfurseman | 1:b66a2d756c8a | 5 | |
mfurseman | 0:993d6b65e255 | 6 | /* Quickly change debug flag to remove blocking serial code */ |
mfurseman | 0:993d6b65e255 | 7 | #define DEBUG |
mfurseman | 0:993d6b65e255 | 8 | #ifdef DEBUG |
mfurseman | 2:50c151183047 | 9 | #include "USBSerial.h" |
mfurseman | 0:993d6b65e255 | 10 | USBSerial serial; |
mfurseman | 0:993d6b65e255 | 11 | #define debug(x, ...) serial.printf(x, ##__VA_ARGS__); |
mfurseman | 0:993d6b65e255 | 12 | #else |
mfurseman | 0:993d6b65e255 | 13 | #define debug(x, ...) |
mfurseman | 0:993d6b65e255 | 14 | #endif |
mfurseman | 0:993d6b65e255 | 15 | |
mfurseman | 1:b66a2d756c8a | 16 | |
mfurseman | 0:993d6b65e255 | 17 | using namespace mbed_cc3000; |
mfurseman | 0:993d6b65e255 | 18 | |
mfurseman | 1:b66a2d756c8a | 19 | |
mfurseman | 0:993d6b65e255 | 20 | /* On board LED */ |
mfurseman | 0:993d6b65e255 | 21 | DigitalOut led(P0_1); |
mfurseman | 0:993d6b65e255 | 22 | |
mfurseman | 0:993d6b65e255 | 23 | /* Serial library for WiFi module */ |
mfurseman | 0:993d6b65e255 | 24 | cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37)); |
mfurseman | 0:993d6b65e255 | 25 | |
mfurseman | 0:993d6b65e255 | 26 | /* Struct to hold connection data */ |
mfurseman | 0:993d6b65e255 | 27 | tNetappIpconfigRetArgs ipinfo; |
mfurseman | 0:993d6b65e255 | 28 | |
mfurseman | 0:993d6b65e255 | 29 | |
mfurseman | 1:b66a2d756c8a | 30 | /* Prints CC3000 connection info */ |
mfurseman | 0:993d6b65e255 | 31 | void printConnectionInfo() { |
mfurseman | 0:993d6b65e255 | 32 | if (( wifi.is_enabled() ) && ( wifi.is_dhcp_configured() )) { |
mfurseman | 0:993d6b65e255 | 33 | wifi.get_ip_config(&ipinfo); |
mfurseman | 0:993d6b65e255 | 34 | } |
mfurseman | 0:993d6b65e255 | 35 | if (! wifi.is_enabled() ) { |
mfurseman | 0:993d6b65e255 | 36 | debug("CC3000 Disabled\r\n"); |
mfurseman | 0:993d6b65e255 | 37 | } |
mfurseman | 0:993d6b65e255 | 38 | else if ( wifi.is_dhcp_configured() ) { |
mfurseman | 0:993d6b65e255 | 39 | debug("SSID : %-33s|\r\n", ipinfo.uaSSID); |
mfurseman | 0:993d6b65e255 | 40 | debug("IP : %-35s|\r\n", wifi.getIPAddress()); |
mfurseman | 0:993d6b65e255 | 41 | } |
mfurseman | 0:993d6b65e255 | 42 | else if ( wifi.is_connected() ) { |
mfurseman | 0:993d6b65e255 | 43 | debug("Connecting, waiting for DHCP\r\n"); |
mfurseman | 0:993d6b65e255 | 44 | } |
mfurseman | 0:993d6b65e255 | 45 | else { |
mfurseman | 0:993d6b65e255 | 46 | debug("Not Connected\r\n"); |
mfurseman | 0:993d6b65e255 | 47 | } |
mfurseman | 0:993d6b65e255 | 48 | } |
mfurseman | 0:993d6b65e255 | 49 | |
mfurseman | 1:b66a2d756c8a | 50 | |
mfurseman | 1:b66a2d756c8a | 51 | /* WiFi DipCortex board setup */ |
mfurseman | 1:b66a2d756c8a | 52 | void init() { |
mfurseman | 0:993d6b65e255 | 53 | NVIC_SetPriority(SSP1_IRQn, 0x0); |
mfurseman | 0:993d6b65e255 | 54 | NVIC_SetPriority(PIN_INT0_IRQn, 0x1); |
mfurseman | 0:993d6b65e255 | 55 | |
mfurseman | 0:993d6b65e255 | 56 | // SysTick set to lower priority than Wi-Fi SPI bus interrupt |
mfurseman | 0:993d6b65e255 | 57 | NVIC_SetPriority(SysTick_IRQn, 0x2); |
mfurseman | 0:993d6b65e255 | 58 | |
mfurseman | 0:993d6b65e255 | 59 | // Enable RAM1 |
mfurseman | 0:993d6b65e255 | 60 | LPC_SYSCON->SYSAHBCLKCTRL |= (0x1 << 26); |
mfurseman | 1:b66a2d756c8a | 61 | |
mfurseman | 1:b66a2d756c8a | 62 | // This may be neccassary for CC3000 |
mfurseman | 0:993d6b65e255 | 63 | wait(1); |
mfurseman | 1:b66a2d756c8a | 64 | } |
mfurseman | 1:b66a2d756c8a | 65 | |
mfurseman | 0:993d6b65e255 | 66 | |
mfurseman | 1:b66a2d756c8a | 67 | /* Connects WiFi assuming existing SmartConfig */ |
mfurseman | 1:b66a2d756c8a | 68 | void connectWifi() { |
mfurseman | 0:993d6b65e255 | 69 | wifi.start(0); |
mfurseman | 0:993d6b65e255 | 70 | wait_ms(750); |
mfurseman | 1:b66a2d756c8a | 71 | wifi._wlan.ioctl_set_connection_policy(0, 0, 1); |
mfurseman | 1:b66a2d756c8a | 72 | // TODO: Timeout and switch on smart config here |
mfurseman | 2:50c151183047 | 73 | // TODO: Use static IP if possible |
mfurseman | 1:b66a2d756c8a | 74 | } |
mfurseman | 0:993d6b65e255 | 75 | |
mfurseman | 2:50c151183047 | 76 | /* Opens a server on port 5678, waits for a connection, sends 'hello world' |
mfurseman | 2:50c151183047 | 77 | to the client, then closes all sockets and returns. */ |
mfurseman | 2:50c151183047 | 78 | void serverTest() { |
mfurseman | 2:50c151183047 | 79 | TCPSocketServer server; |
mfurseman | 2:50c151183047 | 80 | TCPSocketConnection client; |
mfurseman | 2:50c151183047 | 81 | |
mfurseman | 2:50c151183047 | 82 | int32_t status; |
mfurseman | 2:50c151183047 | 83 | char hello[] = "Hello World\r\n"; |
mfurseman | 2:50c151183047 | 84 | |
mfurseman | 2:50c151183047 | 85 | /* Wait for a client connection on 5678 */ |
mfurseman | 2:50c151183047 | 86 | server.bind(5678); |
mfurseman | 2:50c151183047 | 87 | debug("Before server.listen()\r\n"); |
mfurseman | 2:50c151183047 | 88 | server.listen(); |
mfurseman | 2:50c151183047 | 89 | status = server.accept(client); // This returns -1 with no waiting clients |
mfurseman | 2:50c151183047 | 90 | |
mfurseman | 2:50c151183047 | 91 | /* Send hello world message to client */ |
mfurseman | 2:50c151183047 | 92 | debug("Accepted client with status %d\r\n", status); |
mfurseman | 2:50c151183047 | 93 | if(status >= 0) { |
mfurseman | 2:50c151183047 | 94 | client.set_blocking(false, 1500); // Timeout after (1.5)s |
mfurseman | 2:50c151183047 | 95 | debug("Connection from: %s \r\n", client.get_address()); |
mfurseman | 2:50c151183047 | 96 | client.send_all(hello, sizeof(hello)); |
mfurseman | 2:50c151183047 | 97 | } |
mfurseman | 2:50c151183047 | 98 | |
mfurseman | 2:50c151183047 | 99 | /* Disconnect all sockets */ |
mfurseman | 2:50c151183047 | 100 | client.close(); |
mfurseman | 2:50c151183047 | 101 | server.close(); |
mfurseman | 2:50c151183047 | 102 | } |
mfurseman | 1:b66a2d756c8a | 103 | |
mfurseman | 1:b66a2d756c8a | 104 | int main(void) { |
mfurseman | 2:50c151183047 | 105 | init(); |
mfurseman | 2:50c151183047 | 106 | debug("Completed init()\r\n"); |
mfurseman | 2:50c151183047 | 107 | printConnectionInfo(); |
mfurseman | 1:b66a2d756c8a | 108 | connectWifi(); |
mfurseman | 2:50c151183047 | 109 | debug("Completed connectWifi()\r\n"); |
mfurseman | 2:50c151183047 | 110 | printConnectionInfo(); |
mfurseman | 0:993d6b65e255 | 111 | |
mfurseman | 0:993d6b65e255 | 112 | while(1) { |
mfurseman | 2:50c151183047 | 113 | debug("\r\n :: One second test loop :: \r\n"); |
mfurseman | 0:993d6b65e255 | 114 | printConnectionInfo(); |
mfurseman | 2:50c151183047 | 115 | serverTest(); |
mfurseman | 0:993d6b65e255 | 116 | led = !led; |
mfurseman | 0:993d6b65e255 | 117 | wait(1); |
mfurseman | 0:993d6b65e255 | 118 | } |
mfurseman | 0:993d6b65e255 | 119 | } |