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 #include "WiflyInterface.h"
apatel336 0:c0dc3a76f3d4 2
apatel336 0:c0dc3a76f3d4 3 WiflyInterface::WiflyInterface( PinName tx, PinName rx, PinName reset, PinName tcp_status,
apatel336 0:c0dc3a76f3d4 4 const char * ssid, const char * phrase, Security sec) :
apatel336 0:c0dc3a76f3d4 5 Wifly(tx, rx, reset, tcp_status, ssid, phrase, sec)
apatel336 0:c0dc3a76f3d4 6 {
apatel336 0:c0dc3a76f3d4 7 ip_set = false;
apatel336 0:c0dc3a76f3d4 8 }
apatel336 0:c0dc3a76f3d4 9
apatel336 0:c0dc3a76f3d4 10 int WiflyInterface::init()
apatel336 0:c0dc3a76f3d4 11 {
apatel336 0:c0dc3a76f3d4 12 state.dhcp = true;
apatel336 0:c0dc3a76f3d4 13 reset();
apatel336 0:c0dc3a76f3d4 14 return 0;
apatel336 0:c0dc3a76f3d4 15 }
apatel336 0:c0dc3a76f3d4 16
apatel336 0:c0dc3a76f3d4 17 int WiflyInterface::init(const char* ip, const char* mask, const char* gateway)
apatel336 0:c0dc3a76f3d4 18 {
apatel336 0:c0dc3a76f3d4 19 state.dhcp = false;
apatel336 0:c0dc3a76f3d4 20 this->ip = ip;
apatel336 0:c0dc3a76f3d4 21 strcpy(ip_string, ip);
apatel336 0:c0dc3a76f3d4 22 ip_set = true;
apatel336 0:c0dc3a76f3d4 23 this->netmask = mask;
apatel336 0:c0dc3a76f3d4 24 this->gateway = gateway;
apatel336 0:c0dc3a76f3d4 25 reset();
apatel336 0:c0dc3a76f3d4 26
apatel336 0:c0dc3a76f3d4 27 return 0;
apatel336 0:c0dc3a76f3d4 28 }
apatel336 0:c0dc3a76f3d4 29
apatel336 0:c0dc3a76f3d4 30 int WiflyInterface::connect()
apatel336 0:c0dc3a76f3d4 31 {
apatel336 0:c0dc3a76f3d4 32 return join();
apatel336 0:c0dc3a76f3d4 33 }
apatel336 0:c0dc3a76f3d4 34
apatel336 0:c0dc3a76f3d4 35 int WiflyInterface::disconnect()
apatel336 0:c0dc3a76f3d4 36 {
apatel336 0:c0dc3a76f3d4 37 return Wifly::disconnect();
apatel336 0:c0dc3a76f3d4 38 }
apatel336 0:c0dc3a76f3d4 39
apatel336 0:c0dc3a76f3d4 40 char * WiflyInterface::getIPAddress()
apatel336 0:c0dc3a76f3d4 41 {
apatel336 0:c0dc3a76f3d4 42 char * match = 0;
apatel336 0:c0dc3a76f3d4 43 if (!ip_set) {
apatel336 0:c0dc3a76f3d4 44 if (!sendCommand("get ip a\r", NULL, ip_string))
apatel336 0:c0dc3a76f3d4 45 return NULL;
apatel336 0:c0dc3a76f3d4 46 exit();
apatel336 0:c0dc3a76f3d4 47 flush();
apatel336 0:c0dc3a76f3d4 48 match = strstr(ip_string, "<");
apatel336 0:c0dc3a76f3d4 49 if (match != NULL) {
apatel336 0:c0dc3a76f3d4 50 *match = '\0';
apatel336 0:c0dc3a76f3d4 51 }
apatel336 0:c0dc3a76f3d4 52 if (strlen(ip_string) < 6) {
apatel336 0:c0dc3a76f3d4 53 match = strstr(ip_string, ">");
apatel336 0:c0dc3a76f3d4 54 if (match != NULL) {
apatel336 0:c0dc3a76f3d4 55 int len = strlen(match + 1);
apatel336 0:c0dc3a76f3d4 56 memcpy(ip_string, match + 1, len);
apatel336 0:c0dc3a76f3d4 57 }
apatel336 0:c0dc3a76f3d4 58 }
apatel336 0:c0dc3a76f3d4 59 ip_set = true;
apatel336 0:c0dc3a76f3d4 60 }
apatel336 0:c0dc3a76f3d4 61 return ip_string;
apatel336 0:c0dc3a76f3d4 62 }