Library to use a wifly module: RN 131 C/G
Dependents: RN-XV_simple_server
Wifly.cpp
- Committer:
- samux
- Date:
- 2011-08-12
- Revision:
- 4:06ca04e2e279
- Parent:
- 0:2f38aaabc810
- Child:
- 5:9890eb81f730
File content as of revision 4:06ca04e2e279:
#include "mbed.h" #include "Wifly.h" #include <string> Wifly::Wifly( PinName tx, PinName rx, PinName _reset, char * ssid, char * phrase, bool wpa, char * ip, char * netmask, bool dhcp, int baudrate, int bits,Serial::Parity parity, int stop_bits): wifi(tx, rx), reset_pin(_reset) { wifi.baud(baudrate); reset_pin = 1; wifi.format(bits, parity, stop_bits); this->wpa = wpa; this->phrase = phrase; this->ssid = ssid; this->ip = ip; this->netmask = netmask; this->dhcp = dhcp; adhoc = false; } Wifly::Wifly( PinName tx, PinName rx, PinName _reset, char * ssid, char * ip, char * netmask, int channel, int baudrate, int bits,Serial::Parity parity,int stop_bits): wifi(tx, rx), reset_pin(_reset) { wifi.baud(baudrate); reset_pin = 1; wifi.format(bits, parity, stop_bits); adhoc = true; this->ssid = ssid; this->ip = ip; this->netmask = netmask; this->channel = channel; } bool Wifly::Send(char * str, char * ACK) { //We flush the buffer while(wifi.readable()) //printf("%c",wifi.getc()); wifi.getc(); char read; size_t found = string::npos; if (!strcmp(ACK, "NO")) wifi.printf("%s", str); else { Timer tmr; tmr.start(); wifi.printf("%s", str); string checking; while (1) { if(tmr.read() > 3) { //printf("checking: %s\r\n", checking); return false; } else if(wifi.readable()) { read = wifi.getc(); if ( read != '\r' && read != '\n') { checking += read; found = checking.find(ACK); if(found != string::npos) { //printf("checking: %s\r\n", checking); break; } } } } return (found != string::npos); } return true; } bool Wifly::Join() { char cmd[30]; exit(); if(!CmdMode()) { printf("join: cannot enter in cmd mode\r\n"); exit(); return false; } //auth if(!Send("set wlan auth 3\r\n", "AOK")) { printf("join: cannot set auth\r\n"); exit(); return false; } //dhcp sprintf(cmd, "set ip dhcp %d\r\n", (dhcp) ? 1 : 0); if(!Send(cmd, "AOK")) { printf("join: cannot set dhcp\r\n"); exit(); return false; } if(!dhcp) { printf("not dhcp\r\n"); sprintf(cmd, "set ip address %s\r\n", ip); if(!Send(cmd, "AOK")) { printf("Wifly::Join: cannot set ip address\r\n"); exit(); return false; } sprintf(cmd, "set ip netmask %s\r\n", netmask); if(!Send(cmd, "AOK")) { printf("Wifly::join: cannot set netmask\r\n"); exit(); return false; } } //key step if(wpa) sprintf(cmd, "set wlan phrase %s\r\n", phrase); else sprintf(cmd, "set wlan key %s\r\n", phrase); if(!Send(cmd, "AOK")) { printf("join: cannot set phrase\r\n"); exit(); return false; } //join the network sprintf(cmd, "join %s\r\n", ssid); if(!Send(cmd, "IP=")) { printf("join: cannot join %s\r\n", ssid); exit(); return false; } exit(); printf("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase, (wpa) ? "WPA" : "WEP"); return true; } bool Wifly::CreateAdhocNetwork() { if(adhoc) { char cmd[50]; exit(); if(!CmdMode()) { printf("Wifly::CreateAdhocNetwork: cannot enter in cmd mode\r\n"); exit(); return false; } if(!Send("set wlan join 4\r\n", "AOK")) { printf("Wifly::CreateAdhocNetwork: cannot set join 4\r\n"); exit(); return false; } //ssid sprintf(cmd, "set wlan ssid %s\r\n", ssid); if(!Send(cmd, "AOK")) { printf("Wifly::CreateAdhocNetwork: cannot set ssid\r\n"); exit(); return false; } sprintf(cmd, "set wlan channel %d\r\n", channel); if(!Send(cmd, "AOK")) { printf("Wifly::CreateAdhocNetwork: cannot set channel\r\n"); exit(); return false; } sprintf(cmd, "set ip address %s\r\n", ip); if(!Send(cmd, "AOK")) { printf("Wifly::CreateAdhocNetwork: cannot set ip address\r\n"); exit(); return false; } sprintf(cmd, "set ip netmask %s\r\n", netmask); if(!Send(cmd, "AOK")) { printf("Wifly::CreateAdhocNetwork: cannot set netmask\r\n"); exit(); return false; } if(!Send("set ip dhcp 0\r\n", "AOK")) { printf("Wifly::CreateAdhocNetwork: cannot set dhcp off\r\n"); exit(); return false; } if(!Send("save\r\n", "Stor")) { printf("Wifly::CreateAdhocNetwork: cannot save\r\n"); exit(); return false; } Send("reboot\r\n", "NO"); printf("\r\ncreating an adhoc\r\nnetwork: %s\r\nip: %s\r\nnetmask: %s\r\nchannel: %d\r\n\r\n", ssid, ip, netmask, channel); return true; } else { printf("Wifly::join: You don't chose the right constructor for creating an adhoc mode!\r\n"); return false; } } bool Wifly::CmdMode() { int i = 0; while(!Send("$$$", "CMD")) { printf("Wifly::CmdMode: cannot enter in cmd mode\r\n"); i++; if ( i == 4 ) return false; } return true; } void Wifly::reset() { reset_pin = 0; wait(0.2); reset_pin = 1; wait(0.2); } void Wifly::putc(char c) { wifi.putc(c); } void Wifly::read(char * str) { wifi.scanf("%s", str); } bool Wifly::exit() { return Send("exit\r", "EXIT"); } bool Wifly::readable() { return(wifi.readable()); } bool Wifly::changeBaudrate(int baudrate) { char cmd[20]; exit(); if(!CmdMode()) { printf("Wifly::changeBaudrate: cannot enter in cmd mode\r\n"); return false; } sprintf(cmd, "set u b %d\r\n", baudrate); if(!Send(cmd, "AOK")) { printf("Wifly::changeBaudrate: cannot set new baudrate\r\n"); exit(); return false; } if(!Send("save\r\n", "Stor")) { printf("Wifly::changeBaudrate: cannot save\r\n"); exit(); return false; } return true; } char Wifly::getc() { return(wifi.getc()); }