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
Diff: main.cpp
- Revision:
- 2:50c151183047
- Parent:
- 1:b66a2d756c8a
- Child:
- 3:ba11f6207550
--- a/main.cpp Tue Oct 21 14:03:50 2014 +0000 +++ b/main.cpp Sat Oct 25 17:34:45 2014 +0000 @@ -1,11 +1,12 @@ #include "mbed.h" #include "cc3000.h" -#include "USBSerial.h" - +#include "TCPSocketConnection.h" +#include "TCPSocketServer.h" /* Quickly change debug flag to remove blocking serial code */ #define DEBUG #ifdef DEBUG +#include "USBSerial.h" USBSerial serial; #define debug(x, ...) serial.printf(x, ##__VA_ARGS__); #else @@ -31,7 +32,6 @@ if (( wifi.is_enabled() ) && ( wifi.is_dhcp_configured() )) { wifi.get_ip_config(&ipinfo); } - debug("\r\n"); if (! wifi.is_enabled() ) { debug("CC3000 Disabled\r\n"); } @@ -70,16 +70,49 @@ wait_ms(750); wifi._wlan.ioctl_set_connection_policy(0, 0, 1); // TODO: Timeout and switch on smart config here + // TODO: Use static IP if possible } +/* Opens a server on port 5678, waits for a connection, sends 'hello world' + to the client, then closes all sockets and returns. */ +void serverTest() { + TCPSocketServer server; + TCPSocketConnection client; + + int32_t status; + char hello[] = "Hello World\r\n"; + + /* Wait for a client connection on 5678 */ + server.bind(5678); + debug("Before server.listen()\r\n"); + server.listen(); + status = server.accept(client); // This returns -1 with no waiting clients + + /* Send hello world message to client */ + debug("Accepted client with status %d\r\n", status); + if(status >= 0) { + client.set_blocking(false, 1500); // Timeout after (1.5)s + debug("Connection from: %s \r\n", client.get_address()); + client.send_all(hello, sizeof(hello)); + } + + /* Disconnect all sockets */ + client.close(); + server.close(); +} int main(void) { - init(); + init(); + debug("Completed init()\r\n"); + printConnectionInfo(); connectWifi(); + debug("Completed connectWifi()\r\n"); + printConnectionInfo(); while(1) { + debug("\r\n :: One second test loop :: \r\n"); printConnectionInfo(); - debug("test message\r\n"); + serverTest(); led = !led; wait(1); }