mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-30 03:49:13 +03:00
@@ -1,27 +1,120 @@
|
|||||||
#include "Global.h"
|
#include "Global.h"
|
||||||
#include "classes/IoTItem.h"
|
#include "classes/IoTItem.h"
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#ifdef ESP32
|
|
||||||
#include <NimBLEDevice.h>
|
#include <NimBLEDevice.h>
|
||||||
#include <decoder.h>
|
#include <decoder.h>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
// Создаем переменную для хранения данных с датчиков bluetooth
|
// Создаем переменную для хранения данных с датчиков bluetooth
|
||||||
StaticJsonDocument<JSON_BUFFER_SIZE * 4> BLEbuffer;
|
// StaticJsonDocument<JSON_BUFFER_SIZE * 4> BLEbuffer;
|
||||||
JsonObject extBLEdata = BLEbuffer.to<JsonObject>();
|
// DynamicJsonDocument extBLEdata(JSON_BUFFER_SIZE * 4);
|
||||||
|
// JsonObject extBLEdata = BLEbuffer.to<JsonObject>();
|
||||||
|
class BleSens;
|
||||||
|
std::vector<BleSens *> BleSensArray;
|
||||||
|
|
||||||
BLEScan *pBLEScan;
|
class BleSens : public IoTItem
|
||||||
TheengsDecoder decoder;
|
{
|
||||||
StaticJsonDocument<512> doc;
|
private:
|
||||||
|
// описание параметров передаваемых из настроек датчика из веба
|
||||||
|
String _MAC;
|
||||||
|
String _sensor;
|
||||||
|
|
||||||
|
public:
|
||||||
|
String whoIAm(/*String &mac, String &sens*/)
|
||||||
|
{
|
||||||
|
// mac = _MAC;
|
||||||
|
// sens = _sensor;
|
||||||
|
return _MAC;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setBLEdata(JsonObject extBLEdata)
|
||||||
|
{
|
||||||
|
if (_sensor == "last")
|
||||||
|
{
|
||||||
|
int valInt = extBLEdata[_sensor].as<int>();
|
||||||
|
char *s;
|
||||||
|
s = TimeToString(millis() / 1000 - valInt / 1000);
|
||||||
|
value.isDecimal = 0;
|
||||||
|
if (valInt > 0)
|
||||||
|
{
|
||||||
|
value.valS = s;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
value.valS = "";
|
||||||
|
}
|
||||||
|
regEvent(value.valS, _id);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
String valStr = extBLEdata[_sensor].as<String>();
|
||||||
|
if (valStr != "null")
|
||||||
|
{
|
||||||
|
if (value.isDecimal == isDigitDotCommaStr(valStr))
|
||||||
|
{
|
||||||
|
value.isDecimal = 1;
|
||||||
|
value.valD = valStr.toFloat();
|
||||||
|
regEvent(value.valD, _id);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
value.isDecimal = 0;
|
||||||
|
value.valS = valStr;
|
||||||
|
regEvent(value.valS, _id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
value.isDecimal = 0;
|
||||||
|
value.valS = "";
|
||||||
|
regEvent(value.valS, _id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
char *TimeToString(unsigned long t)
|
||||||
|
{
|
||||||
|
static char str[12];
|
||||||
|
long h = t / 3600;
|
||||||
|
t = t % 3600;
|
||||||
|
int m = t / 60;
|
||||||
|
int s = t % 60;
|
||||||
|
sprintf(str, "%02ld:%02d:%02d", h, m, s);
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
BleSens(String parameters) : IoTItem(parameters)
|
||||||
|
{
|
||||||
|
_MAC = jsonReadStr(parameters, "MAC");
|
||||||
|
_sensor = jsonReadStr(parameters, "sensor");
|
||||||
|
BleSensArray.push_back(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
~BleSens(){};
|
||||||
|
};
|
||||||
|
|
||||||
|
//=======================================================================================================
|
||||||
|
|
||||||
|
/** Callback to process the results of the last scan or restart it */
|
||||||
|
void scanEndedCB(NimBLEScanResults results)
|
||||||
|
{
|
||||||
|
int count = results.getCount();
|
||||||
|
SerialPrint("i", F("BLE"), "Scan done! "); // +"Devices found: " + String(count));
|
||||||
|
// pBLEScan->clearResults();
|
||||||
|
}
|
||||||
|
|
||||||
class BleScan : public IoTItem, BLEAdvertisedDeviceCallbacks
|
class BleScan : public IoTItem, BLEAdvertisedDeviceCallbacks
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
//описание параметров передаваемых из настроек датчика из веба
|
// описание параметров передаваемых из настроек датчика из веба
|
||||||
int _scanDuration;
|
int _scanDuration;
|
||||||
String _filter;
|
String _filter;
|
||||||
|
bool _debug;
|
||||||
|
|
||||||
|
StaticJsonDocument<512> doc;
|
||||||
|
BLEScan *pBLEScan;
|
||||||
|
TheengsDecoder decoder;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
//=======================================================================================================
|
|
||||||
std::string convertServiceData(std::string deviceServiceData)
|
std::string convertServiceData(std::string deviceServiceData)
|
||||||
{
|
{
|
||||||
int serviceDataLength = (int)deviceServiceData.length();
|
int serviceDataLength = (int)deviceServiceData.length();
|
||||||
@@ -67,169 +160,77 @@ public:
|
|||||||
|
|
||||||
if (decoder.decodeBLEJson(BLEdata))
|
if (decoder.decodeBLEJson(BLEdata))
|
||||||
{
|
{
|
||||||
|
|
||||||
BLEdata.remove("manufacturerdata");
|
BLEdata.remove("manufacturerdata");
|
||||||
BLEdata.remove("servicedata");
|
BLEdata.remove("servicedata");
|
||||||
|
BLEdata.remove("type");
|
||||||
|
BLEdata.remove("cidc");
|
||||||
|
BLEdata.remove("acts");
|
||||||
|
BLEdata.remove("cont");
|
||||||
|
BLEdata.remove("track");
|
||||||
|
|
||||||
String mac_address = BLEdata["id"].as<const char *>();
|
String mac_address = BLEdata["id"].as<const char *>();
|
||||||
mac_address.replace(":", "");
|
mac_address.replace(":", "");
|
||||||
|
// дописываем время прихода пакета данных
|
||||||
if (_filter != "")
|
BLEdata["last"] = millis();
|
||||||
|
if (_debug)
|
||||||
{
|
{
|
||||||
if (BLEdata[_filter])
|
if ((_filter != "" && BLEdata[_filter]) || _filter == "")
|
||||||
{
|
{
|
||||||
for (JsonPair kv : BLEdata)
|
// for (JsonPair kv : BLEdata)
|
||||||
{
|
// {
|
||||||
extBLEdata[mac_address][kv.key()] = BLEdata[kv.key()];
|
// String val = BLEdata.as<String>();
|
||||||
}
|
String output;
|
||||||
|
serializeJson(BLEdata, output);
|
||||||
// дописываем время прихода пакета данных
|
SerialPrint("i", F("BLE"), _id + " " + output);
|
||||||
extBLEdata[mac_address]["last"] = millis();
|
//}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
BLEdata.remove("servicedatauuid");
|
||||||
|
SerialPrint("i", F("BLE"), "found: " + mac_address);
|
||||||
|
// Перебираем все зарегистрированные сенсоры BleSens
|
||||||
|
for (std::vector<BleSens *>::iterator it = BleSensArray.begin();
|
||||||
|
it != BleSensArray.end(); ++it)
|
||||||
{
|
{
|
||||||
for (JsonPair kv : BLEdata)
|
// Если это данные для нужного сенсора (по его МАКУ)
|
||||||
{
|
if ((*it)->whoIAm() == mac_address)
|
||||||
extBLEdata[mac_address][kv.key()] = BLEdata[kv.key()];
|
// то передаем ему json, дальше он сам разберется
|
||||||
}
|
(*it)->setBLEdata(BLEdata);
|
||||||
// дописываем время прихода пакета данных
|
|
||||||
extBLEdata[mac_address]["last"] = millis();
|
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BleScan(String parameters) : IoTItem(parameters)
|
BleScan(String parameters) : IoTItem(parameters)
|
||||||
{
|
{
|
||||||
_scanDuration = jsonReadInt(parameters, "scanDuration");
|
_scanDuration = jsonReadInt(parameters, "scanDuration");
|
||||||
_filter = jsonReadStr(parameters, "filter");
|
_filter = jsonReadStr(parameters, "filter");
|
||||||
|
jsonRead(parameters, "debug", _debug);
|
||||||
|
|
||||||
if (pBLEScan->isScanning() == false)
|
BLEDevice::init("");
|
||||||
{
|
pBLEScan = BLEDevice::getScan(); // create new scan
|
||||||
SerialPrint("i", F("BLE"), "Start Scanning...");
|
pBLEScan->setAdvertisedDeviceCallbacks(this);
|
||||||
BLEDevice::init("");
|
pBLEScan->setActiveScan(false); // active scan uses more power, but get results faster
|
||||||
pBLEScan = BLEDevice::getScan(); // create new scan
|
pBLEScan->setInterval(100);
|
||||||
pBLEScan->setAdvertisedDeviceCallbacks(this);
|
pBLEScan->setWindow(99); // less or equal setInterval value
|
||||||
pBLEScan->setActiveScan(false); // active scan uses more power, but get results faster
|
pBLEScan->setMaxResults(0); // do not store the scan results, use callback only.
|
||||||
pBLEScan->setInterval(100);
|
|
||||||
pBLEScan->setWindow(99); // less or equal setInterval value
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//=======================================================================================================
|
|
||||||
|
|
||||||
// doByInterval()
|
// doByInterval()
|
||||||
void doByInterval()
|
void doByInterval()
|
||||||
{
|
{
|
||||||
|
if (pBLEScan->isScanning() == false)
|
||||||
if (_scanDuration > 0)
|
|
||||||
{
|
{
|
||||||
BLEScanResults foundDevices = pBLEScan->start(_scanDuration, true);
|
if (_scanDuration > 0)
|
||||||
int count = foundDevices.getCount();
|
{
|
||||||
SerialPrint("i", F("BLE"), "Devices found: " + String(count));
|
SerialPrint("i", F("BLE"), "Start Scanning...");
|
||||||
SerialPrint("i", F("BLE"), "Scan done!");
|
pBLEScan->start(_scanDuration, scanEndedCB, false);
|
||||||
pBLEScan->clearResults();
|
}
|
||||||
}
|
|
||||||
for (JsonPair kv : extBLEdata)
|
|
||||||
{
|
|
||||||
String val = extBLEdata[kv.key()].as<String>();
|
|
||||||
SerialPrint("i", F("BLE"), _id + " " + kv.key().c_str() + " " + val);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//=======================================================================================================
|
|
||||||
|
|
||||||
~BleScan(){};
|
~BleScan(){};
|
||||||
};
|
};
|
||||||
|
|
||||||
class BleSens : public IoTItem
|
//=======================================================================================================
|
||||||
{
|
|
||||||
private:
|
|
||||||
//описание параметров передаваемых из настроек датчика из веба
|
|
||||||
String _MAC;
|
|
||||||
String _sensor;
|
|
||||||
|
|
||||||
public:
|
|
||||||
//=======================================================================================================
|
|
||||||
char *TimeToString(unsigned long t)
|
|
||||||
{
|
|
||||||
static char str[12];
|
|
||||||
long h = t / 3600;
|
|
||||||
t = t % 3600;
|
|
||||||
int m = t / 60;
|
|
||||||
int s = t % 60;
|
|
||||||
sprintf(str, "%02ld:%02d:%02d", h, m, s);
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
BleSens(String parameters) : IoTItem(parameters)
|
|
||||||
{
|
|
||||||
_MAC = jsonReadStr(parameters, "MAC");
|
|
||||||
_sensor = jsonReadStr(parameters, "sensor");
|
|
||||||
}
|
|
||||||
|
|
||||||
//=======================================================================================================
|
|
||||||
|
|
||||||
// doByInterval()
|
|
||||||
void doByInterval()
|
|
||||||
{
|
|
||||||
if (_sensor == "last")
|
|
||||||
{
|
|
||||||
int valInt = extBLEdata[_MAC][_sensor].as<int>();
|
|
||||||
char *s;
|
|
||||||
s = TimeToString(millis() / 1000 - valInt / 1000);
|
|
||||||
value.isDecimal = 0;
|
|
||||||
if (valInt > 0)
|
|
||||||
{
|
|
||||||
value.valS = s;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
value.valS = "";
|
|
||||||
}
|
|
||||||
regEvent(value.valS, _id);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
String valStr = extBLEdata[_MAC][_sensor].as<String>();
|
|
||||||
if (valStr != "null")
|
|
||||||
{
|
|
||||||
if (value.isDecimal = isDigitDotCommaStr(valStr))
|
|
||||||
{
|
|
||||||
value.isDecimal = 1;
|
|
||||||
value.valD = valStr.toFloat();
|
|
||||||
regEvent(value.valD, _id);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
value.isDecimal = 0;
|
|
||||||
value.valS = valStr;
|
|
||||||
regEvent(value.valS, _id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
value.isDecimal = 0;
|
|
||||||
value.valS = "";
|
|
||||||
regEvent(value.valS, _id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//=======================================================================================================
|
|
||||||
|
|
||||||
~BleSens(){};
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Заглушка для ESP8266
|
|
||||||
#ifdef ESP8266
|
|
||||||
class Ble : public IoTItem
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
public:
|
|
||||||
Ble(String parameters) : IoTItem(parameters) {}
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void *getAPI_Ble(String subtype, String param)
|
void *getAPI_Ble(String subtype, String param)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
"configItem": [
|
"configItem": [
|
||||||
{
|
{
|
||||||
"name": "bluetooth сканер",
|
"name": "bluetooth сканер",
|
||||||
"num": 1,
|
|
||||||
"type": "Reading",
|
"type": "Reading",
|
||||||
"subtype": "BleScan",
|
"subtype": "BleScan",
|
||||||
"id": "BleScan",
|
"id": "BleScan",
|
||||||
@@ -12,11 +11,11 @@
|
|||||||
"descr": "",
|
"descr": "",
|
||||||
"int": 135,
|
"int": 135,
|
||||||
"scanDuration": 10,
|
"scanDuration": 10,
|
||||||
"filter": "servicedatauuid"
|
"filter": "servicedatauuid",
|
||||||
|
"debug":1
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "bluetooth датчик",
|
"name": "bluetooth датчик",
|
||||||
"num": 1,
|
|
||||||
"type": "Reading",
|
"type": "Reading",
|
||||||
"subtype": "BleSens",
|
"subtype": "BleSens",
|
||||||
"id": "BleSens",
|
"id": "BleSens",
|
||||||
@@ -26,18 +25,17 @@
|
|||||||
"needSave": 0,
|
"needSave": 0,
|
||||||
"global": 0,
|
"global": 0,
|
||||||
"round": 1,
|
"round": 1,
|
||||||
"int": 60,
|
|
||||||
"MAC": "",
|
"MAC": "",
|
||||||
"sensor": ""
|
"sensor": ""
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"about": {
|
"about": {
|
||||||
"authorName": "AVAKS",
|
"authorName": "AVAKS, v3 - Mit4bmw",
|
||||||
"authorContact": "https://t.me/@avaks_dev",
|
"authorContact": "https://t.me/@avaks, https://t.me/Mit4bmw",
|
||||||
"authorGit": "https://github.com/avaksru",
|
"authorGit": "https://github.com/avaksru, https://github.com/Mit4el",
|
||||||
"specialThanks": "@Serghei63",
|
"specialThanks": "@Serghei63",
|
||||||
"moduleName": "Ble",
|
"moduleName": "Ble",
|
||||||
"moduleVersion": "1.0",
|
"moduleVersion": "3.0",
|
||||||
"usedRam": {
|
"usedRam": {
|
||||||
"esp32_4mb": 1261449,
|
"esp32_4mb": 1261449,
|
||||||
"esp8266_4mb": 0
|
"esp8266_4mb": 0
|
||||||
@@ -50,9 +48,9 @@
|
|||||||
"moduleDesc": "Позволяет получить данные с Bluetooth часов и термометров Mijia, Xiaomi, Cleargrass, ...",
|
"moduleDesc": "Позволяет получить данные с Bluetooth часов и термометров Mijia, Xiaomi, Cleargrass, ...",
|
||||||
"propInfo": {
|
"propInfo": {
|
||||||
"round": "Округление после запятой.",
|
"round": "Округление после запятой.",
|
||||||
"int": "Интервал сканирования BLE окружения (BleScan) / Интервал отправки собранной телеметрии в MQTT (BleSens)",
|
"int": "Интервал сканирования BLE окружения (BleScan) / В BleSens не используется, там обновляется по мене сканирования/поступления",
|
||||||
"scanDuration": "Длительность сканирования ",
|
"scanDuration": "Длительность сканирования ",
|
||||||
"filter": "Позволяет установить фильтр по параметру передаваемому датчиком. Данные будут считываться только с датчиков у которых есть передаваемый параметр указанный в фильтре",
|
"filter": "Позволяет установить фильтр по параметру передаваемому датчиком. Влияет только на вывод лога при debug=1, что бы было легче найти датчики, если много устройств в эфире",
|
||||||
"MAC": "MAC адрес беспроводного датчика",
|
"MAC": "MAC адрес беспроводного датчика",
|
||||||
"sensor": "Тип сенсора: температура / влажность / время / ... "
|
"sensor": "Тип сенсора: температура / влажность / время / ... "
|
||||||
}
|
}
|
||||||
@@ -63,13 +61,25 @@
|
|||||||
"https://github.com/h2zero/NimBLE-Arduino.git",
|
"https://github.com/h2zero/NimBLE-Arduino.git",
|
||||||
"https://github.com/avaksru/decoder.git"
|
"https://github.com/avaksru/decoder.git"
|
||||||
],
|
],
|
||||||
"esp32_4mb3f": [
|
"esp32_16mb": [
|
||||||
"https://github.com/h2zero/NimBLE-Arduino.git",
|
"https://github.com/h2zero/NimBLE-Arduino.git",
|
||||||
"https://github.com/avaksru/decoder.git"
|
"https://github.com/avaksru/decoder.git"
|
||||||
],
|
],
|
||||||
|
"esp32_4mb3f": [
|
||||||
|
"https://github.com/h2zero/NimBLE-Arduino.git",
|
||||||
|
"https://github.com/avaksru/decoder.git"
|
||||||
|
],
|
||||||
"esp32cam_4mb": [
|
"esp32cam_4mb": [
|
||||||
"https://github.com/h2zero/NimBLE-Arduino.git",
|
"https://github.com/h2zero/NimBLE-Arduino.git",
|
||||||
"https://github.com/avaksru/decoder.git"
|
"https://github.com/avaksru/decoder.git"
|
||||||
|
],
|
||||||
|
"esp32s3_16mb": [
|
||||||
|
"https://github.com/h2zero/NimBLE-Arduino.git",
|
||||||
|
"https://github.com/avaksru/decoder.git"
|
||||||
|
],
|
||||||
|
"esp32c3m_4mb": [
|
||||||
|
"https://github.com/h2zero/NimBLE-Arduino.git",
|
||||||
|
"https://github.com/avaksru/decoder.git"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user