Skip to content

Commit

Permalink
checkConnection updates channel pointer and returns status
Browse files Browse the repository at this point in the history
  • Loading branch information
tuna-f1sh committed Dec 10, 2015
1 parent 7986c8a commit 33de863
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 1 deletion.
143 changes: 143 additions & 0 deletions ESP8266wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ const char ATE1[] PROGMEM = "ATE1";
const char CWSAP[] PROGMEM = "AT+CWSAP=\"";

const char IPD[] PROGMEM = "+IPD,";
const char CONNECT[] PROGMEM = "CONNECT";
const char CLOSED[] PROGMEM = "CLOSED";

const char COMMA[] PROGMEM = ",";
const char COMMA_1[] PROGMEM = "\",";
Expand Down Expand Up @@ -418,6 +420,121 @@ bool ESP8266wifi::send(char channel, const char * message, bool sendNow){
return false;
}

bool ESP8266wifi::newConnection(char *channel) {
watchdog();
// setup buffers on stack & copy data from PROGMEM pointers
char buf1[16] = {'\0'};
char buf2[16] = {'\0'};
strcpy_P(buf1, CONNECT);
strcpy_P(buf2, READY);
byte len1 = strlen(buf1);
byte len2 = strlen(buf2);
byte pos = 0;
byte pos1 = 0;
byte pos2 = 0;
byte ret = 0;
char ch = '-';
bool connection = false;

// unload buffer and check match
while (_serialIn->available()) {
char c = readChar();
if (pos == 0) {
ch = c;
}
pos++;
pos1 = (c == buf1[pos1]) ? pos1 + 1 : 0;
pos2 = (c == buf2[pos2]) ? pos2 + 1 : 0;
if (len1 > 0 && pos1 == len1) {
ret = 1;
break;
}
if (len2 > 0 && pos2 == len2) {
ret = 2;
break;
}
}

if (ret == 2)
restart();

if (ret == 1) {
connection = true;
*channel = ch;
if (ch == SERVER)
flags.connectedToServer = true;
}

return connection;
}

byte ESP8266wifi::checkConnection(char *channel) {
watchdog();
// setup buffers on stack & copy data from PROGMEM pointers
char buf1[16] = {'\0'};
char buf2[16] = {'\0'};
char buf3[16] = {'\0'};
strcpy_P(buf1, CONNECT);
strcpy_P(buf2, READY);
strcpy_P(buf3, CLOSED);
byte len1 = strlen(buf1);
byte len2 = strlen(buf2);
byte len3 = strlen(buf3);
byte pos = 0;
byte pos1 = 0;
byte pos2 = 0;
byte pos3 = 0;
byte ret = 0;
char ch = '-';
bool connection = false;

// unload buffer and check match
while (_serialIn->available()) {
char c = readChar();
if (pos == 0) {
ch = c;
}
pos++;
pos1 = (c == buf1[pos1]) ? pos1 + 1 : 0;
pos2 = (c == buf2[pos2]) ? pos2 + 1 : 0;
pos3 = (c == buf3[pos3]) ? pos3 + 1 : 0;
if (len1 > 0 && pos1 == len1) {
ret = 1;
break;
}
if (len2 > 0 && pos2 == len2) {
ret = 2;
break;
}
if (len3 > 0 && pos3 == len3) {
ret = 3;
break;
}
}

// reset
if (ret == 2)
restart();

// new connection
if (ret == 1) {
connection = true;
*channel = ch;
if (ch == SERVER)
flags.connectedToServer = true;
}

// channel disconnected
else if ((ret == 3) && (ch == *channel)) {
connection = false;
if (ch == SERVER)
flags.connectedToServer = false;
}

return ret;
}


WifiMessage ESP8266wifi::listenForIncomingMessage(int timeout){
watchdog();
char buf[16] = {'\0'};
Expand Down Expand Up @@ -500,6 +617,32 @@ byte ESP8266wifi::readCommand(int timeout, const char* text1, const char* text2)
return 0;
}

byte ESP8266wifi::readCommand(const char* text1, const char* text2) {
// setup buffers on stack & copy data from PROGMEM pointers
char buf1[16] = {'\0'};
char buf2[16] = {'\0'};
if (text1 != NULL)
strcpy_P(buf1, (char *) text1);
if (text2 != NULL)
strcpy_P(buf2, (char *) text2);
byte len1 = strlen(buf1);
byte len2 = strlen(buf2);
byte pos1 = 0;
byte pos2 = 0;

// read chars until first match or timeout
while (_serialIn->available()) {
char c = readChar();
pos1 = (c == buf1[pos1]) ? pos1 + 1 : 0;
pos2 = (c == buf2[pos2]) ? pos2 + 1 : 0;
if (len1 > 0 && pos1 == len1)
return 1;
if (len2 > 0 && pos2 == len2)
return 2;
}
return 0;
}

// Reads count chars to a buffer, or until delim char is found
byte ESP8266wifi::readBuffer(char* buf, byte count, char delim) {
byte pos = 0;
Expand Down
13 changes: 12 additions & 1 deletion ESP8266wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
#define HW_RESET_RETRIES 3
#define SERVER_CONNECT_RETRIES_BEFORE_HW_RESET 3

#define CONN_NEW 1
#define CONN_CLOSED 3

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
Expand All @@ -25,14 +28,19 @@

#define SERVER '4'


struct WifiMessage{
public:
bool hasData:1;
char channel;
char * message;
};

struct WifiConnection{
public:
char channel;
bool status:1;
};

struct Flags // 1 byte value (on a system where 8 bits is a byte
{
bool started:1,
Expand Down Expand Up @@ -122,6 +130,8 @@ class ESP8266wifi
* Scan for incoming message, do this as often and as long as you can (use as sleep in loop)
*/
WifiMessage listenForIncomingMessage(int timeoutMillis);
bool newConnection(char *channel);
byte checkConnection(char *channel);

private:
Stream* _serialIn;
Expand Down Expand Up @@ -155,6 +165,7 @@ class ESP8266wifi

void writeCommand(const char* text1, const char* text2 = NULL);
byte readCommand(int timeout, const char* text1 = NULL, const char* text2 = NULL);
byte readCommand(const char* text1, const char* text2);
byte readBuffer(char* buf, byte count, char delim = '\0');
char readChar();

Expand Down

0 comments on commit 33de863

Please sign in to comment.