2020-01-30 07:39:28 +00:00
|
|
|
/*
|
2020-02-08 17:09:17 +00:00
|
|
|
xsns_62_MI_HM10.ino - MI-BLE-sensors via HM-10 support for Tasmota
|
2020-01-30 07:39:28 +00:00
|
|
|
|
|
|
|
Copyright (C) 2020 Christian Baars and Theo Arends
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------------------
|
|
|
|
Version yyyymmdd Action Description
|
|
|
|
--------------------------------------------------------------------------------------------
|
|
|
|
|
2020-02-09 14:32:45 +00:00
|
|
|
0.9.1.0 20200209 added - LYWSD02-support, including setting the time
|
2020-01-30 07:39:28 +00:00
|
|
|
---
|
2020-02-06 16:05:37 +00:00
|
|
|
0.9.0.0 20200130 started - initial development by Christian Baars (support LYWSD03 only)
|
2020-01-30 07:39:28 +00:00
|
|
|
forked - from arendst/tasmota - https://github.com/arendst/Tasmota
|
|
|
|
|
|
|
|
*/
|
2020-02-06 16:05:37 +00:00
|
|
|
#ifdef USE_HM10
|
2020-01-30 08:18:21 +00:00
|
|
|
|
2020-02-06 16:05:37 +00:00
|
|
|
#define XSNS_62 62
|
2020-01-31 09:12:48 +00:00
|
|
|
|
2020-01-30 08:18:21 +00:00
|
|
|
#include <TasmotaSerial.h>
|
2020-01-31 17:50:11 +00:00
|
|
|
#include <vector>
|
2020-01-30 08:18:21 +00:00
|
|
|
|
|
|
|
TasmotaSerial *HM10Serial;
|
2020-02-04 18:30:41 +00:00
|
|
|
#define HM10_BAUDRATE 115200 // default with FW>700 is 115200
|
2020-01-30 08:18:21 +00:00
|
|
|
|
2020-01-31 14:28:34 +00:00
|
|
|
#define HM10_MAX_TASK_NUMBER 12
|
2020-01-30 08:18:21 +00:00
|
|
|
uint8_t HM10_TASK_LIST[HM10_MAX_TASK_NUMBER+1][2]; // first value: kind of task - second value: delay in x * 100ms
|
2020-01-31 17:50:11 +00:00
|
|
|
|
2020-01-30 08:18:21 +00:00
|
|
|
#define HM10_MAX_RX_BUF 512
|
2020-01-31 17:50:11 +00:00
|
|
|
char HM10_RX_STRING[HM10_MAX_RX_BUF] = {0};
|
2020-01-30 08:18:21 +00:00
|
|
|
|
2020-01-31 09:12:48 +00:00
|
|
|
struct {
|
2020-02-01 15:17:20 +00:00
|
|
|
uint8_t current_task_delay; // number of 100ms-cycles
|
2020-01-31 14:28:34 +00:00
|
|
|
uint8_t last_command;
|
|
|
|
uint16_t firmware;
|
2020-02-05 15:56:41 +00:00
|
|
|
uint32_t period; // set manually in addition to TELE-period, is set to TELE-period after start
|
2020-02-04 18:30:41 +00:00
|
|
|
uint32_t serialSpeed;
|
2020-02-08 17:09:17 +00:00
|
|
|
union {
|
|
|
|
uint32_t time;
|
|
|
|
uint8_t timebuf[4];
|
|
|
|
};
|
2020-01-31 09:12:48 +00:00
|
|
|
struct {
|
|
|
|
uint32_t init:1;
|
2020-02-01 15:17:20 +00:00
|
|
|
uint32_t pending_task:1;
|
2020-02-08 17:09:17 +00:00
|
|
|
uint32_t connected:1;
|
2020-02-04 07:13:09 +00:00
|
|
|
uint32_t subscribed:1;
|
|
|
|
uint32_t awaitingHT:1;
|
|
|
|
uint32_t awaitingB:1;
|
2020-01-31 09:12:48 +00:00
|
|
|
// TODO: more to come
|
|
|
|
} mode;
|
2020-02-01 15:17:20 +00:00
|
|
|
struct {
|
2020-02-05 15:56:41 +00:00
|
|
|
uint8_t sensor; // points to to the number 0...255
|
2020-02-01 15:17:20 +00:00
|
|
|
// TODO: more to come
|
|
|
|
} state;
|
2020-01-31 09:12:48 +00:00
|
|
|
} HM10;
|
2020-01-30 08:18:21 +00:00
|
|
|
|
2020-01-31 14:28:34 +00:00
|
|
|
#pragma pack(1)
|
|
|
|
struct {
|
|
|
|
uint16_t temp;
|
|
|
|
uint8_t hum;
|
2020-02-08 17:09:17 +00:00
|
|
|
} LYWSD0x_HT;
|
2020-01-31 14:28:34 +00:00
|
|
|
#pragma pack(0)
|
2020-01-30 08:18:21 +00:00
|
|
|
|
2020-01-31 17:50:11 +00:00
|
|
|
struct mi_sensor_t{
|
2020-02-02 16:44:26 +00:00
|
|
|
uint8_t type; //Flora = 1; MI-HT_V1=2; LYWSD02=3; LYWSD03=4
|
2020-01-31 17:50:11 +00:00
|
|
|
uint8_t serial[6];
|
|
|
|
uint8_t showedUp;
|
2020-02-02 16:44:26 +00:00
|
|
|
float temp; //Flora, MJ_HT_V1, LYWSD0x
|
2020-01-31 17:50:11 +00:00
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
float moisture;
|
|
|
|
float fertility;
|
|
|
|
uint16_t lux;
|
2020-02-02 16:44:26 +00:00
|
|
|
}; // Flora
|
2020-01-31 17:50:11 +00:00
|
|
|
struct {
|
|
|
|
float hum;
|
|
|
|
uint8_t bat;
|
2020-02-02 16:44:26 +00:00
|
|
|
}; // MJ_HT_V1, LYWSD0x
|
2020-01-31 17:50:11 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<mi_sensor_t> MIBLEsensors;
|
|
|
|
|
2020-01-30 08:18:21 +00:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* constants
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define D_CMND_HM10 "HM10"
|
|
|
|
|
2020-02-04 14:02:47 +00:00
|
|
|
const char S_JSON_HM10_COMMAND_NVALUE[] PROGMEM = "{\"" D_CMND_HM10 "%s\":%d}";
|
2020-02-04 18:30:41 +00:00
|
|
|
const char S_JSON_HM10_COMMAND[] PROGMEM = "{\"" D_CMND_HM10 "%s%s\"}";
|
2020-02-08 17:09:17 +00:00
|
|
|
const char kHM10_Commands[] PROGMEM = "Scan|AT|Period|Baud|Time";
|
2020-02-04 14:02:47 +00:00
|
|
|
|
2020-02-04 07:13:09 +00:00
|
|
|
#define FLORA 1
|
|
|
|
#define MJ_HT_V1 2
|
|
|
|
#define LYWSD02 3
|
|
|
|
#define LYWSD03MMC 4
|
|
|
|
|
2020-02-02 16:44:26 +00:00
|
|
|
uint8_t kHM10SlaveID[4][3] = { 0xC4,0x7C,0x8D, // Flora
|
|
|
|
0x58,0x2D,0x34, // MJ_HT_V1
|
|
|
|
0xE7,0x2E,0x00, // LYWSD02
|
|
|
|
0xA4,0xC1,0x38, // LYWSD03
|
|
|
|
};
|
2020-02-01 15:17:20 +00:00
|
|
|
|
2020-02-02 16:44:26 +00:00
|
|
|
const char kHM10SlaveType1[] PROGMEM = "Flora";
|
|
|
|
const char kHM10SlaveType2[] PROGMEM = "MJ_HT_V1";
|
|
|
|
const char kHM10SlaveType3[] PROGMEM = "LYWSD02";
|
|
|
|
const char kHM10SlaveType4[] PROGMEM = "LYWSD03";
|
|
|
|
const char * kHM10SlaveType[] PROGMEM = {kHM10SlaveType1,kHM10SlaveType2,kHM10SlaveType3,kHM10SlaveType4};
|
2020-02-01 15:17:20 +00:00
|
|
|
|
2020-01-30 08:18:21 +00:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* enumerations
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2020-02-04 16:55:48 +00:00
|
|
|
enum HM10_Commands { // commands useable in console or rules
|
|
|
|
CMND_HM10_DISC_SCAN, // re-scan for sensors
|
2020-02-04 18:30:41 +00:00
|
|
|
CMND_HM10_AT, // send AT-command for debugging and special configuration
|
2020-02-05 15:56:41 +00:00
|
|
|
CMND_HM10_PERIOD, // set period like TELE-period in seconds between read-cycles
|
2020-02-08 17:09:17 +00:00
|
|
|
CMND_HM10_BAUD, // serial speed of ESP8266 (<-> HM10), does not change baud rate of HM10
|
|
|
|
CMND_HM10_TIME // set LYWSD02-Time from ESP8266-time
|
2020-02-04 16:55:48 +00:00
|
|
|
};
|
2020-01-30 08:18:21 +00:00
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Task codes defines
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define TASK_HM10_NOTASK 0 // nothing to be done
|
2020-01-31 09:12:48 +00:00
|
|
|
#define TASK_HM10_ROLE1 1 // change role to 1
|
|
|
|
#define TASK_HM10_IMME1 2 // change imme to 1
|
|
|
|
#define TASK_HM10_RENEW 3 // device factory setting
|
|
|
|
#define TASK_HM10_RESET 4 // device reset
|
|
|
|
#define TASK_HM10_DISC 5 // device discovery scan
|
|
|
|
#define TASK_HM10_CONN 6 // connect to given MAC
|
|
|
|
#define TASK_HM10_VERSION 7 // query FW version
|
|
|
|
#define TASK_HM10_NAME 8 // query device name
|
|
|
|
#define TASK_HM10_FEEDBACK 9 // get device response
|
2020-01-31 14:28:34 +00:00
|
|
|
#define TASK_HM10_DISCONN 10 // disconnect
|
2020-02-08 17:09:17 +00:00
|
|
|
#define TASK_HM10_SUB_L3 11 // subscribe to service handle 37
|
2020-02-04 07:13:09 +00:00
|
|
|
#define TASK_HM10_READ_HT 12 // read from handle 36 -> Hum & Temp
|
2020-01-31 14:28:34 +00:00
|
|
|
#define TASK_HM10_FINDALLCHARS 13 // read all available characteristics
|
2020-02-08 17:09:17 +00:00
|
|
|
#define TASK_HM10_UN_L3 14 // subscribe service handle 37
|
2020-02-01 15:17:20 +00:00
|
|
|
#define TASK_HM10_DELAY_SUB 15 // start reading from subscription delayed
|
2020-02-08 17:09:17 +00:00
|
|
|
#define TASK_HM10_READ_BT_L3 16 // read from handle 3A -> Battery
|
|
|
|
#define TASK_HM10_SUB_L2 17 // subscribe to service handle 3C
|
|
|
|
#define TASK_HM10_UN_L2 18 // subscribe service handle 3C
|
|
|
|
#define TASK_HM10_READ_BT_L2 19 // read from handle 43 -> Battery
|
|
|
|
#define TASK_HM10_TIME_L2 20 // set time of LYWSD02 to system time
|
2020-01-31 09:12:48 +00:00
|
|
|
|
2020-01-30 08:18:21 +00:00
|
|
|
#define TASK_HM10_DONE 99 // used, if there was a task in the slot or just to wait
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Helper functions
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
void HM10_Launchtask(uint8_t task, uint8_t slot, uint8_t delay){
|
|
|
|
HM10_TASK_LIST[slot][0] = task;
|
|
|
|
HM10_TASK_LIST[slot][1] = delay;
|
|
|
|
HM10_TASK_LIST[slot+1][0] = TASK_HM10_NOTASK; // the tasks must always be launched in ascending order!!
|
2020-01-31 14:28:34 +00:00
|
|
|
HM10.current_task_delay = HM10_TASK_LIST[0][1];
|
2020-01-30 08:18:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HM10_TaskReplaceInSlot(uint8_t task, uint8_t slot){
|
2020-01-31 14:28:34 +00:00
|
|
|
HM10.last_command = HM10_TASK_LIST[slot][0]; // save command
|
2020-01-30 08:18:21 +00:00
|
|
|
HM10_TASK_LIST[slot][0] = task;
|
|
|
|
}
|
|
|
|
|
2020-02-05 15:56:41 +00:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* chained tasks
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2020-02-02 16:44:26 +00:00
|
|
|
void HM10_Reset(void) { HM10_Launchtask(TASK_HM10_DISCONN,0,1); // disconnect
|
2020-02-01 15:17:20 +00:00
|
|
|
HM10_Launchtask(TASK_HM10_ROLE1,1,1); // set role to 1
|
|
|
|
HM10_Launchtask(TASK_HM10_IMME1,2,1); // set imme to 1
|
|
|
|
HM10_Launchtask(TASK_HM10_RESET,3,1); // reset Device
|
2020-02-02 16:44:26 +00:00
|
|
|
HM10_Launchtask(TASK_HM10_VERSION,4,10); // read SW Version
|
|
|
|
HM10_Launchtask(TASK_HM10_DISC,5,50); // discovery
|
2020-02-01 15:17:20 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 14:02:47 +00:00
|
|
|
void HM10_Discovery_Scan(void) {
|
|
|
|
HM10_Launchtask(TASK_HM10_DISCONN,0,1); // disconnect
|
2020-02-05 15:56:41 +00:00
|
|
|
HM10_Launchtask(TASK_HM10_DISC,1,1); // discovery
|
2020-02-01 15:17:20 +00:00
|
|
|
}
|
2020-02-05 15:56:41 +00:00
|
|
|
|
2020-02-08 17:09:17 +00:00
|
|
|
void HM10_Read_LYWSD03(void) {
|
2020-02-01 15:17:20 +00:00
|
|
|
HM10_Launchtask(TASK_HM10_CONN,0,1); // connect
|
|
|
|
HM10_Launchtask(TASK_HM10_FEEDBACK,1,35); // get OK+CONN
|
2020-02-08 17:09:17 +00:00
|
|
|
HM10_Launchtask(TASK_HM10_SUB_L3,2,20); // subscribe
|
|
|
|
HM10_Launchtask(TASK_HM10_UN_L3,3,80); // unsubscribe
|
|
|
|
HM10_Launchtask(TASK_HM10_READ_BT_L3,4,5); // read Battery
|
2020-02-04 07:13:09 +00:00
|
|
|
HM10_Launchtask(TASK_HM10_DISCONN,5,5); // disconnect
|
2020-02-01 15:17:20 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 17:09:17 +00:00
|
|
|
void HM10_Read_LYWSD02(void) {
|
|
|
|
HM10_Launchtask(TASK_HM10_CONN,0,1); // connect
|
|
|
|
HM10_Launchtask(TASK_HM10_FEEDBACK,1,35); // get OK+CONN
|
|
|
|
HM10_Launchtask(TASK_HM10_SUB_L2,2,20); // subscribe
|
|
|
|
HM10_Launchtask(TASK_HM10_UN_L2,3,80); // unsubscribe
|
|
|
|
HM10_Launchtask(TASK_HM10_READ_BT_L2,4,5); // read Battery
|
|
|
|
HM10_Launchtask(TASK_HM10_DISCONN,5,5); // disconnect
|
|
|
|
}
|
|
|
|
|
|
|
|
void HM10_Time_LYWSD02(void) {
|
|
|
|
HM10_Launchtask(TASK_HM10_DISCONN,0,0); // disconnect
|
|
|
|
HM10_Launchtask(TASK_HM10_CONN,1,5); // connect
|
|
|
|
HM10_Launchtask(TASK_HM10_FEEDBACK,2,35); // get OK+CONN
|
2020-02-09 14:32:45 +00:00
|
|
|
HM10_Launchtask(TASK_HM10_TIME_L2,3,20); // subscribe
|
2020-02-08 17:09:17 +00:00
|
|
|
HM10_Launchtask(TASK_HM10_DISCONN,4,5); // disconnect
|
|
|
|
}
|
|
|
|
|
2020-01-31 17:50:11 +00:00
|
|
|
/**
|
|
|
|
* @brief Return the slot number of a known sensor or return create new sensor slot
|
|
|
|
*
|
|
|
|
* @param _serial BLE address of the sensor
|
2020-02-02 16:44:26 +00:00
|
|
|
* @param _type Type number of the sensor, 0xff for Auto-type
|
2020-01-31 17:50:11 +00:00
|
|
|
* @return uint32_t Known or new slot in the sensors-vector
|
|
|
|
*/
|
|
|
|
uint32_t MIBLEgetSensorSlot(uint8_t (&_serial)[6], uint8_t _type){
|
2020-02-02 16:44:26 +00:00
|
|
|
if(_type==0xff){
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("MIBLE: will test MAC-type"));
|
|
|
|
for (uint32_t i=0;i<4;i++){
|
|
|
|
if(memcmp(_serial,kHM10SlaveID+i,3)==0){
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("MIBLE: MAC is type %u"), i);
|
|
|
|
_type = i+1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("MIBLE: MAC-type is unknown"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(_type==0xff) return _type; // error
|
|
|
|
|
2020-01-31 17:50:11 +00:00
|
|
|
DEBUG_SENSOR_LOG(PSTR("MIBLE: vector size %u"), MIBLEsensors.size());
|
|
|
|
for(uint32_t i=0; i<MIBLEsensors.size(); i++){
|
|
|
|
if(memcmp(_serial,MIBLEsensors.at(i).serial,sizeof(_serial))==0){
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("MIBLE: known sensor at slot: %u"), i);
|
|
|
|
if(MIBLEsensors.at(i).showedUp < 3){ // if we got an intact packet, the sensor should show up several times
|
|
|
|
MIBLEsensors.at(i).showedUp++; // count up to the above number ... now we are pretty sure
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
2020-02-04 07:13:09 +00:00
|
|
|
DEBUG_SENSOR_LOG(PSTR("MIBLE i: %x %x %x %x %x %x"), MIBLEsensors.at(i).serial[0], MIBLEsensors.at(i).serial[1],MIBLEsensors.at(i).serial[2],MIBLEsensors.at(i).serial[3],MIBLEsensors.at(i).serial[4],MIBLEsensors.at(i).serial[5]);
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("MIBLE n: %x %x %x %x %x %x"), _serial[0], _serial[1], _serial[2],_serial[3],_serial[4],_serial[5]);
|
2020-01-31 17:50:11 +00:00
|
|
|
}
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("MIBLE: found new sensor"));
|
|
|
|
mi_sensor_t _newSensor;
|
|
|
|
memcpy(_newSensor.serial,_serial, sizeof(_serial));
|
|
|
|
_newSensor.type = _type;
|
2020-02-04 07:13:09 +00:00
|
|
|
_newSensor.showedUp = 1; // does not matter for HM-10
|
2020-02-02 16:44:26 +00:00
|
|
|
_newSensor.temp =-1000.0f;
|
2020-01-31 17:50:11 +00:00
|
|
|
switch (_type)
|
|
|
|
{
|
|
|
|
case 1:
|
2020-02-02 16:44:26 +00:00
|
|
|
_newSensor.moisture =-1000.0f;
|
|
|
|
_newSensor.fertility =-1000.0f;
|
|
|
|
_newSensor.lux = 0xffff;
|
2020-01-31 17:50:11 +00:00
|
|
|
break;
|
2020-02-02 16:44:26 +00:00
|
|
|
case 2: case 3: case 4:
|
|
|
|
_newSensor.hum=-1.0f;
|
|
|
|
_newSensor.bat=0xff;
|
2020-01-31 17:50:11 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
MIBLEsensors.push_back(_newSensor);
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("MIBLE: new sensor at slot: %u"), MIBLEsensors.size()-1);
|
|
|
|
return MIBLEsensors.size()-1;
|
|
|
|
};
|
2020-01-30 08:18:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* init serial
|
|
|
|
* define serial rx/tx port fixed with 115200 baud
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
void HM10SerialInit(void) {
|
2020-01-31 09:12:48 +00:00
|
|
|
HM10.mode.init = false;
|
2020-02-04 18:30:41 +00:00
|
|
|
HM10.serialSpeed = HM10_BAUDRATE;
|
2020-02-06 16:05:37 +00:00
|
|
|
HM10Serial = new TasmotaSerial(pin[GPIO_HM10_RX], pin[GPIO_HM10_TX], 1, 0, HM10_MAX_RX_BUF);
|
2020-02-04 18:30:41 +00:00
|
|
|
if (HM10Serial->begin(HM10.serialSpeed)) {
|
2020-01-31 14:28:34 +00:00
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%s start serial communication fixed to 115200 baud"),D_CMND_HM10);
|
2020-01-31 09:12:48 +00:00
|
|
|
if (HM10Serial->hardwareSerial()) {
|
2020-01-30 08:18:21 +00:00
|
|
|
ClaimSerial();
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("HM10: claim HW"));
|
|
|
|
}
|
|
|
|
HM10_Reset();
|
2020-02-01 15:17:20 +00:00
|
|
|
HM10.mode.pending_task = 1;
|
|
|
|
HM10.mode.init = 1;
|
2020-02-04 14:02:47 +00:00
|
|
|
HM10.period = Settings.tele_period;
|
2020-02-04 16:55:48 +00:00
|
|
|
DEBUG_SENSOR_LOG(PSTR("%s_TASK_LIST initialized, now return to main loop"),D_CMND_HM10);
|
2020-01-30 08:18:21 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-05 15:56:41 +00:00
|
|
|
/**
|
|
|
|
* @brief convert Mac-String to byte array
|
|
|
|
*
|
|
|
|
* @param string Hex-string, must contain 12 chars (no error checking)
|
|
|
|
* @param _mac Must be a uint8_t[6], filled with zeros
|
|
|
|
*/
|
2020-01-30 08:18:21 +00:00
|
|
|
|
2020-02-05 15:56:41 +00:00
|
|
|
void HM10MACStringToBytes(const char* string, uint8_t _mac[]) {
|
2020-02-02 16:44:26 +00:00
|
|
|
uint32_t index = 0;
|
2020-02-05 15:56:41 +00:00
|
|
|
while (index < 12) {
|
2020-02-02 16:44:26 +00:00
|
|
|
char c = string[index];
|
|
|
|
uint32_t value = 0;
|
|
|
|
if(c >= '0' && c <= '9')
|
|
|
|
value = (c - '0');
|
|
|
|
else if (c >= 'A' && c <= 'F')
|
|
|
|
value = (10 + (c - 'A'));
|
|
|
|
_mac[(index/2)] += value << (((index + 1) % 2) * 4);
|
|
|
|
// DEBUG_SENSOR_LOG(PSTR("HM10: Char: %c, Value: %x, Index/2: %u, valueadded: %x, MAC-index: %x"), c, value,(index/2),value << (((index + 1) % 2) * 4), _mac[index/2]);
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("HM10: MAC-array: %x%x%x%x%x%x"),_mac[0],_mac[1],_mac[2],_mac[3],_mac[4],_mac[5]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-31 09:12:48 +00:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* parse the response
|
|
|
|
\*********************************************************************************************/
|
2020-02-05 15:56:41 +00:00
|
|
|
|
2020-01-31 09:12:48 +00:00
|
|
|
void HM10ParseResponse(char *buf) {
|
2020-01-31 14:28:34 +00:00
|
|
|
if (!strncmp(buf,"OK",2)) {
|
2020-01-31 09:12:48 +00:00
|
|
|
DEBUG_SENSOR_LOG(PSTR("HM10: got OK"));
|
|
|
|
}
|
2020-01-31 14:28:34 +00:00
|
|
|
if (!strncmp(buf,"HMSoft",6)) { //8
|
|
|
|
const char* _fw = "000";
|
|
|
|
memcpy((void *)_fw,(void *)(buf+8),3);
|
|
|
|
HM10.firmware = atoi(_fw);
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("HM10: Firmware: %d"), HM10.firmware);
|
2020-02-02 16:44:26 +00:00
|
|
|
return;
|
2020-01-31 09:12:48 +00:00
|
|
|
}
|
2020-02-02 16:44:26 +00:00
|
|
|
char * _pos = strstr(buf, "IS0:");
|
|
|
|
if(_pos) {
|
|
|
|
const char* _mac = "000000000000";
|
|
|
|
memcpy((void *)_mac,(void *)(_pos+4),12);
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("HM10: found Mac: %s"), _mac);
|
|
|
|
uint8_t _newMacArray[6] = {0};
|
2020-02-05 15:56:41 +00:00
|
|
|
HM10MACStringToBytes(_mac, _newMacArray);
|
2020-02-02 16:44:26 +00:00
|
|
|
DEBUG_SENSOR_LOG(PSTR("HM10: MAC-array: %x%x%x%x%x%x"),_newMacArray[0],_newMacArray[1],_newMacArray[2],_newMacArray[3],_newMacArray[4],_newMacArray[5]);
|
|
|
|
MIBLEgetSensorSlot(_newMacArray, 0xff);
|
2020-02-08 17:09:17 +00:00
|
|
|
}
|
|
|
|
if (strstr(buf, "LOST")){
|
|
|
|
HM10.mode.connected = false;
|
|
|
|
}
|
2020-01-31 14:28:34 +00:00
|
|
|
else {
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("HM10: empty response"));
|
|
|
|
}
|
2020-01-30 08:18:21 +00:00
|
|
|
}
|
|
|
|
|
2020-02-01 15:17:20 +00:00
|
|
|
void HM10readTempHum(char *_buf){
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("HM10: raw data: %x%x%x%x%x%x%x"),_buf[0],_buf[1],_buf[2],_buf[3],_buf[4],_buf[5],_buf[6]);
|
|
|
|
if(_buf[0] != 0 && _buf[1] != 0){
|
2020-02-08 17:09:17 +00:00
|
|
|
memcpy(&LYWSD0x_HT,(void *)_buf,3);
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("HM10: Temperature * 100: %u, Humidity: %u"),LYWSD0x_HT.temp,LYWSD0x_HT.hum);
|
2020-02-02 16:44:26 +00:00
|
|
|
uint32_t _slot = HM10.state.sensor;
|
|
|
|
|
2020-02-01 15:17:20 +00:00
|
|
|
DEBUG_SENSOR_LOG(PSTR("MIBLE: Sensor slot: %u"), _slot);
|
|
|
|
static float _tempFloat;
|
2020-02-08 17:09:17 +00:00
|
|
|
_tempFloat=(float)(LYWSD0x_HT.temp)/100.0f;
|
2020-02-01 15:17:20 +00:00
|
|
|
if(_tempFloat<60){
|
2020-02-02 16:44:26 +00:00
|
|
|
MIBLEsensors.at(_slot).temp=_tempFloat;
|
2020-02-04 13:12:46 +00:00
|
|
|
HM10.mode.awaitingHT = false;
|
|
|
|
HM10.current_task_delay = 0;
|
2020-02-01 15:17:20 +00:00
|
|
|
}
|
2020-02-08 17:09:17 +00:00
|
|
|
_tempFloat=(float)LYWSD0x_HT.hum;
|
2020-02-01 15:17:20 +00:00
|
|
|
if(_tempFloat<100){
|
2020-02-02 16:44:26 +00:00
|
|
|
MIBLEsensors.at(_slot).hum = _tempFloat;
|
2020-02-01 15:17:20 +00:00
|
|
|
DEBUG_SENSOR_LOG(PSTR("LYWSD03: hum updated"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 17:09:17 +00:00
|
|
|
bool HM10readBat(char *_buf){
|
2020-02-04 07:13:09 +00:00
|
|
|
DEBUG_SENSOR_LOG(PSTR("HM10: raw data: %x%x%x%x%x%x%x"),_buf[0],_buf[1],_buf[2],_buf[3],_buf[4],_buf[5],_buf[6]);
|
|
|
|
if(_buf[0] != 0){
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("HM10: Battery: %u"),_buf[0]);
|
|
|
|
uint32_t _slot = HM10.state.sensor;
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("MIBLE: Sensor slot: %u"), _slot);
|
|
|
|
if(_buf[0]<101){
|
|
|
|
MIBLEsensors.at(_slot).bat=_buf[0];
|
2020-02-08 17:09:17 +00:00
|
|
|
return true;
|
2020-02-04 07:13:09 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-08 17:09:17 +00:00
|
|
|
return false;
|
2020-02-04 07:13:09 +00:00
|
|
|
}
|
|
|
|
|
2020-01-30 08:18:21 +00:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* handle the return value from the HM10
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2020-02-04 16:55:48 +00:00
|
|
|
bool HM10SerialHandleFeedback(){ // every 50 milliseconds
|
2020-02-05 15:56:41 +00:00
|
|
|
bool success = false;
|
2020-01-31 09:12:48 +00:00
|
|
|
uint32_t i = 0;
|
|
|
|
char ret[HM10_MAX_RX_BUF] = {0}; // reset array with zeros
|
|
|
|
|
|
|
|
|
2020-01-30 08:18:21 +00:00
|
|
|
while(HM10Serial->available()) {
|
|
|
|
// delay(0);
|
|
|
|
if(i<HM10_MAX_RX_BUF){
|
|
|
|
ret[i] = HM10Serial->read();
|
|
|
|
}
|
|
|
|
i++;
|
2020-01-31 09:12:48 +00:00
|
|
|
success = true;
|
|
|
|
}
|
2020-02-04 07:13:09 +00:00
|
|
|
if(HM10.mode.awaitingHT) {
|
2020-02-08 17:09:17 +00:00
|
|
|
if (HM10.mode.connected) HM10readTempHum(ret);
|
2020-02-04 07:13:09 +00:00
|
|
|
}
|
|
|
|
else if(HM10.mode.awaitingB) {
|
2020-02-08 17:09:17 +00:00
|
|
|
if (HM10.mode.connected) {
|
|
|
|
if (HM10readBat(ret)){
|
|
|
|
HM10.mode.awaitingB = false;
|
|
|
|
HM10.current_task_delay = 0;
|
|
|
|
}
|
|
|
|
}
|
2020-01-31 14:28:34 +00:00
|
|
|
}
|
|
|
|
else if(success) {
|
2020-01-31 09:12:48 +00:00
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%s response: %s"),D_CMND_HM10, (char *)ret);
|
|
|
|
HM10ParseResponse(ret);
|
|
|
|
}
|
|
|
|
else {
|
2020-02-04 16:55:48 +00:00
|
|
|
// DEBUG_SENSOR_LOG(PSTR("%s got no response"),D_CMND_HM10);
|
2020-01-31 09:12:48 +00:00
|
|
|
}
|
2020-01-30 08:18:21 +00:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* execute the next Task
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
void HM10_TaskEvery100ms(){
|
2020-01-31 14:28:34 +00:00
|
|
|
if (HM10.current_task_delay == 0) {
|
2020-01-30 08:18:21 +00:00
|
|
|
uint8_t i = 0;
|
|
|
|
bool runningTaskLoop = true;
|
|
|
|
while (runningTaskLoop) { // always iterate through the whole task list
|
|
|
|
switch(HM10_TASK_LIST[i][0]) { // handle the kind of task
|
2020-01-31 09:12:48 +00:00
|
|
|
case TASK_HM10_ROLE1:
|
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%s set role to 1"),D_CMND_HM10);
|
2020-01-31 14:28:34 +00:00
|
|
|
HM10.current_task_delay = 5; // set task delay
|
2020-01-31 09:12:48 +00:00
|
|
|
HM10_TaskReplaceInSlot(TASK_HM10_FEEDBACK,i);
|
|
|
|
runningTaskLoop = false;
|
|
|
|
HM10Serial->write("AT+ROLE1");
|
|
|
|
break;
|
|
|
|
case TASK_HM10_IMME1:
|
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%s set imme to 1"),D_CMND_HM10);
|
2020-01-31 14:28:34 +00:00
|
|
|
HM10.current_task_delay = 5; // set task delay
|
2020-01-31 09:12:48 +00:00
|
|
|
HM10_TaskReplaceInSlot(TASK_HM10_FEEDBACK,i);
|
|
|
|
runningTaskLoop = false;
|
|
|
|
HM10Serial->write("AT+IMME1");
|
|
|
|
break;
|
|
|
|
case TASK_HM10_DISC:
|
2020-01-31 14:28:34 +00:00
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%s start discovery"),D_CMND_HM10);
|
|
|
|
HM10.current_task_delay = 35; // set task delay
|
2020-01-31 09:12:48 +00:00
|
|
|
HM10_TaskReplaceInSlot(TASK_HM10_FEEDBACK,i);
|
|
|
|
runningTaskLoop = false;
|
|
|
|
HM10Serial->write("AT+DISC?");
|
|
|
|
break;
|
|
|
|
case TASK_HM10_VERSION:
|
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%s read version"),D_CMND_HM10);
|
2020-01-31 14:28:34 +00:00
|
|
|
HM10.current_task_delay = 5; // set task delay
|
2020-01-31 09:12:48 +00:00
|
|
|
HM10_TaskReplaceInSlot(TASK_HM10_FEEDBACK,i);
|
|
|
|
runningTaskLoop = false;
|
|
|
|
HM10Serial->write("AT+VERR?");
|
2020-01-30 08:18:21 +00:00
|
|
|
break;
|
2020-01-31 09:12:48 +00:00
|
|
|
case TASK_HM10_NAME:
|
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%s read name"),D_CMND_HM10);
|
2020-01-31 14:28:34 +00:00
|
|
|
HM10.current_task_delay = 5; // set task delay
|
2020-01-31 09:12:48 +00:00
|
|
|
HM10_TaskReplaceInSlot(TASK_HM10_FEEDBACK,i);
|
2020-01-30 08:18:21 +00:00
|
|
|
runningTaskLoop = false;
|
2020-01-31 09:12:48 +00:00
|
|
|
HM10Serial->write("AT+NAME?");
|
2020-01-30 08:18:21 +00:00
|
|
|
break;
|
2020-01-31 14:28:34 +00:00
|
|
|
case TASK_HM10_CONN:
|
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%s connect"),D_CMND_HM10);
|
|
|
|
HM10.current_task_delay = 2; // set task delay
|
|
|
|
HM10_TaskReplaceInSlot(TASK_HM10_FEEDBACK,i);
|
|
|
|
runningTaskLoop = false;
|
2020-02-01 15:17:20 +00:00
|
|
|
char _con[20];
|
2020-02-02 16:44:26 +00:00
|
|
|
sprintf_P(_con,"AT+CON%02x%02x%02x%02x%02x%02x",MIBLEsensors.at(HM10.state.sensor).serial[0],MIBLEsensors.at(HM10.state.sensor).serial[1],MIBLEsensors.at(HM10.state.sensor).serial[2],MIBLEsensors.at(HM10.state.sensor).serial[3],MIBLEsensors.at(HM10.state.sensor).serial[4],MIBLEsensors.at(HM10.state.sensor).serial[5]);
|
2020-02-01 15:17:20 +00:00
|
|
|
HM10Serial->write(_con);
|
2020-02-04 07:13:09 +00:00
|
|
|
HM10.mode.awaitingB = false;
|
2020-02-08 17:09:17 +00:00
|
|
|
HM10.mode.connected = true;
|
2020-01-31 14:28:34 +00:00
|
|
|
break;
|
|
|
|
case TASK_HM10_DISCONN:
|
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%s disconnect"),D_CMND_HM10);
|
|
|
|
HM10.current_task_delay = 5; // set task delay
|
|
|
|
HM10_TaskReplaceInSlot(TASK_HM10_FEEDBACK,i);
|
|
|
|
runningTaskLoop = false;
|
|
|
|
HM10Serial->write("AT");
|
|
|
|
break;
|
2020-01-31 09:12:48 +00:00
|
|
|
case TASK_HM10_RESET:
|
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%s Reset Device"),D_CMND_HM10);
|
|
|
|
HM10Serial->write("AT+RESET");
|
2020-01-31 14:28:34 +00:00
|
|
|
HM10.current_task_delay = 5; // set task delay
|
2020-01-31 09:12:48 +00:00
|
|
|
HM10_TaskReplaceInSlot(TASK_HM10_FEEDBACK,i);
|
|
|
|
runningTaskLoop = false;
|
|
|
|
break;
|
2020-02-08 17:09:17 +00:00
|
|
|
case TASK_HM10_SUB_L3:
|
2020-01-31 14:28:34 +00:00
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%s subscribe"),D_CMND_HM10);
|
2020-02-01 15:17:20 +00:00
|
|
|
HM10.current_task_delay = 25; // set task delay
|
|
|
|
HM10_TaskReplaceInSlot(TASK_HM10_DELAY_SUB,i);
|
2020-01-31 14:28:34 +00:00
|
|
|
runningTaskLoop = false;
|
|
|
|
HM10Serial->write("AT+NOTIFY_ON0037");
|
2020-02-01 15:17:20 +00:00
|
|
|
break;
|
2020-02-08 17:09:17 +00:00
|
|
|
case TASK_HM10_UN_L3:
|
2020-02-01 15:17:20 +00:00
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%s un-subscribe"),D_CMND_HM10);
|
|
|
|
HM10.current_task_delay = 5; // set task delay
|
|
|
|
HM10_TaskReplaceInSlot(TASK_HM10_FEEDBACK,i);
|
|
|
|
runningTaskLoop = false;
|
2020-02-04 07:13:09 +00:00
|
|
|
HM10.mode.awaitingHT = false;
|
2020-02-01 15:17:20 +00:00
|
|
|
HM10Serial->write("AT+NOTIFYOFF0037");
|
|
|
|
break;
|
2020-02-08 17:09:17 +00:00
|
|
|
case TASK_HM10_SUB_L2:
|
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%s subscribe"),D_CMND_HM10);
|
|
|
|
HM10.current_task_delay = 25; // set task delay
|
|
|
|
HM10_TaskReplaceInSlot(TASK_HM10_DELAY_SUB,i);
|
|
|
|
runningTaskLoop = false;
|
|
|
|
HM10Serial->write("AT+NOTIFY_ON003C");
|
|
|
|
break;
|
|
|
|
case TASK_HM10_UN_L2:
|
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%s un-subscribe"),D_CMND_HM10);
|
|
|
|
HM10.current_task_delay = 5; // set task delay
|
|
|
|
HM10_TaskReplaceInSlot(TASK_HM10_FEEDBACK,i);
|
|
|
|
runningTaskLoop = false;
|
|
|
|
HM10.mode.awaitingHT = false;
|
|
|
|
HM10Serial->write("AT+NOTIFYOFF003C");
|
|
|
|
break;
|
|
|
|
case TASK_HM10_TIME_L2:
|
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%s set time"),D_CMND_HM10);
|
|
|
|
HM10.current_task_delay = 5; // set task delay
|
|
|
|
HM10_TaskReplaceInSlot(TASK_HM10_FEEDBACK,i);
|
|
|
|
runningTaskLoop = false;
|
|
|
|
HM10.time = Rtc.utc_time;
|
|
|
|
HM10Serial->write("AT+SEND_DATAWR002F");
|
|
|
|
HM10Serial->write(HM10.timebuf,4);
|
|
|
|
HM10Serial->write(Rtc.time_timezone / 60);
|
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG,PSTR("%s Time-string: %x%x%x%x%x"),D_CMND_HM10, HM10.timebuf[0],HM10.timebuf[1],HM10.timebuf[2],HM10.timebuf[3],(Rtc.time_timezone /60));
|
|
|
|
break;
|
2020-02-04 07:13:09 +00:00
|
|
|
case TASK_HM10_READ_HT:
|
2020-01-31 14:28:34 +00:00
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%s read handle 0036"),D_CMND_HM10);
|
|
|
|
HM10.current_task_delay = 0; // set task delay
|
|
|
|
HM10_TaskReplaceInSlot(TASK_HM10_FEEDBACK,i);
|
|
|
|
runningTaskLoop = false;
|
|
|
|
HM10Serial->write("AT+READDATA0036?");
|
2020-02-04 07:13:09 +00:00
|
|
|
HM10.mode.awaitingHT = true;
|
|
|
|
break;
|
2020-02-08 17:09:17 +00:00
|
|
|
case TASK_HM10_READ_BT_L3:
|
2020-02-04 07:13:09 +00:00
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%s read handle 003A"),D_CMND_HM10);
|
2020-02-08 17:09:17 +00:00
|
|
|
HM10.current_task_delay = 2; // set task delay
|
2020-02-04 07:13:09 +00:00
|
|
|
HM10_TaskReplaceInSlot(TASK_HM10_FEEDBACK,i);
|
|
|
|
runningTaskLoop = false;
|
|
|
|
HM10Serial->write("AT+READDATA003A?");
|
|
|
|
HM10.mode.awaitingB = true;
|
2020-02-01 15:17:20 +00:00
|
|
|
break;
|
2020-02-08 17:09:17 +00:00
|
|
|
case TASK_HM10_READ_BT_L2:
|
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%s read handle 0043"),D_CMND_HM10);
|
|
|
|
HM10.current_task_delay = 2; // set task delay
|
2020-01-31 14:28:34 +00:00
|
|
|
HM10_TaskReplaceInSlot(TASK_HM10_FEEDBACK,i);
|
|
|
|
runningTaskLoop = false;
|
2020-02-08 17:09:17 +00:00
|
|
|
HM10Serial->write("AT+READDATA0043?");
|
|
|
|
HM10.mode.awaitingB = true;
|
2020-02-01 15:17:20 +00:00
|
|
|
break;
|
2020-02-08 17:09:17 +00:00
|
|
|
// case TASK_HM10_FINDALLCHARS:
|
|
|
|
// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%s find all chars"),D_CMND_HM10);
|
|
|
|
// HM10.current_task_delay = 5; // set task delay
|
|
|
|
// HM10_TaskReplaceInSlot(TASK_HM10_FEEDBACK,i);
|
|
|
|
// runningTaskLoop = false;
|
|
|
|
// HM10Serial->write("AT+FINDALLCHARS?");
|
|
|
|
// break;
|
2020-01-31 09:12:48 +00:00
|
|
|
case TASK_HM10_FEEDBACK:
|
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%s get response"),D_CMND_HM10);
|
|
|
|
HM10SerialHandleFeedback();
|
2020-01-31 14:28:34 +00:00
|
|
|
HM10.current_task_delay = HM10_TASK_LIST[i+1][1];; // set task delay
|
2020-01-30 08:18:21 +00:00
|
|
|
HM10_TASK_LIST[i][0] = TASK_HM10_DONE; // no feedback for reset
|
2020-01-31 09:12:48 +00:00
|
|
|
runningTaskLoop = false;
|
2020-01-30 08:18:21 +00:00
|
|
|
break;
|
2020-02-01 15:17:20 +00:00
|
|
|
case TASK_HM10_DELAY_SUB:
|
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%s start reading"),D_CMND_HM10);
|
|
|
|
HM10SerialHandleFeedback();
|
|
|
|
HM10.current_task_delay = HM10_TASK_LIST[i+1][1];; // set task delay
|
|
|
|
HM10_TASK_LIST[i][0] = TASK_HM10_DONE; // no feedback for reset
|
2020-02-04 07:13:09 +00:00
|
|
|
HM10.mode.awaitingHT = true;
|
2020-02-01 15:17:20 +00:00
|
|
|
runningTaskLoop = false;
|
|
|
|
break;
|
2020-01-30 08:18:21 +00:00
|
|
|
case TASK_HM10_DONE: // this entry was already handled
|
2020-01-31 09:12:48 +00:00
|
|
|
// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%sFound done HM10_TASK"),D_CMND_HM10);
|
|
|
|
// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("%snext slot:%u, i: %u"),D_CMND_HM10, HM10_TASK_LIST[i+1][0],i);
|
2020-01-30 08:18:21 +00:00
|
|
|
if(HM10_TASK_LIST[i+1][0] == TASK_HM10_NOTASK) { // check the next entry and if there is none
|
2020-02-04 16:55:48 +00:00
|
|
|
DEBUG_SENSOR_LOG(PSTR("%sno Tasks left"),D_CMND_HM10);
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("%sHM10_TASK_DONE current slot %u"),D_CMND_HM10, i);
|
2020-01-30 08:18:21 +00:00
|
|
|
for (uint8_t j = 0; j < HM10_MAX_TASK_NUMBER+1; j++) { // do a clean-up:
|
2020-02-04 16:55:48 +00:00
|
|
|
DEBUG_SENSOR_LOG(PSTR("%sHM10_TASK cleanup slot %u"),D_CMND_HM10, j);
|
2020-01-30 08:18:21 +00:00
|
|
|
HM10_TASK_LIST[j][0] = TASK_HM10_NOTASK; // reset all task entries
|
|
|
|
HM10_TASK_LIST[j][1] = 0; // reset all delays
|
|
|
|
}
|
|
|
|
runningTaskLoop = false; // return to main loop
|
2020-02-05 15:56:41 +00:00
|
|
|
HM10.mode.pending_task = 0; // back to main loop control
|
2020-01-30 08:18:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-01-31 09:12:48 +00:00
|
|
|
i++;
|
2020-01-30 08:18:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2020-01-31 14:28:34 +00:00
|
|
|
HM10.current_task_delay--; // count down every 100 ms
|
2020-01-30 08:18:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-05 15:56:41 +00:00
|
|
|
/**
|
|
|
|
* @brief Main loop of the driver, "high level"-loop
|
|
|
|
*
|
|
|
|
*/
|
2020-02-04 14:02:47 +00:00
|
|
|
|
2020-02-01 15:17:20 +00:00
|
|
|
void HM10EverySecond(){
|
|
|
|
if(HM10.firmware == 0) return;
|
|
|
|
if(HM10.mode.pending_task == 1) return;
|
2020-02-04 18:30:41 +00:00
|
|
|
if (MIBLEsensors.size()==0) return;
|
|
|
|
|
2020-02-02 18:07:31 +00:00
|
|
|
static uint32_t _counter = 0;
|
|
|
|
static uint32_t _nextSensorSlot = 0;
|
|
|
|
if(_counter==0) {
|
2020-02-04 16:55:48 +00:00
|
|
|
HM10.state.sensor = _nextSensorSlot;
|
|
|
|
_nextSensorSlot++;
|
2020-02-09 14:32:45 +00:00
|
|
|
if(MIBLEsensors.at(HM10.state.sensor).type==LYWSD03MMC) {
|
2020-02-02 18:07:31 +00:00
|
|
|
HM10.mode.pending_task = 1;
|
2020-02-08 17:09:17 +00:00
|
|
|
HM10_Read_LYWSD03();
|
|
|
|
}
|
|
|
|
if(MIBLEsensors.at(HM10.state.sensor).type==LYWSD02) {
|
|
|
|
HM10.mode.pending_task = 1;
|
|
|
|
HM10_Read_LYWSD02();
|
2020-02-02 16:44:26 +00:00
|
|
|
}
|
|
|
|
if (HM10.state.sensor==MIBLEsensors.size()-1) {
|
2020-02-02 18:07:31 +00:00
|
|
|
_nextSensorSlot= 0;
|
2020-02-04 18:30:41 +00:00
|
|
|
_counter++;
|
2020-02-02 16:44:26 +00:00
|
|
|
}
|
2020-02-04 16:55:48 +00:00
|
|
|
DEBUG_SENSOR_LOG(PSTR("%s active sensor now: %u"),D_CMND_HM10, HM10.state.sensor);
|
2020-02-01 15:17:20 +00:00
|
|
|
}
|
2020-02-04 18:30:41 +00:00
|
|
|
else _counter++;
|
|
|
|
if (_counter>HM10.period) _counter = 0;
|
2020-02-01 15:17:20 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 14:02:47 +00:00
|
|
|
bool HM10Cmd(void) {
|
|
|
|
char command[CMDSZ];
|
|
|
|
bool serviced = true;
|
|
|
|
uint8_t disp_len = strlen(D_CMND_HM10);
|
|
|
|
|
|
|
|
if (!strncasecmp_P(XdrvMailbox.topic, PSTR(D_CMND_HM10), disp_len)) { // prefix
|
|
|
|
uint32_t command_code = GetCommandCode(command, sizeof(command), XdrvMailbox.topic + disp_len, kHM10_Commands);
|
|
|
|
switch (command_code) {
|
|
|
|
case CMND_HM10_PERIOD:
|
|
|
|
if (XdrvMailbox.data_len > 0) {
|
|
|
|
if (command_code == CMND_HM10_PERIOD) { HM10.period = XdrvMailbox.payload; }
|
|
|
|
}
|
2020-02-04 16:55:48 +00:00
|
|
|
else {
|
|
|
|
if (command_code == CMND_HM10_PERIOD) XdrvMailbox.payload = HM10.period;
|
|
|
|
}
|
2020-02-04 14:02:47 +00:00
|
|
|
Response_P(S_JSON_HM10_COMMAND_NVALUE, command, XdrvMailbox.payload);
|
|
|
|
break;
|
2020-02-04 18:30:41 +00:00
|
|
|
case CMND_HM10_BAUD:
|
|
|
|
if (XdrvMailbox.data_len > 0) {
|
|
|
|
if (command_code == CMND_HM10_BAUD) {
|
|
|
|
HM10.serialSpeed = XdrvMailbox.payload;
|
|
|
|
HM10Serial->begin(HM10.serialSpeed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (command_code == CMND_HM10_BAUD) XdrvMailbox.payload = HM10.serialSpeed;
|
|
|
|
}
|
|
|
|
Response_P(S_JSON_HM10_COMMAND_NVALUE, command, XdrvMailbox.payload);
|
|
|
|
break;
|
2020-02-08 17:09:17 +00:00
|
|
|
case CMND_HM10_TIME:
|
|
|
|
if (XdrvMailbox.data_len > 0) {
|
|
|
|
if(MIBLEsensors.size()>XdrvMailbox.payload){
|
|
|
|
if(MIBLEsensors.at(XdrvMailbox.payload).type == LYWSD02){
|
|
|
|
HM10.state.sensor = XdrvMailbox.payload;
|
|
|
|
HM10_Time_LYWSD02();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Response_P(S_JSON_HM10_COMMAND_NVALUE, command, XdrvMailbox.payload);
|
|
|
|
break;
|
2020-02-04 18:30:41 +00:00
|
|
|
case CMND_HM10_AT:
|
|
|
|
HM10Serial->write("AT"); // without an argument this will disconnect
|
|
|
|
if (strlen(XdrvMailbox.data)!=0) {
|
|
|
|
HM10Serial->write("+");
|
|
|
|
HM10Serial->write(XdrvMailbox.data); // pass everything without checks
|
|
|
|
Response_P(S_JSON_HM10_COMMAND, ":AT+",XdrvMailbox.data);
|
|
|
|
}
|
|
|
|
else Response_P(S_JSON_HM10_COMMAND, ":AT",XdrvMailbox.data);
|
|
|
|
break;
|
2020-02-04 14:02:47 +00:00
|
|
|
case CMND_HM10_DISC_SCAN:
|
2020-02-04 18:30:41 +00:00
|
|
|
if (command_code == CMND_HM10_DISC_SCAN) { HM10_Discovery_Scan(); }
|
|
|
|
Response_P(S_JSON_HM10_COMMAND, command, "");
|
2020-02-04 14:02:47 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// else for Unknown command
|
|
|
|
serviced = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return serviced;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-01 15:17:20 +00:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Presentation
|
|
|
|
\*********************************************************************************************/
|
2020-01-30 08:18:21 +00:00
|
|
|
|
2020-02-04 16:55:48 +00:00
|
|
|
const char HTTP_HM10[] PROGMEM = "{s}HM10" " Firmware " "{m}%u{e}";
|
2020-02-05 19:34:38 +00:00
|
|
|
const char HTTP_HM10_SERIAL[] PROGMEM = "{s}%s %s{m}%02x:%02x:%02x:%02x:%02x:%02x%{e}";
|
2020-02-04 16:55:48 +00:00
|
|
|
const char HTTP_BATTERY[] PROGMEM = "{s}%s" " Battery" "{m}%u%%{e}";
|
|
|
|
const char HTTP_HM10_FLORA_DATA[] PROGMEM = "{s}%s" " Fertility" "{m}%sus/cm{e}";
|
2020-02-05 19:34:38 +00:00
|
|
|
const char HTTP_HM10_HL[] PROGMEM = "{s}<hr>{m}<hr>{e}";
|
2020-02-02 18:07:31 +00:00
|
|
|
|
2020-01-31 14:28:34 +00:00
|
|
|
void HM10Show(bool json)
|
|
|
|
{
|
2020-02-02 16:44:26 +00:00
|
|
|
if (json) {
|
|
|
|
for (uint32_t i = 0; i < MIBLEsensors.size(); i++) {
|
2020-02-02 18:07:31 +00:00
|
|
|
char slave[33];
|
2020-02-04 07:13:09 +00:00
|
|
|
sprintf_P(slave,"%s-%02x%02x%02x",kHM10SlaveType[MIBLEsensors.at(i).type-1],MIBLEsensors.at(i).serial[3],MIBLEsensors.at(i).serial[4],MIBLEsensors.at(i).serial[5]);
|
2020-02-02 16:44:26 +00:00
|
|
|
char temperature[33]; // all sensors have temperature
|
|
|
|
dtostrfd(MIBLEsensors.at(i).temp, Settings.flag2.temperature_resolution, temperature);
|
2020-02-02 18:07:31 +00:00
|
|
|
|
|
|
|
ResponseAppend_P(PSTR(",\"%s\":{"),slave);
|
|
|
|
if(MIBLEsensors.at(i).temp!=-1000.0f){ // this is the error code -> no temperature
|
|
|
|
ResponseAppend_P(PSTR("\"" D_JSON_TEMPERATURE "\":%s"), temperature);
|
|
|
|
}
|
2020-02-04 07:13:09 +00:00
|
|
|
if (MIBLEsensors.at(i).type==FLORA){
|
2020-02-02 18:07:31 +00:00
|
|
|
char lux[33];
|
|
|
|
char moisture[33];
|
|
|
|
char fertility[33];
|
|
|
|
dtostrfd((float)MIBLEsensors.at(i).lux, 0, lux);
|
|
|
|
dtostrfd(MIBLEsensors.at(i).moisture, 0, moisture);
|
|
|
|
dtostrfd(MIBLEsensors.at(i).fertility, 0, fertility);
|
|
|
|
if(MIBLEsensors.at(i).lux!=0xffff){ // this is the error code -> no temperature
|
|
|
|
ResponseAppend_P(PSTR(",\"" D_JSON_ILLUMINANCE "\":%s"), lux);
|
|
|
|
}
|
2020-02-09 14:32:45 +00:00
|
|
|
if(MIBLEsensors.at(i).moisture!=-1000.0f){ // this is the error code -> no moisture
|
2020-02-02 18:07:31 +00:00
|
|
|
ResponseAppend_P(PSTR(",\"" D_JSON_MOISTURE "\":%s"), moisture);
|
|
|
|
}
|
2020-02-09 14:32:45 +00:00
|
|
|
if(MIBLEsensors.at(i).fertility!=-1000.0f){ // this is the error code -> no fertility
|
2020-02-02 18:07:31 +00:00
|
|
|
ResponseAppend_P(PSTR(",\"Fertility\":%s"), fertility);
|
|
|
|
}
|
|
|
|
}
|
2020-02-04 07:13:09 +00:00
|
|
|
if (MIBLEsensors.at(i).type>FLORA){
|
2020-02-02 18:07:31 +00:00
|
|
|
char humidity[33];
|
|
|
|
dtostrfd(MIBLEsensors.at(i).hum, Settings.flag2.humidity_resolution, humidity);
|
2020-02-09 14:32:45 +00:00
|
|
|
if(MIBLEsensors.at(i).hum!=-1.0f){ // this is the error code -> no humidity
|
2020-02-02 18:07:31 +00:00
|
|
|
ResponseAppend_P(PSTR(",\"" D_JSON_HUMIDITY "\":%s"), humidity);
|
|
|
|
}
|
2020-02-09 14:32:45 +00:00
|
|
|
if(MIBLEsensors.at(i).bat!=0xff){ // this is the error code -> no battery
|
2020-02-02 18:07:31 +00:00
|
|
|
ResponseAppend_P(PSTR(",\"Battery\":%u"), MIBLEsensors.at(i).bat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ResponseAppend_P(PSTR("}"));
|
2020-02-02 16:44:26 +00:00
|
|
|
}
|
2020-01-31 14:28:34 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
} else {
|
|
|
|
WSContentSend_PD(HTTP_HM10, HM10.firmware);
|
2020-02-02 16:44:26 +00:00
|
|
|
for (uint32_t i = 0; i < MIBLEsensors.size(); i++) {
|
2020-02-05 19:34:38 +00:00
|
|
|
WSContentSend_PD(HTTP_HM10_HL);
|
|
|
|
WSContentSend_PD(HTTP_HM10_SERIAL, kHM10SlaveType[MIBLEsensors.at(i).type-1], D_MAC_ADDRESS, MIBLEsensors.at(i).serial[0], MIBLEsensors.at(i).serial[1],MIBLEsensors.at(i).serial[2],MIBLEsensors.at(i).serial[3],MIBLEsensors.at(i).serial[4],MIBLEsensors.at(i).serial[5]);
|
2020-02-02 18:07:31 +00:00
|
|
|
if(MIBLEsensors.at(i).temp!=-1000.0f){
|
|
|
|
char temperature[33];
|
|
|
|
dtostrfd(MIBLEsensors.at(i).temp, Settings.flag2.temperature_resolution, temperature);
|
|
|
|
WSContentSend_PD(HTTP_SNS_TEMP, kHM10SlaveType[MIBLEsensors.at(i).type-1], temperature, TempUnit());
|
|
|
|
}
|
2020-02-04 07:13:09 +00:00
|
|
|
if (MIBLEsensors.at(i).type==FLORA){
|
2020-02-05 19:34:38 +00:00
|
|
|
if(MIBLEsensors.at(i).lux!=0xffff){ // this is the error code -> no valid value
|
2020-02-02 18:07:31 +00:00
|
|
|
WSContentSend_PD(HTTP_SNS_ILLUMINANCE, kHM10SlaveType[MIBLEsensors.at(i).type-1], MIBLEsensors.at(i).lux);
|
|
|
|
}
|
2020-02-05 19:34:38 +00:00
|
|
|
if(MIBLEsensors.at(i).moisture!=-1000.0f){ // this is the error code -> no valid value
|
2020-02-02 18:07:31 +00:00
|
|
|
WSContentSend_PD(HTTP_SNS_MOISTURE, kHM10SlaveType[MIBLEsensors.at(i).type-1], MIBLEsensors.at(i).moisture);
|
|
|
|
}
|
2020-02-05 19:34:38 +00:00
|
|
|
if(MIBLEsensors.at(i).fertility!=-1000.0f){ // this is the error code -> no valid value
|
2020-02-02 18:07:31 +00:00
|
|
|
char fertility[33];
|
|
|
|
dtostrfd(MIBLEsensors.at(i).fertility, 0, fertility);
|
|
|
|
WSContentSend_PD(HTTP_HM10_FLORA_DATA, kHM10SlaveType[MIBLEsensors.at(i).type-1], fertility);
|
|
|
|
}
|
|
|
|
}
|
2020-02-04 07:13:09 +00:00
|
|
|
if (MIBLEsensors.at(i).type>FLORA){ // everything "above" Flora
|
2020-02-02 18:07:31 +00:00
|
|
|
if(MIBLEsensors.at(i).hum!=-1.0f){ // this is the error code -> no humidity
|
|
|
|
char humidity[33];
|
|
|
|
dtostrfd(MIBLEsensors.at(i).hum, Settings.flag2.humidity_resolution, humidity);
|
|
|
|
WSContentSend_PD(HTTP_SNS_HUM, kHM10SlaveType[MIBLEsensors.at(i).type-1], humidity);
|
|
|
|
}
|
|
|
|
if(MIBLEsensors.at(i).bat!=0xff){
|
|
|
|
WSContentSend_PD(HTTP_BATTERY, kHM10SlaveType[MIBLEsensors.at(i).type-1], MIBLEsensors.at(i).bat);
|
|
|
|
}
|
2020-02-02 16:44:26 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-31 14:28:34 +00:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
2020-01-30 08:18:21 +00:00
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2020-02-06 16:05:37 +00:00
|
|
|
bool Xsns62(uint8_t function)
|
2020-01-30 08:18:21 +00:00
|
|
|
{
|
|
|
|
bool result = false;
|
|
|
|
|
2020-02-06 16:05:37 +00:00
|
|
|
if ((pin[GPIO_HM10_RX] < 99) && (pin[GPIO_HM10_TX] < 99)) {
|
2020-01-30 08:18:21 +00:00
|
|
|
switch (function) {
|
2020-01-31 09:12:48 +00:00
|
|
|
case FUNC_INIT:
|
2020-02-05 15:56:41 +00:00
|
|
|
HM10SerialInit(); // init and start communication
|
2020-01-30 08:18:21 +00:00
|
|
|
break;
|
2020-02-02 16:44:26 +00:00
|
|
|
case FUNC_EVERY_50_MSECOND:
|
2020-02-06 16:05:37 +00:00
|
|
|
HM10SerialHandleFeedback(); // check for device feedback very often
|
2020-02-02 16:44:26 +00:00
|
|
|
break;
|
2020-01-30 08:18:21 +00:00
|
|
|
case FUNC_EVERY_100_MSECOND:
|
2020-02-04 16:55:48 +00:00
|
|
|
if (HM10_TASK_LIST[0][0] != TASK_HM10_NOTASK) {
|
2020-02-05 15:56:41 +00:00
|
|
|
HM10_TaskEvery100ms(); // something has to be done, we'll check in the next step
|
2020-01-30 08:18:21 +00:00
|
|
|
}
|
2020-01-31 09:12:48 +00:00
|
|
|
break;
|
2020-02-01 15:17:20 +00:00
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
HM10EverySecond();
|
|
|
|
break;
|
2020-02-04 14:02:47 +00:00
|
|
|
case FUNC_COMMAND:
|
|
|
|
result = HM10Cmd();
|
|
|
|
break;
|
2020-01-31 14:28:34 +00:00
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
HM10Show(1);
|
|
|
|
break;
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
case FUNC_WEB_SENSOR:
|
|
|
|
HM10Show(0);
|
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
2020-01-30 08:18:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2020-02-06 16:05:37 +00:00
|
|
|
}
|
|
|
|
#endif //USE_HM10
|