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

Committer:
mfurseman
Date:
Fri Oct 31 23:27:35 2014 +0000
Revision:
3:ba11f6207550
Parent:
2:50c151183047
Child:
4:1b5c2a2cdeb7
Working with modified library

Who changed what in which revision?

UserRevisionLine numberNew 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 3:ba11f6207550 6 /* MAC 08:00:28:57:43:b8 */
mfurseman 3:ba11f6207550 7
mfurseman 3:ba11f6207550 8 /* Quickly change debug flag to remove USB serial code */
mfurseman 3:ba11f6207550 9 //#define DEBUG
mfurseman 0:993d6b65e255 10 #ifdef DEBUG
mfurseman 2:50c151183047 11 #include "USBSerial.h"
mfurseman 3:ba11f6207550 12 USBSerial serial; // This must be instantiated in main
mfurseman 0:993d6b65e255 13 #define debug(x, ...) serial.printf(x, ##__VA_ARGS__);
mfurseman 0:993d6b65e255 14 #else
mfurseman 0:993d6b65e255 15 #define debug(x, ...)
mfurseman 0:993d6b65e255 16 #endif
mfurseman 0:993d6b65e255 17
mfurseman 3:ba11f6207550 18 /* Client commands */
mfurseman 3:ba11f6207550 19 #define CMD_NULL 0
mfurseman 3:ba11f6207550 20 #define CMD_C_ECHO 97
mfurseman 3:ba11f6207550 21 #define CMD_C_LED_ON 98
mfurseman 3:ba11f6207550 22 #define CMD_C_LED_OFF 99
mfurseman 3:ba11f6207550 23
mfurseman 1:b66a2d756c8a 24
mfurseman 0:993d6b65e255 25 using namespace mbed_cc3000;
mfurseman 0:993d6b65e255 26
mfurseman 1:b66a2d756c8a 27
mfurseman 0:993d6b65e255 28 /* On board LED */
mfurseman 0:993d6b65e255 29 DigitalOut led(P0_1);
mfurseman 0:993d6b65e255 30
mfurseman 0:993d6b65e255 31 /* Serial library for WiFi module */
mfurseman 0:993d6b65e255 32 cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37));
mfurseman 0:993d6b65e255 33
mfurseman 0:993d6b65e255 34 /* Struct to hold connection data */
mfurseman 0:993d6b65e255 35 tNetappIpconfigRetArgs ipinfo;
mfurseman 0:993d6b65e255 36
mfurseman 0:993d6b65e255 37
mfurseman 1:b66a2d756c8a 38 /* Prints CC3000 connection info */
mfurseman 3:ba11f6207550 39 void printConnectionInfo()
mfurseman 3:ba11f6207550 40 {
mfurseman 0:993d6b65e255 41 if (( wifi.is_enabled() ) && ( wifi.is_dhcp_configured() )) {
mfurseman 0:993d6b65e255 42 wifi.get_ip_config(&ipinfo);
mfurseman 3:ba11f6207550 43 }
mfurseman 0:993d6b65e255 44 if (! wifi.is_enabled() ) {
mfurseman 3:ba11f6207550 45 debug("CC3000 Disabled\r\n");
mfurseman 3:ba11f6207550 46 } else if ( wifi.is_dhcp_configured() ) {
mfurseman 0:993d6b65e255 47 debug("SSID : %-33s|\r\n", ipinfo.uaSSID);
mfurseman 3:ba11f6207550 48 debug("IP : %-35s|\r\n", wifi.getIPAddress());
mfurseman 3:ba11f6207550 49 } else if ( wifi.is_connected() ) {
mfurseman 3:ba11f6207550 50 debug("Connecting, waiting for DHCP\r\n");
mfurseman 3:ba11f6207550 51 } else {
mfurseman 3:ba11f6207550 52 debug("Not Connected\r\n");
mfurseman 0:993d6b65e255 53 }
mfurseman 0:993d6b65e255 54 }
mfurseman 0:993d6b65e255 55
mfurseman 1:b66a2d756c8a 56
mfurseman 1:b66a2d756c8a 57 /* WiFi DipCortex board setup */
mfurseman 3:ba11f6207550 58 void init()
mfurseman 3:ba11f6207550 59 {
mfurseman 3:ba11f6207550 60 NVIC_SetPriority(SSP1_IRQn, 0x0);
mfurseman 0:993d6b65e255 61 NVIC_SetPriority(PIN_INT0_IRQn, 0x1);
mfurseman 3:ba11f6207550 62
mfurseman 0:993d6b65e255 63 // SysTick set to lower priority than Wi-Fi SPI bus interrupt
mfurseman 3:ba11f6207550 64 NVIC_SetPriority(SysTick_IRQn, 0x2);
mfurseman 3:ba11f6207550 65
mfurseman 0:993d6b65e255 66 // Enable RAM1
mfurseman 0:993d6b65e255 67 LPC_SYSCON->SYSAHBCLKCTRL |= (0x1 << 26);
mfurseman 3:ba11f6207550 68
mfurseman 1:b66a2d756c8a 69 // This may be neccassary for CC3000
mfurseman 0:993d6b65e255 70 wait(1);
mfurseman 1:b66a2d756c8a 71 }
mfurseman 1:b66a2d756c8a 72
mfurseman 0:993d6b65e255 73
mfurseman 1:b66a2d756c8a 74 /* Connects WiFi assuming existing SmartConfig */
mfurseman 3:ba11f6207550 75 void connectWifi()
mfurseman 3:ba11f6207550 76 {
mfurseman 0:993d6b65e255 77 wifi.start(0);
mfurseman 0:993d6b65e255 78 wait_ms(750);
mfurseman 1:b66a2d756c8a 79 wifi._wlan.ioctl_set_connection_policy(0, 0, 1);
mfurseman 1:b66a2d756c8a 80 // TODO: Timeout and switch on smart config here
mfurseman 2:50c151183047 81 // TODO: Use static IP if possible
mfurseman 1:b66a2d756c8a 82 }
mfurseman 0:993d6b65e255 83
mfurseman 2:50c151183047 84
mfurseman 3:ba11f6207550 85 /* Where it all begins */
mfurseman 3:ba11f6207550 86 int main(void)
mfurseman 3:ba11f6207550 87 {
mfurseman 2:50c151183047 88 init();
mfurseman 2:50c151183047 89 debug("Completed init()\r\n");
mfurseman 2:50c151183047 90 printConnectionInfo();
mfurseman 3:ba11f6207550 91
mfurseman 1:b66a2d756c8a 92 connectWifi();
mfurseman 2:50c151183047 93 debug("Completed connectWifi()\r\n");
mfurseman 2:50c151183047 94 printConnectionInfo();
mfurseman 0:993d6b65e255 95
mfurseman 0:993d6b65e255 96 while(1) {
mfurseman 3:ba11f6207550 97 debug("\r\nOne second client attachment loop\r\n");
mfurseman 0:993d6b65e255 98 printConnectionInfo();
mfurseman 3:ba11f6207550 99
mfurseman 3:ba11f6207550 100 debug("Creating server and client sockets\r\n");
mfurseman 3:ba11f6207550 101 wait_ms(15);
mfurseman 3:ba11f6207550 102 TCPSocketConnection client; // is_connected not reliable when using non-blocking sockets
mfurseman 3:ba11f6207550 103 TCPSocketServer server;
mfurseman 3:ba11f6207550 104 int32_t status;
mfurseman 3:ba11f6207550 105
mfurseman 3:ba11f6207550 106 server.bind(5678);
mfurseman 3:ba11f6207550 107 server.listen();
mfurseman 3:ba11f6207550 108 status = server.accept(client);
mfurseman 3:ba11f6207550 109 wait_ms(15);
mfurseman 3:ba11f6207550 110 int n_timeout = 1;
mfurseman 3:ba11f6207550 111 debug("Accept client returned with status %d\r\n", status);
mfurseman 3:ba11f6207550 112 if(status >= 0) {
mfurseman 3:ba11f6207550 113 client.set_blocking(false, 1000); // 5 ms time out is min for CC3000
mfurseman 3:ba11f6207550 114 debug("Connection from: %s \r\n", client.get_address());
mfurseman 3:ba11f6207550 115 wait_ms(15);
mfurseman 3:ba11f6207550 116
mfurseman 3:ba11f6207550 117 while(1) {
mfurseman 3:ba11f6207550 118 debug("\r\nClient connected loop - 0 ms\r\n");
mfurseman 3:ba11f6207550 119
mfurseman 3:ba11f6207550 120 wait_ms(15);
mfurseman 3:ba11f6207550 121 char command = 0;
mfurseman 3:ba11f6207550 122 client.set_blocking(false, 5); // 5 ms time out is min for CC3000
mfurseman 3:ba11f6207550 123 status = client.receive(&command, 1);
mfurseman 3:ba11f6207550 124 if(status == 1) {
mfurseman 3:ba11f6207550 125 debug("Recieved data from client: %d with status %d\r\n", command, status);
mfurseman 3:ba11f6207550 126 switch(command) {
mfurseman 3:ba11f6207550 127 case CMD_C_ECHO:
mfurseman 3:ba11f6207550 128 wait_ms(15);
mfurseman 3:ba11f6207550 129 char buffer[3];
mfurseman 3:ba11f6207550 130 client.set_blocking(false, 2000);
mfurseman 3:ba11f6207550 131 status = client.receive_all(buffer, sizeof(buffer));
mfurseman 3:ba11f6207550 132 debug("Echo test recieved: %s Status: %d\r\n", buffer, status);
mfurseman 3:ba11f6207550 133
mfurseman 3:ba11f6207550 134 wait_ms(15);
mfurseman 3:ba11f6207550 135 status = client.send_all(buffer, sizeof(buffer));
mfurseman 3:ba11f6207550 136 debug("Echo test send completed with status: %d\r\n");
mfurseman 3:ba11f6207550 137 break;
mfurseman 3:ba11f6207550 138
mfurseman 3:ba11f6207550 139 case CMD_C_LED_ON:
mfurseman 3:ba11f6207550 140 led = 1;
mfurseman 3:ba11f6207550 141 break;
mfurseman 3:ba11f6207550 142
mfurseman 3:ba11f6207550 143 case CMD_C_LED_OFF:
mfurseman 3:ba11f6207550 144 led = 0;
mfurseman 3:ba11f6207550 145 break;
mfurseman 3:ba11f6207550 146
mfurseman 3:ba11f6207550 147 default:
mfurseman 3:ba11f6207550 148 debug("Command %d not recognised\r\n", command);
mfurseman 3:ba11f6207550 149 break;
mfurseman 3:ba11f6207550 150 }
mfurseman 3:ba11f6207550 151 }
mfurseman 3:ba11f6207550 152
mfurseman 3:ba11f6207550 153 wait_ms(15);
mfurseman 3:ba11f6207550 154 /* Check to see if the non-blocking socket is closed */
mfurseman 3:ba11f6207550 155 if((n_timeout++) % 100 == 0) {
mfurseman 3:ba11f6207550 156 client.set_blocking(false, 1000); // 5 ms time out is min for CC3000
mfurseman 3:ba11f6207550 157 status = client.send("abc\r\n", 5);
mfurseman 3:ba11f6207550 158 debug("Single byte send returned with status %d\r\n", status);
mfurseman 3:ba11f6207550 159 if(status < 0) {
mfurseman 3:ba11f6207550 160 break;
mfurseman 3:ba11f6207550 161 }
mfurseman 3:ba11f6207550 162 }
mfurseman 3:ba11f6207550 163 }
mfurseman 3:ba11f6207550 164 debug("Client connection lost\r\n");
mfurseman 3:ba11f6207550 165 wait_ms(15);
mfurseman 3:ba11f6207550 166 }
mfurseman 3:ba11f6207550 167 debug("Should now return to the top of while(1) after changing LED\r\n");
mfurseman 3:ba11f6207550 168 wait_ms(15);
mfurseman 3:ba11f6207550 169
mfurseman 3:ba11f6207550 170 debug("After wait_ms(15)\r\n");
mfurseman 0:993d6b65e255 171 led = !led;
mfurseman 3:ba11f6207550 172 debug("After led = !led\r\n");
mfurseman 3:ba11f6207550 173 wait(1.);
mfurseman 3:ba11f6207550 174 debug("After wait(1.)\r\n");
mfurseman 3:ba11f6207550 175 }
mfurseman 0:993d6b65e255 176 }