From e72076dc5e72a831f76aaf654405e00607913dea Mon Sep 17 00:00:00 2001 From: "iot-playground.com" Date: Tue, 27 Jan 2015 09:45:46 +0100 Subject: [PATCH] Esp8266 lib update - new types, bugfixes Esp8266 lib update - new types, bugfixes. --- Esp8266EasyIoT/Esp8266EasyIoT.cpp | 132 ++++++++++++++++++--------- Esp8266EasyIoT/Esp8266EasyIoT.h | 45 ++++----- Esp8266EasyIoT/Esp8266EasyIoTMsg.cpp | 31 ++++++- Esp8266EasyIoT/Esp8266EasyIoTMsg.h | 48 ++++++---- 4 files changed, 169 insertions(+), 87 deletions(-) diff --git a/Esp8266EasyIoT/Esp8266EasyIoT.cpp b/Esp8266EasyIoT/Esp8266EasyIoT.cpp index d8a5ac1..73e16dc 100644 --- a/Esp8266EasyIoT/Esp8266EasyIoT.cpp +++ b/Esp8266EasyIoT/Esp8266EasyIoT.cpp @@ -1,4 +1,5 @@ /* + V1.1 - additional data types V1.0 - first version Created by Igor Jarc @@ -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(&msg); uint8_t* buff = reinterpret_cast(&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); diff --git a/Esp8266EasyIoT/Esp8266EasyIoT.h b/Esp8266EasyIoT/Esp8266EasyIoT.h index 88cbc9c..562997b 100644 --- a/Esp8266EasyIoT/Esp8266EasyIoT.h +++ b/Esp8266EasyIoT/Esp8266EasyIoT.h @@ -1,4 +1,5 @@ /* + V1.1 - additional data types V1.0 - first version Created by Igor Jarc @@ -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 diff --git a/Esp8266EasyIoT/Esp8266EasyIoTMsg.cpp b/Esp8266EasyIoT/Esp8266EasyIoTMsg.cpp index 86a3169..5848395 100644 --- a/Esp8266EasyIoT/Esp8266EasyIoTMsg.cpp +++ b/Esp8266EasyIoT/Esp8266EasyIoTMsg.cpp @@ -1,4 +1,5 @@ /* + V1.1 - additional data types V1.0 - first version Created by Igor Jarc @@ -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; + } +} /* diff --git a/Esp8266EasyIoT/Esp8266EasyIoTMsg.h b/Esp8266EasyIoT/Esp8266EasyIoTMsg.h index 992a7e1..e5b4fb7 100644 --- a/Esp8266EasyIoT/Esp8266EasyIoTMsg.h +++ b/Esp8266EasyIoT/Esp8266EasyIoTMsg.h @@ -1,6 +1,7 @@ /* + V1.1 - additional data types V1.0 - first version - + Created by Igor Jarc See http://iot-playground.com for details @@ -16,8 +17,8 @@ #endif -#define LIBRARY_VERSION "0.1" -#define PROTOCOL_VERSION 1 +#define LIBRARY_VERSION "1.1" +#define PROTOCOL_VERSION 2 #define MAX_MESSAGE_LENGTH 127 #define HEADER_SIZE 9 #define MAX_PAYLOAD (MAX_MESSAGE_LENGTH - HEADER_SIZE) @@ -70,10 +71,12 @@ typedef enum { } e_command_type; typedef enum { - I_ID_REQUEST = 0, // sprejem - I_ID_RESPONSE = 1, // oddaja - I_PING = 2, // sprejem/oddaja - I_PING_RESPONSE = 3 // oddaja/oddaja + I_ID_REQUEST = 0, // receive + I_ID_RESPONSE = 1, // send + I_PING = 2, // send/receive + I_PING_RESPONSE = 3, // send/receive + I_BATTERY_LEVEL = 4, // send + I_TIME = 5 // send } e_internal_type; @@ -85,7 +88,12 @@ typedef enum { S_ANALOG_INPUT = 4, // AI S_TEMP = 5, // AI S_HUM = 6, // AI - S_LEAK = 7 // DI + S_LEAK = 7, // DI + S_BARO = 8, // AI + S_DIMMER = 10, // AO, DO + S_TEMP_AO = 11, // AO + S_HUM_AO = 12, // AO + S_LIGHT_LEVEL = 13, // AI } e_sensor_type; @@ -94,14 +102,19 @@ typedef enum { } e_data_type; - // Type of sensor data (for set/req/ack messages) +// Type of sensor data (for set/req/ack messages) typedef enum { - V_UNDEFINED = 0, - V_DIGITAL_INPUT = 1, - V_DIGITAL_OUTPUT = 2, - V_DOOR = 3, - V_TEMP = 4, - V_HUM = 5, + V_UNDEFINED = 0, + V_DIGITAL_VALUE = 1, + V_DOOR = 2, + V_TEMP = 3, + V_HUM = 4, + V_PRESSURE = 5, + V_FORECAST = 6, + V_ANALOG_VALUE = 7, + V_DIMMER = 8, + V_LIGHT_LEVEL = 9, + V_LEAK = 10, } ; @@ -117,17 +130,18 @@ class Esp8266EasyIoTMsg Esp8266EasyIoTMsg& set(const char* value); Esp8266EasyIoTMsg& set(float value, uint8_t decimals); + Esp8266EasyIoTMsg& set(uint8_t value); int getInt() const; uint16_t getUInt() const; bool getBool() const; - + float getFloat() const; + unsigned long getULong() const; #else typedef union { struct { - #endif uint8_t start; // start uint8_t version; // protocol version