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
Revision 7:e42b7fa7ef70, committed 2013-06-02
- Comitter:
- leihen
- Date:
- Sun Jun 02 00:20:45 2013 +0000
- Parent:
- 5:48d55083d2ff
- Child:
- 8:ac134ae11893
- Commit message:
- Added possibility to control baud rate.
; Increased internal buffer sizes.
Changed in this revision
Wifly/Wifly.cpp | Show annotated file Show diff for this revision Revisions of this file |
Wifly/Wifly.h | Show annotated file Show diff for this revision Revisions of this file |
--- 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) {
--- a/Wifly/Wifly.h Sat Jun 01 15:03:14 2013 +0000 +++ b/Wifly/Wifly.h Sun Jun 02 00:20:45 2013 +0000 @@ -46,6 +46,20 @@ class Wifly { +public: + typedef enum + { + Wifly_2400, + Wifly_4800, + Wifly_9600, + Wifly_19200, + Wifly_38400, + Wifly_57600, + Wifly_115200, + Wifly_230400, + Wifly_460800, + Wifly_921600 + } WiflyBaudrate_t; public: /* @@ -59,7 +73,7 @@ * @param phrase WEP or WPA key * @param sec Security type (NONE, WEP_128 or WPA) */ - Wifly( PinName tx, PinName rx, PinName reset, PinName tcp_status, const char * ssid, const char * phrase, Security sec); + Wifly( PinName tx, PinName rx, PinName reset, PinName tcp_status, const char * ssid, const char * phrase, Security sec, WiflyBaudrate_t baud = Wifly_115200); /* * Connect the wifi module to the ssid contained in the constructor. @@ -215,7 +229,7 @@ * @ returns the time from RTC */ std::string getTime(bool uptime); - + bool gethostbyname(const char * host, char * ip); static Wifly * getInstance() { @@ -223,6 +237,16 @@ }; protected: + + /** Set the baud rate for communication + * + * @param baud : sets the baud rate for communication between wifly and host computer + * + * @ returns true if successful, false otherwise + */ + bool setBaudRate(int baud); + WiflyBaudrate_t m_baudrate; + Serial wifi; DigitalOut reset_pin; DigitalIn tcp_status;