Skip to content

Commit

Permalink
Esp8266 lib update - new types, bugfixes
Browse files Browse the repository at this point in the history
Esp8266 lib update - new types, bugfixes.
  • Loading branch information
iot-playground committed Jan 27, 2015
1 parent 23ba3c8 commit e72076d
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 87 deletions.
132 changes: 89 additions & 43 deletions Esp8266EasyIoT/Esp8266EasyIoT.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
V1.1 - additional data types
V1.0 - first version
Created by Igor Jarc <[email protected]>
Expand Down Expand Up @@ -28,12 +29,11 @@ Esp8266EasyIoT::Esp8266EasyIoT(){
rxFlush();
}

//#ifdef DEBUG

void Esp8266EasyIoT::begin(void (*_msgCallback)(const Esp8266EasyIoTMsg &), int resetPin, Stream *serial)
{
begin(_msgCallback, resetPin, serial, NULL);
}

void Esp8266EasyIoT::begin(void (*_msgCallback)(const Esp8266EasyIoTMsg &), int resetPin, Stream *serial, Stream *serialDebug = NULL)
{
debug(PSTR("begin\n"));
Expand All @@ -46,7 +46,6 @@ void Esp8266EasyIoT::begin(void (*_msgCallback)(const Esp8266EasyIoTMsg &), int
else
this->isDebug = false;
#endif
//begin(_msgCallback, serial);
this->_serial = serial;
this->msgCallback = _msgCallback;

Expand All @@ -60,44 +59,22 @@ void Esp8266EasyIoT::begin(void (*_msgCallback)(const Esp8266EasyIoTMsg &), int

delay(2000);

while(processesp() != E_IDLE) delay(1);
//while(processesp() != E_IDLE) delay(1);

// wait some time
for(int i=0;i<500;i++)
{
if (processesp() != E_IDLE)
delay(10);
else
break;
}

// Try to fetch node-id from gateway
if (_nodeId == AUTO) {
requestNodeId();
}
//this->isDebug = false;
/////////////////////////////////////
//this->isDebug = true;
}
//#endif


//void Esp8266EasyIoT::begin(void (*_msgCallback)(const Esp8266EasyIoTMsg &), Stream *serial)
//{
// this->_serial = serial;
// this->msgCallback = _msgCallback;
//
// // Read settings from EEPROM
// eeprom_read_block((void*)&_nodeId, (void*)EEPROM_NODE_ID_ADDRESS, sizeof(uint16_t));
//
// debug(PSTR("nodeid:%d\n"), _nodeId);
//
// delay(2000);
//
//
// debug(PSTR("begin 1\n"));
//
// while(processesp() != E_IDLE) delay(1);
//
// // Try to fetch node-id from gateway
// if (_nodeId == AUTO) {
// requestNodeId();
// }
//
// debug(PSTR("begin\n"));
// this->isDebug = false;
//}

void Esp8266EasyIoT::hwReset()
{
Expand Down Expand Up @@ -127,10 +104,37 @@ void Esp8266EasyIoT::requestNodeId()
}
}

void Esp8266EasyIoT::requestTime(void (* _timeCallback)(unsigned long)) {
timeCallback = _timeCallback;
writeesp(build(msg, _nodeId, C_INTERNAL, I_TIME, NODE_SENSOR_ID, false).set(""));

_waitingCommandResponse = true;
_commandRespondTimer = millis();

//waitIdle();

if (processesp() != E_IDLE)
{
for(int i=0;i<30;i++)
{
if (processesp() == E_IDLE)
break;
delay(10);
}
}
}


void Esp8266EasyIoT::present(uint8_t childSensorId, uint8_t sensorType, bool enableAck) {
sendinternal(build(msg, _nodeId, C_PRESENTATION, sensorType, childSensorId, enableAck).set(LIBRARY_VERSION));
}


void Esp8266EasyIoT::request(uint8_t childSensorId, uint8_t variableType)
{
sendinternal(build(msg, _nodeId, C_REQ, variableType, childSensorId, false).set(""));
}

// external send
void Esp8266EasyIoT::send(Esp8266EasyIoTMsg &message)
{
Expand All @@ -143,10 +147,30 @@ void Esp8266EasyIoT::send(Esp8266EasyIoTMsg &message)
// internal send
void Esp8266EasyIoT::sendinternal(Esp8266EasyIoTMsg &message)
{
//message.crc8();
//waitIdle();
if (waitIdle() && writeesp(message))
waitIdle();
//if (waitIdle() && writeesp(message))
// waitIdle();

if (processesp() != E_IDLE)
{
for(int i=0;i<30;i++)
{
if (processesp() == E_IDLE)
break;
delay(10);
}
}

writeesp(message);

if (processesp() != E_IDLE)
{
for(int i=0;i<30;i++)
{
if (processesp() == E_IDLE)
break;
delay(10);
}
}
}

bool Esp8266EasyIoT::waitIdle() {
Expand Down Expand Up @@ -186,6 +210,8 @@ bool Esp8266EasyIoT::process()

if (calcCrc == msg.crc)
{
msg.data[msg.length] = '\0';

if (command == C_INTERNAL)
{
debug(PSTR("Command\n"));
Expand Down Expand Up @@ -215,6 +241,16 @@ bool Esp8266EasyIoT::process()
_waitingCommandResponse = false;
resetPingTimmer();
}
else if (type == I_TIME)
{
debug(PSTR("TIME received\n"));
if (timeCallback != NULL) {
timeCallback(msg.getULong());
}
_waitingCommandResponse = false;
resetPingTimmer();
}

else
{
debug(PSTR("Unkonwn command\n"));
Expand Down Expand Up @@ -421,6 +457,7 @@ e_internal_state Esp8266EasyIoT::processesp()
_errorState = E_START;
_rxFlushProcessed = false;
startTimmer(1000);
processesp();
}
else if (rxchopUntil("Unlink", true, true) || rxchopUntil("FAIL", true, true) || rxchopUntil("ERROR", true, true))
{
Expand All @@ -446,7 +483,6 @@ e_internal_state Esp8266EasyIoT::processesp()
int i = 0;
bool startFound = false;

//uint8_t* buff = reinterpret_cast<uint8_t*>(&msg);
uint8_t* buff = reinterpret_cast<uint8_t*>(&msg);

// copy message
Expand All @@ -468,8 +504,18 @@ e_internal_state Esp8266EasyIoT::processesp()
process();
};
}
else
{
debug(PSTR("receive no :\n"));
_state = E_IDLE;
rxFlush();
}
}
}
else
debug(PSTR("receive no OK\n"));
}
else
debug(PSTR("receive no +IPD\n"));
break;

case E_CIPSEND:
Expand All @@ -492,8 +538,9 @@ e_internal_state Esp8266EasyIoT::processesp()
_state = E_WAIT_OK;
_okState = E_IDLE;
_errorState = E_CIPSTART;

}
else if (!isTimeout(_startTime, _respondTimeout))
else if (isTimeout(_startTime, _respondTimeout))
{
_errorState = E_CIPSTART;
_rxFlushProcessed = true;
Expand Down Expand Up @@ -551,7 +598,6 @@ void Esp8266EasyIoT::receiveAll()
}
}


bool Esp8266EasyIoT::rxPos(const char* reference, byte* from, byte* to)
{
return rxPos(reference, _rxHead, _rxTail, from, to);
Expand Down
45 changes: 23 additions & 22 deletions Esp8266EasyIoT/Esp8266EasyIoT.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
V1.1 - additional data types
V1.0 - first version
Created by Igor Jarc <[email protected]>
Expand Down Expand Up @@ -50,6 +51,7 @@ typedef enum {
E_CIPSEND_1, //
E_IDLE, // socket open waiting for send or receive
E_RECEIVE, // receive data
E_RECEIVE1, // receive data
E_CIPCLOSE, // close connection
E_HWRESET, // HW reset

Expand Down Expand Up @@ -82,11 +84,29 @@ class Esp8266EasyIoT
void present(uint8_t sensorId, uint8_t sensorType, bool ack=false);
void send(Esp8266EasyIoTMsg &message);
void hwReset();

uint16_t _nodeId;
private:
void requestTime(void (* timeCallback)(unsigned long));

void request(uint8_t sensorId, uint8_t variableType);

protected:
Esp8266EasyIoTMsg msg;
Esp8266EasyIoTMsg ack;
int _resetPin;

private:
bool writeesp(Esp8266EasyIoTMsg &message);
void executeCommand(String cmd, unsigned long respondTimeout);
bool isOk(bool chop = true);
bool isError(bool chop = true);

void receiveAll();
void requestNodeId();
void sendinternal(Esp8266EasyIoTMsg &message);
e_internal_state processesp();
#ifdef DEBUG
void debugPrint(const char *fmt, ... );
#endif

bool isDebug;
Stream *_serial;
Expand All @@ -105,26 +125,6 @@ class Esp8266EasyIoT
void setPingTimmer();
void resetPingTimmer();

#ifdef DEBUG
void debugPrint(const char *fmt, ... );
#endif

protected:
Esp8266EasyIoTMsg msg;
Esp8266EasyIoTMsg ack;
int _resetPin;
// Esp8266EasyIoTMsg msgsend;

//link fuctions
private:
e_internal_state processesp();
bool writeesp(Esp8266EasyIoTMsg &message);
void executeCommand(String cmd, unsigned long respondTimeout);
bool isOk(bool chop = true);
bool isError(bool chop = true);

void receiveAll();

e_internal_state _state;
e_internal_state _okState;
e_internal_state _errorState;
Expand All @@ -150,6 +150,7 @@ class Esp8266EasyIoT
uint8_t _txLen;

void (*msgCallback)(const Esp8266EasyIoTMsg &);
void (*timeCallback)(unsigned long);
};
#endif

Expand Down
31 changes: 26 additions & 5 deletions Esp8266EasyIoT/Esp8266EasyIoTMsg.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
V1.1 - additional data types
V1.0 - first version
Created by Igor Jarc <[email protected]>
Expand Down Expand Up @@ -29,18 +30,21 @@ Esp8266EasyIoTMsg& Esp8266EasyIoTMsg::set(const char* value) {
length = len;
miSetPayloadType(P_STRING);
strncpy(data, value, min(length, MAX_PAYLOAD));
// calculate CRC8
//crc8();
return *this;
}

Esp8266EasyIoTMsg& Esp8266EasyIoTMsg::set(uint8_t value) {
length = 1;
miSetPayloadType(P_BYTE);
data[0] = value;
return *this;
}

Esp8266EasyIoTMsg& Esp8266EasyIoTMsg::set(float value, uint8_t decimals) {
length = 5; // 32 bit float + persi
miSetPayloadType(P_FLOAT32);
fValue=value;
fPrecision = decimals;
//crc8();
return *this;
}

Expand All @@ -53,10 +57,8 @@ uint16_t Esp8266EasyIoTMsg::getUInt() const {
} else {
return 0;
}

}


int Esp8266EasyIoTMsg::getInt() const {
if (miGetPayloadType() == P_INT16) {
return iValue;
Expand All @@ -71,6 +73,25 @@ bool Esp8266EasyIoTMsg::getBool() const {
return getInt();
}

float Esp8266EasyIoTMsg::getFloat() const {
if (miGetPayloadType() == P_FLOAT32) {
return fValue;
} else if (miGetPayloadType() == P_STRING) {
return atof(data);
} else {
return 0;
}
}

unsigned long Esp8266EasyIoTMsg::getULong() const {
if (miGetPayloadType() == P_ULONG32) {
return ulValue;
} else if (miGetPayloadType() == P_STRING) {
return atol(data);
} else {
return 0;
}
}


/*
Expand Down
Loading

0 comments on commit e72076d

Please sign in to comment.