Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: RN-XV_simple_server
Wifly.cpp
- Committer:
- samux
- Date:
- 2011-08-15
- Revision:
- 9:984c1436dd42
- Parent:
- 7:c6cf9ae117b1
- Child:
- 10:d3e3e925f62f
File content as of revision 9:984c1436dd42:
#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):
wifi(tx, rx), reset_pin(_reset)
{
wifi.baud(baudrate);
reset_pin = 1;
wifi.format(8, Serial::None, 1);
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):
wifi(tx, rx), reset_pin(_reset)
{
wifi.baud(baudrate);
reset_pin = 1;
wifi.format(8, Serial::None, 1);
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;
}
//no echo
if(!Send("set uart mode 1\r\n", "AOK"))
{
printf("join: cannot set no echo\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;
}
//no echo
if(!Send("set uart mode 1\r\n", "AOK"))
{
printf("join: cannot set no echo\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()
{
if(!Send("$$$", "CMD"))
{
printf("Wifly::CmdMode: cannot enter in cmd mode\r\n");
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());
}