diff --git a/main/User_config.h b/main/User_config.h index 554eef3..dddd487 100644 --- a/main/User_config.h +++ b/main/User_config.h @@ -1,17 +1,17 @@ -/* - OpenMQTTGateway - ESP8266 or Arduino program for home automation +/* + OpenMQTTGateway - ESP8266 or Arduino program for home automation - Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker + Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker Send and receiving command by MQTT - + This program enables to: - receive MQTT data from a topic and send signals corresponding to the received MQTT data - publish MQTT data to a different topic related to received signals - + Copyright: (c)Florian ROBERT - + This file is part of OpenMQTTGateway. - + OpenMQTTGateway 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 @@ -41,11 +41,11 @@ * - mqtt_pass * - mqtt_server * - mqtt_port - * + * * To completely disable WifiManager, define ESPWifiManualSetup. * If you do so, please don't forget to set these variables before compiling - * - * Otherwise you can provide these credentials on the web interface after connecting + * + * Otherwise you can provide these credentials on the web interface after connecting * to the access point with your password (SSID: WifiManager_ssid, password: WifiManager_password) */ /*-------------DEFINE GATEWAY NAME BELOW IT CAN ALSO BE DEFINED IN platformio.ini----------------*/ @@ -172,6 +172,10 @@ const byte mac[] = {0xDE, 0xED, 0xBA, 0xFE, 0x54, 0x95}; //W5100 ethernet shield #endif #if defined(ESP8266) || defined(ESP32) +// Uncomment to use a device running TheengsGateway to decode BLE data. (https://github.com/theengs/gateway) +// Set the topic to the subscribe topic configured in the TheengGateway +// #define MQTTDecodeTopic "MQTTDecode" + // The root ca certificate used for validating the MQTT broker // The certificate must be in PEM ascii format const char* certificate PROGMEM = R"EOF(" diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index 2035f05..6e55afb 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -66,7 +66,9 @@ using namespace std; # define device_flags_isBlackL 1 << 2 # define device_flags_connect 1 << 3 +# ifndef MQTTDecodeTopic TheengsDecoder decoder; +# endif struct decompose { int start; @@ -95,7 +97,16 @@ void pubBTMainCore(JsonObject& data, bool haPresenceEnabled = true) { if (abs((int)data["rssi"] | 0) < minRssi && data.containsKey("id")) { String mac_address = data["id"].as(); mac_address.replace(":", ""); +# ifdef MQTTDecodeTopic + String mactopic; + if (data.containsKey("model")) { + mactopic = subjectBTtoMQTT + String("/") + mac_address; + } else { + mactopic = String("/") + MQTTDecodeTopic; + } +# else String mactopic = subjectBTtoMQTT + String("/") + mac_address; +# endif pub((char*)mactopic.c_str(), data); } if (haPresenceEnabled && data.containsKey("distance")) { @@ -870,6 +881,7 @@ void launchBTDiscovery() { p->sensorModel_id != TheengsDecoder::BLE_ID_NUM::GAEN) { String macWOdots = String(p->macAdr); macWOdots.replace(":", ""); +# ifndef MQTTDecodeTopic if (p->sensorModel_id > TheengsDecoder::BLE_ID_NUM::UNKNOWN_MODEL && p->sensorModel_id < TheengsDecoder::BLE_ID_NUM::BLE_ID_MAX && p->sensorModel_id != TheengsDecoder::BLE_ID_NUM::HHCCJCY01HHCC) { // Exception on HHCCJCY01HHCC as this one is discoverable and connectable for battery retrieving @@ -889,11 +901,11 @@ void launchBTDiscovery() { String discovery_topic = String(subjectBTtoMQTT) + "/" + macWOdots; String entity_name = String(model_id.c_str()) + "-" + String(prop.key().c_str()); String unique_id = macWOdots + "-" + String(prop.key().c_str()); -# if OpenHABDiscovery +# if OpenHABDiscovery String value_template = "{{ value_json." + String(prop.key().c_str()) + "}}"; -# else +# else String value_template = "{{ value_json." + String(prop.key().c_str()) + " | is_defined }}"; -# endif +# endif if (p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::SBS1 && !strcmp(prop.key().c_str(), "state")) { String payload_on = "{\"SBS1\":\"on\",\"mac\":\"" + String(p->macAdr) + "\"}"; String payload_off = "{\"SBS1\":\"off\",\"mac\":\"" + String(p->macAdr) + "\"}"; @@ -915,29 +927,33 @@ void launchBTDiscovery() { } } } - } else if (p->sensorModel_id > BLEconectable::id::MIN && - p->sensorModel_id < BLEconectable::id::MAX || - p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::HHCCJCY01HHCC) { - // Discovery of sensors from which we retrieve data by connect - if (p->sensorModel_id == BLEconectable::id::DT24_BLE) { - DT24Discovery(macWOdots.c_str(), "DT24-BLE"); + } else +# endif + { + if (p->sensorModel_id > BLEconectable::id::MIN && + p->sensorModel_id < BLEconectable::id::MAX || + p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::HHCCJCY01HHCC) { + // Discovery of sensors from which we retrieve data only by connect + if (p->sensorModel_id == BLEconectable::id::DT24_BLE) { + DT24Discovery(macWOdots.c_str(), "DT24-BLE"); + } + if (p->sensorModel_id == BLEconectable::id::LYWSD03MMC) { + LYWSD03MMCDiscovery(macWOdots.c_str(), "LYWSD03MMC"); + } + if (p->sensorModel_id == BLEconectable::id::MHO_C401) { + MHO_C401Discovery(macWOdots.c_str(), "MHO-C401"); + } + if (p->sensorModel_id == BLEconectable::id::XMWSDJ04MMC) { + XMWSDJ04MMCDiscovery(macWOdots.c_str(), "XMWSDJ04MMC"); + } + if (p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::HHCCJCY01HHCC) { + HHCCJCY01HHCCDiscovery(macWOdots.c_str(), "HHCCJCY01HHCC"); + } + } else { + Log.trace(F("Device UNKNOWN_MODEL %s" CR), p->macAdr); } - if (p->sensorModel_id == BLEconectable::id::LYWSD03MMC) { - LYWSD03MMCDiscovery(macWOdots.c_str(), "LYWSD03MMC"); - } - if (p->sensorModel_id == BLEconectable::id::MHO_C401) { - MHO_C401Discovery(macWOdots.c_str(), "MHO-C401"); - } - if (p->sensorModel_id == BLEconectable::id::XMWSDJ04MMC) { - XMWSDJ04MMCDiscovery(macWOdots.c_str(), "XMWSDJ04MMC"); - } - if (p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::HHCCJCY01HHCC) { - HHCCJCY01HHCCDiscovery(macWOdots.c_str(), "HHCCJCY01HHCC"); - } - } else { - Log.trace(F("Device UNKNOWN_MODEL %s" CR), p->macAdr); + p->isDisc = true; // we don't need the semaphore and all the search magic via createOrUpdateDevice } - p->isDisc = true; // we don't need the semaphore and all the search magic via createOrUpdateDevice } else { Log.trace(F("Device already discovered or that doesn't require discovery %s" CR), p->macAdr); } @@ -977,8 +993,10 @@ void PublishDeviceData(JsonObject& BLEdata, bool processBLEData) { void process_bledata(JsonObject& BLEdata) { const char* mac = BLEdata["id"].as(); - int model_id = decoder.decodeBLEJson(BLEdata); + int model_id = -1; int mac_type = BLEdata["mac_type"].as(); +# ifndef MQTTDecodeTopic + model_id = decoder.decodeBLEJson(BLEdata); if (model_id >= 0) { // Broadcaster devices Log.trace(F("Decoder found device: %s" CR), BLEdata["model_id"].as()); if (model_id == TheengsDecoder::BLE_ID_NUM::HHCCJCY01HHCC) { @@ -986,22 +1004,37 @@ void process_bledata(JsonObject& BLEdata) { } else { createOrUpdateDevice(mac, device_flags_init, model_id, mac_type); } - } else if (BLEdata.containsKey("name")) { // Connectable devices - std::string name = BLEdata["name"]; - if (name.compare("LYWSD03MMC") == 0) - model_id = BLEconectable::id::LYWSD03MMC; - else if (name.compare("DT24-BLE") == 0) - model_id = BLEconectable::id::DT24_BLE; - else if (name.compare("MHO-C401") == 0) - model_id = BLEconectable::id::MHO_C401; - else if (name.compare("XMWSDJ04MMC") == 0) - model_id = BLEconectable::id::XMWSDJ04MMC; + } else +# endif + { + if (BLEdata.containsKey("name")) { // Connectable devices + std::string name = BLEdata["name"]; + if (name.compare("LYWSD03MMC") == 0) + model_id = BLEconectable::id::LYWSD03MMC; + else if (name.compare("DT24-BLE") == 0) + model_id = BLEconectable::id::DT24_BLE; + else if (name.compare("MHO-C401") == 0) + model_id = BLEconectable::id::MHO_C401; + else if (name.compare("XMWSDJ04MMC") == 0) + model_id = BLEconectable::id::XMWSDJ04MMC; - if (model_id > 0) { - Log.trace(F("Connectable device found: %s" CR), name.c_str()); - createOrUpdateDevice(mac, device_flags_connect, model_id, mac_type); + if (model_id > 0) { + Log.trace(F("Connectable device found: %s" CR), name.c_str()); + createOrUpdateDevice(mac, device_flags_connect, model_id, mac_type); + } } - } else { +# ifdef MQTTDecodeTopic + else if (model_id < 0 && BLEdata.containsKey("servicedata")) { + const char* service_data = (const char*)(BLEdata["servicedata"] | ""); + if (strstr(service_data, "209800") != NULL) { + model_id == TheengsDecoder::BLE_ID_NUM::HHCCJCY01HHCC; + Log.trace(F("Connectable device found: HHCCJCY01HHCC" CR)); + createOrUpdateDevice(mac, device_flags_connect, model_id, mac_type); + } + } +# endif + } + if (model_id < 0) { Log.trace(F("No device found " CR)); } } diff --git a/main/config_BT.h b/main/config_BT.h index 7fd1e08..c6c88be 100644 --- a/main/config_BT.h +++ b/main/config_BT.h @@ -52,7 +52,10 @@ bool bleConnect = AttemptBLECOnnect; /*----------------------BT topics & parameters-------------------------*/ #define subjectBTtoMQTT "/BTtoMQTT" #define subjectMQTTtoBTset "/commands/MQTTtoBT/config" -#define MinimumRSSI -100 //default minimum rssi value, all the devices below -90 will not be reported +// Uncomment to send undecoded device data to another gateway device for decoding +// #define MQTTDecodeTopic "BTtoMQTT/undecoded" + +#define MinimumRSSI -100 //default minimum rssi value, all the devices below -90 will not be reported #ifndef Scan_duration # define Scan_duration 10000 //define the time for a scan diff --git a/platformio.ini b/platformio.ini index d198d66..8ab786f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -408,6 +408,23 @@ build_flags = '-DLED_SEND_RECEIVE_ON=0' '-DGateway_Name="OpenMQTTGateway_ESP32_BLE"' +[env:esp32dev-ble-mqtt-decode] +platform = ${com.esp32_platform} +board = esp32dev +board_build.partitions = min_spiffs.csv +lib_deps = + ${com-esp.lib_deps} + ${libraries.wifimanager32} + ${libraries.ble} + ${libraries.decoder} +build_flags = + ${com-esp.build_flags} + '-DZgatewayBT="BT"' + '-DLED_SEND_RECEIVE=2' + '-DLED_SEND_RECEIVE_ON=0' + '-DGateway_Name="OpenMQTTGateway_ESP32_BLE"' + '-DMQTTDecodeTopic="BTtoMQTT/undecoded"' + [env:esp32dev-ble-aws] platform = ${com.esp32_platform} board = esp32dev