Control a robot over the internet using UDP and a Wifly module (WiFi).

Dependencies:   Motor TextLCD WiflyInterface mbed-rtos mbed

Committer:
apatel336
Date:
Thu Oct 17 13:27:56 2013 +0000
Revision:
0:c0dc3a76f3d4
Initial Release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
apatel336 0:c0dc3a76f3d4 1 /* Copyright (C) 2012 mbed.org, MIT License
apatel336 0:c0dc3a76f3d4 2 *
apatel336 0:c0dc3a76f3d4 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
apatel336 0:c0dc3a76f3d4 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
apatel336 0:c0dc3a76f3d4 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
apatel336 0:c0dc3a76f3d4 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
apatel336 0:c0dc3a76f3d4 7 * furnished to do so, subject to the following conditions:
apatel336 0:c0dc3a76f3d4 8 *
apatel336 0:c0dc3a76f3d4 9 * The above copyright notice and this permission notice shall be included in all copies or
apatel336 0:c0dc3a76f3d4 10 * substantial portions of the Software.
apatel336 0:c0dc3a76f3d4 11 *
apatel336 0:c0dc3a76f3d4 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
apatel336 0:c0dc3a76f3d4 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
apatel336 0:c0dc3a76f3d4 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
apatel336 0:c0dc3a76f3d4 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
apatel336 0:c0dc3a76f3d4 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
apatel336 0:c0dc3a76f3d4 17 */
apatel336 0:c0dc3a76f3d4 18
apatel336 0:c0dc3a76f3d4 19 #include "TCPSocketServer.h"
apatel336 0:c0dc3a76f3d4 20 #include <string>
apatel336 0:c0dc3a76f3d4 21
apatel336 0:c0dc3a76f3d4 22 TCPSocketServer::TCPSocketServer() {}
apatel336 0:c0dc3a76f3d4 23
apatel336 0:c0dc3a76f3d4 24 // Server initialization
apatel336 0:c0dc3a76f3d4 25 int TCPSocketServer::bind(int port) {
apatel336 0:c0dc3a76f3d4 26 char cmd[20];
apatel336 0:c0dc3a76f3d4 27
apatel336 0:c0dc3a76f3d4 28 // set TCP protocol
apatel336 0:c0dc3a76f3d4 29 wifi->setProtocol(TCP);
apatel336 0:c0dc3a76f3d4 30
apatel336 0:c0dc3a76f3d4 31 // set local port
apatel336 0:c0dc3a76f3d4 32 sprintf(cmd, "set i l %d\r", port);
apatel336 0:c0dc3a76f3d4 33 if (!wifi->sendCommand(cmd, "AOK"))
apatel336 0:c0dc3a76f3d4 34 return -1;
apatel336 0:c0dc3a76f3d4 35
apatel336 0:c0dc3a76f3d4 36 // save
apatel336 0:c0dc3a76f3d4 37 if (!wifi->sendCommand("save\r", "Stor"))
apatel336 0:c0dc3a76f3d4 38 return -1;
apatel336 0:c0dc3a76f3d4 39
apatel336 0:c0dc3a76f3d4 40 // reboot
apatel336 0:c0dc3a76f3d4 41 wifi->reboot();
apatel336 0:c0dc3a76f3d4 42
apatel336 0:c0dc3a76f3d4 43 // connect the network
apatel336 0:c0dc3a76f3d4 44 if (wifi->isDHCP()) {
apatel336 0:c0dc3a76f3d4 45 if (!wifi->sendCommand("join\r", "DHCP=ON", NULL, 10000))
apatel336 0:c0dc3a76f3d4 46 return -1;
apatel336 0:c0dc3a76f3d4 47 } else {
apatel336 0:c0dc3a76f3d4 48 if (!wifi->sendCommand("join\r", "Associated", NULL, 10000))
apatel336 0:c0dc3a76f3d4 49 return -1;
apatel336 0:c0dc3a76f3d4 50 }
apatel336 0:c0dc3a76f3d4 51
apatel336 0:c0dc3a76f3d4 52 // exit
apatel336 0:c0dc3a76f3d4 53 wifi->exit();
apatel336 0:c0dc3a76f3d4 54
apatel336 0:c0dc3a76f3d4 55 wait(0.2);
apatel336 0:c0dc3a76f3d4 56 wifi->flush();
apatel336 0:c0dc3a76f3d4 57 return 0;
apatel336 0:c0dc3a76f3d4 58 }
apatel336 0:c0dc3a76f3d4 59
apatel336 0:c0dc3a76f3d4 60 int TCPSocketServer::listen(int backlog) {
apatel336 0:c0dc3a76f3d4 61 if (backlog != 1)
apatel336 0:c0dc3a76f3d4 62 return -1;
apatel336 0:c0dc3a76f3d4 63 return 0;
apatel336 0:c0dc3a76f3d4 64 }
apatel336 0:c0dc3a76f3d4 65
apatel336 0:c0dc3a76f3d4 66
apatel336 0:c0dc3a76f3d4 67 int TCPSocketServer::accept(TCPSocketConnection& connection) {
apatel336 0:c0dc3a76f3d4 68 int nb_available = 0, pos = 0;
apatel336 0:c0dc3a76f3d4 69 char c;
apatel336 0:c0dc3a76f3d4 70 string str;
apatel336 0:c0dc3a76f3d4 71 bool o_find = false;
apatel336 0:c0dc3a76f3d4 72 while (1) {
apatel336 0:c0dc3a76f3d4 73 while(!wifi->readable());
apatel336 0:c0dc3a76f3d4 74 nb_available = wifi->readable();
apatel336 0:c0dc3a76f3d4 75 for (int i = 0; i < nb_available; i++) {
apatel336 0:c0dc3a76f3d4 76 c = wifi->getc();
apatel336 0:c0dc3a76f3d4 77 if (c == '*') {
apatel336 0:c0dc3a76f3d4 78 o_find = true;
apatel336 0:c0dc3a76f3d4 79 }
apatel336 0:c0dc3a76f3d4 80 if (o_find && c != '\r' && c != '\n') {
apatel336 0:c0dc3a76f3d4 81 str += c;
apatel336 0:c0dc3a76f3d4 82 pos = str.find("*OPEN*");
apatel336 0:c0dc3a76f3d4 83 if (pos != string::npos) {
apatel336 0:c0dc3a76f3d4 84 wifi->flush();
apatel336 0:c0dc3a76f3d4 85 return 0;
apatel336 0:c0dc3a76f3d4 86 }
apatel336 0:c0dc3a76f3d4 87 }
apatel336 0:c0dc3a76f3d4 88 }
apatel336 0:c0dc3a76f3d4 89 }
apatel336 0:c0dc3a76f3d4 90 }