Skip to content

move example to correct folder, search 'ready' works for me #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 63 additions & 63 deletions ESP8266.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,55 +54,55 @@ ESP8266::ESP8266(int mode, long baudrate, int debugLevel)

int ESP8266::initializeWifi(DataCallback dcb, ConnectCallback ccb)
{

if (dcb) {
_dcb = dcb;
}

if (ccb) {
_ccb = ccb;
_ccb = ccb;
}

wifi.begin(_baudrate);
wifi.setTimeout(5000);
wifi.setTimeout(5000);

//delay(500);
clearResults();

// check for presence of wifi module
wifi.println(F("AT"));
delay(500);
if(!searchResults("OK", 1000, _debugLevel)) {
return WIFI_ERR_AT;
}
}

//delay(500);

// reset WiFi module
wifi.println(F("AT+RST"));
delay(500);
if(!searchResults("Ready", 5000, _debugLevel)) {
if(!searchResults("ready", 5000, _debugLevel)) {
return WIFI_ERR_RESET;
}

delay(500);

// set the connectivity mode 1=sta, 2=ap, 3=sta+ap
wifi.print(F("AT+CWMODE="));
wifi.println(_mode);
//delay(500);

clearResults();

return WIFI_ERR_NONE;
}

int ESP8266::connectWifi(char *ssid, char *password)
{

strcpy(_ssid, ssid);
strcpy(_password, password);

// set the access point value and connect
wifi.print(F("AT+CWJAP=\""));
wifi.print(ssid);
Expand All @@ -113,32 +113,32 @@ int ESP8266::connectWifi(char *ssid, char *password)
if(!searchResults("OK", 30000, _debugLevel)) {
return WIFI_ERR_CONNECT;
}

// enable multi-connection mode
if (!setLinkMode(1)) {
return WIFI_ERR_LINK;
}

// get the IP assigned by DHCP
getIP();

// get the broadcast address (assumes class c w/ .255)
getBroadcast();

return WIFI_ERR_NONE;
}

bool ESP8266::disconnectWifi()
{

}

bool ESP8266::enableBeacon(char *device)
{
// you can only beacon if you're a server
if (_connectMode != CONNECT_MODE_SERVER)
return false;

bool ret;
strcpy(_device, device);
ret = startUDPChannel(BCN_CHAN, _broadcast, BEACON_PORT);
Expand Down Expand Up @@ -169,9 +169,9 @@ void ESP8266::run()
int v;
char _data[255];
unsigned long currentMillis = millis();

if (currentMillis - _previousMillis < _beaconInterval) {

// process wifi messages
while(wifi.available() > 0) {
v = wifi.read();
Expand All @@ -186,22 +186,22 @@ void ESP8266::run()
_wctr++;
}
}


} else {
_previousMillis = currentMillis;
if (_beacon == false) return;

// create message text
char *line1 = "{\"event\": \"beacon\", \"ip\": \"";
char *line3 = "\", \"port\": ";
char *line5 = ", \"device\": \"";
char *line7 = "\"}\r\n";

// convert port to a string
char p[6];
itoa(_port, p, 10);

// get lenthg of message text
memset(_data, 0, 255);
strcat(_data, line1);
Expand All @@ -211,32 +211,32 @@ void ESP8266::run()
strcat(_data, line5);
strcat(_data, _device);
strcat(_data, line7);

sendData(BCN_CHAN, _data);
}
}

bool ESP8266::startServer(int port, long timeout)
{

// cache the port number for the beacon
_port = port;

wifi.print(F("AT+CIPSERVER="));
wifi.print(SVR_CHAN);
wifi.print(F(","));
wifi.println(_port);
if(!searchResults("OK", 500, _debugLevel)){
return false;
}

// send AT command
wifi.print(F("AT+CIPSTO="));
wifi.println(timeout);
if(!searchResults("OK", 500, _debugLevel)) {
return false;
}

_connectMode = CONNECT_MODE_SERVER;
return true;
}
Expand All @@ -253,7 +253,7 @@ bool ESP8266::startClient(char *ip, int port, long timeout)
if(!searchResults("OK", timeout, _debugLevel)) {
return false;
}

_connectMode = CONNECT_MODE_CLIENT;
return true;
}
Expand All @@ -268,7 +268,7 @@ int ESP8266::scan(char *out, int max)
int timeout = 10000;
int count = 0;
int c = 0;

if (_debugLevel > 0) {
char num[6];
itoa(max, num, 10);
Expand Down Expand Up @@ -298,45 +298,45 @@ int ESP8266::scan(char *out, int max)
void ESP8266::processWifiMessage() {
int packet_len;
int channel;
char *pb;
char *pb;

// if the message is simply "Link", then we have a live connection
if(strncmp(_wb, "Link", 5) == 0) {

// reduce the beacon frequency by increasing the interval
_beaconInterval = 30000; //false;

// flag the connection as active
_connected = true;

// if a connection callback is set, call it
if (_ccb) _ccb();
} else

// message packet received from the server
if(strncmp(_wb, "+IPD,", 5)==0) {

// get the channel and length of the packet
sscanf(_wb+5, "%d,%d", &channel, &packet_len);

// cache the channel ID - this is used to reply
_replyChan = channel;

// if the packet contained data, move the pointer past the header
if (packet_len > 0) {
pb = _wb+5;
while(*pb!=':') pb++;
pb++;

// execute the callback passing a pointer to the message
if (_dcb) {
_dcb(pb);
}

// DANGER WILL ROBINSON - there is no ring buffer or other safety net here.
// the application should either use the data immediately or make a copy of it!
// do NOT block in the callback or bad things may happen

}
} else {
// other messages might wind up here - some useful, some not.
Expand All @@ -351,15 +351,15 @@ bool ESP8266::sendData(int chan, char *data) {
wifi.print(chan);
wifi.print(",");
wifi.println(strlen(data));

// send the data
wifi.println(data);

delay(50);

// to debug only
searchResults("OK", 500, _debugLevel);

return true;
}

Expand Down Expand Up @@ -388,7 +388,7 @@ bool ESP8266::startUDPChannel(int chan, char *address, int port) {

// private convenience functions
bool ESP8266::getIP() {

char c;
char buf[15];
int dots, ptr = 0;
Expand All @@ -400,7 +400,7 @@ bool ESP8266::getIP() {
delay(500);
while (wifi.available() > 0) {
c = wifi.read();

// increment the dot counter if we have a "."
if ((int)c == 46) {
dots++;
Expand Down Expand Up @@ -430,13 +430,13 @@ bool ESP8266::getIP() {


bool ESP8266::getBroadcast() {

int i, c, dots = 0;

if (strlen(_ipaddress) < 7) {
return false;
}

memset(_broadcast, 0, 15);
for (i = 0; i < strlen(_ipaddress); i++) {
c = _ipaddress[i];
Expand All @@ -448,14 +448,14 @@ bool ESP8266::getBroadcast() {
_broadcast[i++] = 50;
_broadcast[i++] = 53;
_broadcast[i++] = 53;

return true;
}

void ESP8266::debug(char *msg) {
if (_dcb && (_debugLevel > 0)) {
_dcb(msg);
}
}
}

bool ESP8266::searchResults(char *target, long timeout, int dbg)
Expand All @@ -465,13 +465,13 @@ bool ESP8266::searchResults(char *target, long timeout, int dbg)
int targetLength = strlen(target);
int count = 0;
char _data[255];

memset(_data, 0, 255);

long _startMillis = millis();
do {
c = wifi.read();

if (c >= 0) {

if (dbg > 0) {
Expand All @@ -486,7 +486,7 @@ bool ESP8266::searchResults(char *target, long timeout, int dbg)

if (c != target[index])
index = 0;

if (c == target[index]){
if(++index >= targetLength){
if (dbg > 1)
Expand Down
Loading