2020-04-21 08:58:33 +01:00
|
|
|
/*
|
|
|
|
xsns_62_MI_ESP32.ino - MI-BLE-sensors via ESP32 support for Tasmota
|
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
Copyright (C) 2021 Christian Baars and Theo Arends
|
2020-04-21 08:58:33 +01:00
|
|
|
|
|
|
|
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-11-17 08:30:59 +00:00
|
|
|
0.9.1.7 20201116 changed - small bugfixes, add BLOCK and OPTION command, send BLE scan via MQTT
|
|
|
|
-------
|
2020-10-23 09:18:57 +01:00
|
|
|
0.9.1.6 20201022 changed - Beacon support, RSSI at TELEPERIOD, refactoring
|
|
|
|
-------
|
2020-10-21 07:26:38 +01:00
|
|
|
0.9.1.5 20201021 changed - HASS related ('null', hold back discovery), number of found sensors for RULES
|
|
|
|
-------
|
2020-10-20 13:49:10 +01:00
|
|
|
0.9.1.4 20201020 changed - use BearSSL for decryption, revert to old TELEPERIOD-cycle as default
|
|
|
|
-------
|
2020-09-26 18:24:58 +01:00
|
|
|
0.9.1.3 20200926 changed - Improve HA discovery, make key+MAC case insensitive
|
|
|
|
-------
|
2020-09-16 07:06:27 +01:00
|
|
|
0.9.1.3 20200916 changed - add ATC (custom FW for LYWSD03MMC), API adaption for NimBLE-Arduino 1.0.2
|
2020-09-16 07:03:55 +01:00
|
|
|
-------
|
2020-08-02 14:07:18 +01:00
|
|
|
0.9.1.2 20200802 changed - add MHO-C303
|
|
|
|
-------
|
2020-07-15 16:41:12 +01:00
|
|
|
0.9.1.1 20200715 changed - add MHO-C401, refactoring
|
|
|
|
-------
|
2020-07-12 17:17:27 +01:00
|
|
|
0.9.1.0 20200712 changed - add lights and yeerc, add pure passive mode with decryption,
|
|
|
|
lots of refactoring
|
|
|
|
-------
|
2020-07-06 19:10:43 +01:00
|
|
|
0.9.0.1 20200706 changed - adapt to new NimBLE-API, tweak scan process
|
2020-07-12 17:17:27 +01:00
|
|
|
-------
|
2020-04-21 08:58:33 +01:00
|
|
|
0.9.0.0 20200413 started - initial development by Christian Baars
|
|
|
|
forked - from arendst/tasmota - https://github.com/arendst/Tasmota
|
|
|
|
|
|
|
|
*/
|
2020-04-21 10:06:30 +01:00
|
|
|
#ifdef ESP32 // ESP32 only. Use define USE_HM10 for ESP8266 support
|
|
|
|
|
2020-04-21 08:58:33 +01:00
|
|
|
#ifdef USE_MI_ESP32
|
|
|
|
|
|
|
|
#define XSNS_62 62
|
2020-07-12 17:17:27 +01:00
|
|
|
#define USE_MI_DECRYPTION
|
2020-04-21 08:58:33 +01:00
|
|
|
|
|
|
|
#include <NimBLEDevice.h>
|
|
|
|
#include <vector>
|
2020-07-12 17:17:27 +01:00
|
|
|
#ifdef USE_MI_DECRYPTION
|
2020-10-20 13:49:10 +01:00
|
|
|
#include <t_bearssl.h>
|
2020-07-12 17:17:27 +01:00
|
|
|
#endif //USE_MI_DECRYPTION
|
2020-04-21 08:58:33 +01:00
|
|
|
|
|
|
|
void MI32scanEndedCB(NimBLEScanResults results);
|
|
|
|
void MI32notifyCB(NimBLERemoteCharacteristic* pRemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify);
|
|
|
|
|
|
|
|
struct {
|
|
|
|
uint16_t perPage = 4;
|
|
|
|
uint32_t period; // set manually in addition to TELE-period, is set to TELE-period after start
|
2020-10-21 07:26:38 +01:00
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
uint32_t init:1;
|
|
|
|
uint32_t connected:1;
|
|
|
|
uint32_t autoScan:1;
|
|
|
|
uint32_t canScan:1;
|
|
|
|
uint32_t runningScan:1;
|
|
|
|
uint32_t canConnect:1;
|
|
|
|
uint32_t willConnect:1;
|
|
|
|
uint32_t readingDone:1;
|
|
|
|
uint32_t shallSetTime:1;
|
|
|
|
uint32_t willSetTime:1;
|
|
|
|
uint32_t shallReadBatt:1;
|
|
|
|
uint32_t willReadBatt:1;
|
|
|
|
uint32_t shallSetUnit:1;
|
|
|
|
uint32_t willSetUnit:1;
|
|
|
|
uint32_t shallTriggerTele:1;
|
|
|
|
uint32_t triggeredTele:1;
|
2020-11-17 08:30:59 +00:00
|
|
|
uint32_t shallClearResults:1; // BLE scan results
|
2020-10-21 07:26:38 +01:00
|
|
|
uint32_t shallShowStatusInfo:1; // react to amount of found sensors via RULES
|
|
|
|
uint32_t firstAutodiscoveryDone:1;
|
2020-11-17 08:30:59 +00:00
|
|
|
uint32_t activeBeacon:1;
|
|
|
|
uint32_t shallShowScanResult:1;
|
|
|
|
uint32_t shallShowBlockList:1;
|
2020-10-21 07:26:38 +01:00
|
|
|
};
|
|
|
|
uint32_t all = 0;
|
2020-04-21 08:58:33 +01:00
|
|
|
} mode;
|
|
|
|
struct {
|
2020-10-23 09:18:57 +01:00
|
|
|
uint8_t sensor; // points to to the number 0...255
|
|
|
|
uint8_t beaconScanCounter; // countdown timer in seconds
|
2020-04-21 08:58:33 +01:00
|
|
|
} state;
|
2020-07-20 07:51:17 +01:00
|
|
|
struct {
|
2020-10-20 13:49:10 +01:00
|
|
|
uint32_t allwaysAggregate:1; // always show all known values of one sensor in brdigemode
|
|
|
|
uint32_t noSummary:1; // no sensor values at TELE-period
|
|
|
|
uint32_t directBridgeMode:1; // send every received BLE-packet as a MQTT-message in real-time
|
2020-10-21 07:26:38 +01:00
|
|
|
uint32_t holdBackFirstAutodiscovery:1; // allows to trigger it later
|
2020-07-20 07:51:17 +01:00
|
|
|
uint32_t showRSSI:1;
|
|
|
|
uint32_t ignoreBogusBattery:1;
|
2020-10-21 07:26:38 +01:00
|
|
|
uint32_t minimalSummary:1; // DEPRECATED!!
|
2020-07-20 07:51:17 +01:00
|
|
|
} option;
|
2020-04-21 08:58:33 +01:00
|
|
|
} MI32;
|
|
|
|
|
|
|
|
#pragma pack(1) // byte-aligned structures to read the sensor data
|
|
|
|
|
|
|
|
struct {
|
2020-11-22 09:44:02 +00:00
|
|
|
int16_t temp;
|
2020-04-21 08:58:33 +01:00
|
|
|
uint8_t hum;
|
|
|
|
uint16_t volt; // LYWSD03 only
|
|
|
|
} LYWSD0x_HT;
|
|
|
|
struct {
|
|
|
|
uint8_t spare;
|
2020-11-22 09:44:02 +00:00
|
|
|
int16_t temp;
|
2020-04-21 08:58:33 +01:00
|
|
|
uint16_t hum;
|
|
|
|
} CGD1_HT;
|
|
|
|
struct {
|
2020-11-22 09:44:02 +00:00
|
|
|
int16_t temp;
|
2020-04-21 08:58:33 +01:00
|
|
|
uint8_t spare;
|
|
|
|
uint32_t lux;
|
|
|
|
uint8_t moist;
|
|
|
|
uint16_t fert;
|
|
|
|
} Flora_TLMF; // temperature, lux, moisture, fertility
|
|
|
|
|
|
|
|
|
|
|
|
struct mi_beacon_t{
|
|
|
|
uint16_t frame;
|
|
|
|
uint16_t productID;
|
|
|
|
uint8_t counter;
|
2020-07-12 17:17:27 +01:00
|
|
|
uint8_t MAC[6];
|
2020-04-21 08:58:33 +01:00
|
|
|
uint8_t spare;
|
|
|
|
uint8_t type;
|
|
|
|
uint8_t ten;
|
|
|
|
uint8_t size;
|
|
|
|
union {
|
|
|
|
struct{ //0d
|
2020-11-22 09:44:02 +00:00
|
|
|
int16_t temp;
|
2020-04-21 08:58:33 +01:00
|
|
|
uint16_t hum;
|
|
|
|
}HT;
|
|
|
|
uint8_t bat; //0a
|
2020-11-22 09:44:02 +00:00
|
|
|
int16_t temp; //04
|
2020-04-21 08:58:33 +01:00
|
|
|
uint16_t hum; //06
|
|
|
|
uint32_t lux; //07
|
|
|
|
uint8_t moist; //08
|
|
|
|
uint16_t fert; //09
|
2020-07-12 17:17:27 +01:00
|
|
|
uint32_t NMT; //17
|
|
|
|
struct{ //01
|
|
|
|
uint16_t num;
|
2020-10-30 11:29:48 +00:00
|
|
|
uint8_t longPress;
|
|
|
|
}Btn;
|
2020-04-21 08:58:33 +01:00
|
|
|
};
|
2020-07-12 17:17:27 +01:00
|
|
|
uint8_t padding[12];
|
2020-04-21 08:58:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct cg_packet_t {
|
|
|
|
uint16_t frameID;
|
2020-07-12 17:17:27 +01:00
|
|
|
uint8_t MAC[6];
|
2020-04-21 08:58:33 +01:00
|
|
|
uint16_t mode;
|
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
int16_t temp; // -9 - 59 °C
|
|
|
|
uint16_t hum;
|
|
|
|
};
|
|
|
|
uint8_t bat;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-07-12 17:17:27 +01:00
|
|
|
struct encPacket_t{
|
|
|
|
// the packet is longer, but this part is enough to decrypt
|
|
|
|
uint16_t PID;
|
|
|
|
uint8_t frameCnt;
|
|
|
|
uint8_t MAC[6];
|
|
|
|
uint8_t payload[16]; // only a pointer to the address, size is variable
|
|
|
|
};
|
|
|
|
|
|
|
|
union mi_bindKey_t{
|
|
|
|
struct{
|
|
|
|
uint8_t key[16];
|
|
|
|
uint8_t MAC[6];
|
|
|
|
};
|
|
|
|
uint8_t buf[22];
|
|
|
|
};
|
|
|
|
|
2020-09-16 07:03:55 +01:00
|
|
|
struct ATCPacket_t{
|
|
|
|
uint8_t MAC[6];
|
2020-11-22 09:44:02 +00:00
|
|
|
uint16_t temp; //sadly this is in wrong endianess
|
2020-09-16 07:03:55 +01:00
|
|
|
uint8_t hum;
|
|
|
|
uint8_t batPer;
|
|
|
|
uint16_t batMV;
|
|
|
|
uint8_t frameCnt;
|
|
|
|
};
|
|
|
|
|
2020-04-21 08:58:33 +01:00
|
|
|
#pragma pack(0)
|
|
|
|
|
|
|
|
struct mi_sensor_t{
|
|
|
|
uint8_t type; //Flora = 1; MI-HT_V1=2; LYWSD02=3; LYWSD03=4; CGG1=5; CGD1=6
|
2020-07-12 17:17:27 +01:00
|
|
|
uint8_t lastCnt; //device generated counter of the packet
|
|
|
|
uint8_t shallSendMQTT;
|
|
|
|
uint8_t MAC[6];
|
2020-07-20 07:51:17 +01:00
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
uint32_t temp:1;
|
|
|
|
uint32_t hum:1;
|
|
|
|
uint32_t tempHum:1; //every hum sensor has temp too, easier to use Tasmota dew point functions
|
|
|
|
uint32_t lux:1;
|
|
|
|
uint32_t moist:1;
|
|
|
|
uint32_t fert:1;
|
|
|
|
uint32_t bat:1;
|
|
|
|
uint32_t NMT:1;
|
|
|
|
uint32_t PIR:1;
|
|
|
|
uint32_t Btn:1;
|
|
|
|
};
|
|
|
|
uint32_t raw;
|
|
|
|
} feature;
|
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
uint32_t temp:1;
|
|
|
|
uint32_t hum:1;
|
|
|
|
uint32_t tempHum:1; //can be combined from the sensor
|
|
|
|
uint32_t lux:1;
|
|
|
|
uint32_t moist:1;
|
|
|
|
uint32_t fert:1;
|
|
|
|
uint32_t bat:1;
|
|
|
|
uint32_t NMT:1;
|
|
|
|
uint32_t motion:1;
|
|
|
|
uint32_t noMotion:1;
|
|
|
|
uint32_t Btn:1;
|
|
|
|
};
|
|
|
|
uint32_t raw;
|
|
|
|
} eventType;
|
2020-10-30 11:29:48 +00:00
|
|
|
|
2020-10-23 09:18:57 +01:00
|
|
|
int RSSI;
|
2020-07-12 17:17:27 +01:00
|
|
|
uint32_t lastTime;
|
|
|
|
uint32_t lux;
|
2020-04-21 08:58:33 +01:00
|
|
|
float temp; //Flora, MJ_HT_V1, LYWSD0x, CGx
|
|
|
|
union {
|
|
|
|
struct {
|
2020-07-15 16:41:12 +01:00
|
|
|
uint8_t moisture;
|
|
|
|
uint16_t fertility;
|
2020-07-12 17:17:27 +01:00
|
|
|
char firmware[6]; // actually only for FLORA but hopefully we can add for more devices
|
2020-04-21 08:58:33 +01:00
|
|
|
}; // Flora
|
|
|
|
struct {
|
|
|
|
float hum;
|
|
|
|
}; // MJ_HT_V1, LYWSD0x
|
2020-07-12 17:17:27 +01:00
|
|
|
struct {
|
|
|
|
uint16_t events; //"alarms" since boot
|
|
|
|
uint32_t NMT; // no motion time in seconds for the MJYD2S
|
|
|
|
};
|
|
|
|
uint16_t Btn;
|
2020-04-21 08:58:33 +01:00
|
|
|
};
|
2020-07-12 17:17:27 +01:00
|
|
|
union {
|
|
|
|
uint8_t bat; // many values seem to be hard-coded garbage (LYWSD0x, GCD1)
|
2020-04-21 08:58:33 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-10-23 09:18:57 +01:00
|
|
|
struct scan_entry_t {
|
|
|
|
uint8_t MAC[6];
|
|
|
|
uint16_t CID;
|
|
|
|
uint16_t SVC;
|
|
|
|
uint16_t UUID;
|
|
|
|
int32_t RSSI;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct generic_beacon_t {
|
|
|
|
uint8_t MAC[6];
|
|
|
|
uint32_t time;
|
|
|
|
int32_t RSSI;
|
|
|
|
uint16_t CID; // company identifier
|
|
|
|
uint16_t UUID; // the first, if more than one exists
|
|
|
|
uint16_t SVC;
|
|
|
|
bool active = false;
|
|
|
|
};
|
|
|
|
|
2020-11-17 08:30:59 +00:00
|
|
|
struct MAC_t {
|
|
|
|
uint8_t buf[6];
|
|
|
|
};
|
|
|
|
|
2020-04-21 08:58:33 +01:00
|
|
|
std::vector<mi_sensor_t> MIBLEsensors;
|
2020-07-12 17:17:27 +01:00
|
|
|
std::vector<mi_bindKey_t> MIBLEbindKeys;
|
2020-10-23 09:18:57 +01:00
|
|
|
std::array<generic_beacon_t,4> MIBLEbeacons; // we support a fixed number
|
2020-11-17 08:30:59 +00:00
|
|
|
std::vector<scan_entry_t> MIBLEscanResult;
|
|
|
|
std::vector<MAC_t> MIBLEBlockList;
|
2020-07-12 17:17:27 +01:00
|
|
|
|
|
|
|
static BLEScan* MI32Scan;
|
2020-04-21 08:58:33 +01:00
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* constants
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define D_CMND_MI32 "MI32"
|
|
|
|
|
2020-11-13 16:00:44 +00:00
|
|
|
const char kMI32_Commands[] PROGMEM = D_CMND_MI32 "|"
|
|
|
|
#ifdef USE_MI_DECRYPTION
|
|
|
|
"Key|"
|
|
|
|
#endif // USE_MI_DECRYPTION
|
2020-11-17 08:30:59 +00:00
|
|
|
"Period|Time|Page|Battery|Unit|Beacon|Block|Option";
|
2020-11-13 16:00:44 +00:00
|
|
|
|
|
|
|
void (*const MI32_Commands[])(void) PROGMEM = {
|
|
|
|
#ifdef USE_MI_DECRYPTION
|
|
|
|
&CmndMi32Key,
|
|
|
|
#endif // USE_MI_DECRYPTION
|
2020-11-17 08:30:59 +00:00
|
|
|
&CmndMi32Period, &CmndMi32Time, &CmndMi32Page, &CmndMi32Battery, &CmndMi32Unit, &CmndMi32Beacon, &CmndMi32Block, &CmndMi32Option };
|
2020-04-21 08:58:33 +01:00
|
|
|
|
|
|
|
#define FLORA 1
|
|
|
|
#define MJ_HT_V1 2
|
|
|
|
#define LYWSD02 3
|
|
|
|
#define LYWSD03MMC 4
|
|
|
|
#define CGG1 5
|
|
|
|
#define CGD1 6
|
2020-07-12 17:17:27 +01:00
|
|
|
#define NLIGHT 7
|
|
|
|
#define MJYD2S 8
|
|
|
|
#define YEERC 9
|
2020-07-15 16:41:12 +01:00
|
|
|
#define MHOC401 10
|
2020-08-02 14:07:18 +01:00
|
|
|
#define MHOC303 11
|
2020-09-16 07:03:55 +01:00
|
|
|
#define ATC 12
|
2020-07-15 16:41:12 +01:00
|
|
|
|
2020-09-16 07:03:55 +01:00
|
|
|
#define MI32_TYPES 12 //count this manually
|
2020-04-21 08:58:33 +01:00
|
|
|
|
2020-08-02 14:07:18 +01:00
|
|
|
const uint16_t kMI32DeviceID[MI32_TYPES]={ 0x0098, // Flora
|
2020-04-21 08:58:33 +01:00
|
|
|
0x01aa, // MJ_HT_V1
|
|
|
|
0x045b, // LYWSD02
|
|
|
|
0x055b, // LYWSD03
|
|
|
|
0x0347, // CGG1
|
2020-07-12 17:17:27 +01:00
|
|
|
0x0576, // CGD1
|
|
|
|
0x03dd, // NLIGHT
|
|
|
|
0x07f6, // MJYD2S
|
2020-07-15 16:41:12 +01:00
|
|
|
0x0153, // yee-rc
|
2020-08-02 14:07:18 +01:00
|
|
|
0x0387, // MHO-C401
|
2020-09-16 07:03:55 +01:00
|
|
|
0x06d3, // MHO-C303
|
|
|
|
0x0a1c // ATC -> this is a fake ID
|
2020-04-21 08:58:33 +01:00
|
|
|
};
|
|
|
|
|
2020-07-12 17:17:27 +01:00
|
|
|
const char kMI32DeviceType1[] PROGMEM = "Flora";
|
|
|
|
const char kMI32DeviceType2[] PROGMEM = "MJ_HT_V1";
|
|
|
|
const char kMI32DeviceType3[] PROGMEM = "LYWSD02";
|
|
|
|
const char kMI32DeviceType4[] PROGMEM = "LYWSD03";
|
|
|
|
const char kMI32DeviceType5[] PROGMEM = "CGG1";
|
|
|
|
const char kMI32DeviceType6[] PROGMEM = "CGD1";
|
|
|
|
const char kMI32DeviceType7[] PROGMEM = "NLIGHT";
|
|
|
|
const char kMI32DeviceType8[] PROGMEM = "MJYD2S";
|
|
|
|
const char kMI32DeviceType9[] PROGMEM = "YEERC";
|
2020-07-15 16:41:12 +01:00
|
|
|
const char kMI32DeviceType10[] PROGMEM ="MHOC401";
|
2020-08-02 14:07:18 +01:00
|
|
|
const char kMI32DeviceType11[] PROGMEM ="MHOC303";
|
2020-09-16 07:03:55 +01:00
|
|
|
const char kMI32DeviceType12[] PROGMEM ="ATC";
|
|
|
|
const char * kMI32DeviceType[] PROGMEM = {kMI32DeviceType1,kMI32DeviceType2,kMI32DeviceType3,kMI32DeviceType4,kMI32DeviceType5,kMI32DeviceType6,kMI32DeviceType7,kMI32DeviceType8,kMI32DeviceType9,kMI32DeviceType10,kMI32DeviceType11,kMI32DeviceType12};
|
2020-04-21 08:58:33 +01:00
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* enumerations
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
enum MI32_Commands { // commands useable in console or rules
|
|
|
|
CMND_MI32_PERIOD, // set period like TELE-period in seconds between read-cycles
|
|
|
|
CMND_MI32_TIME, // set LYWSD02-Time from ESP8266-time
|
|
|
|
CMND_MI32_PAGE, // sensor entries per web page, which will be shown alternated
|
2020-05-29 19:18:39 +01:00
|
|
|
CMND_MI32_BATTERY, // read all battery levels
|
2020-07-12 17:17:27 +01:00
|
|
|
CMND_MI32_UNIT, // toggles the displayed unit between C/F (LYWSD02)
|
2020-10-23 09:18:57 +01:00
|
|
|
CMND_MI32_KEY, // add bind key to a mac for packet decryption
|
2020-11-17 08:30:59 +00:00
|
|
|
CMND_MI32_BEACON, // add up to 4 beacons defined by their MAC addresses
|
|
|
|
CMND_MI32_BLOCK, // block BLE sensors defined by their MAC addresses
|
|
|
|
CMND_MI32_OPTION // change driver options at runtime
|
2020-04-21 08:58:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
enum MI32_TASK {
|
|
|
|
MI32_TASK_SCAN = 0,
|
|
|
|
MI32_TASK_CONN = 1,
|
|
|
|
MI32_TASK_TIME = 2,
|
|
|
|
MI32_TASK_BATT = 3,
|
2020-05-29 19:18:39 +01:00
|
|
|
MI32_TASK_UNIT = 4,
|
2020-04-21 08:58:33 +01:00
|
|
|
};
|
|
|
|
|
2020-10-23 09:18:57 +01:00
|
|
|
enum MI32_BEACON_CMND {
|
|
|
|
MI32_BEACON_ON = 0,
|
|
|
|
MI32_BEACON_OFF = 1,
|
|
|
|
MI32_BEACON_DEL = 2,
|
|
|
|
};
|
|
|
|
|
2020-04-21 08:58:33 +01:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Classes
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
class MI32SensorCallback : public NimBLEClientCallbacks {
|
|
|
|
void onConnect(NimBLEClient* pclient) {
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG,PSTR("connected %s"), kMI32DeviceType[(MIBLEsensors[MI32.state.sensor].type)-1]);
|
2020-04-21 08:58:33 +01:00
|
|
|
MI32.mode.willConnect = 0;
|
|
|
|
MI32.mode.connected = 1;
|
|
|
|
}
|
|
|
|
void onDisconnect(NimBLEClient* pclient) {
|
|
|
|
MI32.mode.connected = 0;
|
2020-11-02 18:32:08 +00:00
|
|
|
MI32.mode.willReadBatt = 0;
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG,PSTR("disconnected %s"), kMI32DeviceType[(MIBLEsensors[MI32.state.sensor].type)-1]);
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
bool onConnParamsUpdateRequest(NimBLEClient* MI32Client, const ble_gap_upd_params* params) {
|
|
|
|
if(params->itvl_min < 24) { /** 1.25ms units */
|
|
|
|
return false;
|
|
|
|
} else if(params->itvl_max > 40) { /** 1.25ms units */
|
|
|
|
return false;
|
|
|
|
} else if(params->latency > 2) { /** Number of intervals allowed to skip */
|
|
|
|
return false;
|
|
|
|
} else if(params->supervision_timeout > 100) { /** 10ms units */
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-21 10:06:30 +01:00
|
|
|
return true;
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class MI32AdvCallbacks: public NimBLEAdvertisedDeviceCallbacks {
|
|
|
|
void onResult(NimBLEAdvertisedDevice* advertisedDevice) {
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("Advertised Device: %s Buffer: %u"),advertisedDevice->getAddress().toString().c_str(),advertisedDevice->getServiceData(0).length());
|
2020-10-23 09:18:57 +01:00
|
|
|
int RSSI = advertisedDevice->getRSSI();
|
2020-04-21 08:58:33 +01:00
|
|
|
uint8_t addr[6];
|
|
|
|
memcpy(addr,advertisedDevice->getAddress().getNative(),6);
|
|
|
|
MI32_ReverseMAC(addr);
|
2020-10-23 09:18:57 +01:00
|
|
|
if (advertisedDevice->getServiceDataCount() == 0) {
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("No Xiaomi Device: %s Buffer: %u"),advertisedDevice->getAddress().toString().c_str(),advertisedDevice->getServiceData(0).length());
|
2020-10-23 09:18:57 +01:00
|
|
|
if(MI32.state.beaconScanCounter==0 && !MI32.mode.activeBeacon){
|
|
|
|
MI32Scan->erase(advertisedDevice->getAddress());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
MI32HandleGenericBeacon(advertisedDevice->getPayload(), advertisedDevice->getPayloadLength(), RSSI, addr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-11 21:09:26 +01:00
|
|
|
}
|
2020-10-23 09:18:57 +01:00
|
|
|
uint16_t UUID = advertisedDevice->getServiceDataUUID(0).getNative()->u16.value;
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("UUID: %x"),UUID);
|
2020-10-23 09:18:57 +01:00
|
|
|
|
|
|
|
size_t ServiceDataLength = advertisedDevice->getServiceData(0).length();
|
|
|
|
if(UUID==0xfe95) {
|
2020-11-17 08:30:59 +00:00
|
|
|
if(MI32isInBlockList(addr) == true) return;
|
2020-10-23 09:18:57 +01:00
|
|
|
MI32ParseResponse((char*)advertisedDevice->getServiceData(0).data(),ServiceDataLength, addr, RSSI);
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
2020-10-23 09:18:57 +01:00
|
|
|
else if(UUID==0xfdcd) {
|
2020-11-17 08:30:59 +00:00
|
|
|
if(MI32isInBlockList(addr) == true) return;
|
2020-10-23 09:18:57 +01:00
|
|
|
MI32parseCGD1Packet((char*)advertisedDevice->getServiceData(0).data(),ServiceDataLength, addr, RSSI);
|
2020-05-29 23:41:49 +01:00
|
|
|
}
|
2020-10-23 09:18:57 +01:00
|
|
|
else if(UUID==0x181a) { //ATC
|
2020-11-17 08:30:59 +00:00
|
|
|
if(MI32isInBlockList(addr) == true) return;
|
2020-10-23 09:18:57 +01:00
|
|
|
MI32ParseATCPacket((char*)advertisedDevice->getServiceData(0).data(),ServiceDataLength, addr, RSSI);
|
2020-09-16 07:03:55 +01:00
|
|
|
}
|
2020-05-29 23:41:49 +01:00
|
|
|
else {
|
2020-10-23 09:18:57 +01:00
|
|
|
if(MI32.state.beaconScanCounter!=0 || MI32.mode.activeBeacon){
|
|
|
|
MI32HandleGenericBeacon(advertisedDevice->getPayload(), advertisedDevice->getPayloadLength(), RSSI, addr);
|
|
|
|
}
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("No Xiaomi Device: %x: %s Buffer: %u"), UUID, advertisedDevice->getAddress().toString().c_str(),advertisedDevice->getServiceData(0).length());
|
2020-07-12 17:17:27 +01:00
|
|
|
MI32Scan->erase(advertisedDevice->getAddress());
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static MI32AdvCallbacks MI32ScanCallbacks;
|
|
|
|
static MI32SensorCallback MI32SensorCB;
|
|
|
|
static NimBLEClient* MI32Client;
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* BLE callback functions
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
void MI32scanEndedCB(NimBLEScanResults results){
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG,PSTR("Scan ended"));
|
2020-04-21 08:58:33 +01:00
|
|
|
MI32.mode.runningScan = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MI32notifyCB(NimBLERemoteCharacteristic* pRemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify){
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG,PSTR("Notified length: %u"),length);
|
2020-04-21 08:58:33 +01:00
|
|
|
switch(MIBLEsensors[MI32.state.sensor].type){
|
2020-07-15 16:41:12 +01:00
|
|
|
case LYWSD03MMC: case LYWSD02: case MHOC401:
|
2020-04-21 08:58:33 +01:00
|
|
|
MI32readHT_LY((char*)pData);
|
|
|
|
MI32.mode.readingDone = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MI32.mode.readingDone = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Helper functions
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2020-10-23 09:18:57 +01:00
|
|
|
/**
|
|
|
|
* @brief Remove all colons from null terminated char array
|
2020-10-30 11:29:48 +00:00
|
|
|
*
|
2020-10-23 09:18:57 +01:00
|
|
|
* @param _string Typically representing a MAC-address like AA:BB:CC:DD:EE:FF
|
|
|
|
*/
|
|
|
|
void MI32stripColon(char* _string){
|
|
|
|
uint32_t _length = strlen(_string);
|
|
|
|
uint32_t _index = 0;
|
|
|
|
while (_index < _length) {
|
|
|
|
char c = _string[_index];
|
|
|
|
if(c==':'){
|
|
|
|
memmove(_string+_index,_string+_index+1,_length-_index);
|
|
|
|
}
|
|
|
|
_index++;
|
|
|
|
}
|
|
|
|
_string[_index] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Convert string that repesents a hexadecimal number to a byte array
|
|
|
|
*
|
|
|
|
* @param _string input string in format: AABBCCDDEEFF or AA:BB:CC:DD:EE:FF, caseinsensitive
|
|
|
|
* @param _mac target byte array must match the correct size (i.e. AA:BB -> uint8_t bytes[2])
|
|
|
|
*/
|
|
|
|
|
|
|
|
void MI32HexStringToBytes(char* _string, uint8_t* _byteArray) {
|
|
|
|
MI32stripColon(_string);
|
|
|
|
UpperCase(_string,_string);
|
|
|
|
uint32_t index = 0;
|
|
|
|
uint32_t _end = strlen(_string);
|
|
|
|
memset(_byteArray,0,_end/2);
|
|
|
|
while (index < _end) {
|
|
|
|
char c = _string[index];
|
|
|
|
uint8_t value = 0;
|
|
|
|
if(c >= '0' && c <= '9')
|
|
|
|
value = (c - '0');
|
|
|
|
else if (c >= 'A' && c <= 'F')
|
|
|
|
value = (10 + (c - 'A'));
|
|
|
|
_byteArray[(index/2)] += value << (((index + 1) % 2) * 4);
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Reverse an array of 6 bytes
|
2020-10-30 11:29:48 +00:00
|
|
|
*
|
2020-10-23 09:18:57 +01:00
|
|
|
* @param _mac a byte array of size 6 (typicalliy representing a MAC address)
|
|
|
|
*/
|
2020-04-21 08:58:33 +01:00
|
|
|
void MI32_ReverseMAC(uint8_t _mac[]){
|
|
|
|
uint8_t _reversedMAC[6];
|
|
|
|
for (uint8_t i=0; i<6; i++){
|
|
|
|
_reversedMAC[5-i] = _mac[i];
|
|
|
|
}
|
|
|
|
memcpy(_mac,_reversedMAC, sizeof(_reversedMAC));
|
|
|
|
}
|
|
|
|
|
2020-07-12 17:17:27 +01:00
|
|
|
#ifdef USE_MI_DECRYPTION
|
|
|
|
void MI32AddKey(char* payload){
|
|
|
|
mi_bindKey_t keyMAC;
|
2020-10-23 09:18:57 +01:00
|
|
|
MI32HexStringToBytes(payload,keyMAC.buf);
|
2020-07-12 17:17:27 +01:00
|
|
|
bool unknownKey = true;
|
|
|
|
for(uint32_t i=0; i<MIBLEbindKeys.size(); i++){
|
|
|
|
if(memcmp(keyMAC.MAC,MIBLEbindKeys[i].MAC,sizeof(keyMAC.MAC))==0){
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG,PSTR("known key"));
|
2020-07-12 17:17:27 +01:00
|
|
|
unknownKey=false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(unknownKey){
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG,PSTR("New key"));
|
2020-07-12 17:17:27 +01:00
|
|
|
MIBLEbindKeys.push_back(keyMAC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Decrypts payload in place
|
2020-10-30 11:29:48 +00:00
|
|
|
*
|
2020-07-12 17:17:27 +01:00
|
|
|
* @param _buf - pointer to the buffer at position of PID
|
2020-10-20 13:49:10 +01:00
|
|
|
* @param _bufSize - buffersize (last position is two bytes behind last byte of TAG)
|
2020-07-12 17:17:27 +01:00
|
|
|
* @param _type - sensor type
|
|
|
|
* @return int - error code, 0 for success
|
|
|
|
*/
|
|
|
|
int MI32_decryptPacket(char *_buf, uint16_t _bufSize, uint32_t _type){
|
|
|
|
encPacket_t *packet = (encPacket_t*)_buf;
|
2020-10-20 13:49:10 +01:00
|
|
|
uint8_t payload[8];
|
|
|
|
size_t data_len = _bufSize - 9 - 4 - 3 - 1 - 1 ; // _bufsize - header - tag - ext.counter - RSSI - spare(?)
|
2020-07-12 17:17:27 +01:00
|
|
|
int ret = 0;
|
|
|
|
uint8_t nonce[12];
|
2020-10-20 13:49:10 +01:00
|
|
|
uint32_t tag;
|
2020-07-12 17:17:27 +01:00
|
|
|
const unsigned char authData[1] = {0x11};
|
|
|
|
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("Decrypt Size of Buffer: %u, payload length: %u"), _bufSize, data_len);
|
2020-10-20 13:49:10 +01:00
|
|
|
// AddLogBuffer(LOG_LEVEL_DEBUG,(uint8_t*)_buf, _bufSize);
|
|
|
|
|
2020-07-12 17:17:27 +01:00
|
|
|
// nonce: device MAC, device type, frame cnt, ext. cnt
|
|
|
|
for (uint32_t i = 0; i<6; i++){
|
|
|
|
nonce[i] = packet->MAC[i];
|
|
|
|
}
|
|
|
|
memcpy((uint8_t*)&nonce+6,(uint8_t*)&packet->PID,2);
|
|
|
|
nonce[8] = packet->frameCnt;
|
2020-10-20 13:49:10 +01:00
|
|
|
memcpy((uint8_t*)&nonce+9,(uint8_t*)&_buf[_bufSize-9],3);
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("nonceCnt1 and 2: %02x %02x %02x"),nonce[9],nonce[10],nonce[11]);
|
2020-10-20 13:49:10 +01:00
|
|
|
memcpy((uint8_t*)&tag,(uint8_t*)&_buf[_bufSize-6],4);
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("tag: %02x %02x %02x %02x"),tag[0],tag[1],tag[2],tag[3]);
|
2020-07-12 17:17:27 +01:00
|
|
|
|
|
|
|
MI32_ReverseMAC(packet->MAC);
|
|
|
|
uint8_t _bindkey[16] = {0x0};
|
2020-10-20 13:49:10 +01:00
|
|
|
bool foundNoKey = true;
|
2021-01-14 16:03:01 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG,PSTR("M32: Search key for MAC: %02x %02x %02x %02x %02x %02x"), packet->MAC[0], packet->MAC[1], packet->MAC[2], packet->MAC[3], packet->MAC[4], packet->MAC[5]);
|
2020-07-12 17:17:27 +01:00
|
|
|
for(uint32_t i=0; i<MIBLEbindKeys.size(); i++){
|
|
|
|
if(memcmp(packet->MAC,MIBLEbindKeys[i].MAC,sizeof(packet->MAC))==0){
|
|
|
|
memcpy(_bindkey,MIBLEbindKeys[i].key,sizeof(_bindkey));
|
2021-01-14 16:03:01 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG,PSTR("M32: Decryption Key found"));
|
2020-10-20 13:49:10 +01:00
|
|
|
foundNoKey = false;
|
|
|
|
break;
|
2020-07-12 17:17:27 +01:00
|
|
|
}
|
2020-10-20 13:49:10 +01:00
|
|
|
}
|
|
|
|
if(foundNoKey){
|
2021-01-14 16:03:01 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG,PSTR("M32: No Key found !!"));
|
2020-10-20 13:49:10 +01:00
|
|
|
return -2;
|
2020-07-12 17:17:27 +01:00
|
|
|
}
|
|
|
|
|
2020-10-20 13:49:10 +01:00
|
|
|
br_aes_small_ctrcbc_keys keyCtx;
|
|
|
|
br_aes_small_ctrcbc_init(&keyCtx, _bindkey, sizeof(_bindkey));
|
|
|
|
|
|
|
|
br_ccm_context ctx;
|
|
|
|
br_ccm_init(&ctx, &keyCtx.vtable);
|
|
|
|
br_ccm_reset(&ctx, nonce, sizeof(nonce), sizeof(authData), data_len, sizeof(tag));
|
|
|
|
br_ccm_aad_inject(&ctx, authData, sizeof(authData));
|
|
|
|
br_ccm_flip(&ctx);
|
|
|
|
|
|
|
|
|
|
|
|
memcpy(payload,packet->payload,data_len); //we want to be sure about 4-byte alignement
|
|
|
|
br_ccm_run(&ctx, 0, payload, data_len);
|
|
|
|
memcpy((uint8_t*)packet->payload+1,payload,data_len); //back to the packet
|
|
|
|
|
|
|
|
ret = br_ccm_check_tag(&ctx, &tag);
|
|
|
|
|
2021-01-14 16:03:01 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG,PSTR("M32: Err:%i, Decrypted : %02x %02x %02x %02x %02x "), ret, packet->payload[1],packet->payload[2],packet->payload[3],packet->payload[4],packet->payload[5]);
|
2020-10-20 13:49:10 +01:00
|
|
|
return ret-1;
|
2020-07-12 17:17:27 +01:00
|
|
|
}
|
|
|
|
#endif // USE_MI_DECRYPTION
|
|
|
|
|
2020-10-21 07:26:38 +01:00
|
|
|
#ifdef USE_HOME_ASSISTANT
|
|
|
|
/**
|
|
|
|
* @brief For HASS only, changes last entry of JSON in mqtt_data to 'null'
|
|
|
|
*/
|
|
|
|
|
|
|
|
void MI32nullifyEndOfMQTT_DATA(){
|
2020-10-30 11:29:48 +00:00
|
|
|
char *p = TasmotaGlobal.mqtt_data + strlen(TasmotaGlobal.mqtt_data);
|
2020-10-21 07:26:38 +01:00
|
|
|
while(true){
|
|
|
|
*p--;
|
|
|
|
if(p[0]==':'){
|
|
|
|
p[1] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ResponseAppend_P(PSTR("null"));
|
|
|
|
}
|
|
|
|
#endif // USE_HOME_ASSISTANT
|
|
|
|
|
2020-04-21 08:58:33 +01:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* common functions
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Return the slot number of a known sensor or return create new sensor slot
|
|
|
|
*
|
2020-07-12 17:17:27 +01:00
|
|
|
* @param _MAC BLE address of the sensor
|
2020-04-21 08:58:33 +01:00
|
|
|
* @param _type Type number of the sensor
|
|
|
|
* @return uint32_t Known or new slot in the sensors-vector
|
|
|
|
*/
|
2020-07-12 17:17:27 +01:00
|
|
|
uint32_t MIBLEgetSensorSlot(uint8_t (&_MAC)[6], uint16_t _type, uint8_t counter){
|
2020-04-21 08:58:33 +01:00
|
|
|
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("%s: will test ID-type: %x"),D_CMND_MI32, _type);
|
|
|
|
bool _success = false;
|
2020-08-02 14:07:18 +01:00
|
|
|
for (uint32_t i=0;i<MI32_TYPES;i++){ // i < sizeof(kMI32DeviceID) gives compiler warning
|
2020-07-12 17:17:27 +01:00
|
|
|
if(_type == kMI32DeviceID[i]){
|
2021-01-14 16:03:01 +00:00
|
|
|
DEBUG_SENSOR_LOG(PSTR("M32: ID is type %u"), i);
|
2020-04-21 08:58:33 +01:00
|
|
|
_type = i+1;
|
|
|
|
_success = true;
|
|
|
|
}
|
|
|
|
else {
|
2020-07-12 17:17:27 +01:00
|
|
|
DEBUG_SENSOR_LOG(PSTR("%s: ID-type is not: %x"),D_CMND_MI32,kMI32DeviceID[i]);
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!_success) return 0xff;
|
2020-04-21 10:06:30 +01:00
|
|
|
|
2020-04-21 08:58:33 +01:00
|
|
|
DEBUG_SENSOR_LOG(PSTR("%s: vector size %u"),D_CMND_MI32, MIBLEsensors.size());
|
|
|
|
for(uint32_t i=0; i<MIBLEsensors.size(); i++){
|
2020-07-12 17:17:27 +01:00
|
|
|
if(memcmp(_MAC,MIBLEsensors[i].MAC,sizeof(_MAC))==0){
|
2020-04-21 08:58:33 +01:00
|
|
|
DEBUG_SENSOR_LOG(PSTR("%s: known sensor at slot: %u"),D_CMND_MI32, i);
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("Counters: %x %x"),MIBLEsensors[i].lastCnt, counter);
|
2020-07-12 17:17:27 +01:00
|
|
|
if(MIBLEsensors[i].lastCnt==counter) {
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("Old packet"));
|
2020-07-12 17:17:27 +01:00
|
|
|
return 0xff; // packet received before, stop here
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
2020-07-12 17:17:27 +01:00
|
|
|
DEBUG_SENSOR_LOG(PSTR("%s: i: %x %x %x %x %x %x"),D_CMND_MI32, MIBLEsensors[i].MAC[5], MIBLEsensors[i].MAC[4],MIBLEsensors[i].MAC[3],MIBLEsensors[i].MAC[2],MIBLEsensors[i].MAC[1],MIBLEsensors[i].MAC[0]);
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("%s: n: %x %x %x %x %x %x"),D_CMND_MI32, _MAC[5], _MAC[4], _MAC[3],_MAC[2],_MAC[1],_MAC[0]);
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("%s: found new sensor"),D_CMND_MI32);
|
|
|
|
mi_sensor_t _newSensor;
|
2020-07-12 17:17:27 +01:00
|
|
|
memcpy(_newSensor.MAC,_MAC, sizeof(_MAC));
|
2020-04-21 08:58:33 +01:00
|
|
|
_newSensor.type = _type;
|
2020-07-20 07:51:17 +01:00
|
|
|
_newSensor.eventType.raw = 0;
|
|
|
|
_newSensor.feature.raw = 0;
|
2020-04-21 08:58:33 +01:00
|
|
|
_newSensor.temp =NAN;
|
|
|
|
_newSensor.bat=0x00;
|
2020-10-23 09:18:57 +01:00
|
|
|
_newSensor.RSSI=0xffff;
|
2020-07-12 17:17:27 +01:00
|
|
|
_newSensor.lux = 0x00ffffff;
|
2020-04-21 08:58:33 +01:00
|
|
|
switch (_type)
|
|
|
|
{
|
2020-07-12 17:17:27 +01:00
|
|
|
case FLORA:
|
2020-07-15 16:41:12 +01:00
|
|
|
_newSensor.moisture =0xff;
|
|
|
|
_newSensor.fertility =0xffff;
|
2020-07-12 20:19:40 +01:00
|
|
|
_newSensor.firmware[0]='\0';
|
2020-07-20 07:51:17 +01:00
|
|
|
_newSensor.feature.temp=1;
|
|
|
|
_newSensor.feature.moist=1;
|
|
|
|
_newSensor.feature.fert=1;
|
|
|
|
_newSensor.feature.lux=1;
|
|
|
|
_newSensor.feature.bat=1;
|
|
|
|
break;
|
2020-10-30 11:29:48 +00:00
|
|
|
case NLIGHT:
|
2020-07-20 07:51:17 +01:00
|
|
|
_newSensor.events=0x00;
|
|
|
|
_newSensor.feature.PIR=1;
|
|
|
|
_newSensor.feature.NMT=1;
|
2020-04-21 08:58:33 +01:00
|
|
|
break;
|
2020-07-20 07:51:17 +01:00
|
|
|
case MJYD2S:
|
2020-07-12 17:17:27 +01:00
|
|
|
_newSensor.NMT=0;
|
|
|
|
_newSensor.events=0x00;
|
2020-07-20 07:51:17 +01:00
|
|
|
_newSensor.feature.PIR=1;
|
|
|
|
_newSensor.feature.NMT=1;
|
|
|
|
_newSensor.feature.lux=1;
|
|
|
|
_newSensor.feature.bat=1;
|
|
|
|
break;
|
|
|
|
case YEERC:
|
|
|
|
_newSensor.feature.Btn=1;
|
2020-04-21 08:58:33 +01:00
|
|
|
break;
|
2020-07-15 16:41:12 +01:00
|
|
|
default:
|
|
|
|
_newSensor.hum=NAN;
|
2020-07-20 07:51:17 +01:00
|
|
|
_newSensor.feature.temp=1;
|
|
|
|
_newSensor.feature.hum=1;
|
|
|
|
_newSensor.feature.tempHum=1;
|
|
|
|
_newSensor.feature.bat=1;
|
2020-07-15 16:41:12 +01:00
|
|
|
break;
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
MIBLEsensors.push_back(_newSensor);
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG,PSTR("%s: new %s at slot: %u"),D_CMND_MI32, kMI32DeviceType[_type-1],MIBLEsensors.size()-1);
|
2020-10-21 07:26:38 +01:00
|
|
|
MI32.mode.shallShowStatusInfo = 1;
|
2020-04-21 08:58:33 +01:00
|
|
|
return MIBLEsensors.size()-1;
|
|
|
|
};
|
|
|
|
|
2020-07-12 17:17:27 +01:00
|
|
|
/**
|
|
|
|
* @brief trigger real-time message for PIR or RC
|
2020-10-30 11:29:48 +00:00
|
|
|
*
|
2020-07-12 17:17:27 +01:00
|
|
|
*/
|
|
|
|
void MI32triggerTele(void){
|
2020-07-20 07:51:17 +01:00
|
|
|
MI32.mode.triggeredTele = 1;
|
2020-10-30 11:29:48 +00:00
|
|
|
ResponseClear();
|
2020-07-12 17:17:27 +01:00
|
|
|
if (MqttShowSensor()) {
|
|
|
|
MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_SENSOR), Settings.flag.mqtt_sensor_retain);
|
|
|
|
#ifdef USE_RULES
|
|
|
|
RulesTeleperiod(); // Allow rule based HA messages
|
|
|
|
#endif // USE_RULES
|
|
|
|
}
|
|
|
|
}
|
2020-10-21 07:26:38 +01:00
|
|
|
|
2020-10-23 09:18:57 +01:00
|
|
|
/**
|
|
|
|
* @brief Is called after every finding of new BLE sensor
|
2020-10-30 11:29:48 +00:00
|
|
|
*
|
2020-10-23 09:18:57 +01:00
|
|
|
*/
|
2020-10-21 07:26:38 +01:00
|
|
|
void MI32StatusInfo() {
|
|
|
|
MI32.mode.shallShowStatusInfo = 0;
|
|
|
|
Response_P(PSTR("{\"%s\":{\"found\":%u}}"), D_CMND_MI32, MIBLEsensors.size());
|
|
|
|
XdrvRulesProcess();
|
|
|
|
}
|
|
|
|
|
2020-04-21 08:58:33 +01:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* init NimBLE
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2020-11-09 19:27:11 +00:00
|
|
|
void MI32PreInit(void) {
|
|
|
|
MIBLEsensors.reserve(10);
|
|
|
|
MIBLEbindKeys.reserve(10);
|
2020-11-17 08:30:59 +00:00
|
|
|
MIBLEscanResult.reserve(20);
|
2020-11-09 19:27:11 +00:00
|
|
|
MI32.mode.init = false;
|
|
|
|
|
|
|
|
//test section for options
|
|
|
|
MI32.option.allwaysAggregate = 1;
|
|
|
|
MI32.option.noSummary = 0;
|
|
|
|
MI32.option.minimalSummary = 0;
|
|
|
|
MI32.option.directBridgeMode = 0;
|
|
|
|
MI32.option.showRSSI = 1;
|
|
|
|
MI32.option.ignoreBogusBattery = 1; // from advertisements
|
|
|
|
MI32.option.holdBackFirstAutodiscovery = 1;
|
2021-01-14 16:03:01 +00:00
|
|
|
AddLog_P(LOG_LEVEL_INFO,PSTR("M32: pre-init"));
|
2020-11-09 19:27:11 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 08:58:33 +01:00
|
|
|
void MI32Init(void) {
|
2020-11-13 16:00:44 +00:00
|
|
|
if (MI32.mode.init) { return; }
|
|
|
|
|
|
|
|
if (TasmotaGlobal.global_state.wifi_down) { return; }
|
2021-01-14 16:03:01 +00:00
|
|
|
|
|
|
|
TasmotaGlobal.wifi_stay_asleep = true;
|
2020-11-13 16:00:44 +00:00
|
|
|
if (WiFi.getSleep() == false) {
|
2021-01-14 16:03:01 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG,PSTR("M32: Put WiFi modem in sleep mode"));
|
|
|
|
WiFi.setSleep(true); // Sleep
|
2020-11-11 17:02:39 +00:00
|
|
|
}
|
2020-11-13 16:00:44 +00:00
|
|
|
|
2020-04-21 08:58:33 +01:00
|
|
|
if (!MI32.mode.init) {
|
|
|
|
NimBLEDevice::init("");
|
2021-01-14 16:03:01 +00:00
|
|
|
AddLog_P(LOG_LEVEL_INFO,PSTR("M32: Init BLE device"));
|
2020-04-21 08:58:33 +01:00
|
|
|
MI32.mode.canScan = 1;
|
|
|
|
MI32.mode.init = 1;
|
|
|
|
MI32.period = Settings.tele_period;
|
|
|
|
|
|
|
|
MI32StartScanTask(); // Let's get started !!
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Task section
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
void MI32StartTask(uint32_t task){
|
|
|
|
switch(task){
|
|
|
|
case MI32_TASK_SCAN:
|
|
|
|
if (MI32.mode.canScan == 0 || MI32.mode.willConnect == 1) return;
|
|
|
|
if (MI32.mode.runningScan == 1 || MI32.mode.connected == 1) return;
|
|
|
|
MI32StartScanTask();
|
|
|
|
break;
|
|
|
|
case MI32_TASK_CONN:
|
|
|
|
if (MI32.mode.canConnect == 0 || MI32.mode.willConnect == 1 ) return;
|
2020-07-12 17:17:27 +01:00
|
|
|
if (MI32.mode.connected == 1) return;
|
2020-04-21 08:58:33 +01:00
|
|
|
MI32StartSensorTask();
|
|
|
|
break;
|
|
|
|
case MI32_TASK_TIME:
|
|
|
|
if (MI32.mode.shallSetTime == 0) return;
|
|
|
|
MI32StartTimeTask();
|
|
|
|
break;
|
|
|
|
case MI32_TASK_BATT:
|
|
|
|
if (MI32.mode.willReadBatt == 1) return;
|
2020-07-20 07:51:17 +01:00
|
|
|
switch(MIBLEsensors[MI32.state.sensor].type) {
|
|
|
|
case LYWSD03MMC: case MHOC401: // the "original" battery value is crap ...
|
|
|
|
MI32.mode.willReadBatt = 1;
|
|
|
|
MI32StartSensorTask(); // ... but the part of the temp/hum-message is good!
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MI32StartBatteryTask();
|
|
|
|
}
|
2020-04-21 08:58:33 +01:00
|
|
|
break;
|
2020-05-29 19:18:39 +01:00
|
|
|
case MI32_TASK_UNIT:
|
|
|
|
if (MI32.mode.shallSetUnit == 0) return;
|
|
|
|
MI32StartUnitTask();
|
|
|
|
break;
|
2020-04-21 08:58:33 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-29 23:41:49 +01:00
|
|
|
bool MI32ConnectActiveSensor(){ // only use inside a task !!
|
|
|
|
MI32.mode.connected = 0;
|
2020-07-12 17:17:27 +01:00
|
|
|
|
|
|
|
NimBLEAddress _address = NimBLEAddress(MIBLEsensors[MI32.state.sensor].MAC);
|
2020-04-21 08:58:33 +01:00
|
|
|
if(NimBLEDevice::getClientListSize()) {
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("%s: found any clients in the list"),D_CMND_MI32);
|
2020-05-29 23:41:49 +01:00
|
|
|
MI32Client = NimBLEDevice::getClientByPeerAddress(_address);
|
2020-04-21 08:58:33 +01:00
|
|
|
if(MI32Client){
|
2020-05-29 23:41:49 +01:00
|
|
|
// Should be impossible
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("%s: got connected client"),D_CMND_MI32);
|
2020-04-21 10:06:30 +01:00
|
|
|
}
|
2020-04-21 08:58:33 +01:00
|
|
|
else {
|
2020-05-29 23:41:49 +01:00
|
|
|
// Should be the norm after the first iteration
|
2020-04-21 08:58:33 +01:00
|
|
|
MI32Client = NimBLEDevice::getDisconnectedClient();
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("%s: got disconnected client"),D_CMND_MI32);
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
}
|
2020-05-29 23:41:49 +01:00
|
|
|
|
|
|
|
if(NimBLEDevice::getClientListSize() >= NIMBLE_MAX_CONNECTIONS) {
|
|
|
|
MI32.mode.willConnect = 0;
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("%s: max connection already reached"),D_CMND_MI32);
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-21 08:58:33 +01:00
|
|
|
if(!MI32Client) {
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("%s: will create client"),D_CMND_MI32);
|
2020-04-21 08:58:33 +01:00
|
|
|
MI32Client = NimBLEDevice::createClient();
|
|
|
|
MI32Client->setClientCallbacks(&MI32SensorCB , false);
|
2020-05-29 23:41:49 +01:00
|
|
|
MI32Client->setConnectionParams(12,12,0,48);
|
|
|
|
MI32Client->setConnectTimeout(30);
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("%s: did create new client"),D_CMND_MI32);
|
2020-05-26 19:44:40 +01:00
|
|
|
}
|
2020-07-12 17:17:27 +01:00
|
|
|
vTaskDelay(300/ portTICK_PERIOD_MS);
|
2020-05-29 23:41:49 +01:00
|
|
|
if (!MI32Client->connect(_address,false)) {
|
|
|
|
MI32.mode.willConnect = 0;
|
2020-07-12 17:17:27 +01:00
|
|
|
// NimBLEDevice::deleteClient(MI32Client);
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("%s: did not connect client"),D_CMND_MI32);
|
2020-05-29 23:41:49 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
// }
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MI32StartScanTask(){
|
|
|
|
if (MI32.mode.connected) return;
|
|
|
|
MI32.mode.runningScan = 1;
|
|
|
|
xTaskCreatePinnedToCore(
|
|
|
|
MI32ScanTask, /* Function to implement the task */
|
|
|
|
"MI32ScanTask", /* Name of the task */
|
2020-11-02 18:32:08 +00:00
|
|
|
2048, /* Stack size in words */
|
2020-04-21 08:58:33 +01:00
|
|
|
NULL, /* Task input parameter */
|
|
|
|
0, /* Priority of the task */
|
|
|
|
NULL, /* Task handle. */
|
|
|
|
0); /* Core where the task should run */
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG,PSTR("%s: Start scanning"),D_CMND_MI32);
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MI32ScanTask(void *pvParameters){
|
2020-05-29 23:41:49 +01:00
|
|
|
if (MI32Scan == nullptr) MI32Scan = NimBLEDevice::getScan();
|
2020-09-15 09:46:08 +01:00
|
|
|
// DEBUG_SENSOR_LOG(PSTR("%s: Scan Cache Length: %u"),D_CMND_MI32, MI32Scan->getResults().getCount());
|
2020-07-12 17:17:27 +01:00
|
|
|
MI32Scan->setInterval(70);
|
|
|
|
MI32Scan->setWindow(50);
|
|
|
|
MI32Scan->setAdvertisedDeviceCallbacks(&MI32ScanCallbacks,true);
|
2020-05-29 23:41:49 +01:00
|
|
|
MI32Scan->setActiveScan(false);
|
2020-09-15 09:46:08 +01:00
|
|
|
MI32Scan->start(0, MI32scanEndedCB, true); // never stop scanning, will pause automatically while connecting
|
2020-07-12 17:17:27 +01:00
|
|
|
|
2020-04-21 08:58:33 +01:00
|
|
|
uint32_t timer = 0;
|
2020-07-12 17:17:27 +01:00
|
|
|
for(;;){
|
|
|
|
if(MI32.mode.shallClearResults){
|
|
|
|
MI32Scan->clearResults();
|
|
|
|
MI32.mode.shallClearResults=0;
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
2020-07-12 17:17:27 +01:00
|
|
|
vTaskDelay(10000/ portTICK_PERIOD_MS);
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
vTaskDelete( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
void MI32StartSensorTask(){
|
|
|
|
MI32.mode.willConnect = 1;
|
2020-07-15 16:41:12 +01:00
|
|
|
switch(MIBLEsensors[MI32.state.sensor].type){
|
|
|
|
case LYWSD03MMC: case MHOC401:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MI32.mode.willConnect = 0;
|
|
|
|
return;
|
2020-07-12 17:17:27 +01:00
|
|
|
}
|
2020-07-15 16:41:12 +01:00
|
|
|
|
2020-04-21 08:58:33 +01:00
|
|
|
xTaskCreatePinnedToCore(
|
|
|
|
MI32SensorTask, /* Function to implement the task */
|
|
|
|
"MI32SensorTask", /* Name of the task */
|
2020-07-12 17:17:27 +01:00
|
|
|
4096, /* Stack size in words */
|
2020-04-21 08:58:33 +01:00
|
|
|
NULL, /* Task input parameter */
|
|
|
|
15, /* Priority of the task */
|
|
|
|
NULL, /* Task handle. */
|
|
|
|
0); /* Core where the task should run */
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG,PSTR("%s: Start sensor connections"),D_CMND_MI32);
|
|
|
|
AddLog_P(LOG_LEVEL_DEBUG,PSTR("%s: with sensor: %u"),D_CMND_MI32, MI32.state.sensor);
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MI32SensorTask(void *pvParameters){
|
2020-05-29 23:41:49 +01:00
|
|
|
if (MI32ConnectActiveSensor()){
|
|
|
|
uint32_t timer = 0;
|
|
|
|
while (MI32.mode.connected == 0){
|
|
|
|
if (timer>1000){
|
|
|
|
MI32Client->disconnect();
|
2020-07-12 17:17:27 +01:00
|
|
|
// NimBLEDevice::deleteClient(MI32Client);
|
2020-05-29 23:41:49 +01:00
|
|
|
MI32.mode.willConnect = 0;
|
2020-07-20 07:51:17 +01:00
|
|
|
MI32.mode.willReadBatt = 0; //could be a "battery task" for LYWSD03MMC or MHO-C401
|
2020-05-29 23:41:49 +01:00
|
|
|
vTaskDelay(100/ portTICK_PERIOD_MS);
|
|
|
|
vTaskDelete( NULL );
|
|
|
|
}
|
|
|
|
timer++;
|
|
|
|
vTaskDelay(10/ portTICK_PERIOD_MS);
|
|
|
|
}
|
|
|
|
|
|
|
|
timer = 150;
|
|
|
|
switch(MIBLEsensors[MI32.state.sensor].type){
|
2020-07-15 16:41:12 +01:00
|
|
|
case LYWSD03MMC: case MHOC401:
|
2020-05-29 23:41:49 +01:00
|
|
|
MI32.mode.readingDone = 0;
|
|
|
|
if(MI32connectLYWSD03forNotification()) timer=0;
|
|
|
|
break;
|
|
|
|
default:
|
2020-04-21 08:58:33 +01:00
|
|
|
break;
|
|
|
|
}
|
2020-10-30 11:29:48 +00:00
|
|
|
|
2020-05-29 23:41:49 +01:00
|
|
|
while (!MI32.mode.readingDone){
|
|
|
|
if (timer>150){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
timer++;
|
|
|
|
vTaskDelay(100/ portTICK_PERIOD_MS);
|
|
|
|
}
|
|
|
|
MI32Client->disconnect();
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("%s: requested disconnect"),D_CMND_MI32);
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
2020-07-12 17:17:27 +01:00
|
|
|
|
2020-04-21 08:58:33 +01:00
|
|
|
MI32.mode.connected = 0;
|
|
|
|
vTaskDelete( NULL );
|
|
|
|
}
|
|
|
|
|
2020-05-29 23:41:49 +01:00
|
|
|
bool MI32connectLYWSD03forNotification(){
|
2020-04-21 08:58:33 +01:00
|
|
|
NimBLERemoteService* pSvc = nullptr;
|
|
|
|
NimBLERemoteCharacteristic* pChr = nullptr;
|
2020-05-29 23:41:49 +01:00
|
|
|
static BLEUUID serviceUUID(0xebe0ccb0,0x7a0a,0x4b0c,0x8a1a6ff2997da3a6);
|
|
|
|
static BLEUUID charUUID(0xebe0ccc1,0x7a0a,0x4b0c,0x8a1a6ff2997da3a6);
|
2020-04-21 10:06:30 +01:00
|
|
|
pSvc = MI32Client->getService(serviceUUID);
|
2020-04-21 08:58:33 +01:00
|
|
|
if(pSvc) {
|
|
|
|
pChr = pSvc->getCharacteristic(charUUID);
|
2020-04-21 10:06:30 +01:00
|
|
|
}
|
2020-05-29 23:41:49 +01:00
|
|
|
if (pChr){
|
|
|
|
if(pChr->canNotify()) {
|
2020-09-15 09:46:08 +01:00
|
|
|
if(pChr->subscribe(true,MI32notifyCB,false)) {
|
2020-05-29 23:41:49 +01:00
|
|
|
return true;
|
|
|
|
}
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
}
|
2020-05-29 23:41:49 +01:00
|
|
|
return false;
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MI32StartTimeTask(){
|
|
|
|
MI32.mode.willConnect = 1;
|
|
|
|
xTaskCreatePinnedToCore(
|
|
|
|
MI32TimeTask, /* Function to implement the task */
|
|
|
|
"MI32TimeTask", /* Name of the task */
|
2020-07-12 17:17:27 +01:00
|
|
|
4096, /* Stack size in words */
|
2020-04-21 08:58:33 +01:00
|
|
|
NULL, /* Task input parameter */
|
|
|
|
15, /* Priority of the task */
|
|
|
|
NULL, /* Task handle. */
|
|
|
|
0); /* Core where the task should run */
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("%s: Start time set"),D_CMND_MI32);
|
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("%s: with sensor: %u"),D_CMND_MI32, MI32.state.sensor);
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MI32TimeTask(void *pvParameters){
|
2020-08-02 14:07:18 +01:00
|
|
|
if (MIBLEsensors[MI32.state.sensor].type != LYWSD02 && MIBLEsensors[MI32.state.sensor].type != MHOC303) {
|
2020-04-21 08:58:33 +01:00
|
|
|
MI32.mode.shallSetTime = 0;
|
|
|
|
vTaskDelete( NULL );
|
|
|
|
}
|
2020-08-02 14:07:18 +01:00
|
|
|
if(MI32ConnectActiveSensor()){
|
2020-05-29 23:41:49 +01:00
|
|
|
uint32_t timer = 0;
|
|
|
|
while (MI32.mode.connected == 0){
|
|
|
|
if (timer>1000){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
timer++;
|
|
|
|
vTaskDelay(10/ portTICK_PERIOD_MS);
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
|
2020-05-29 23:41:49 +01:00
|
|
|
NimBLERemoteService* pSvc = nullptr;
|
|
|
|
NimBLERemoteCharacteristic* pChr = nullptr;
|
|
|
|
static BLEUUID serviceUUID(0xEBE0CCB0,0x7A0A,0x4B0C,0x8A1A6FF2997DA3A6);
|
|
|
|
static BLEUUID charUUID(0xEBE0CCB7,0x7A0A,0x4B0C,0x8A1A6FF2997DA3A6);
|
|
|
|
pSvc = MI32Client->getService(serviceUUID);
|
|
|
|
if(pSvc) {
|
|
|
|
pChr = pSvc->getCharacteristic(charUUID);
|
|
|
|
|
2020-05-29 19:18:39 +01:00
|
|
|
}
|
2020-10-30 11:29:48 +00:00
|
|
|
if (pChr){
|
2020-05-29 23:41:49 +01:00
|
|
|
if(pChr->canWrite()) {
|
|
|
|
union {
|
|
|
|
uint8_t buf[5];
|
|
|
|
uint32_t time;
|
|
|
|
} _utc;
|
|
|
|
_utc.time = Rtc.utc_time;
|
|
|
|
_utc.buf[4] = Rtc.time_timezone / 60;
|
|
|
|
|
|
|
|
if(!pChr->writeValue(_utc.buf,sizeof(_utc.buf),true)) { // true is important !
|
|
|
|
MI32.mode.willConnect = 0;
|
|
|
|
MI32Client->disconnect();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
MI32.mode.shallSetTime = 0;
|
|
|
|
MI32.mode.willSetTime = 0;
|
|
|
|
}
|
|
|
|
}
|
2020-05-29 19:18:39 +01:00
|
|
|
}
|
2020-05-29 23:41:49 +01:00
|
|
|
MI32Client->disconnect();
|
2020-05-29 19:18:39 +01:00
|
|
|
}
|
2020-07-12 17:17:27 +01:00
|
|
|
|
2020-05-29 19:18:39 +01:00
|
|
|
MI32.mode.connected = 0;
|
2020-07-12 17:17:27 +01:00
|
|
|
MI32.mode.canScan = 1;
|
2020-05-29 19:18:39 +01:00
|
|
|
vTaskDelete( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
void MI32StartUnitTask(){
|
|
|
|
MI32.mode.willConnect = 1;
|
|
|
|
xTaskCreatePinnedToCore(
|
|
|
|
MI32UnitTask, /* Function to implement the task */
|
|
|
|
"MI32UnitTask", /* Name of the task */
|
2020-07-12 17:17:27 +01:00
|
|
|
4096, /* Stack size in words */
|
2020-05-29 19:18:39 +01:00
|
|
|
NULL, /* Task input parameter */
|
|
|
|
15, /* Priority of the task */
|
|
|
|
NULL, /* Task handle. */
|
|
|
|
0); /* Core where the task should run */
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("%s: Start unit set"),D_CMND_MI32);
|
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("%s: with sensor: %u"),D_CMND_MI32, MI32.state.sensor);
|
2020-05-29 19:18:39 +01:00
|
|
|
}
|
2020-05-26 19:44:40 +01:00
|
|
|
|
2020-05-29 19:18:39 +01:00
|
|
|
void MI32UnitTask(void *pvParameters){
|
2020-08-02 14:07:18 +01:00
|
|
|
if (MIBLEsensors[MI32.state.sensor].type != LYWSD02 && MIBLEsensors[MI32.state.sensor].type != MHOC303) {
|
2020-05-29 19:18:39 +01:00
|
|
|
MI32.mode.shallSetUnit = 0;
|
|
|
|
vTaskDelete( NULL );
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
2020-05-29 19:18:39 +01:00
|
|
|
|
2020-10-30 11:29:48 +00:00
|
|
|
if(MI32ConnectActiveSensor()){
|
2020-05-29 23:41:49 +01:00
|
|
|
uint32_t timer = 0;
|
|
|
|
while (MI32.mode.connected == 0){
|
|
|
|
if (timer>1000){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
timer++;
|
|
|
|
vTaskDelay(10/ portTICK_PERIOD_MS);
|
2020-05-26 19:44:40 +01:00
|
|
|
}
|
2020-05-29 19:18:39 +01:00
|
|
|
|
2020-05-29 23:41:49 +01:00
|
|
|
NimBLERemoteService* pSvc = nullptr;
|
|
|
|
NimBLERemoteCharacteristic* pChr = nullptr;
|
2020-07-12 17:17:27 +01:00
|
|
|
static BLEUUID serviceUUID(0xEBE0CCB0,0x7A0A,0x4B0C,0x8A1A6FF2997DA3A6);
|
|
|
|
static BLEUUID charUUID(0xEBE0CCBE,0x7A0A,0x4B0C,0x8A1A6FF2997DA3A6);
|
2020-05-29 23:41:49 +01:00
|
|
|
pSvc = MI32Client->getService(serviceUUID);
|
|
|
|
if(pSvc) {
|
|
|
|
pChr = pSvc->getCharacteristic(charUUID);
|
2020-05-29 19:18:39 +01:00
|
|
|
}
|
|
|
|
|
2020-05-29 23:41:49 +01:00
|
|
|
if(pChr->canRead()){
|
|
|
|
uint8_t curUnit;
|
|
|
|
const char *buf = pChr->readValue().c_str();
|
|
|
|
if( buf[0] != 0 && buf[0]<101 ){
|
|
|
|
curUnit = buf[0];
|
|
|
|
}
|
2020-05-29 19:18:39 +01:00
|
|
|
|
2020-05-29 23:41:49 +01:00
|
|
|
if(pChr->canWrite()) {
|
|
|
|
curUnit = curUnit == 0x01?0xFF:0x01; // C/F
|
|
|
|
|
|
|
|
if(!pChr->writeValue(&curUnit,sizeof(curUnit),true)) { // true is important !
|
|
|
|
MI32.mode.willConnect = 0;
|
|
|
|
MI32Client->disconnect();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
MI32.mode.shallSetUnit = 0;
|
|
|
|
MI32.mode.willSetUnit = 0;
|
|
|
|
}
|
|
|
|
}
|
2020-05-29 19:18:39 +01:00
|
|
|
}
|
2020-05-29 23:41:49 +01:00
|
|
|
MI32Client->disconnect();
|
2020-05-29 19:18:39 +01:00
|
|
|
}
|
2020-07-12 17:17:27 +01:00
|
|
|
|
2020-04-21 08:58:33 +01:00
|
|
|
MI32.mode.connected = 0;
|
2020-07-12 17:17:27 +01:00
|
|
|
MI32.mode.canScan = 1;
|
2020-04-21 08:58:33 +01:00
|
|
|
vTaskDelete( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
void MI32StartBatteryTask(){
|
|
|
|
if (MI32.mode.connected) return;
|
|
|
|
MI32.mode.willReadBatt = 1;
|
2020-05-29 23:41:49 +01:00
|
|
|
MI32.mode.willConnect = 1;
|
|
|
|
MI32.mode.canScan = 0;
|
2020-07-12 17:17:27 +01:00
|
|
|
|
|
|
|
switch (MIBLEsensors[MI32.state.sensor].type){
|
2020-07-15 16:41:12 +01:00
|
|
|
case LYWSD03MMC: case MJ_HT_V1: case CGG1: case NLIGHT: case MJYD2S: case YEERC: case MHOC401:
|
2020-07-12 17:17:27 +01:00
|
|
|
MI32.mode.willConnect = 0;
|
|
|
|
MI32.mode.willReadBatt = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-21 08:58:33 +01:00
|
|
|
xTaskCreatePinnedToCore(
|
|
|
|
MI32BatteryTask, /* Function to implement the task */
|
|
|
|
"MI32BatteryTask", /* Name of the task */
|
2020-07-12 17:17:27 +01:00
|
|
|
4096, /* Stack size in words */
|
2020-04-21 08:58:33 +01:00
|
|
|
NULL, /* Task input parameter */
|
|
|
|
15, /* Priority of the task */
|
|
|
|
NULL, /* Task handle. */
|
|
|
|
0); /* Core where the task should run */
|
|
|
|
}
|
|
|
|
|
|
|
|
void MI32BatteryTask(void *pvParameters){
|
|
|
|
// all reported battery values are probably crap, but we allow the reading on demand
|
|
|
|
|
|
|
|
MI32.mode.connected = 0;
|
2020-05-29 23:41:49 +01:00
|
|
|
if(MI32ConnectActiveSensor()){
|
|
|
|
uint32_t timer = 0;
|
|
|
|
while (MI32.mode.connected == 0){
|
|
|
|
if (timer>1000){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
timer++;
|
|
|
|
vTaskDelay(30/ portTICK_PERIOD_MS);
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
|
2020-05-29 23:41:49 +01:00
|
|
|
switch(MIBLEsensors[MI32.state.sensor].type){
|
2020-07-12 17:17:27 +01:00
|
|
|
case FLORA: case LYWSD02: case CGD1:
|
|
|
|
MI32batteryRead(MIBLEsensors[MI32.state.sensor].type);
|
2020-05-29 19:18:39 +01:00
|
|
|
break;
|
2020-05-29 23:41:49 +01:00
|
|
|
}
|
|
|
|
MI32Client->disconnect();
|
|
|
|
}
|
2020-04-21 08:58:33 +01:00
|
|
|
MI32.mode.willReadBatt = 0;
|
|
|
|
MI32.mode.connected = 0;
|
|
|
|
vTaskDelete( NULL );
|
|
|
|
}
|
|
|
|
|
2020-07-12 17:17:27 +01:00
|
|
|
void MI32batteryRead(uint32_t _type){
|
2020-04-21 08:58:33 +01:00
|
|
|
uint32_t timer = 0;
|
|
|
|
while (!MI32.mode.connected){
|
|
|
|
if (timer>1000){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
timer++;
|
2020-05-29 23:41:49 +01:00
|
|
|
vTaskDelay(10/ portTICK_PERIOD_MS);
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
2020-07-12 17:17:27 +01:00
|
|
|
DEBUG_SENSOR_LOG(PSTR("%s connected for battery"),kMI32DeviceType[MIBLEsensors[MI32.state.sensor].type-1] );
|
2020-04-21 08:58:33 +01:00
|
|
|
NimBLERemoteService* pSvc = nullptr;
|
|
|
|
NimBLERemoteCharacteristic* pChr = nullptr;
|
|
|
|
|
2020-07-12 17:17:27 +01:00
|
|
|
switch(_type){
|
|
|
|
case FLORA:
|
|
|
|
{
|
|
|
|
static BLEUUID _serviceUUID(0x00001204,0x0000,0x1000,0x800000805f9b34fb);
|
|
|
|
static BLEUUID _charUUID(0x00001a02,0x0000,0x1000,0x800000805f9b34fb);
|
|
|
|
pSvc = MI32Client->getService(_serviceUUID);
|
|
|
|
if(pSvc) {
|
|
|
|
pChr = pSvc->getCharacteristic(_charUUID);
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
}
|
2020-07-12 17:17:27 +01:00
|
|
|
break;
|
|
|
|
case LYWSD02:
|
|
|
|
{
|
|
|
|
static BLEUUID _serviceUUID(0xEBE0CCB0,0x7A0A,0x4B0C,0x8A1A6FF2997DA3A6);
|
|
|
|
static BLEUUID _charUUID(0xEBE0CCC4,0x7A0A,0x4B0C,0x8A1A6FF2997DA3A6);
|
|
|
|
pSvc = MI32Client->getService(_serviceUUID);
|
|
|
|
if(pSvc) {
|
|
|
|
pChr = pSvc->getCharacteristic(_charUUID);
|
|
|
|
}
|
2020-05-29 23:41:49 +01:00
|
|
|
}
|
2020-07-12 17:17:27 +01:00
|
|
|
break;
|
|
|
|
case CGD1:
|
|
|
|
{
|
|
|
|
static BLEUUID _serviceUUID((uint16_t)0x180F);
|
|
|
|
static BLEUUID _charUUID((uint16_t)0x2A19);
|
|
|
|
pSvc = MI32Client->getService(_serviceUUID);
|
|
|
|
if(pSvc) {
|
|
|
|
pChr = pSvc->getCharacteristic(_charUUID);
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
}
|
2020-07-12 17:17:27 +01:00
|
|
|
break;
|
2020-05-29 19:18:39 +01:00
|
|
|
}
|
2020-07-12 17:17:27 +01:00
|
|
|
|
2020-05-29 23:41:49 +01:00
|
|
|
if (pChr){
|
2020-07-12 17:17:27 +01:00
|
|
|
DEBUG_SENSOR_LOG(PSTR("%s: got %s char %s"),D_CMND_MI32, kMI32DeviceType[MIBLEsensors[MI32.state.sensor].type-1], pChr->getUUID().toString().c_str());
|
2020-05-29 23:41:49 +01:00
|
|
|
if(pChr->canRead()) {
|
|
|
|
const char *buf = pChr->readValue().c_str();
|
|
|
|
MI32readBat((char*)buf);
|
|
|
|
}
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
2020-05-29 23:41:49 +01:00
|
|
|
MI32.mode.readingDone = 1;
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* parse the response from advertisements
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2020-07-12 17:17:27 +01:00
|
|
|
void MI32parseMiBeacon(char * _buf, uint32_t _slot, uint16_t _bufSize){
|
2020-04-21 08:58:33 +01:00
|
|
|
float _tempFloat;
|
|
|
|
mi_beacon_t _beacon;
|
2020-07-12 17:17:27 +01:00
|
|
|
|
|
|
|
if (MIBLEsensors[_slot].type==MJ_HT_V1 || MIBLEsensors[_slot].type==CGG1 || MIBLEsensors[_slot].type==YEERC){
|
|
|
|
memcpy((uint8_t*)&_beacon+1,(uint8_t*)_buf, sizeof(_beacon)-1); // shift by one byte for the MJ_HT_V1 DANGER!!!
|
|
|
|
memcpy((uint8_t*)&_beacon.MAC,(uint8_t*)&_beacon.MAC+1,6); // but shift back the MAC
|
|
|
|
_beacon.counter = _buf[4]; // restore the counter
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
else{
|
2020-07-12 17:17:27 +01:00
|
|
|
memcpy((char *)&_beacon, _buf, _bufSize);
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
|
2020-07-12 17:17:27 +01:00
|
|
|
MIBLEsensors[_slot].lastCnt = _beacon.counter;
|
|
|
|
#ifdef USE_MI_DECRYPTION
|
2020-10-20 13:49:10 +01:00
|
|
|
int decryptRet = 0;
|
2020-07-12 17:17:27 +01:00
|
|
|
switch(MIBLEsensors[_slot].type){
|
2020-07-15 16:41:12 +01:00
|
|
|
case LYWSD03MMC: case MHOC401:
|
2020-07-12 17:17:27 +01:00
|
|
|
if (_beacon.frame == 0x5858){
|
2020-10-20 13:49:10 +01:00
|
|
|
decryptRet = MI32_decryptPacket((char*)&_beacon.productID,_bufSize, LYWSD03MMC); //start with PID
|
|
|
|
// AddLogBuffer(LOG_LEVEL_DEBUG,(uint8_t*)&_beacon.productID,_bufSize);
|
2020-07-12 17:17:27 +01:00
|
|
|
}
|
2020-10-23 09:18:57 +01:00
|
|
|
else return; // 0x3058 holds no data, TODO: check for unpaired devices, that need connections
|
2020-07-12 17:17:27 +01:00
|
|
|
break;
|
|
|
|
case MJYD2S:
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG,PSTR("MJYD2S: %x"),_beacon.frame);
|
2020-07-12 17:17:27 +01:00
|
|
|
if (_beacon.frame == 0x5948){ // Now let's build/recreate a special MiBeacon
|
|
|
|
memmove((uint8_t*)&_beacon.MAC+6,(uint8_t*)&_beacon.MAC, _bufSize); // shift payload by the size of the MAC = 6 bytes
|
|
|
|
memcpy((uint8_t*)&_beacon.MAC,MIBLEsensors[_slot].MAC,6); // now insert the real MAC from our internal vector
|
|
|
|
_bufSize+=6; // the packet has grown
|
|
|
|
MI32_ReverseMAC(_beacon.MAC); // payload MAC is always reversed
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG,PSTR("MJYD2S: special packet"));
|
2020-07-12 17:17:27 +01:00
|
|
|
}
|
|
|
|
if (_beacon.frame != 0x5910){
|
2020-10-20 13:49:10 +01:00
|
|
|
decryptRet = MI32_decryptPacket((char*)&_beacon.productID,_bufSize,MJYD2S); //start with PID
|
2020-07-12 17:17:27 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-10-20 13:49:10 +01:00
|
|
|
if(decryptRet!=0){
|
2021-01-14 16:03:01 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG,PSTR("M32: Decryption failed with error: %d"),decryptRet);
|
2020-10-20 13:49:10 +01:00
|
|
|
return;
|
2020-07-12 17:17:27 +01:00
|
|
|
}
|
2020-10-20 13:49:10 +01:00
|
|
|
#endif //USE_MI_DECRYPTION
|
2020-04-21 08:58:33 +01:00
|
|
|
|
2020-07-12 17:17:27 +01:00
|
|
|
if(MIBLEsensors[_slot].type==6){
|
2020-07-20 07:51:17 +01:00
|
|
|
DEBUG_SENSOR_LOG(PSTR("CGD1 no support for MiBeacon, type %u"),MIBLEsensors[_slot].type);
|
2020-04-21 08:58:33 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG,PSTR("%s at slot %u with payload type: %02x"), kMI32DeviceType[MIBLEsensors[_slot].type-1],_slot,_beacon.type);
|
2020-04-21 08:58:33 +01:00
|
|
|
switch(_beacon.type){
|
2020-07-12 17:17:27 +01:00
|
|
|
case 0x01:
|
|
|
|
MIBLEsensors[_slot].Btn=_beacon.Btn.num + (_beacon.Btn.longPress/2)*6;
|
2020-07-20 07:51:17 +01:00
|
|
|
MIBLEsensors[_slot].eventType.Btn = 1;
|
2020-10-20 13:49:10 +01:00
|
|
|
MI32.mode.shallTriggerTele = 1;
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("Mode 1: U16: %u Button"), MIBLEsensors[_slot].Btn );
|
2020-07-12 17:17:27 +01:00
|
|
|
break;
|
2020-04-21 08:58:33 +01:00
|
|
|
case 0x04:
|
2020-07-12 17:17:27 +01:00
|
|
|
_tempFloat=(float)(_beacon.temp)/10.0f;
|
|
|
|
if(_tempFloat<60){
|
2020-07-20 07:51:17 +01:00
|
|
|
MIBLEsensors[_slot].temp=_tempFloat;
|
|
|
|
MIBLEsensors[_slot].eventType.temp = 1;
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("Mode 4: temp updated"));
|
2020-07-12 17:17:27 +01:00
|
|
|
}
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("Mode 4: U16: %u Temp"), _beacon.temp );
|
2020-04-21 08:58:33 +01:00
|
|
|
break;
|
|
|
|
case 0x06:
|
2020-07-12 17:17:27 +01:00
|
|
|
_tempFloat=(float)(_beacon.hum)/10.0f;
|
|
|
|
if(_tempFloat<101){
|
2020-07-20 07:51:17 +01:00
|
|
|
MIBLEsensors[_slot].hum=_tempFloat;
|
|
|
|
MIBLEsensors[_slot].eventType.hum = 1;
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("Mode 6: hum updated"));
|
2020-07-12 17:17:27 +01:00
|
|
|
}
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("Mode 6: U16: %u Hum"), _beacon.hum);
|
2020-04-21 08:58:33 +01:00
|
|
|
break;
|
|
|
|
case 0x07:
|
2020-07-12 17:17:27 +01:00
|
|
|
MIBLEsensors[_slot].lux=_beacon.lux & 0x00ffffff;
|
|
|
|
if(MIBLEsensors[_slot].type==MJYD2S){
|
2020-07-20 07:51:17 +01:00
|
|
|
MIBLEsensors[_slot].eventType.noMotion = 1;
|
2020-07-12 17:17:27 +01:00
|
|
|
}
|
2020-07-20 07:51:17 +01:00
|
|
|
MIBLEsensors[_slot].eventType.lux = 1;
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("Mode 7: U24: %u Lux"), _beacon.lux & 0x00ffffff);
|
2020-04-21 10:06:30 +01:00
|
|
|
break;
|
2020-04-21 08:58:33 +01:00
|
|
|
case 0x08:
|
2020-07-20 07:51:17 +01:00
|
|
|
MIBLEsensors[_slot].moisture=_beacon.moist;
|
|
|
|
MIBLEsensors[_slot].eventType.moist = 1;
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("Mode 8: moisture updated"));
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("Mode 8: U8: %u Moisture"), _beacon.moist);
|
2020-04-21 08:58:33 +01:00
|
|
|
break;
|
|
|
|
case 0x09:
|
2020-07-15 16:41:12 +01:00
|
|
|
MIBLEsensors[_slot].fertility=_beacon.fert;
|
2020-07-20 07:51:17 +01:00
|
|
|
MIBLEsensors[_slot].eventType.fert = 1;
|
2020-07-12 17:17:27 +01:00
|
|
|
DEBUG_SENSOR_LOG(PSTR("Mode 9: fertility updated"));
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("Mode 9: U16: %u Fertility"), _beacon.fert);
|
2020-04-21 08:58:33 +01:00
|
|
|
break;
|
|
|
|
case 0x0a:
|
2020-07-20 07:51:17 +01:00
|
|
|
if(MI32.option.ignoreBogusBattery){
|
|
|
|
if(MIBLEsensors[_slot].type==LYWSD03MMC || MIBLEsensors[_slot].type==MHOC401){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-07-12 17:17:27 +01:00
|
|
|
if(_beacon.bat<101){
|
|
|
|
MIBLEsensors[_slot].bat = _beacon.bat;
|
2020-07-20 07:51:17 +01:00
|
|
|
MIBLEsensors[_slot].eventType.bat = 1;
|
2020-07-12 17:17:27 +01:00
|
|
|
DEBUG_SENSOR_LOG(PSTR("Mode a: bat updated"));
|
|
|
|
}
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("Mode a: U8: %u %%"), _beacon.bat);
|
2020-04-21 08:58:33 +01:00
|
|
|
break;
|
|
|
|
case 0x0d:
|
2020-07-12 17:17:27 +01:00
|
|
|
_tempFloat=(float)(_beacon.HT.temp)/10.0f;
|
|
|
|
if(_tempFloat<60){
|
|
|
|
MIBLEsensors[_slot].temp = _tempFloat;
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("Mode d: temp updated"));
|
|
|
|
}
|
|
|
|
_tempFloat=(float)(_beacon.HT.hum)/10.0f;
|
|
|
|
if(_tempFloat<100){
|
|
|
|
MIBLEsensors[_slot].hum = _tempFloat;
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("Mode d: hum updated"));
|
|
|
|
}
|
2020-07-20 07:51:17 +01:00
|
|
|
MIBLEsensors[_slot].eventType.tempHum = 1;
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("Mode d: U16: %x Temp U16: %x Hum"), _beacon.HT.temp, _beacon.HT.hum);
|
2020-07-12 17:17:27 +01:00
|
|
|
break;
|
|
|
|
#ifdef USE_MI_DECRYPTION
|
|
|
|
case 0x0f:
|
|
|
|
if (_beacon.ten!=0) break;
|
2020-07-20 07:51:17 +01:00
|
|
|
MIBLEsensors[_slot].eventType.motion = 1;
|
2020-07-12 17:17:27 +01:00
|
|
|
MIBLEsensors[_slot].lastTime = millis();
|
|
|
|
MIBLEsensors[_slot].events++;
|
|
|
|
MIBLEsensors[_slot].lux = _beacon.lux;
|
2020-07-20 07:51:17 +01:00
|
|
|
MIBLEsensors[_slot].eventType.lux = 1;
|
2020-10-20 13:49:10 +01:00
|
|
|
MIBLEsensors[_slot].NMT = 0;
|
|
|
|
MI32.mode.shallTriggerTele = 1;
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("PIR: primary"),MIBLEsensors[_slot].lux );
|
2020-07-12 17:17:27 +01:00
|
|
|
break;
|
|
|
|
case 0x17:
|
|
|
|
MIBLEsensors[_slot].NMT = _beacon.NMT;
|
2020-07-20 07:51:17 +01:00
|
|
|
MIBLEsensors[_slot].eventType.NMT = 1;
|
2020-10-20 13:49:10 +01:00
|
|
|
MI32.mode.shallTriggerTele = 1;
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("Mode 17: NMT: %u seconds"), _beacon.NMT);
|
2020-07-12 17:17:27 +01:00
|
|
|
break;
|
|
|
|
#endif //USE_MI_DECRYPTION
|
|
|
|
default:
|
|
|
|
if (MIBLEsensors[_slot].type==NLIGHT){
|
2020-07-20 07:51:17 +01:00
|
|
|
MIBLEsensors[_slot].eventType.motion = 1; //PIR
|
2020-07-12 17:17:27 +01:00
|
|
|
MIBLEsensors[_slot].events++;
|
2020-10-30 11:29:48 +00:00
|
|
|
MIBLEsensors[_slot].NMT = 0;
|
2020-07-12 17:17:27 +01:00
|
|
|
MIBLEsensors[_slot].lastTime = millis();
|
2020-10-20 13:49:10 +01:00
|
|
|
MI32.mode.shallTriggerTele = 1;
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_DEBUG,PSTR("PIR: primary"),MIBLEsensors[_slot].lux );
|
2020-07-12 17:17:27 +01:00
|
|
|
}
|
2020-10-20 13:49:10 +01:00
|
|
|
else{
|
|
|
|
AddLogBuffer(LOG_LEVEL_DEBUG,(uint8_t*)_buf,_bufSize);
|
|
|
|
}
|
2020-04-21 08:58:33 +01:00
|
|
|
break;
|
|
|
|
}
|
2020-07-20 07:51:17 +01:00
|
|
|
if(MIBLEsensors[_slot].eventType.raw == 0) return;
|
|
|
|
MIBLEsensors[_slot].shallSendMQTT = 1;
|
2020-10-20 13:49:10 +01:00
|
|
|
if(MI32.option.directBridgeMode) MI32.mode.shallTriggerTele = 1;
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
|
2020-10-23 09:18:57 +01:00
|
|
|
void MI32ParseATCPacket(char * _buf, uint32_t length, uint8_t addr[6], int RSSI){
|
2020-09-16 07:03:55 +01:00
|
|
|
ATCPacket_t *_packet = (ATCPacket_t*)_buf;
|
|
|
|
uint32_t _slot = MIBLEgetSensorSlot(_packet->MAC, 0x0a1c, _packet->frameCnt); // This must be a hard-coded fake ID
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG,PSTR("%s at slot %u"), kMI32DeviceType[MIBLEsensors[_slot].type-1],_slot);
|
2020-09-16 07:03:55 +01:00
|
|
|
if(_slot==0xff) return;
|
|
|
|
|
2020-10-23 09:18:57 +01:00
|
|
|
MIBLEsensors[_slot].RSSI=RSSI;
|
2020-09-16 07:03:55 +01:00
|
|
|
|
2020-11-22 09:44:02 +00:00
|
|
|
MIBLEsensors.at(_slot).temp = (float)(int16_t(__builtin_bswap16(_packet->temp)))/10.0f;
|
2020-09-16 07:03:55 +01:00
|
|
|
MIBLEsensors.at(_slot).hum = (float)_packet->hum;
|
|
|
|
MIBLEsensors[_slot].eventType.tempHum = 1;
|
|
|
|
MIBLEsensors.at(_slot).bat = _packet->batPer;
|
|
|
|
MIBLEsensors[_slot].eventType.bat = 1;
|
|
|
|
|
|
|
|
MIBLEsensors[_slot].shallSendMQTT = 1;
|
2020-10-20 13:49:10 +01:00
|
|
|
if(MI32.option.directBridgeMode) MI32.mode.shallTriggerTele = 1;
|
2020-09-16 07:03:55 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-10-23 09:18:57 +01:00
|
|
|
void MI32parseCGD1Packet(char * _buf, uint32_t length, uint8_t addr[6], int RSSI){ // no MiBeacon
|
2020-04-21 08:58:33 +01:00
|
|
|
uint8_t _addr[6];
|
|
|
|
memcpy(_addr,addr,6);
|
2020-07-12 17:17:27 +01:00
|
|
|
uint32_t _slot = MIBLEgetSensorSlot(_addr, 0x0576, 0); // This must be hard-coded, no object-id in Cleargrass-packet, we have no packet counter too
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG,PSTR("%s at slot %u"), kMI32DeviceType[MIBLEsensors[_slot].type-1],_slot);
|
2020-04-21 08:58:33 +01:00
|
|
|
if(_slot==0xff) return;
|
2020-10-23 09:18:57 +01:00
|
|
|
MIBLEsensors[_slot].RSSI=RSSI;
|
2020-04-21 08:58:33 +01:00
|
|
|
cg_packet_t _packet;
|
|
|
|
memcpy((char*)&_packet,_buf,sizeof(_packet));
|
|
|
|
switch (_packet.mode){
|
|
|
|
case 0x0401:
|
|
|
|
float _tempFloat;
|
|
|
|
_tempFloat=(float)(_packet.temp)/10.0f;
|
|
|
|
if(_tempFloat<60){
|
|
|
|
MIBLEsensors.at(_slot).temp = _tempFloat;
|
2020-07-20 07:51:17 +01:00
|
|
|
MIBLEsensors[_slot].eventType.temp = 1;
|
2020-04-21 08:58:33 +01:00
|
|
|
DEBUG_SENSOR_LOG(PSTR("CGD1: temp updated"));
|
|
|
|
}
|
|
|
|
_tempFloat=(float)(_packet.hum)/10.0f;
|
|
|
|
if(_tempFloat<100){
|
|
|
|
MIBLEsensors.at(_slot).hum = _tempFloat;
|
2020-07-20 07:51:17 +01:00
|
|
|
MIBLEsensors[_slot].eventType.hum = 1;
|
2020-04-21 08:58:33 +01:00
|
|
|
DEBUG_SENSOR_LOG(PSTR("CGD1: hum updated"));
|
|
|
|
}
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("CGD1: U16: %x Temp U16: %x Hum"), _packet.temp, _packet.hum);
|
|
|
|
break;
|
|
|
|
case 0x0102:
|
|
|
|
if(_packet.bat<101){
|
|
|
|
MIBLEsensors.at(_slot).bat = _packet.bat;
|
2020-07-20 07:51:17 +01:00
|
|
|
MIBLEsensors[_slot].eventType.bat = 1;
|
2020-04-21 08:58:33 +01:00
|
|
|
DEBUG_SENSOR_LOG(PSTR("Mode a: bat updated"));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2021-01-14 16:03:01 +00:00
|
|
|
DEBUG_SENSOR_LOG(PSTR("M32: Unexpected CGD1-packet"));
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
2020-07-20 07:51:17 +01:00
|
|
|
if(MIBLEsensors[_slot].eventType.raw == 0) return;
|
|
|
|
MIBLEsensors[_slot].shallSendMQTT = 1;
|
2020-10-20 13:49:10 +01:00
|
|
|
if(MI32.option.directBridgeMode) MI32.mode.shallTriggerTele = 1;
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
|
2020-10-23 09:18:57 +01:00
|
|
|
void MI32ParseResponse(char *buf, uint16_t bufsize, uint8_t addr[6], int RSSI) {
|
2020-07-12 17:17:27 +01:00
|
|
|
if(bufsize<9) { //9 is from the NLIGHT
|
2020-04-21 08:58:33 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-07-12 17:17:27 +01:00
|
|
|
uint16_t _type= buf[3]*256 + buf[2];
|
2020-11-06 16:09:13 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_INFO, PSTR("%02x %02x %02x %02x"),(uint8_t)buf[0], (uint8_t)buf[1],(uint8_t)buf[2],(uint8_t)buf[3]);
|
2020-04-21 08:58:33 +01:00
|
|
|
uint8_t _addr[6];
|
|
|
|
memcpy(_addr,addr,6);
|
2020-07-12 17:17:27 +01:00
|
|
|
uint16_t _slot = MIBLEgetSensorSlot(_addr, _type, buf[4]);
|
2020-07-10 17:03:07 +01:00
|
|
|
if(_slot!=0xff) {
|
2020-10-23 09:18:57 +01:00
|
|
|
MIBLEsensors[_slot].RSSI=RSSI;
|
2020-07-12 20:01:36 +01:00
|
|
|
MI32parseMiBeacon(buf,_slot,bufsize);
|
2020-07-10 17:03:07 +01:00
|
|
|
}
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
|
2020-10-23 09:18:57 +01:00
|
|
|
/**
|
|
|
|
* @brief Parse a BLE advertisement packet
|
2020-10-30 11:29:48 +00:00
|
|
|
*
|
|
|
|
* @param payload
|
|
|
|
* @param payloadLength
|
|
|
|
* @param CID
|
|
|
|
* @param SVC
|
|
|
|
* @param UUID
|
2020-10-23 09:18:57 +01:00
|
|
|
*/
|
|
|
|
void MI32ParseGenericBeacon(uint8_t* payload, size_t payloadLength, uint16_t* CID, uint16_t*SVC, uint16_t* UUID){
|
2021-01-14 16:03:01 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG_MORE,PSTR("M32: Beacon:____________"));
|
2020-10-23 09:18:57 +01:00
|
|
|
for (uint32_t i = 0; i<payloadLength;){
|
|
|
|
uint32_t ADtype = payload[i+1];
|
|
|
|
uint32_t offset = payload[i];
|
|
|
|
switch(ADtype){
|
|
|
|
case 0x01:
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG_MORE,PSTR("Flags: %02x"), payload[i+2]);
|
2020-10-23 09:18:57 +01:00
|
|
|
break;
|
|
|
|
case 0x02: case 0x03:
|
|
|
|
*UUID = payload[i+3]*256 + payload[i+2];
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG_MORE,PSTR("UUID: %04x"), *UUID);
|
2020-10-23 09:18:57 +01:00
|
|
|
break;
|
|
|
|
case 0x08: case 0x09:
|
|
|
|
{
|
|
|
|
uint8_t _saveChar = payload[i+offset+1];
|
|
|
|
payload[i+offset+1] = 0;
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG_MORE,PSTR("Name: %s"), (char*)&payload[i+2]);
|
2020-10-23 09:18:57 +01:00
|
|
|
payload[i+offset+1] = _saveChar;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x0a:
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG_MORE,PSTR("TxPow: %02u"), payload[i+2]);
|
2020-10-23 09:18:57 +01:00
|
|
|
break;
|
|
|
|
case 0xff:
|
|
|
|
*CID = payload[i+3]*256 + payload[i+2];
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG_MORE,PSTR("CID: %04x"), *CID);
|
2020-10-23 09:18:57 +01:00
|
|
|
break;
|
|
|
|
case 0x16:
|
|
|
|
*SVC = payload[i+3]*256 + payload[i+2];
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG_MORE,PSTR("SVC: %04x"), *SVC);
|
2020-10-23 09:18:57 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
i+=offset+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Handle a generic BLE advertisment in a running scan or to check a beacon
|
2020-10-30 11:29:48 +00:00
|
|
|
*
|
|
|
|
* @param payload
|
|
|
|
* @param payloadLength
|
|
|
|
* @param RSSI
|
|
|
|
* @param addr
|
2020-10-23 09:18:57 +01:00
|
|
|
*/
|
|
|
|
void MI32HandleGenericBeacon(uint8_t* payload, size_t payloadLength, int RSSI, uint8_t* addr){
|
2020-10-30 11:29:48 +00:00
|
|
|
if(MI32.state.beaconScanCounter==0){ //handle beacon
|
2020-10-23 09:18:57 +01:00
|
|
|
for(auto &_beacon : MIBLEbeacons){
|
|
|
|
if(memcmp(addr,_beacon.MAC,6)==0){
|
|
|
|
MI32ParseGenericBeacon(payload,payloadLength,&_beacon.CID,&_beacon.SVC,&_beacon.UUID);
|
|
|
|
_beacon.time = 0;
|
|
|
|
_beacon.RSSI = RSSI;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// else handle scan
|
2020-11-17 08:30:59 +00:00
|
|
|
if(MIBLEscanResult.size()>19) {
|
2021-01-14 16:03:01 +00:00
|
|
|
AddLog_P(LOG_LEVEL_INFO,PSTR("M32: Scan buffer full"));
|
2020-10-23 09:18:57 +01:00
|
|
|
MI32.state.beaconScanCounter = 1;
|
|
|
|
return;
|
|
|
|
}
|
2020-11-17 08:30:59 +00:00
|
|
|
for(auto _scanResult : MIBLEscanResult){
|
2020-10-23 09:18:57 +01:00
|
|
|
if(memcmp(addr,_scanResult.MAC,6)==0){
|
2021-01-14 16:03:01 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_INFO,PSTR("M32: known device"));
|
2020-10-23 09:18:57 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
scan_entry_t _new;
|
|
|
|
_new.RSSI = RSSI;
|
|
|
|
_new.CID = 0;
|
|
|
|
_new.SVC = 0;
|
|
|
|
_new.UUID = 0;
|
|
|
|
memcpy(_new.MAC,addr,sizeof(_new.MAC));
|
|
|
|
MI32ParseGenericBeacon(payload,payloadLength,&_new.CID,&_new.SVC,&_new.UUID);
|
2020-11-17 08:30:59 +00:00
|
|
|
MIBLEscanResult.push_back(_new);
|
2020-10-23 09:18:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Add a beacon defined by its MAC-address, if only zeros are given, the beacon will be deactivated
|
2020-10-30 11:29:48 +00:00
|
|
|
*
|
2020-10-23 09:18:57 +01:00
|
|
|
* @param index 1-4 beacons are currently supported
|
|
|
|
* @param data null terminated char array representing a MAC-address in hex
|
|
|
|
*/
|
|
|
|
void MI32addBeacon(uint8_t index, char* data){
|
|
|
|
auto &_new = MIBLEbeacons[index-1]; //TODO: check
|
|
|
|
MI32HexStringToBytes(data,_new.MAC);
|
|
|
|
char _MAC[18];
|
|
|
|
ToHex_P(MIBLEbeacons[index-1].MAC,6,_MAC,18,':');
|
|
|
|
char _empty[6] = {0};
|
|
|
|
_new.time = 0;
|
|
|
|
if(memcmp(_empty,_new.MAC,6) == 0){
|
|
|
|
_new.active = false;
|
2021-01-14 16:03:01 +00:00
|
|
|
AddLog_P(LOG_LEVEL_INFO,PSTR("M32: Beacon%u deactivated"), index);
|
2020-10-23 09:18:57 +01:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
_new.active = true;
|
|
|
|
MI32.mode.activeBeacon = 1;
|
2021-01-14 16:03:01 +00:00
|
|
|
AddLog_P(LOG_LEVEL_INFO,PSTR("M32: Beacon added with MAC: %s"), _MAC);
|
2020-10-23 09:18:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Present BLE scan in the console, after that deleting the scan data
|
2020-10-30 11:29:48 +00:00
|
|
|
*
|
2020-10-23 09:18:57 +01:00
|
|
|
*/
|
|
|
|
void MI32showScanResults(){
|
2020-11-17 08:30:59 +00:00
|
|
|
size_t _size = MIBLEscanResult.size();
|
|
|
|
ResponseAppend_P(PSTR(",\"BLEScan\":{\"Found\":%u,\"Devices\":["), _size);
|
|
|
|
for(auto _scanResult : MIBLEscanResult){
|
2020-10-23 09:18:57 +01:00
|
|
|
char _MAC[18];
|
|
|
|
ToHex_P(_scanResult.MAC,6,_MAC,18,':');
|
2020-11-17 08:30:59 +00:00
|
|
|
ResponseAppend_P(PSTR("{\"MAC\":\"%s\",\"CID\":\"0x%04x\",\"SVC\":\"0x%04x\",\"UUID\":\"0x%04x\",\"RSSI\":%d},"), _MAC, _scanResult.CID, _scanResult.SVC, _scanResult.UUID, _scanResult.RSSI);
|
|
|
|
}
|
|
|
|
if(_size != 0)TasmotaGlobal.mqtt_data[strlen(TasmotaGlobal.mqtt_data)-1] = 0; // delete last comma
|
|
|
|
ResponseAppend_P(PSTR("]}"));
|
|
|
|
MIBLEscanResult.clear();
|
|
|
|
MI32.mode.shallShowScanResult = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MI32showBlockList(){
|
|
|
|
ResponseAppend_P(PSTR(",\"Block\":["));
|
|
|
|
for(auto _scanResult : MIBLEBlockList){
|
|
|
|
char _MAC[18];
|
|
|
|
ToHex_P(_scanResult.buf,6,_MAC,18,':');
|
|
|
|
ResponseAppend_P(PSTR("\"%s\","), _MAC);
|
|
|
|
}
|
|
|
|
if(MIBLEBlockList.size()!=0) TasmotaGlobal.mqtt_data[strlen(TasmotaGlobal.mqtt_data)-1] = 0; // delete last comma
|
|
|
|
ResponseAppend_P(PSTR("]"));
|
|
|
|
MI32.mode.shallShowBlockList = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MI32isInBlockList(uint8_t* MAC){
|
|
|
|
bool isBlocked = false;
|
|
|
|
for(auto &_blockedMAC : MIBLEBlockList){
|
|
|
|
if(memcmp(_blockedMAC.buf,MAC,6) == 0) isBlocked = true;
|
2020-10-23 09:18:57 +01:00
|
|
|
}
|
2020-11-17 08:30:59 +00:00
|
|
|
return isBlocked;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MI32removeMIBLEsensor(uint8_t* MAC){
|
|
|
|
MIBLEsensors.erase( std::remove_if( MIBLEsensors.begin() , MIBLEsensors.end(), [MAC]( mi_sensor_t _sensor )->bool
|
2021-01-01 12:44:04 +00:00
|
|
|
{ return (memcmp(_sensor.MAC,MAC,6) == 0); }
|
2020-11-17 08:30:59 +00:00
|
|
|
), end( MIBLEsensors ) );
|
2020-10-23 09:18:57 +01:00
|
|
|
}
|
2020-04-21 08:58:33 +01:00
|
|
|
/***********************************************************************\
|
|
|
|
* Read data from connections
|
|
|
|
\***********************************************************************/
|
|
|
|
|
|
|
|
void MI32readHT_LY(char *_buf){
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("%s: raw data: %x%x%x%x%x%x%x"),D_CMND_MI32,_buf[0],_buf[1],_buf[2],_buf[3],_buf[4],_buf[5],_buf[6]);
|
|
|
|
if(_buf[0] != 0 && _buf[1] != 0){
|
|
|
|
memcpy(&LYWSD0x_HT,(void *)_buf,sizeof(LYWSD0x_HT));
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG, PSTR("%s: T * 100: %u, H: %u, V: %u"),D_CMND_MI32,LYWSD0x_HT.temp,LYWSD0x_HT.hum, LYWSD0x_HT.volt);
|
2020-04-21 08:58:33 +01:00
|
|
|
uint32_t _slot = MI32.state.sensor;
|
|
|
|
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("MIBLE: Sensor slot: %u"), _slot);
|
|
|
|
static float _tempFloat;
|
|
|
|
_tempFloat=(float)(LYWSD0x_HT.temp)/100.0f;
|
|
|
|
if(_tempFloat<60){
|
|
|
|
MIBLEsensors[_slot].temp=_tempFloat;
|
2020-07-12 17:17:27 +01:00
|
|
|
// MIBLEsensors[_slot].showedUp=255; // this sensor is real
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
_tempFloat=(float)LYWSD0x_HT.hum;
|
|
|
|
if(_tempFloat<100){
|
|
|
|
MIBLEsensors[_slot].hum = _tempFloat;
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("LYWSD0x: hum updated"));
|
|
|
|
}
|
2020-07-20 07:51:17 +01:00
|
|
|
MIBLEsensors[_slot].eventType.tempHum = 1;
|
2020-07-15 16:41:12 +01:00
|
|
|
if (MIBLEsensors[_slot].type == LYWSD03MMC || MIBLEsensors[_slot].type == MHOC401){
|
2020-07-12 17:17:27 +01:00
|
|
|
MIBLEsensors[_slot].bat = ((float)LYWSD0x_HT.volt-2100.0f)/12.0f;
|
2020-07-20 07:51:17 +01:00
|
|
|
MI32.mode.willReadBatt = 0;
|
|
|
|
MIBLEsensors[_slot].eventType.bat = 1;
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
2020-07-20 07:51:17 +01:00
|
|
|
MIBLEsensors[_slot].shallSendMQTT = 1;
|
|
|
|
MI32.mode.shallTriggerTele = 1;
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MI32readBat(char *_buf){
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("%s: raw data: %x%x%x%x%x%x%x"),D_CMND_MI32,_buf[0],_buf[1],_buf[2],_buf[3],_buf[4],_buf[5],_buf[6]);
|
|
|
|
if(_buf[0] != 0){
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG,PSTR("%s: Battery: %u"),D_CMND_MI32,_buf[0]);
|
2020-04-21 08:58:33 +01:00
|
|
|
uint32_t _slot = MI32.state.sensor;
|
|
|
|
DEBUG_SENSOR_LOG(PSTR("MIBLE: Sensor slot: %u"), _slot);
|
|
|
|
if(_buf[0]<101){
|
|
|
|
MIBLEsensors[_slot].bat=_buf[0];
|
2020-07-12 17:17:27 +01:00
|
|
|
if(MIBLEsensors[_slot].type==FLORA){
|
|
|
|
memcpy(MIBLEsensors[_slot].firmware, _buf+2, 5);
|
|
|
|
MIBLEsensors[_slot].firmware[5] = '\0';
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG,PSTR("%s: Firmware: %s"),D_CMND_MI32,MIBLEsensors[_slot].firmware);
|
2020-07-12 17:17:27 +01:00
|
|
|
}
|
2020-07-20 07:51:17 +01:00
|
|
|
MIBLEsensors[_slot].eventType.bat = 1;
|
|
|
|
MIBLEsensors[_slot].shallSendMQTT = 1;
|
|
|
|
MI32.mode.shallTriggerTele = 1;
|
|
|
|
return true;
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-20 07:51:17 +01:00
|
|
|
/**
|
|
|
|
* @brief Launch functions from Core 1 to make race conditions less likely
|
2020-10-30 11:29:48 +00:00
|
|
|
*
|
2020-07-20 07:51:17 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
void MI32Every50mSecond(){
|
|
|
|
if(MI32.mode.shallTriggerTele){
|
|
|
|
MI32.mode.shallTriggerTele = 0;
|
|
|
|
MI32triggerTele();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 08:58:33 +01:00
|
|
|
/**
|
|
|
|
* @brief Main loop of the driver, "high level"-loop
|
2020-04-21 10:06:30 +01:00
|
|
|
*
|
2020-04-21 08:58:33 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
void MI32EverySecond(bool restart){
|
|
|
|
static uint32_t _counter = MI32.period - 15;
|
|
|
|
static uint32_t _nextSensorSlot = 0;
|
|
|
|
|
2020-07-12 17:17:27 +01:00
|
|
|
for (uint32_t i = 0; i < MIBLEsensors.size(); i++) {
|
|
|
|
if(MIBLEsensors[i].type==NLIGHT || MIBLEsensors[i].type==MJYD2S){
|
|
|
|
MIBLEsensors[i].NMT++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-23 09:18:57 +01:00
|
|
|
uint32_t _idx = 0;
|
|
|
|
uint32_t _activeBeacons = 0;
|
|
|
|
for (auto &_beacon : MIBLEbeacons){
|
|
|
|
_idx++;
|
|
|
|
if(_beacon.active == false) continue;
|
|
|
|
_activeBeacons++;
|
|
|
|
_beacon.time++;
|
2020-11-02 18:32:08 +00:00
|
|
|
Response_P(PSTR("{\"Beacon%u\":{\"Time\":%u}}"), _idx, _beacon.time);
|
2020-10-23 09:18:57 +01:00
|
|
|
XdrvRulesProcess();
|
|
|
|
}
|
|
|
|
if(_activeBeacons==0) MI32.mode.activeBeacon = 0;
|
|
|
|
|
|
|
|
if(MI32.state.beaconScanCounter!=0){
|
|
|
|
MI32.state.beaconScanCounter--;
|
|
|
|
if(MI32.state.beaconScanCounter==0){
|
2020-11-17 08:30:59 +00:00
|
|
|
MI32.mode.shallShowScanResult = 1;
|
|
|
|
MI32triggerTele();
|
2020-10-23 09:18:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 07:26:38 +01:00
|
|
|
if(MI32.mode.shallShowStatusInfo == 1){
|
|
|
|
MI32StatusInfo();
|
|
|
|
}
|
|
|
|
|
2020-04-21 08:58:33 +01:00
|
|
|
if(restart){
|
|
|
|
_counter = 0;
|
|
|
|
MI32.mode.canScan = 0;
|
|
|
|
MI32.mode.canConnect = 1;
|
2020-05-29 23:41:49 +01:00
|
|
|
MI32.mode.willReadBatt = 0;
|
|
|
|
MI32.mode.willConnect = 0;
|
2020-04-21 08:58:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MI32.mode.shallSetTime) {
|
|
|
|
MI32.mode.canScan = 0;
|
|
|
|
MI32.mode.canConnect = 0;
|
|
|
|
if (MI32.mode.willSetTime == 0){
|
|
|
|
MI32.mode.willSetTime = 1;
|
|
|
|
MI32StartTask(MI32_TASK_TIME);
|
2020-04-21 10:06:30 +01:00
|
|
|
}
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
|
2020-05-29 19:18:39 +01:00
|
|
|
if (MI32.mode.shallSetUnit) {
|
|
|
|
MI32.mode.canScan = 0;
|
|
|
|
MI32.mode.canConnect = 0;
|
|
|
|
if (MI32.mode.willSetUnit == 0){
|
|
|
|
MI32.mode.willSetUnit = 1;
|
|
|
|
MI32StartTask(MI32_TASK_UNIT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 08:58:33 +01:00
|
|
|
if (MI32.mode.willReadBatt) return;
|
|
|
|
|
|
|
|
if (_counter>MI32.period) {
|
|
|
|
_counter = 0;
|
|
|
|
MI32.mode.canScan = 0;
|
|
|
|
MI32.mode.canConnect = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(MI32.mode.connected == 1 || MI32.mode.willConnect == 1) return;
|
|
|
|
|
|
|
|
if(MIBLEsensors.size()==0) {
|
|
|
|
if (MI32.mode.runningScan == 0 && MI32.mode.canScan == 1) MI32StartTask(MI32_TASK_SCAN);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_counter==0) {
|
2020-10-30 11:29:48 +00:00
|
|
|
|
2020-04-21 08:58:33 +01:00
|
|
|
MI32.state.sensor = _nextSensorSlot;
|
|
|
|
MI32.mode.canScan = 0;
|
2020-07-12 17:17:27 +01:00
|
|
|
// if (MI32.mode.runningScan|| MI32.mode.connected || MI32.mode.willConnect) return;
|
|
|
|
if (MI32.mode.connected || MI32.mode.willConnect) return;
|
2020-04-21 08:58:33 +01:00
|
|
|
_nextSensorSlot++;
|
|
|
|
MI32.mode.canConnect = 1;
|
|
|
|
if(MI32.mode.connected == 0) {
|
2020-10-23 09:18:57 +01:00
|
|
|
if (MI32.mode.shallReadBatt) {
|
|
|
|
//TODO: decide automatically, which sensor can not work without connections
|
2020-11-06 16:09:13 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG,PSTR("%s: active sensor now: %u of %u"),D_CMND_MI32, MI32.state.sensor, MIBLEsensors.size()-1);
|
|
|
|
AddLog_P(LOG_LEVEL_DEBUG, PSTR("will connect to %s"),kMI32DeviceType[MIBLEsensors[MI32.state.sensor].type-1] );
|
2020-04-21 08:58:33 +01:00
|
|
|
|
|
|
|
MI32StartTask(MI32_TASK_BATT);
|
2020-07-12 17:17:27 +01:00
|
|
|
}
|
|
|
|
#ifndef USE_MI_DECRYPTION // turn off connections, because we only listen to advertisements
|
2020-04-21 08:58:33 +01:00
|
|
|
else{
|
|
|
|
MI32StartTask(MI32_TASK_CONN);
|
2020-07-12 17:17:27 +01:00
|
|
|
}
|
|
|
|
#endif //USE_MI_DECRYPTION
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
2020-05-29 23:41:49 +01:00
|
|
|
if (_nextSensorSlot>(MIBLEsensors.size()-1)) {
|
2020-04-21 08:58:33 +01:00
|
|
|
_nextSensorSlot= 0;
|
|
|
|
_counter++;
|
|
|
|
if (MI32.mode.shallReadBatt){
|
|
|
|
MI32.mode.shallReadBatt = 0;
|
|
|
|
}
|
|
|
|
MI32.mode.canConnect = 0;
|
|
|
|
MI32.mode.canScan = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else _counter++;
|
|
|
|
if (MI32.state.sensor>MIBLEsensors.size()-1) {
|
|
|
|
_nextSensorSlot = 0;
|
|
|
|
MI32.mode.canScan = 1;
|
|
|
|
}
|
|
|
|
MI32StartTask(MI32_TASK_SCAN);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Commands
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2020-11-13 16:00:44 +00:00
|
|
|
void CmndMi32Period(void) {
|
|
|
|
if (XdrvMailbox.data_len > 0) {
|
|
|
|
if (1 == XdrvMailbox.payload) {
|
|
|
|
MI32EverySecond(true);
|
|
|
|
} else {
|
|
|
|
MI32.period = XdrvMailbox.payload;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ResponseCmndNumber(MI32.period);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndMi32Time(void) {
|
|
|
|
if (XdrvMailbox.data_len > 0) {
|
|
|
|
if (MIBLEsensors.size() > XdrvMailbox.payload) {
|
|
|
|
if ((LYWSD02 == MIBLEsensors[XdrvMailbox.payload].type) || (MHOC303 == MIBLEsensors[XdrvMailbox.payload].type)) {
|
2021-01-14 16:03:01 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG, PSTR("M32: Will set Time"));
|
2020-11-13 16:00:44 +00:00
|
|
|
MI32.state.sensor = XdrvMailbox.payload;
|
|
|
|
MI32.mode.canScan = 0;
|
|
|
|
MI32.mode.canConnect = 0;
|
|
|
|
MI32.mode.shallSetTime = 1;
|
|
|
|
MI32.mode.willSetTime = 0;
|
|
|
|
ResponseCmndNumber(XdrvMailbox.payload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndMi32Page(void) {
|
|
|
|
if (XdrvMailbox.payload > 0) {
|
|
|
|
MI32.perPage = XdrvMailbox.payload;
|
|
|
|
}
|
|
|
|
ResponseCmndNumber(MI32.perPage);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndMi32Battery(void) {
|
|
|
|
MI32EverySecond(true);
|
|
|
|
MI32.mode.shallReadBatt = 1;
|
|
|
|
MI32.mode.canConnect = 1;
|
|
|
|
ResponseCmndDone();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndMi32Unit(void) {
|
|
|
|
if (XdrvMailbox.data_len > 0) {
|
|
|
|
if (MIBLEsensors.size() > XdrvMailbox.payload) {
|
|
|
|
if ((LYWSD02 == MIBLEsensors[XdrvMailbox.payload].type) || (MHOC303 == MIBLEsensors[XdrvMailbox.payload].type)) {
|
2021-01-14 16:03:01 +00:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG,PSTR("M32: Will set Unit"));
|
2020-11-13 16:00:44 +00:00
|
|
|
MI32.state.sensor = XdrvMailbox.payload;
|
|
|
|
MI32.mode.canScan = 0;
|
|
|
|
MI32.mode.canConnect = 0;
|
|
|
|
MI32.mode.shallSetUnit = 1;
|
|
|
|
MI32.mode.willSetUnit = 0;
|
|
|
|
ResponseCmndNumber(XdrvMailbox.payload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-30 11:29:48 +00:00
|
|
|
#ifdef USE_MI_DECRYPTION
|
2020-11-13 16:00:44 +00:00
|
|
|
void CmndMi32Key(void) {
|
|
|
|
if (44 == XdrvMailbox.data_len) { // a KEY-MAC-string
|
|
|
|
MI32AddKey(XdrvMailbox.data);
|
|
|
|
ResponseCmndDone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // USE_MI_DECRYPTION
|
|
|
|
|
|
|
|
void CmndMi32Beacon(void) {
|
|
|
|
if (XdrvMailbox.data_len == 0) {
|
|
|
|
switch (XdrvMailbox.index) {
|
|
|
|
case 0:
|
|
|
|
MI32.state.beaconScanCounter = 8;
|
|
|
|
ResponseCmndIdxChar(PSTR("Scanning..."));
|
2020-07-12 17:17:27 +01:00
|
|
|
break;
|
2020-11-13 16:00:44 +00:00
|
|
|
case 1: case 2: case 3: case 4:
|
|
|
|
char _MAC[18];
|
|
|
|
ResponseCmndIdxChar(ToHex_P(MIBLEbeacons[XdrvMailbox.index-1].MAC, 6, _MAC, 18, ':'));
|
2020-10-23 09:18:57 +01:00
|
|
|
break;
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
} else {
|
2020-11-13 16:00:44 +00:00
|
|
|
if ((12 == XdrvMailbox.data_len) || (17 == XdrvMailbox.data_len)) { // MAC-string without or with colons
|
|
|
|
switch (XdrvMailbox.index) {
|
|
|
|
case 1: case 2: case 3: case 4:
|
|
|
|
MI32addBeacon(XdrvMailbox.index, XdrvMailbox.data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ResponseCmndIdxChar(XdrvMailbox.data);
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-17 08:30:59 +00:00
|
|
|
void CmndMi32Block(void){
|
|
|
|
if (XdrvMailbox.data_len == 0) {
|
|
|
|
switch (XdrvMailbox.index) {
|
|
|
|
case 0:
|
|
|
|
MIBLEBlockList.clear();
|
2021-01-14 16:03:01 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_INFO,PSTR("M32: Size of ilist: %u"), MIBLEBlockList.size());
|
|
|
|
ResponseCmndIdxChar(PSTR("Block list cleared"));
|
2020-11-17 08:30:59 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
2021-01-14 16:03:01 +00:00
|
|
|
ResponseCmndIdxChar(PSTR("Show block list"));
|
2021-01-01 12:44:04 +00:00
|
|
|
break;
|
2020-11-17 08:30:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
MAC_t _MACasBytes;
|
|
|
|
MI32HexStringToBytes(XdrvMailbox.data,_MACasBytes.buf);
|
|
|
|
switch (XdrvMailbox.index) {
|
|
|
|
case 0:
|
|
|
|
MIBLEBlockList.erase( std::remove_if( begin( MIBLEBlockList ), end( MIBLEBlockList ), [_MACasBytes]( MAC_t& _entry )->bool
|
2021-01-01 12:44:04 +00:00
|
|
|
{ return (memcmp(_entry.buf,_MACasBytes.buf,6) == 0); }
|
2020-11-17 08:30:59 +00:00
|
|
|
), end( MIBLEBlockList ) );
|
|
|
|
ResponseCmndIdxChar(PSTR("MAC not blocked anymore"));
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
bool _notYetInList = true;
|
|
|
|
for (auto &_entry : MIBLEBlockList) {
|
|
|
|
if (memcmp(_entry.buf,_MACasBytes.buf,6) == 0){
|
|
|
|
_notYetInList = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_notYetInList) {
|
|
|
|
MIBLEBlockList.push_back(_MACasBytes);
|
|
|
|
ResponseCmndIdxChar(XdrvMailbox.data);
|
|
|
|
MI32removeMIBLEsensor(_MACasBytes.buf);
|
|
|
|
}
|
2021-01-14 16:03:01 +00:00
|
|
|
// AddLog_P(LOG_LEVEL_INFO,PSTR("M32: Size of ilist: %u"), MIBLEBlockList.size());
|
2021-01-01 12:44:04 +00:00
|
|
|
break;
|
2020-11-17 08:30:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
MI32.mode.shallShowBlockList = 1;
|
|
|
|
MI32triggerTele();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CmndMi32Option(void){
|
|
|
|
bool onOff = atoi(XdrvMailbox.data);
|
|
|
|
switch(XdrvMailbox.index) {
|
|
|
|
case 0:
|
|
|
|
MI32.option.allwaysAggregate = onOff;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
MI32.option.noSummary = onOff;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
MI32.option.directBridgeMode = onOff;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ResponseCmndDone();
|
|
|
|
}
|
|
|
|
|
2020-04-21 08:58:33 +01:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Presentation
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2020-11-22 09:44:02 +00:00
|
|
|
const char HTTP_MI32[] PROGMEM = "{s}MI ESP32 v0917a{m}%u%s / %u{e}";
|
2020-09-26 18:24:58 +01:00
|
|
|
const char HTTP_MI32_MAC[] PROGMEM = "{s}%s %s{m}%s{e}";
|
2020-07-13 16:57:30 +01:00
|
|
|
const char HTTP_RSSI[] PROGMEM = "{s}%s " D_RSSI "{m}%d dBm{e}";
|
2020-07-09 19:47:01 +01:00
|
|
|
const char HTTP_BATTERY[] PROGMEM = "{s}%s" " Battery" "{m}%u %%{e}";
|
2020-07-12 17:17:27 +01:00
|
|
|
const char HTTP_LASTBUTTON[] PROGMEM = "{s}%s Last Button{m}%u {e}";
|
|
|
|
const char HTTP_EVENTS[] PROGMEM = "{s}%s Events{m}%u {e}";
|
|
|
|
const char HTTP_NMT[] PROGMEM = "{s}%s No motion{m}> %u seconds{e}";
|
2020-07-09 19:47:01 +01:00
|
|
|
const char HTTP_MI32_FLORA_DATA[] PROGMEM = "{s}%s" " Fertility" "{m}%u us/cm{e}";
|
2020-04-21 08:58:33 +01:00
|
|
|
const char HTTP_MI32_HL[] PROGMEM = "{s}<hr>{m}<hr>{e}";
|
|
|
|
|
|
|
|
void MI32Show(bool json)
|
|
|
|
{
|
|
|
|
if (json) {
|
2020-11-17 08:30:59 +00:00
|
|
|
if(MI32.mode.shallShowScanResult) {
|
|
|
|
return MI32showScanResults();
|
|
|
|
}
|
|
|
|
else if(MI32.mode.shallShowBlockList) {
|
|
|
|
return MI32showBlockList();
|
|
|
|
}
|
2020-09-26 18:24:58 +01:00
|
|
|
#ifdef USE_HOME_ASSISTANT
|
|
|
|
bool _noSummarySave = MI32.option.noSummary;
|
|
|
|
bool _minimalSummarySave = MI32.option.minimalSummary;
|
|
|
|
if(hass_mode==2){
|
2020-10-21 07:26:38 +01:00
|
|
|
if(MI32.option.holdBackFirstAutodiscovery){
|
|
|
|
if(!MI32.mode.firstAutodiscoveryDone){
|
|
|
|
MI32.mode.firstAutodiscoveryDone = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-09-26 18:24:58 +01:00
|
|
|
MI32.option.noSummary = false;
|
|
|
|
MI32.option.minimalSummary = false;
|
|
|
|
}
|
|
|
|
#endif //USE_HOME_ASSISTANT
|
|
|
|
|
2020-07-12 17:17:27 +01:00
|
|
|
if(!MI32.mode.triggeredTele){
|
|
|
|
MI32.mode.shallClearResults=1;
|
2020-07-20 07:51:17 +01:00
|
|
|
if(MI32.option.noSummary) return; // no message at TELEPERIOD
|
2020-07-12 17:17:27 +01:00
|
|
|
}
|
2020-07-15 16:41:12 +01:00
|
|
|
|
2020-07-20 07:51:17 +01:00
|
|
|
for (uint32_t i = 0; i < MIBLEsensors.size(); i++) {
|
|
|
|
if(MI32.mode.triggeredTele && MIBLEsensors[i].eventType.raw == 0) continue;
|
|
|
|
if(MI32.mode.triggeredTele && MIBLEsensors[i].shallSendMQTT==0) continue;
|
2020-10-30 11:29:48 +00:00
|
|
|
|
2020-07-20 07:51:17 +01:00
|
|
|
ResponseAppend_P(PSTR(",\"%s-%02x%02x%02x\":"), // do not add the '{' now ...
|
2020-07-12 17:17:27 +01:00
|
|
|
kMI32DeviceType[MIBLEsensors[i].type-1],
|
|
|
|
MIBLEsensors[i].MAC[3], MIBLEsensors[i].MAC[4], MIBLEsensors[i].MAC[5]);
|
2020-04-21 10:46:41 +01:00
|
|
|
|
2020-10-30 11:29:48 +00:00
|
|
|
uint32_t _positionCurlyBracket = strlen(TasmotaGlobal.mqtt_data); // ... this will be a ',' first, but later be replaced
|
2020-07-15 16:41:12 +01:00
|
|
|
|
2020-07-20 07:51:17 +01:00
|
|
|
if((!MI32.mode.triggeredTele && !MI32.option.minimalSummary)||MI32.mode.triggeredTele){
|
|
|
|
bool tempHumSended = false;
|
|
|
|
if(MIBLEsensors[i].feature.tempHum){
|
|
|
|
if(MIBLEsensors[i].eventType.tempHum || !MI32.mode.triggeredTele || MI32.option.allwaysAggregate){
|
2020-09-26 18:24:58 +01:00
|
|
|
if (!isnan(MIBLEsensors[i].hum) && !isnan(MIBLEsensors[i].temp)
|
|
|
|
#ifdef USE_HOME_ASSISTANT
|
2020-10-21 07:26:38 +01:00
|
|
|
||(hass_mode!=-1)
|
2020-09-26 18:24:58 +01:00
|
|
|
#endif //USE_HOME_ASSISTANT
|
|
|
|
) {
|
2020-07-20 07:51:17 +01:00
|
|
|
ResponseAppend_P(PSTR(","));
|
|
|
|
ResponseAppendTHD(MIBLEsensors[i].temp, MIBLEsensors[i].hum);
|
|
|
|
tempHumSended = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(MIBLEsensors[i].feature.temp && !tempHumSended){
|
|
|
|
if(MIBLEsensors[i].eventType.temp || !MI32.mode.triggeredTele || MI32.option.allwaysAggregate) {
|
2020-09-26 18:24:58 +01:00
|
|
|
if (!isnan(MIBLEsensors[i].temp)
|
|
|
|
#ifdef USE_HOME_ASSISTANT
|
2020-10-21 07:26:38 +01:00
|
|
|
||(hass_mode!=-1)
|
2020-09-26 18:24:58 +01:00
|
|
|
#endif //USE_HOME_ASSISTANT
|
|
|
|
) {
|
2020-07-20 07:51:17 +01:00
|
|
|
char temperature[FLOATSZ];
|
|
|
|
dtostrfd(MIBLEsensors[i].temp, Settings.flag2.temperature_resolution, temperature);
|
|
|
|
ResponseAppend_P(PSTR(",\"" D_JSON_TEMPERATURE "\":%s"), temperature);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(MIBLEsensors[i].feature.hum && !tempHumSended){
|
|
|
|
if(MIBLEsensors[i].eventType.hum || !MI32.mode.triggeredTele || MI32.option.allwaysAggregate) {
|
2020-09-26 18:24:58 +01:00
|
|
|
if (!isnan(MIBLEsensors[i].hum)
|
|
|
|
#ifdef USE_HOME_ASSISTANT
|
2020-10-21 07:26:38 +01:00
|
|
|
||(hass_mode!=-1)
|
2020-09-26 18:24:58 +01:00
|
|
|
#endif //USE_HOME_ASSISTANT
|
|
|
|
) {
|
2020-07-20 07:51:17 +01:00
|
|
|
char hum[FLOATSZ];
|
|
|
|
dtostrfd(MIBLEsensors[i].hum, Settings.flag2.humidity_resolution, hum);
|
|
|
|
ResponseAppend_P(PSTR(",\"" D_JSON_HUMIDITY "\":%s"), hum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (MIBLEsensors[i].feature.lux){
|
|
|
|
if(MIBLEsensors[i].eventType.lux || !MI32.mode.triggeredTele || MI32.option.allwaysAggregate){
|
2020-09-26 18:24:58 +01:00
|
|
|
if (MIBLEsensors[i].lux!=0x0ffffff
|
|
|
|
#ifdef USE_HOME_ASSISTANT
|
2020-10-21 07:26:38 +01:00
|
|
|
||(hass_mode!=-1)
|
2020-09-26 18:24:58 +01:00
|
|
|
#endif //USE_HOME_ASSISTANT
|
|
|
|
) { // this is the error code -> no lux
|
2020-07-20 07:51:17 +01:00
|
|
|
ResponseAppend_P(PSTR(",\"" D_JSON_ILLUMINANCE "\":%u"), MIBLEsensors[i].lux);
|
2020-10-21 07:26:38 +01:00
|
|
|
#ifdef USE_HOME_ASSISTANT
|
|
|
|
if (MIBLEsensors[i].lux==0x0ffffff) MI32nullifyEndOfMQTT_DATA();
|
|
|
|
#endif //USE_HOME_ASSISTANT
|
2020-07-20 07:51:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (MIBLEsensors[i].feature.moist){
|
|
|
|
if(MIBLEsensors[i].eventType.moist || !MI32.mode.triggeredTele || MI32.option.allwaysAggregate){
|
2020-09-26 18:24:58 +01:00
|
|
|
if (MIBLEsensors[i].moisture!=0xff
|
|
|
|
#ifdef USE_HOME_ASSISTANT
|
2020-10-21 07:26:38 +01:00
|
|
|
||(hass_mode!=-1)
|
2020-09-26 18:24:58 +01:00
|
|
|
#endif //USE_HOME_ASSISTANT
|
|
|
|
) {
|
2020-07-20 07:51:17 +01:00
|
|
|
ResponseAppend_P(PSTR(",\"" D_JSON_MOISTURE "\":%u"), MIBLEsensors[i].moisture);
|
2020-10-21 07:26:38 +01:00
|
|
|
#ifdef USE_HOME_ASSISTANT
|
|
|
|
if (MIBLEsensors[i].moisture==0xff) MI32nullifyEndOfMQTT_DATA();
|
|
|
|
#endif //USE_HOME_ASSISTANT
|
2020-07-20 07:51:17 +01:00
|
|
|
}
|
|
|
|
}
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
2020-07-20 07:51:17 +01:00
|
|
|
if (MIBLEsensors[i].feature.fert){
|
|
|
|
if(MIBLEsensors[i].eventType.fert || !MI32.mode.triggeredTele || MI32.option.allwaysAggregate){
|
2020-09-26 18:24:58 +01:00
|
|
|
if (MIBLEsensors[i].fertility!=0xffff
|
|
|
|
#ifdef USE_HOME_ASSISTANT
|
2020-10-21 07:26:38 +01:00
|
|
|
||(hass_mode!=-1)
|
2020-09-26 18:24:58 +01:00
|
|
|
#endif //USE_HOME_ASSISTANT
|
|
|
|
) {
|
2020-07-20 07:51:17 +01:00
|
|
|
ResponseAppend_P(PSTR(",\"Fertility\":%u"), MIBLEsensors[i].fertility);
|
2020-10-21 07:26:38 +01:00
|
|
|
#ifdef USE_HOME_ASSISTANT
|
|
|
|
if (MIBLEsensors[i].fertility==0xffff) MI32nullifyEndOfMQTT_DATA();
|
|
|
|
#endif //USE_HOME_ASSISTANT
|
2020-07-20 07:51:17 +01:00
|
|
|
}
|
|
|
|
}
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
2020-07-20 07:51:17 +01:00
|
|
|
if (MIBLEsensors[i].feature.Btn){
|
2020-09-26 18:24:58 +01:00
|
|
|
if(MIBLEsensors[i].eventType.Btn
|
|
|
|
#ifdef USE_HOME_ASSISTANT
|
|
|
|
||(hass_mode==2)
|
|
|
|
#endif //USE_HOME_ASSISTANT
|
|
|
|
){
|
2020-07-20 07:51:17 +01:00
|
|
|
ResponseAppend_P(PSTR(",\"Btn\":%u"),MIBLEsensors[i].Btn);
|
|
|
|
}
|
2020-04-21 10:46:41 +01:00
|
|
|
}
|
2020-07-20 07:51:17 +01:00
|
|
|
} // minimal summary
|
|
|
|
if (MIBLEsensors[i].feature.PIR){
|
|
|
|
if(MIBLEsensors[i].eventType.motion || !MI32.mode.triggeredTele){
|
|
|
|
if(MI32.mode.triggeredTele) ResponseAppend_P(PSTR(",\"PIR\":1")); // only real-time
|
|
|
|
ResponseAppend_P(PSTR(",\"Events\":%u"),MIBLEsensors[i].events);
|
2020-04-21 10:46:41 +01:00
|
|
|
}
|
2020-07-20 07:51:17 +01:00
|
|
|
else if(MIBLEsensors[i].eventType.noMotion && MI32.mode.triggeredTele){
|
|
|
|
ResponseAppend_P(PSTR(",\"PIR\":0"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MIBLEsensors[i].type == FLORA && !MI32.mode.triggeredTele) {
|
2020-07-13 20:24:38 +01:00
|
|
|
if (MIBLEsensors[i].firmware[0] != '\0') { // this is the error code -> no firmware
|
2020-07-13 20:08:10 +01:00
|
|
|
ResponseAppend_P(PSTR(",\"Firmware\":\"%s\""), MIBLEsensors[i].firmware);
|
|
|
|
}
|
2020-04-21 10:46:41 +01:00
|
|
|
}
|
2020-07-20 07:51:17 +01:00
|
|
|
|
|
|
|
if (MIBLEsensors[i].feature.NMT || !MI32.mode.triggeredTele){
|
|
|
|
if(MIBLEsensors[i].eventType.NMT){
|
|
|
|
ResponseAppend_P(PSTR(",\"NMT\":%u"), MIBLEsensors[i].NMT);
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
2020-04-21 10:46:41 +01:00
|
|
|
}
|
2020-07-20 07:51:17 +01:00
|
|
|
if (MIBLEsensors[i].feature.bat){
|
|
|
|
if(MIBLEsensors[i].eventType.bat || !MI32.mode.triggeredTele || MI32.option.allwaysAggregate){
|
2020-09-26 18:24:58 +01:00
|
|
|
if (MIBLEsensors[i].bat != 0x00
|
|
|
|
#ifdef USE_HOME_ASSISTANT
|
2020-10-21 07:26:38 +01:00
|
|
|
||(hass_mode!=-1)
|
2020-09-26 18:24:58 +01:00
|
|
|
#endif //USE_HOME_ASSISTANT
|
|
|
|
) { // this is the error code -> no battery
|
2020-07-20 07:51:17 +01:00
|
|
|
ResponseAppend_P(PSTR(",\"Battery\":%u"), MIBLEsensors[i].bat);
|
2020-10-21 07:26:38 +01:00
|
|
|
#ifdef USE_HOME_ASSISTANT
|
|
|
|
if (MIBLEsensors[i].bat == 0x00) MI32nullifyEndOfMQTT_DATA();
|
|
|
|
#endif //USE_HOME_ASSISTANT
|
2020-07-20 07:51:17 +01:00
|
|
|
}
|
|
|
|
}
|
2020-04-21 10:46:41 +01:00
|
|
|
}
|
2020-10-23 09:18:57 +01:00
|
|
|
if (MI32.option.showRSSI) ResponseAppend_P(PSTR(",\"RSSI\":%d"), MIBLEsensors[i].RSSI);
|
2020-07-20 07:51:17 +01:00
|
|
|
|
2020-10-30 11:29:48 +00:00
|
|
|
if(_positionCurlyBracket==strlen(TasmotaGlobal.mqtt_data)) ResponseAppend_P(PSTR(",")); // write some random char, to be overwritten in the next step
|
2020-04-21 10:46:41 +01:00
|
|
|
ResponseAppend_P(PSTR("}"));
|
2020-10-30 11:29:48 +00:00
|
|
|
TasmotaGlobal.mqtt_data[_positionCurlyBracket] = '{';
|
2020-07-20 07:51:17 +01:00
|
|
|
MIBLEsensors[i].eventType.raw = 0;
|
|
|
|
if(MIBLEsensors[i].shallSendMQTT==1){
|
|
|
|
MIBLEsensors[i].shallSendMQTT = 0;
|
2020-09-26 18:24:58 +01:00
|
|
|
continue;
|
2020-07-20 07:51:17 +01:00
|
|
|
}
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
2020-07-20 07:51:17 +01:00
|
|
|
MI32.mode.triggeredTele = 0;
|
2020-10-23 09:18:57 +01:00
|
|
|
// add beacons
|
|
|
|
uint32_t _idx = 0;
|
|
|
|
for (auto _beacon : MIBLEbeacons){
|
|
|
|
_idx++;
|
|
|
|
if(!_beacon.active) continue;
|
|
|
|
char _MAC[18];
|
|
|
|
ToHex_P(_beacon.MAC,6,_MAC,18,':');
|
|
|
|
ResponseAppend_P(PSTR(",\"Beacon%u\":{\"MAC\":\"%s\",\"CID\":\"0x%04x\",\"SVC\":\"0x%04x\","
|
|
|
|
"\"UUID\":\"0x%04x\",\"Time\":%u,\"RSSI\":%d}"),
|
|
|
|
_idx,_MAC,_beacon.CID,_beacon.SVC,_beacon.UUID,_beacon.time,_beacon.RSSI);
|
|
|
|
}
|
2020-09-26 18:24:58 +01:00
|
|
|
#ifdef USE_HOME_ASSISTANT
|
|
|
|
if(hass_mode==2){
|
|
|
|
MI32.option.noSummary = _noSummarySave;
|
|
|
|
MI32.option.minimalSummary = _minimalSummarySave;
|
|
|
|
}
|
|
|
|
#endif //USE_HOME_ASSISTANT
|
2020-04-21 08:58:33 +01:00
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
} else {
|
|
|
|
static uint16_t _page = 0;
|
|
|
|
static uint16_t _counter = 0;
|
|
|
|
int32_t i = _page * MI32.perPage;
|
|
|
|
uint32_t j = i + MI32.perPage;
|
|
|
|
if (j+1>MIBLEsensors.size()){
|
|
|
|
j = MIBLEsensors.size();
|
|
|
|
}
|
|
|
|
char stemp[5] ={0};
|
|
|
|
if (MIBLEsensors.size()-(_page*MI32.perPage)>1 && MI32.perPage!=1) {
|
|
|
|
sprintf_P(stemp,"-%u",j);
|
|
|
|
}
|
|
|
|
if (MIBLEsensors.size()==0) i=-1; // only for the GUI
|
|
|
|
|
|
|
|
WSContentSend_PD(HTTP_MI32, i+1,stemp,MIBLEsensors.size());
|
|
|
|
for (i; i<j; i++) {
|
|
|
|
WSContentSend_PD(HTTP_MI32_HL);
|
2020-09-26 18:24:58 +01:00
|
|
|
char _MAC[18];
|
|
|
|
ToHex_P(MIBLEsensors[i].MAC,6,_MAC,18,':');
|
|
|
|
WSContentSend_PD(HTTP_MI32_MAC, kMI32DeviceType[MIBLEsensors[i].type-1], D_MAC_ADDRESS, _MAC);
|
2020-10-23 09:18:57 +01:00
|
|
|
WSContentSend_PD(HTTP_RSSI, kMI32DeviceType[MIBLEsensors[i].type-1], MIBLEsensors[i].RSSI);
|
2020-04-21 10:46:41 +01:00
|
|
|
if (MIBLEsensors[i].type==FLORA) {
|
|
|
|
if (!isnan(MIBLEsensors[i].temp)) {
|
2020-04-21 08:58:33 +01:00
|
|
|
char temperature[FLOATSZ];
|
|
|
|
dtostrfd(MIBLEsensors[i].temp, Settings.flag2.temperature_resolution, temperature);
|
2020-07-12 17:17:27 +01:00
|
|
|
WSContentSend_PD(HTTP_SNS_TEMP, kMI32DeviceType[MIBLEsensors[i].type-1], temperature, TempUnit());
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
2020-07-15 16:41:12 +01:00
|
|
|
if (MIBLEsensors[i].moisture!=0xff) {
|
|
|
|
WSContentSend_PD(HTTP_SNS_MOISTURE, kMI32DeviceType[MIBLEsensors[i].type-1], MIBLEsensors[i].moisture);
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
2020-07-15 16:41:12 +01:00
|
|
|
if (MIBLEsensors[i].fertility!=0xffff) {
|
|
|
|
WSContentSend_PD(HTTP_MI32_FLORA_DATA, kMI32DeviceType[MIBLEsensors[i].type-1], MIBLEsensors[i].fertility);
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
}
|
2020-04-21 10:46:41 +01:00
|
|
|
if (MIBLEsensors[i].type>FLORA) { // everything "above" Flora
|
|
|
|
if (!isnan(MIBLEsensors[i].hum) && !isnan(MIBLEsensors[i].temp)) {
|
2020-07-12 17:17:27 +01:00
|
|
|
WSContentSend_THD(kMI32DeviceType[MIBLEsensors[i].type-1], MIBLEsensors[i].temp, MIBLEsensors[i].hum);
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
2020-04-21 10:06:30 +01:00
|
|
|
}
|
2020-07-12 17:17:27 +01:00
|
|
|
#ifdef USE_MI_DECRYPTION
|
|
|
|
if (MIBLEsensors[i].type==NLIGHT || MIBLEsensors[i].type==MJYD2S) {
|
|
|
|
#else
|
|
|
|
if (MIBLEsensors[i].type==NLIGHT) {
|
|
|
|
#endif //USE_MI_DECRYPTION
|
|
|
|
WSContentSend_PD(HTTP_EVENTS, kMI32DeviceType[MIBLEsensors[i].type-1], MIBLEsensors[i].events);
|
|
|
|
if(MIBLEsensors[i].NMT>0) WSContentSend_PD(HTTP_NMT, kMI32DeviceType[MIBLEsensors[i].type-1], MIBLEsensors[i].NMT);
|
|
|
|
}
|
|
|
|
if (MIBLEsensors[i].lux!=0x00ffffff) { // this is the error code -> no valid value
|
|
|
|
WSContentSend_PD(HTTP_SNS_ILLUMINANCE, kMI32DeviceType[MIBLEsensors[i].type-1], MIBLEsensors[i].lux);
|
|
|
|
}
|
2020-04-21 08:58:33 +01:00
|
|
|
if(MIBLEsensors[i].bat!=0x00){
|
2020-07-12 17:17:27 +01:00
|
|
|
WSContentSend_PD(HTTP_BATTERY, kMI32DeviceType[MIBLEsensors[i].type-1], MIBLEsensors[i].bat);
|
|
|
|
}
|
|
|
|
if (MIBLEsensors[i].type==YEERC){
|
|
|
|
WSContentSend_PD(HTTP_LASTBUTTON, kMI32DeviceType[MIBLEsensors[i].type-1], MIBLEsensors[i].Btn);
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_counter++;
|
|
|
|
if(_counter>3) {
|
|
|
|
_page++;
|
|
|
|
_counter=0;
|
|
|
|
}
|
2020-04-21 10:46:41 +01:00
|
|
|
if (MIBLEsensors.size()%MI32.perPage==0 && _page==MIBLEsensors.size()/MI32.perPage) { _page = 0; }
|
|
|
|
if (_page>MIBLEsensors.size()/MI32.perPage) { _page = 0; }
|
2020-10-23 09:18:57 +01:00
|
|
|
|
|
|
|
//always at the bottom of the page
|
|
|
|
uint32_t _idx=0;
|
|
|
|
if(MI32.mode.activeBeacon){
|
|
|
|
WSContentSend_PD(HTTP_MI32_HL);
|
|
|
|
char _sbeacon[] = "Beacon1";
|
|
|
|
for (auto &_beacon : MIBLEbeacons){
|
|
|
|
_idx++;
|
|
|
|
if(!_beacon.active) continue;
|
|
|
|
WSContentSend_PD(HTTP_MI32_HL);
|
|
|
|
_sbeacon[6] = _idx + 0x30;
|
|
|
|
char _MAC[18];
|
|
|
|
ToHex_P(_beacon.MAC,6,_MAC,18,':');
|
|
|
|
WSContentSend_PD(HTTP_MI32_MAC, _sbeacon, D_MAC_ADDRESS, _MAC);
|
|
|
|
WSContentSend_PD(HTTP_RSSI, _sbeacon, _beacon.RSSI);
|
|
|
|
if(_beacon.CID!=0) WSContentSend_PD(PSTR("{s}Beacon%u CID{m}0x%04X{e}"),_idx, _beacon.CID);
|
|
|
|
if(_beacon.SVC!=0) WSContentSend_PD(PSTR("{s}Beacon%u SVC{m}0x%04X{e}"),_idx, _beacon.SVC);
|
|
|
|
if(_beacon.UUID!=0) WSContentSend_PD(PSTR("{s}Beacon%u UUID{m}0x%04X{e}"),_idx, _beacon.UUID);
|
|
|
|
WSContentSend_PD(PSTR("{s}Beacon%u Time{m}%u seconds{e}"),_idx, _beacon.time);
|
|
|
|
}
|
|
|
|
}
|
2020-04-21 08:58:33 +01:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
bool Xsns62(uint8_t function)
|
|
|
|
{
|
2020-11-13 16:09:43 +00:00
|
|
|
if (!Settings.flag5.mi32_enable) { return false; } // SetOption115 - Enable ESP32 MI32 BLE
|
2020-11-13 16:00:44 +00:00
|
|
|
|
2020-04-21 08:58:33 +01:00
|
|
|
bool result = false;
|
2020-11-13 16:00:44 +00:00
|
|
|
|
2020-04-21 08:58:33 +01:00
|
|
|
if (FUNC_INIT == function){
|
2020-11-09 19:27:11 +00:00
|
|
|
MI32PreInit();
|
2020-04-21 08:58:33 +01:00
|
|
|
}
|
|
|
|
|
2020-11-13 16:00:44 +00:00
|
|
|
if (!MI32.mode.init) {
|
|
|
|
if (function == FUNC_EVERY_250_MSECOND) {
|
2020-11-09 19:27:11 +00:00
|
|
|
MI32Init();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_EVERY_50_MSECOND:
|
|
|
|
MI32Every50mSecond();
|
|
|
|
break;
|
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
MI32EverySecond(false);
|
|
|
|
break;
|
|
|
|
case FUNC_COMMAND:
|
2020-11-13 16:00:44 +00:00
|
|
|
result = DecodeCommand(kMI32_Commands, MI32_Commands);
|
2020-11-09 19:27:11 +00:00
|
|
|
break;
|
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
MI32Show(1);
|
|
|
|
break;
|
2020-04-21 08:58:33 +01:00
|
|
|
#ifdef USE_WEBSERVER
|
2020-11-09 19:27:11 +00:00
|
|
|
case FUNC_WEB_SENSOR:
|
|
|
|
MI32Show(0);
|
|
|
|
break;
|
2020-04-21 08:58:33 +01:00
|
|
|
#endif // USE_WEBSERVER
|
2020-11-09 19:27:11 +00:00
|
|
|
}
|
2020-04-21 08:58:33 +01:00
|
|
|
return result;
|
|
|
|
}
|
2020-04-21 10:06:30 +01:00
|
|
|
#endif // USE_MI_ESP32
|
2020-07-12 17:17:27 +01:00
|
|
|
#endif // ESP32
|