forked from iot-playground/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Esp8266 lib update - new types, bugfixes
Esp8266 lib update - new types, bugfixes.
- Loading branch information
1 parent
23ba3c8
commit e72076d
Showing
4 changed files
with
169 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]> | ||
|
@@ -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")); | ||
|
@@ -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; | ||
|
||
|
@@ -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() | ||
{ | ||
|
@@ -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) | ||
{ | ||
|
@@ -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() { | ||
|
@@ -186,6 +210,8 @@ bool Esp8266EasyIoT::process() | |
|
||
if (calcCrc == msg.crc) | ||
{ | ||
msg.data[msg.length] = '\0'; | ||
|
||
if (command == C_INTERNAL) | ||
{ | ||
debug(PSTR("Command\n")); | ||
|
@@ -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")); | ||
|
@@ -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)) | ||
{ | ||
|
@@ -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 | ||
|
@@ -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: | ||
|
@@ -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; | ||
|
@@ -551,7 +598,6 @@ void Esp8266EasyIoT::receiveAll() | |
} | ||
} | ||
|
||
|
||
bool Esp8266EasyIoT::rxPos(const char* reference, byte* from, byte* to) | ||
{ | ||
return rxPos(reference, _rxHead, _rxTail, from, to); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]> | ||
|
@@ -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 | ||
|
||
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -150,6 +150,7 @@ class Esp8266EasyIoT | |
uint8_t _txLen; | ||
|
||
void (*msgCallback)(const Esp8266EasyIoTMsg &); | ||
void (*timeCallback)(unsigned long); | ||
}; | ||
#endif | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]> | ||
|
@@ -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; | ||
} | ||
|
||
|
@@ -53,10 +57,8 @@ uint16_t Esp8266EasyIoTMsg::getUInt() const { | |
} else { | ||
return 0; | ||
} | ||
|
||
} | ||
|
||
|
||
int Esp8266EasyIoTMsg::getInt() const { | ||
if (miGetPayloadType() == P_INT16) { | ||
return iValue; | ||
|
@@ -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; | ||
} | ||
} | ||
|
||
|
||
/* | ||
|
Oops, something went wrong.