Forked mbed official WiflyInterface (interface for Roving Networks Wifly modules) which includes the possibility to use TCPSocketServer::accept as a non-blocking cal.
Dependents: WiFlyHTTPServerSample MultiThreadingHTTPServer
Fork of WiflyInterface by
Diff: Wifly/Wifly.cpp
- Revision:
- 7:e42b7fa7ef70
- Parent:
- 5:48d55083d2ff
- Child:
- 8:ac134ae11893
--- a/Wifly/Wifly.cpp Sat Jun 01 15:03:14 2013 +0000 +++ b/Wifly/Wifly.cpp Sun Jun 02 00:20:45 2013 +0000 @@ -22,7 +22,7 @@ #include <algorithm> //Debug is disabled by default -#if (1 && !defined(TARGET_LPC11U24)) +#if (0 && !defined(TARGET_LPC11U24)) #define DBG(x, ...) std::printf("[Wifly : DBG]"x"\r\n", ##__VA_ARGS__); #define WARN(x, ...) std::printf("[Wifly : WARN]"x"\r\n", ##__VA_ARGS__); #define ERR(x, ...) std::printf("[Wifly : ERR]"x"\r\n", ##__VA_ARGS__); @@ -40,10 +40,13 @@ #define MAX_TRY_JOIN 3 +const int std_baudrates[] = { 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600 }; + + Wifly * Wifly::inst; -Wifly::Wifly( PinName tx, PinName rx, PinName _reset, PinName tcp_status, const char * ssid, const char * phrase, Security sec): - wifi(tx, rx), reset_pin(_reset), tcp_status(tcp_status), buf_wifly(512) +Wifly::Wifly( PinName tx, PinName rx, PinName _reset, PinName tcp_status, const char * ssid, const char * phrase, Security sec, WiflyBaudrate_t baud): + wifi(tx, rx), reset_pin(_reset), tcp_status(tcp_status), buf_wifly(512), m_baudrate(baud) { memset(&state, 0, sizeof(state)); state.sec = sec; @@ -63,11 +66,15 @@ inst = this; attach_rx(false); state.cmd_mode = false; + + reset(); } bool Wifly::join() { - char cmd[20]; + char cmd[50]; + + setBaudRate(std_baudrates[m_baudrate]); for (int i= 0; i < MAX_TRY_JOIN; i++) { @@ -159,10 +166,10 @@ continue; } + enableTime(1); + if (!sendCommand("save\r", "Stor")) continue; - - enableTime(1); exit(); @@ -173,6 +180,81 @@ return false; } +int Wifly::enableTime(int minutes, const char* ntp_address) +{ + char cmd[30]; + + //let module automatically conntect to timeserver and get the actual time + sprintf(cmd, "set t e %d\r", minutes); + if (!sendCommand(cmd, "AOK")) { + ERR("Failed to modify time function !"); + return -1; + } + + //set the NTP server address + sprintf(cmd, "set t a %s\r", ntp_address); + if (!sendCommand(cmd, "AOK")) { + ERR("Failed to modify time server address !"); + } + + if (!sendCommand("set option format 1\r", "AOK")) { + ERR("Failed to set option format to ASCII !"); + } + + if (!sendCommand("set time zone 0\r", "AOK")) { + ERR("Failed to set time zone !"); + } + + sendCommand("time\r", NULL, NULL); + + flush(); + + exit(); + + return 0; +} + +string Wifly::getTime(bool uptime) +{ + char buf[100]; + + sendCommand("time\r", NULL, NULL, 10000); + if (!sendCommand("show time\r", NULL, buf, 10000)) + return ""; + INFO("\r\nReceived Time : %s\r\n", buf); + + exit(); + return buf; +} + +bool Wifly::setBaudRate(int baud) +{ + char str[35]; + bool bfound = false; + + INFO("Trying baudrates.\n"); + sprintf(str, "set uart raw %d\r", baud); + for( int i = 0 ; i < sizeof(std_baudrates)/sizeof(int) ; i++) { + // try all standard baudrates until the correct one is found + INFO("Trying at %d baud\n", std_baudrates[i]); + wifi.baud(std_baudrates[i]); + if (sendCommand(str, "AOK")) { + bfound = true; + break; + } + } + + if (bfound) { + wait(0.05); + if (!sendCommand("save\r", "STOR")) + return false; + + reboot(); + + wifi.baud(baud); + } + return true; +} bool Wifly::setProtocol(Protocol p) { @@ -216,7 +298,7 @@ bool Wifly::connect(const char * host, int port) { char rcv[20]; - char cmd[20]; + char cmd[50]; // try to open sprintf(cmd, "open %s %d\r", host, port); @@ -301,53 +383,6 @@ buf_wifly.flush(); } -int Wifly::enableTime(int minutes, const char* ntp_address) -{ - char cmd[30]; - - //let module automatically conntect to timeserver and get the actual time - sprintf(cmd, "set t e %d\r", minutes); - if (!sendCommand(cmd, "AOK")) { - ERR("Failed to modify time function !"); - return -1; - } - - //set the NTP server address - sprintf(cmd, "set t a %s\r", ntp_address); - if (!sendCommand(cmd, "AOK")) { - ERR("Failed to modify time server address !"); - } - - if (!sendCommand("set option format 1\r", "AOK")) { - ERR("Failed to set option format to ASCII !"); - } - - if (!sendCommand("set time zone 0\r", "AOK")) { - ERR("Failed to set time zone !"); - } - - sendCommand("time\r", NULL, NULL); - - flush(); - - exit(); - - return 0; -} - -string Wifly::getTime(bool uptime) -{ - char buf[100]; - -// sendCommand("time\r", NULL, NULL, 10000); - if (!sendCommand("show time\r", NULL, buf, 10000)) - return ""; - INFO("\r\nReceived Time : %s\r\n", buf); - - exit(); - return buf; -} - bool Wifly::sendCommand(const char * cmd, const char * ack, char * res, int timeout) { if (!state.cmd_mode) {